-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasketCheckout.java
112 lines (93 loc) · 2.81 KB
/
BasketCheckout.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
package com.basket.manager.main;
import java.util.Iterator;
import java.util.Scanner;
import com.basket.manager.Basket;
import com.basket.manager.Item;
public class BasketCheckout {
public static void checkoutBasket()
{
Scanner scan = new Scanner(System.in);
String itemName = null;
int qty;
double price;
System.out.println(readMe());
String userId;
System.out.println("Enter userId (First Name): ");
userId = scan.next();
String addItemToBasket = "y";
Basket basket = new Basket(userId);
boolean itemNotExits = true;
do{
Items.getItemNames();
System.out.println("Enter ItemName: ");
itemName = scan.next();
while(itemNotExits){
if(Items.contains(itemName)){
itemNotExits = false;
continue;
}
else{
System.out.println("Invalid ItemName, Please try again using name from Item List: ");
itemName = scan.next();
}
}
System.out.println("Enter quantity: ");
qty = scan.nextInt();
System.out.println("Enter price: ");
price = scan.nextDouble();
basket.addItem(new Item(itemName, qty, price));
System.out.println("Add more items to basket(y/n)? ");
addItemToBasket = scan.next();
itemNotExits = true;
}
while(addItemToBasket.equals("y"));
System.out.println("\nNo. of Products : "+basket.productCount());
System.out.println ("Basket Owned By: "+basket.getUserId());
System.out.println ("Basket Total: " +basket.getBasketTotalCost());
final Iterator<Item> it = basket.getBasketDetails().iterator();
while(it.hasNext()){
System.out.println (it.next().toString());
}
scan.close();
}
public static String readMe(){
return "----Basket Manager----\n" +
"\nThis program takes a basket of items and outputs its total cost."+
"\nThe basket can contain the following items:"+
"\nBananas "+
"\nOranges "+
"\nApples "+
"\nLemons "+
"\nPeaches " +
"\nAssumption - User will input prices & quantity for items.\n";
}
public enum Items
{
Bananas, Oranges, Apples, Lemons, Peaches;
public static void getItemNames() {
StringBuilder bd = new StringBuilder();
bd.append("Pick from the following Items: ");
boolean isFirst = true;
for(Items value: Items.values()){
if(isFirst){
bd.append(value.name());
isFirst= false;
}
else{
bd.append(",");
bd.append(value.name());
}
}
System.out.println(bd.toString());
}
public static boolean contains(String s) {
try {
Items.valueOf(s);
return true;
}
catch (Exception e) {
return false;
}
}
}
}