-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListProgram.java
More file actions
201 lines (167 loc) · 5.5 KB
/
ArrayListProgram.java
File metadata and controls
201 lines (167 loc) · 5.5 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
/*
* Name : Nabeel Kahlil Maulana
* NIM : 2201773134
* program name : Book storage stock manager
*
* features:
* 1. show All books in storage
* 2. add new Books to storage
* 3. add stock on existing books
* 4. subtract stock on existing books
* 5. check duplications
* 6. clarity menu (uses blank spaces)
* 7. class objects inside arrayList
*
* github: https://github.com/chawza
*/
import java.util.ArrayList;
import java.util.Scanner;
/*
things that need to get done:
1. coming soons
*/
class StorageBook {
String name;
int stock;
StorageBook (String nameInput, int numInput) {
name = nameInput;
stock = numInput;
}
void addStock(int numAdd) {
stock += numAdd;
}
void subStock(int numInput) {
stock -= numInput;
}
}
public class source{
static Scanner scan = new Scanner(System.in);
static ArrayList<StorageBook> book = new ArrayList<StorageBook>();
public static void main(String[] args) {
//default storage capacity
book.add(new StorageBook("Wheel of Time", 20));
book.add(new StorageBook("Oliver Twists", 15));
book.add(new StorageBook("Frankenstain", 27));
int bookIdx;
int menuInput = 0;
while(menuInput != 5) {
printBooks();
System.out.println("\nMenu:");
System.out.println("1. Show all Books in storage\n2. Add new books to storage\n3. Add stock to existing books\n4. Subract stock from existing books\n5. Exits program");
System.out.print("User Choice: ");
menuInput = scan.nextInt();
scan.nextLine();
switch (menuInput) {
case 1: //skips everything to show books
printBlanks();
printBooks();
System.out.println("press enter to continue...");
scan.nextLine();
break;
case 2: //if the user choose to add new Books to the storage
System.out.print("Please insert new book title: ");
//user input book title
String newBookInp; // book length can only under 30 characters
do {
newBookInp = scan.nextLine();
if(newBookInp.length() > 30 || newBookInp.length() < 8) {
System.out.println("Book title length range is 8 to 29!\nPlease re-input book title!");
}
}while(newBookInp.length() > 30 || newBookInp.length() < 8);
System.out.print("Please add amount of book to add: ");
int newBookStock = scan.nextInt();
scan.nextLine();
if(checkDuplication(newBookInp)) {
int inpIdx = findBookIdx(newBookInp);
book.get(inpIdx-1).addStock(newBookStock);
}
else {
addNewBooks(newBookInp, newBookStock);
}
break;
case 3: //if the user add the stock to existing books
printBlanks();
if(!printBooks()) {
break;
}
System.out.println("\nPlease choose the book to add based on index: ");
bookIdx = scan.nextInt();
scan.nextLine();
System.out.println("Please input the number of book to add: ");
int numAdd = scan.nextInt();
scan.nextLine();
book.get(bookIdx-1).stock += numAdd;
break;
case 4: //if the user subtract the stock to existing books
printBlanks();
if(!printBooks()) {
scan.nextLine();
break;
}
System.out.println("\nPlease choose the book to subtract based on index: ");
bookIdx = scan.nextInt();
scan.nextLine();
System.out.println("Please input the number of book to subtract:");
int numSub;
do {
numSub = scan.nextInt();
scan.nextLine();
} while (numSub >= book.get(bookIdx-1).stock);
book.get(bookIdx-1).stock -= numSub;
break;
case 5: //if the user choose to exits the program
printBlanks();
System.out.println("Thank you for using the program.");
break;
default: //user choose the wrong input
System.out.println("You chose the wrong input, please re-enter again!");
break;
}
printBlanks();
}
}
//prints all available books
static boolean printBooks() {
//check whether books are available in the storage
if(book.size() == 0) {
System.out.println("Ther are no book in the Storage!\nPlease add new book to add");
return false;
}
System.out.println("Storage Books Premium: the best place to store books".toUpperCase());
System.out.println("No.\tBook Title\t\tAvailable Stock");
for(int a = 0; a < book.size(); a++) {
System.out.println(a + 1 + "\t" + book.get(a).name + "\t\t" + book.get(a).stock);
}
return true;
}
//add new object and add it to the arraylist(book)
static void addNewBooks(String newName, int newStock) {
StorageBook tempBook = new StorageBook(newName, newStock);
System.out.println(tempBook.stock + " books of " + tempBook.name + " has been added to the storage");
book.add(tempBook);
}
//just print newlines for clarity
static void printBlanks() {
for(int a = 0; a < 15; a++) {
System.out.println();
}
}
//check if the book has already exists
static boolean checkDuplication(String name) {
for(int a = 0; a < book.size(); a++) {
if(book.get(a).name.toLowerCase().equals(name.toLowerCase())) {
System.out.println("the book is already added in the storage!");
return true;
}
}
return false;
}
static int findBookIdx(String name) {
for(int a = 0; a < book.size(); a++) {
if(book.get(a).name.toLowerCase().equals(name.toLowerCase())) {
return (a+1);
}
}
return 0;
}
}