-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMDS.java
More file actions
248 lines (217 loc) · 8.62 KB
/
MDS.java
File metadata and controls
248 lines (217 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.util.HashMap;
import java.util.*;
//Andrew Kolkmeier ask170003
//Jade Rodriguez jrr170005
// If you want to create additional classes, place them in this file as subclasses of MDS
public class MDS {
// class to create entries in the search
public class Entry {
private int id;
private int price;
private TreeSet<Integer> description;
// Entry constructor
public Entry(int id, TreeSet<Integer> description, int price){
this.id = id;
this.description = description;
this.price = price;
}
}
TreeMap <Integer, Entry> treeMap;
HashMap<Integer, TreeSet<Integer>> hashMap;
// Constructors
public MDS() {
treeMap = new TreeMap<>(); // id/entry map
hashMap = new HashMap<>(); // id/description map
}
/* Public methods of MDS. Do not change their signatures.
__________________________________________________________________
a. Insert(id,price,list): insert a new item whose description is given
in the list. If an entry with the same id already exists, then its
description and price are replaced by the new values, unless list
is null or empty, in which case, just the price is updated.
Returns 1 if the item is new, and 0 otherwise.
*/
public int insert(int id, int price, java.util.List<Integer> list) {
// id not found in treemap
if (treeMap.containsKey(id) == false)
{
//create new entry and put entry in tree map according to id
Entry newEntry = new Entry(id, new TreeSet<>(list), price);
treeMap.put(id, newEntry);
// iterate through description list
for (int descId : list)
// hashmap has descId, replace it
if (hashMap.containsKey(descId))
{
TreeSet<Integer> oldSet = hashMap.get(descId); // put exsisting id in oldSet
oldSet.add(id);
hashMap.replace(descId, oldSet); // update it
}
//hashmap does not have descId, add it
else
{
TreeSet<Integer> newSet = new TreeSet<>();
newSet.add(id);
hashMap.put(descId, newSet);
}
return 1;
}
// id in treemap
else
{
// update price only if list is empty
if (list.isEmpty())
{
Entry newEntry = treeMap.get(id);
newEntry.price = price;
treeMap.replace(id, newEntry);
}
// list is not empty, update both price and description
else
{
Entry newEntry = new Entry (id, new TreeSet<>(list), price); // create new entry
Entry oldDesc = treeMap.get(id); // get existing entry
TreeSet<Integer> oldSet = oldDesc.description;
treeMap.replace(id,newEntry); // replace entry in treeset
hashMap.remove(id, oldDesc); // remoive old entry in hashmap and add new one
hashMap.put(id,new TreeSet<>(list));
}
}
return 0;
}
// b. Find(id): return price of item with given id (or 0, if not found).
public int find(int id) {
Entry entry = treeMap.get(id);
if (entry != null) return entry.price; // entry found, return price
else return 0; // entry does not exist
}
/*
c. Delete(id): delete item from storage. Returns the sum of the
ints that are in the description of the item deleted,
or 0, if such an id did not exist.
*/
public int delete(int id) {
Entry entry = treeMap.get(id);
int descSum = 0;
if (entry == null) return 0; // entry does not exist
else
{
for (long i : entry.description){
descSum += i; // add desription ints together
}
treeMap.remove(id); // removes entry from tree
return descSum; // returns sum of description ints
}
}
/*
d. FindMinPrice(n): given an integer, find items whose description
contains that number (exact match with one of the ints in the
item's description), and return lowest price of those items.
Return 0 if there is no such item.
*/
public int findMinPrice(int n) {
if(hashMap.containsKey(n)){
TreeSet<Integer> compSet = hashMap.get(n);
int minPrice = 0;
if(treeMap.containsKey(compSet.first())){
minPrice = treeMap.get(compSet.first()).price;
}
for(int id : compSet)
{
if(treeMap.containsKey(id)){
if(treeMap.get(id).price < minPrice)
{
minPrice = treeMap.get(id).price;
}
}
}
return minPrice;
}
else{
return 0;
}
}
/*
e. FindMaxPrice(n): given an integer, find items whose description
contains that number, and return highest price of those items.
Return 0 if there is no such item.
*/
public int findMaxPrice(int n) {
if(!hashMap.containsKey(n)){
return 0;
}
TreeSet<Integer> compSet = hashMap.get(n);
int maxPrice = 0;
if(treeMap.containsKey(compSet.first())){
maxPrice = treeMap.get(compSet.first()).price;
}
for(int id : compSet){
if(treeMap.containsKey(id))
{
int current = treeMap.get(id).price;
if(maxPrice > current){
maxPrice = current;
}
}
}
return maxPrice;
}
/*
f. FindPriceRange(n,low,high): given int n, find the number
of items whose description contains n, and in addition,
their prices fall within the given range, [low, high].
*/
public int findPriceRange(int n, int low, int high) {
if(hashMap.containsKey(n)){
int numOfItems = 0;
TreeSet<Integer> compSet = hashMap.get(n);
for(Integer id : compSet){
if(treeMap.get(id).price >= low && treeMap.get(id).price <= high){
numOfItems +=1;
}
}
return numOfItems;
}
else{
return 0;
}
}
/*
increase the price of every product, whose id is in
the range [l,h] by r%. Discard any fractional pennies in the new prices
of items. Note that you are truncating, not rounding.
Returns the sum of the net increases of the prices.
*/
public int priceHike(int low, int high, int rate){
int hikeAmount = 0;
for(int i = low; i <= high; i++){
if(treeMap.containsKey(i)){
hikeAmount = hikeAmount + (treeMap.get(i).price*(rate/100));
treeMap.get(i).price = treeMap.get(i).price + (treeMap.get(i).price*(rate/100));
}
}
return hikeAmount;
}
/*
g. RemoveNames(id, list): Remove elements of list from the description of id.
It is possible that some of the items in the list are not in the
id's description. Return the sum of the numbers that are actually
deleted from the description of id. Return 0 if there is no such id.
*/
public int removeNames(int id, java.util.List<Integer> list) {
if(treeMap.containsKey(id)){
Entry entry = treeMap.get(id);
int removedItems = 0;
for(Integer listElement : list){
if(entry.description.contains(listElement)){
removedItems += listElement;
entry.description.remove(listElement);
}
}
return removedItems;
}
else{
return 0;
}
}
}