-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dish.java
138 lines (113 loc) · 4.3 KB
/
Dish.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
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
public class Dish {
private String name;
private double price;
private String menuSection;
private String foodGroups;
public Dish() {
this.name = "UNKNOWN NAME";
this.price = -543.21;
this.menuSection = "UNKNOWN SECTION";
this.foodGroups = "";
}
public Dish(String menuSection) {
this();
this.menuSection = menuSection;
}
public Dish(String name, double price, String menuSection, String foodGroups) {
this.name = name;
this.price = price;
this.menuSection = menuSection;
this.foodGroups = foodGroups;
}
public void setName(String name) {
if (!name.isEmpty()) {
this.name = name;
}
}
public String getName() {
return this.name;
}
public void setPrice(double price) {
if (price >= 0) {
this.price = price;
}
}
public double getPrice() {
return this.price;
}
public void setMenuSection(String section) {
this.menuSection = section;
}
public String getMenuSection() {
return this.menuSection;
}
public void setFoodGroups(String foodGroups) {
StringBuilder sanitizedFoodGroups = new StringBuilder();
for (int i = 0; i < foodGroups.length(); i++) {
char currentChar = foodGroups.charAt(i);
// Check if the character is a known food group character
if (currentChar == FoodConstants.FOOD_GROUP_CHARS[0] ||
currentChar == FoodConstants.FOOD_GROUP_CHARS[1] ||
currentChar == FoodConstants.FOOD_GROUP_CHARS[2] ||
currentChar == FoodConstants.FOOD_GROUP_CHARS[3] ||
currentChar == FoodConstants.FOOD_GROUP_CHARS[4]) {
// Only include valid characters
if (sanitizedFoodGroups.indexOf(String.valueOf(currentChar)) == -1) {
// Avoid duplicates
sanitizedFoodGroups.append(currentChar);
}
}
}
// Set the member variable to the sanitized string
this.foodGroups = sanitizedFoodGroups.toString();
}
public String getFoodGroups() {
return this.foodGroups;
}
public String toString() {
if (foodGroups.isEmpty() && price < 0) {
return name + ", " + menuSection;
} else if (foodGroups.isEmpty()) {
return name + ", " + menuSection + ": $" + price;
} else if (price < 0) {
return name + " (" + foodGroups + "), " + menuSection;
} else {
return name + " (" + foodGroups + "), " + menuSection + ": $" + price;
}
}
public String toMenuString() {
String foodGroupsString = getFoodGroupsDescription();
return name + ": $" + this.price + "\n" + " " + foodGroupsString;
}
private String getFoodGroupsDescription() {
StringBuilder description = new StringBuilder();
if (!foodGroups.isEmpty()) {
String[] groups = foodGroups.split("");
for (String group : groups) {
switch (group) {
case "m" -> description.append("MEAT, ");
case "v" -> description.append("VEGETABLE, ");
case "d" -> description.append("DAIRY, ");
case "n" -> description.append("NUTS, ");
case "g" -> description.append("GRAINS, ");
// Add cases for other known food groups if needed
}
}
description.setLength(description.length() - 2); // Remove the extra comma and space at the end
}
return description.toString();
}
public boolean isVegetarian() {
return !foodGroups.contains("m");
}
public boolean isVegan() {
return !foodGroups.contains("m") && !foodGroups.contains("d");
}
public boolean isSame(Dish other) {
if (this == other) {
return true;
}
return this.name.equals(other.name) || this.price == other.price
|| this.menuSection.equals(other.menuSection) || this.foodGroups.equals(other.foodGroups);
}
}