-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
172 lines (141 loc) · 5.95 KB
/
Main.java
File metadata and controls
172 lines (141 loc) · 5.95 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
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static ArrayList<Menu> menu;
private static Scanner sc;
public static void main(String[] args) {
System.out.println("Hello world!");
UI ui = new UI();
menu = new ArrayList<Menu>();
sc = new Scanner(System.in);
int input=0;
do {
try {
input = ui.display();
switch (input) {
case 1:
// show all menu
System.out.print("\033[H\033[2J");
System.out.println("Here is the Menu List :");
for (Menu m : menu) {
System.out.println(m);
}
break;
case 2:
// Add a new Menu
addnewMenu();
break;
case 3:
// Update a Menu
updateMenu();
break;
case 4:
deleteMenu();
break;
default:
System.out.println("Invalid Input!");
}
} catch (Exception e) {
System.out.println("Invalid Input!");
sc.nextLine(); // Clearing the input buffer
}
} while (input != 5);
System.out.print("\033[H\033[2J");
System.out.println("Exiting program. Goodbye!");
}
public static void addnewMenu() {
System.out.print("\033[H\033[2J");
System.out.println("Add New menu Item :");
System.out.print("Enter Name : ");
String name = sc.nextLine();
System.out.println("Enter a short description : ");
String description = sc.nextLine();
System.out.print("Enter price : ");
double price = sc.nextDouble();
sc.nextLine();
Menu newMenu = null;
System.out.print("Is it Food (F) or Drink (D) : ");
String userInput = sc.next().toLowerCase();
sc.nextLine();
if (userInput.equals("f")) {
System.out.println("Enter food's cuisine : ");
String cuisine = sc.nextLine();
System.out.print("\033[H\033[2J");
newMenu = new Food(name, description, price, cuisine);
}
else if (userInput.equals("d")) {
System.out.println("Enter the type of the drink (alcohol/non-alcohol)");
String type = sc.nextLine();
System.out.print("\033[H\033[2J");
newMenu = new Drink(name, description, price, type);
} else {
System.out.println("Wrong Input!");
System.out.println("Closing console...");
// Exit the Java program
System.exit(0);
}
menu.add(newMenu);
}
public static void updateMenu() {
System.out.print("\033[H\033[2J");
System.out.println("Update Menu Item :");
System.out.print("Enter the name of the menu item to update: ");
String nameToUpdate = sc.nextLine();
int updateIndex = searchByName(nameToUpdate);
if (updateIndex != -1) {
Menu menuToUpdate = menu.get(updateIndex);
System.out.print("Enter Name [" + menuToUpdate.getName() + "]: ");
String name = sc.nextLine();
name = name.isEmpty() ? menuToUpdate.getName() : name;
System.out.print("Enter a short description [" + menuToUpdate.getDescription() + "]: ");
String description = sc.nextLine();
description = description.isEmpty() ? menuToUpdate.getDescription() : description;
System.out.print("Enter price [" + menuToUpdate.getPrice() + "]: ");
double price;
try {
price = Double.parseDouble(sc.nextLine());
} catch (NumberFormatException e) {
price = menuToUpdate.getPrice();
}
if (menuToUpdate instanceof Food) {
Food foodToUpdate = (Food) menuToUpdate;
System.out.print("Enter food's cuisine [" + foodToUpdate.getCuisine() + "]: ");
String cuisine = sc.nextLine();
cuisine = cuisine.isEmpty() ? foodToUpdate.getCuisine() : cuisine;
menu.set(updateIndex, new Food(name, description, price, cuisine));
}
else if (menuToUpdate instanceof Drink) {
Drink drinkToUpdate = (Drink) menuToUpdate;
System.out.print("Enter the type of the drink (alcohol/non-alcohol) [" + drinkToUpdate.getType() + "]: ");
String type = sc.nextLine();
type = type.isEmpty() ? drinkToUpdate.getType() : type;
menu.set(updateIndex, new Drink(name, description, price, type));
}
System.out.println("Menu item updated successfully!");
} else {
System.out.println("Menu item not found!");
}
}
public static void deleteMenu() {
System.out.print("\033[H\033[2J");
System.out.println("Delete a Menu Item :");
System.out.print("Enter the name of item to Delete: ");
String nameToDelete = sc.nextLine();
int Index = searchByName(nameToDelete);
menu.remove(Index);
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("The targeted item has been deleted! \n");
}
public static int searchByName(String nameToUpdate) {
for (int i = 0; i < menu.size(); i++) {
if (menu.get(i).getName().equalsIgnoreCase(nameToUpdate)) {
return i;
}
}
return -1;
}
}