-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMadisonBuffet.java
More file actions
286 lines (264 loc) · 7.54 KB
/
MadisonBuffet.java
File metadata and controls
286 lines (264 loc) · 7.54 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// --== CS400 File Header Information ==--
// Name: Aristidis Giannopoulos
// Email: [email protected]
// Team: NC
// TA: Daniel Finer
// Lecturer: Gary Dahl
// Notes to Grader: <optional extra notes>
import java.util.LinkedList;
import java.util.NoSuchElementException;
/**
*
* @author Aristidis
*
* @param <String>
* @param <Double>
*/
public class MadisonBuffet implements MapADT<String, Double> {
final private double MAX_CAP = 0.8;
private int capacity = 0;
private int size = 0;
public LinkedList<LinkedNode>[] hashTable;
private int keyP;
/**
* Constructor that initializes default values
*
* @return
*/
@SuppressWarnings("unchecked")
public MadisonBuffet() {
capacity = 10;
size = 0;
hashTable = new LinkedList[capacity];
for (int i = 0; i < capacity; i++) {
hashTable[i] = null;
}
}
/**
* Gets the index for the array at for a specific key.
*
* @param key
* @return the index for a specific key
*/
private int getIndex(String key) {
int hash = Math.abs(key.hashCode());
return hash % capacity;
}
/**
* Gets the index for an array with ranked indexes based on rating
*
* @param key
* @return the index for a specific rating
*/
private int rankIndex(Double key) {
double index = key * 10;
return (int) index;
}
/**
* Adds an element to the HashTable with a key and a value
*
* @param key
* @param value
* @return true if element is added, false otherwise
*/
@SuppressWarnings({"unchecked"})
@Override
public boolean put(String key, Double value) throws NoSuchElementException {
value = Math.round(value*10)/10.0;
if (value*10 > 50 || value*10 <0) {
System.out.println("Rating is not within a 0-5 range!");
return false;
}
int index = getIndex(key);
LinkedList<LinkedNode> tempList = hashTable[index];
LinkedList<LinkedNode>[] tempArray;
if (tempList != null && containsKey(key)) {
System.out.println("The key already exists!");
return false;
}
if (tempList != null && !containsKey(key)) {
tempList.add(new LinkedNode(key, value));
hashTable[index] = tempList;
size++;
if (size >= (MAX_CAP * capacity)) {
tempArray = new LinkedList[capacity * 2];
for (int i = 0; i < capacity; i++) {
if (hashTable[i] != null) {
for (LinkedNode newNode : hashTable[i]) {
int tempIndex = Math.abs(newNode.name.hashCode()) % (capacity * 2);
if (tempArray[tempIndex] == null) {
tempArray[tempIndex] = new LinkedList<LinkedNode>();
}
tempArray[tempIndex].add(new LinkedNode(newNode.name, newNode.rating));
}
}
}
capacity = capacity * 2;
hashTable = tempArray;
}
return true;
} else {
tempList = new LinkedList();
tempList.add(new LinkedNode(key, value));
hashTable[index] = tempList;
size++;
return true;
}
}
/**
* Gets the value at a specific key
*
* @param key
* @return value at a specific key
*/
@SuppressWarnings("unchecked")
@Override
public Double get(String key) throws NoSuchElementException {
int index = getIndex(key);
LinkedList<LinkedNode> iterateList = hashTable[index];
if (iterateList == null) {
throw new NoSuchElementException("Null List");
}
for (int i = 0; i < iterateList.size(); i++) {
if (containsKey(key)) {
return (Double) iterateList.get(keyP).rating;
}
}
throw new NoSuchElementException("No key");
}
/**
* Gets the size of the HashTable
*
* @return size
*/
@Override
public int size() {
return size;
}
/**
* Checks if the HashTable contains a specific key.
*
* @param key
* @return true if the key is in the HashMap
*/
@Override
public boolean containsKey(String key) {
int index = getIndex(key);
LinkedList<LinkedNode> iterateList = hashTable[index];
if (iterateList == null) {
return false;
}
for (int i = 0; i < iterateList.size(); i++) {
if (iterateList.get(i).name.equals(key)) {
keyP = i;
return true;
}
}
return false;
}
/**
* Removes a keypair for the HashMap
*
* @param key
* @returns the ValueType for the removed key
*/
public Double remove(String key) {
int index = getIndex(key);
LinkedList<LinkedNode> iterateList = hashTable[index];
Double remove = null;
if (iterateList == null) {
return remove;
}
for (int i = 0; i < iterateList.size(); i++) {
if (iterateList.get(i).name.equals(key)) {
remove = (Double) iterateList.get(i).rating;
iterateList.remove(i);
hashTable[index] = iterateList;
--size;
return remove;
}
}
return remove;
}
/**
* Clears the hashmap
*
* @return
*/
public void clear() {
for (int i = 0; i < size; ++i) {
hashTable[i] = null;
}
}
/**
* Creates a string with all the restaurants and their ratings
*
* @return the string
*/
public String showAll() {
String output = "";
for (int i = 0; i < capacity; ++i) {
if (hashTable[i] == null) {
continue;
} else {
for (int j = 0; j < hashTable[i].size(); ++j) {
output = output + hashTable[i].get(j).name + ": " + hashTable[i].get(j).rating + "\n";
}
}
}
return output;
}
/**
* Checks through the hashtable if a given string name matches a given string name
*
* @param name
* @return if found returns the restaurant and rating otherwise returns "No Such Restaurant Found"
*/
public String showName(String name) {
String output = "";
for (int i = 0; i < capacity; ++i) {
if (hashTable[i] == null) {
continue;
} else {
for (int j = 0; j < hashTable[i].size(); ++j) {
if (hashTable[i].get(j).name.equalsIgnoreCase(name)) {
output = hashTable[i].get(j).name + ": " + hashTable[i].get(j).rating + "\n";
return output;
}
}
}
}
return "No Such Restaurant Found";
}
@SuppressWarnings("unchecked")
public String rankRating() {
String output = "";
LinkedList<LinkedNode>[] rankHashTable = new LinkedList[51];
for (int i = 0; i < capacity; ++i) {
if (hashTable[i] == null) {
continue;
} else {
for (int j = 0; j < hashTable[i].size(); ++j) {
if (rankHashTable[rankIndex(hashTable[i].get(j).rating)] == null) {
LinkedList<LinkedNode> tempList = new LinkedList<LinkedNode>();
tempList.add(new LinkedNode(hashTable[i].get(j).name, hashTable[i].get(j).rating));
rankHashTable[rankIndex(hashTable[i].get(j).rating)] = tempList;
} else {
rankHashTable[rankIndex(hashTable[i].get(j).rating)].add(new LinkedNode(hashTable[i].get(j).name, hashTable[i].get(j).rating));
}
}
}
}
for (int i = 50; i >=0; --i) {
if (rankHashTable[i] == null) {
continue;
} else {
for (int j = 0; j < rankHashTable[i].size(); ++j) {
output =
output + rankHashTable[i].get(j).name + ": " + rankHashTable[i].get(j).rating + "\n";
}
}
}
return output;
}
}