-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
149 lines (128 loc) · 3.51 KB
/
Main.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
139
140
141
142
143
144
145
146
147
148
149
package main;
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static ArrayList<Bread> breadList = new ArrayList<>();
public void sortBreads() {
for(int i = 0; i < breadList.size(); i++) {
for(int j = 1; j < breadList.size() - i; j++) {
if(breadList.get(j-1).name.compareTo(breadList.get(j).name) > 0) {
Bread temp = breadList.get(j-1);
breadList.set(j-1, breadList.get(j));
breadList.set(j, temp);
}
}
}
}
public void viewBread() {
for(Bread b : breadList) {
System.out.println("\nid " + b.id);
System.out.println("name " + b.name);
System.out.println("type " + b.type);
System.out.println("price " + b.price);
}
}
public void deleteBread() {
if(breadList.size() > 0) {
String remCust;
System.out.println("Delete by Name: ");
remCust = sc.nextLine();
boolean found = false;
for(int i = 0; i < breadList.size(); i++) {
if(remCust.equals(breadList.get(i).name)) {
breadList.remove(i);
found = true;
System.out.println(remCust + " successfully deleted..");
break;
}
}
if(!found) {
System.out.println(remCust + " not found in the list");
}
for(Bread b : breadList) {
System.out.println("\nid " + b.id);
System.out.println("name " + b.name);
System.out.println("type " + b.type);
System.out.println("price " + b.price);
}
} else {
System.out.println("No data to be deleted!");
}
}
public Main() {
int input = 0;
do {
System.out.println("Bread Menu");
System.out.println("==============");
System.out.println("1. Order Bread");
System.out.println("2. View Bread");
System.out.println("3. Delete Bread");
System.out.println("4. Exit");
System.out.print(">>");
input = sc.nextInt(); sc.nextLine();
switch(input) {
case 1:
String name;
do {
System.out.println("Enter bread name [Shorter than 3 words]: ");
name = sc.nextLine();
} while(!(alpNum(name)));
String type = null;
do {
System.out.println("Enter bread type [Regular|Croissant|Baguette]: ");
type = sc.nextLine();
} while(!(type.equals("Regular") || type.equals("Croissant") || type.equals("Baguette")));
double price = 0;
do {
System.out.println("Enter bread price [1000 - 100000 inclusive]: ");
price = sc.nextInt(); sc.nextLine();
} while(!(price >= 1000 && price <= 100000));
//ID
int ranID = new Random().nextInt(1000);
String id = String.format("BR%03d", ranID);
breadList.add(new Bread(id, name, type, price));
sortBreads();
break;
case 2:
sortBreads();
viewBread();
sc.nextLine(); // agar berhenti dlu
break;
case 3:
deleteBread();
sortBreads();
sc.nextLine();
break;
}
} while(!(input == 4));
}
public static void main(String[] args) {
new Main();
}
public static boolean alpNum(String input) {
boolean containsLetter = false;
boolean containsDigit = false;
boolean containsUnique = true;
if(input.equals(input.toUpperCase())) {
return true;
}
for(char a : input.toCharArray()) {
if(Character.isLetter(a)) {
containsLetter = true;
}
else if(Character.isDigit(a)) {
containsDigit = true;
}
if(containsLetter && containsDigit) {
return true;
}
}
for(Bread names : breadList) {
if(input.matches(names.name))
System.out.println("the name was taken before..");
containsUnique = false;
break;
}
return containsLetter && containsDigit && containsUnique;
}
}