-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserMenu.java
More file actions
223 lines (222 loc) · 9.22 KB
/
userMenu.java
File metadata and controls
223 lines (222 loc) · 9.22 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
/* This class will display the inventory that store has
* and also display the amount after user choose the items that user wants to purchase
*/
package bookstoresystem;
import java.util.*;
public class userMenu {
userMenu(){}
Scanner in = new Scanner(System.in);
public void userMenu(ArrayList<Books> books, ArrayList<Dvds> dvds){
int get_input=0;
int bookSize = books.size();
int dvdSize = dvds.size();
//set another two variables for shopping carts
LinkedList<Integer> BooksCart = new LinkedList<>();
LinkedList<Integer> dvdsCart = new LinkedList<>();
Collections.sort(books, new SortBooks());
Collections.sort(dvds, new SortDvds());
while(get_input != 9){
try{
//display the menu for user to choose
displayMenu();
//get input
get_input = in.nextInt();
switch (get_input) {
case 1:
displayBooks(books, "Book");//method for display the books menu
break;
case 2:
displayDvds(dvds, "DVD");//method for display the dvds menu
break;
case 3:
getItem(BooksCart, bookSize);//get the input from user and save it into list
break;
case 4:
getItem(dvdsCart, dvdSize);//get the input from user and save it into list
break;
case 5:
deleteItem(BooksCart);//remove item from cart
break;
case 6:
deleteItem(dvdsCart);//remove item from cart
break;
case 7:
displayCarts(BooksCart, dvdsCart, books, dvds);//display cart
break;
case 8://display and clear cart
displayCarts(BooksCart, dvdsCart, books, dvds);
BooksCart.clear();
dvdsCart.clear();
System.out.println("Thanks you for shoppiny");
break;
case 9:
break;
default:
System.out.println("Wrong Input, Pleae enter again");
break;
}
}
catch(InputMismatchException e){
System.out.println("This option is not acceptable. please enter again");
in = new Scanner (System.in);
}
}
}
//display main menu
private void displayMenu() {
System.out.println("**Welcome to the Mustangs Books and DVDs Store**"+"\n\n"+
"Choose from the following options:"+"\n"+
"1 - Browse books inventory(price low to high)"+
"\n"+"2 - Browse DVDs inventory(price low to high"
+"\n"+"3 - add a book to the cart"+"\n"+"4 - add a DVD to the cart"
+"\n"+"5 - Delete a book from cart"+"\n"+"6 – Delete a DVD from cart"
+"\n"+"7 - View cart"+"\n"+"8 - Checkout"+"\n"+"9 - Exit Store");
System.out.print("Please enter the option:");
}
//display books catelog
private void displayBooks(ArrayList<Books> books, String itemType) {
int x = 1;
System.out.println(itemType+" "+"\n-------------------------------------------------------------------------------------------------------------");
for(Books book: books){
System.out.println("Item Numer: " + x +"| "+book.toString());
x++;
}
System.out.println("-------------------------------------------------------------------------------------------------------------");
}
//display dvd catelog
private void displayDvds(ArrayList<Dvds> dvds, String itemType) {
int x = 1;
System.out.println(itemType+" "+"\n-------------------------------------------------------------------------------------------------------------");
for(Dvds dvd: dvds){
System.out.println("Item Numer: " + x +"| "+ dvd.toString());
x++;
}
System.out.println("-------------------------------------------------------------------------------------------------------------");
}
//store item into cart
private void getItem(LinkedList<Integer> cart, int size) {
int x;
while(true){
try{
Validator va = new Validator();
System.out.println("Please enter Item number to add into cart(0 to return): ");
x =in.nextInt();
if(!va.isPositiveInput(x)){
System.out.println("Please enter positive number.");
}
else if(x>size){
System.out.println("Do not have this option.");
}
else if(x == 0){
break;
}
else{
cart.add(x);
System.out.println("Add item successfully");
}
}
catch(InputMismatchException e){
System.out.println("Input Error");
in= new Scanner(System.in);
}
}
}
//dilete item from cart
private void deleteItem(LinkedList<Integer> cart){
int x = -1;
if(cart.isEmpty()){
System.out.println("Your Cart is empty");
}
else
{
while( x!= 0){
try{
System.out.print("Please enter Item number to delete (o to return): ");
x = in.nextInt();
if(x==0){
}
else{
if(cart.contains(x)){
cart.remove((Integer)x);
System.out.println("Remove Item Successfully");
}
else{
System.out.println("Do not have this item in the cart");
}
}
}
catch(InputMismatchException e){
System.out.println("Input Error");
in= new Scanner(System.in);
}
}
}
}
//display item
private void displayCarts(LinkedList<Integer> booksCart, LinkedList<Integer> dvdsCart, ArrayList<Books> books, ArrayList<Dvds> dvds){
double total = 0;
System.out.println("------------------------------------------------Shoppint Cart------------------------------------------------");
if(booksCart.isEmpty() && dvdsCart.isEmpty()){
System.out.println("\t\tYour Cart is empty");
System.out.println("-------------------------------------------------------------------------------------------------------------");
}
else if(booksCart.isEmpty() && !dvdsCart.isEmpty()){
for(int i = 0; i < dvdsCart.size(); i++){
int x = i+1;
System.out.println("Item Numer: " + x +"| "+ dvds.get(i).toString());
total += dvds.get(i).price;
}
total += total * 0.0825;
System.out.printf("%-25s$%.2f%n","Total+Tax",total);
System.out.println("-------------------------------------------------------------------------------------------------------------");
}
else if(!booksCart.isEmpty() && dvdsCart.isEmpty()){
for(int i = 0; i < booksCart.size(); i++){
int x = i+1;
System.out.println("Item Numer: " + x +"| "+ books.get(i).toString());
total += books.get(i).price;
}
total += total * 0.0825;
System.out.printf("%-25s$%.2f%n","Total+Tax",total);
System.out.println("-------------------------------------------------------------------------------------------------------------");
}
else{
for(int i = 0; i < booksCart.size(); i++){
int x = i+1;
System.out.println("Item Numer: " + x +"| "+ books.get(i).toString());
total += books.get(i).price;
}
for(int i = 0; i < dvdsCart.size(); i++){
int x = i+1;
System.out.println("Item Numer: " + x +"| "+ dvds.get(i).toString());
total += dvds.get(i).price;
}
total += total * 0.0825;
System.out.printf("%-25s$%.2f%n","Total+Tax",total);
System.out.println("-------------------------------------------------------------------------------------------------------------");
}
}
}
//sort function
class SortBooks implements Comparator{
@Override
public int compare(Object o1, Object o2){
Books b1 = (Books) o1;
Books b2 = (Books) o2;
if(b1.price>b2.price){
return 1;
}
else return -1;
}
}
class SortDvds implements Comparator{
@Override
public int compare(Object o1, Object o2){
Dvds d1 = (Dvds) o1;
Dvds d2 = (Dvds) o2;
if(d1.price>d2.price){
return 1;
}
else return -1;
}
}