-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.java
111 lines (99 loc) · 2.8 KB
/
Menu.java
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Menu for Restaurant.
*/
public class Menu implements MenuInterface {
//Menu
static HashMap<FoodTypes, List<FoodInterface>> menu;
public Menu() {
menu = new HashMap<>();
}
@Override
public void addMenuItem(FoodInterface item) {
if (item == null) {
throw new IllegalArgumentException("Food to add is null");
}
if (!menu.containsKey(item.getType())) {
menu.put(item.getType(), new ArrayList<>());
} else {
List<FoodInterface> list = menu.get(item.getType());
for (FoodInterface each : list) {
if (each.getName().equalsIgnoreCase(item.getName())) {
return;
}
}
}
menu.get(item.getType()).add(item);
}
@Override
public void removeMenuItem(String name, FoodTypes type) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name to remove is null or empty. Can't remove.");
}
if (menu.containsKey(type)) {
List<FoodInterface> list = menu.get(type);
for (FoodInterface each : list) {
if (each.getName().equalsIgnoreCase(name)) {
list.remove(each);
break;
}
}
if (menu.get(type).size() == 0) {
menu.remove(type);
}
}
}
@Override
public void removeMenuItem(FoodInterface item) {
if (item == null) {
throw new IllegalArgumentException("Item is null. Can't remove.");
}
if (menu.containsKey(item.getType())) {
List<FoodInterface> list = menu.get(item.getType());
for (FoodInterface each : list) {
if (each.getName().equalsIgnoreCase(item.getName())) {
list.remove(each);
break;
}
}
}
}
@Override
public FoodInterface getFoodItem(String name) {
if(name == null){
throw new IllegalArgumentException("Name of food to find is null.");
}
for (Map.Entry<FoodTypes, List<FoodInterface>> entry : menu.entrySet()) {
List<FoodInterface> list = entry.getValue();
for (FoodInterface each : list) {
if(each.getName().equalsIgnoreCase(name)) {
return each;
}
}
}
return null;
}
@Override
public String toString() {
String str = "";
for (Map.Entry<FoodTypes, List<FoodInterface>> entry : menu.entrySet()) {
List<FoodInterface> list = entry.getValue();
str += entry.getKey() + ":\n";
for (FoodInterface each : list) {
str += each.toString();
}
str += "------------------------\n";
}
if (str.isEmpty()) {
str += "Menu is empty.";
}
return str;
}
@Override
public HashMap getMenu() {
return menu;
}
}