This repository was archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBookManagement.java
More file actions
370 lines (344 loc) · 12.6 KB
/
Copy pathBookManagement.java
File metadata and controls
370 lines (344 loc) · 12.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package com.library.management;
import com.library.component.Book;
import com.library.component.Dictionary;
import com.library.component.EducationBook;
import com.library.component.ReferenceBook;
import com.library.util.Day;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class BookManagement implements Management, File {
private Book[] books;
private static String bookMenu = """
1. Education Book
2. Reference Book
3. Dictionary
""";
public BookManagement() {
this.books = new Book[0];
}
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
public boolean idExists(int id) {
for (Book book : books)
if (book.getStatus().equals("enabled") && id == book.getId())
return true;
return false;
}
@Override
public void input() {
Scanner sc = new Scanner(System.in);
String choice, input;
int n = 0;
boolean hasError;
do {
System.out.println("""
Inputting books will overwrite all of the books.
All existing books will be lost.
\tDo you want to continue?
1. Yes
2. No
""");
System.out.print("Enter your choice: ");
choice = sc.nextLine();
if (choice.equals("1"))
break;
if (choice.equals("2"))
return;
} while (true);
do {
hasError = false;
System.out.print("Enter number of books: ");
input = sc.nextLine();
try {
n = Integer.parseInt(input);
} catch (Exception e) {
hasError = true;
}
} while (hasError || n < 0);
Book[] newBooks = new Book[n];
books = new Book[0];
for (int i = 0; i < n; i++) {
do {
newBooks[i] = null;
System.out.println("\t\t\tBOOK " + (i + 1));
System.out.println(bookMenu);
System.out.print("Enter your choice: ");
choice = sc.nextLine();
if (choice.equals("1"))
newBooks[i] = new EducationBook();
if (choice.equals("2"))
newBooks[i] = new ReferenceBook();
if (choice.equals("3"))
newBooks[i] = new Dictionary();
} while (newBooks[i] == null);
newBooks[i].input();
books = Arrays.copyOf(books, books.length + 1);
books[i] = newBooks[i];
}
writeFile(books, false);
}
@Override
public void output() {
boolean hasBooks = false;
for (Book book : books) {
if (book.getStatus().equals("enabled")) {
hasBooks = true;
break;
}
}
if (!hasBooks) {
System.out.println("There are no books yet.");
return;
}
System.out.printf(" ID %15s NAME %13s REMAIN PRICE %4s PUBLISH DAY %14s NOTE\n", "", "", "", "");
for (Book book : books)
if (book.getStatus().equals("enabled"))
book.output();
}
@Override
public void add() {
Scanner sc = new Scanner(System.in);
String choice, input;
int k = 0, n = books.length;
boolean hasError;
do {
hasError = false;
System.out.print("Enter number of books: ");
input = sc.nextLine();
try {
k = Integer.parseInt(input);
} catch (Exception e) {
hasError = true;
}
} while (hasError || k < 0);
Book[] newBooks = new Book[k];
for (int i = 0; i < k; i++) {
do {
newBooks[i] = null;
System.out.println("\t\t\tBOOK " + (i + 1));
System.out.println(bookMenu);
System.out.print("Enter your choice: ");
choice = sc.nextLine();
if (choice.equals("1"))
newBooks[i] = new EducationBook();
if (choice.equals("2"))
newBooks[i] = new ReferenceBook();
if (choice.equals("3"))
newBooks[i] = new Dictionary();
} while (newBooks[i] == null);
newBooks[i].input();
books = Arrays.copyOf(books, books.length + 1);
books[i + n] = newBooks[i];
}
writeFile(newBooks, true);
}
@Override
public void edit() {
Scanner sc = new Scanner(System.in);
int id = Book.inputId("Enter ID (4 digits): ");
Book book = findBook(id);
if (book == null)
System.out.println("Book not found!");
else {
System.out.printf(" ID %15s NAME %13s REMAIN PRICE %4s PUBLISH DAY %14s NOTE\n", "", "", "", "");
book.output();
do {
System.out.println("""
\n\tEdit?
1. Yes
2. No
""");
System.out.print("Enter your choice: ");
String choice = sc.nextLine();
if (choice.equals("1")) {
book.setId(0); // reset ID to be able to edit ID
book.input();
System.out.println("Book edited!");
writeFile(books, false);
break;
}
if (choice.equals("2"))
break;
} while (true);
}
}
@Override
public void remove() {
Scanner sc = new Scanner(System.in);
int id = Book.inputId("Enter ID (4 digits): ");
Book book = findBook(id);
if (book == null)
System.out.println("Book not found!");
else {
System.out.printf(" ID %15s NAME %13s REMAIN PRICE %4s PUBLISH DAY %14s NOTE\n", "", "", "", "");
book.output();
do {
System.out.println("""
\n\tRemove?
1. Yes
2. No
""");
System.out.print("Enter your choice: ");
String choice = sc.nextLine();
if (choice.equals("1")) {
book.setStatus("disabled");
System.out.println("Book removed!");
writeFile(books, false);
break;
}
if (choice.equals("2"))
break;
} while (true);
}
}
@Override
public void find() {
Scanner sc = new Scanner(System.in);
String choice;
Book[] book = new Book[1];
do {
System.out.println("""
1. Find Book by ID.
2. Find Book by name.
3. Find Book between low price and high price
""");
System.out.print("Enter your choice: ");
choice = sc.nextLine();
if (choice.equals("1")) { // Find Book by ID
int id = Book.inputId("Enter ID (4 digits): ");
book[0] = findBook(id);
break;
}
if (choice.equals("2")) { // Find Book by name
String name = Book.inputName("Enter name: ");
book[0] = findBook(name);
break;
}
if (choice.equals("3")) { // Find Book between prices
double low = Book.inputPrice("Enter low price: ");
double high = Book.inputPrice("Enter high price: ");
book = findBook(low, high);
break;
}
} while (true);
if (book.length == 0 || book[0] == null) // if (find between prices not found || find by ID/name not found)
System.out.println("Book not found!");
else {
System.out.printf(" ID %17s NAME %15s REMAIN PRICE %6s PUBLISH DAY %16s NOTE\n", "", "", "", "");
for (Book b : book)
b.output();
}
}
public Book findBook(int id) {
for (Book book : books)
if (book.getStatus().equals("enabled") && book.getId() == id)
return book;
return null;
}
public Book findBook(String name) {
for (Book book : books)
if (book.getStatus().equals("enabled") && book.getName().equalsIgnoreCase(name))
return book;
return null;
}
public Book[] findBook(double low, double high) {
Book[] booksFound = new Book[0];
for (Book book : books) {
if (book.getStatus().equals("enabled") && book.getPrice() >= low && book.getPrice() <= high) {
booksFound = Arrays.copyOf(booksFound, booksFound.length + 1);
booksFound[booksFound.length - 1] = book; // if found, add the book to array
}
}
return booksFound;
}
@Override
public void statistic() {
/*
Thống kê số lượng sách theo từng thể loại
Tính phần trăm số lượng mỗi thể loại so với tổng số lượng sách
*/
int n = 0;
int edu = 0, eduRemain = 0;
int ref = 0, refRemain = 0;
int dic = 0, dicRemain = 0;
int all = 0, remain;
for (Book book : books) {
if (book.getStatus().equals("disabled"))
continue;
n++;
remain = book.getRemain();
if (book instanceof EducationBook) {
edu++;
eduRemain += remain;
}
if (book instanceof ReferenceBook) {
ref++;
refRemain += remain;
}
if (book instanceof Dictionary) {
dic++;
dicRemain += remain;
}
all += remain;
}
System.out.printf("Number of books: %d\n", n);
System.out.printf("Education books: %d\t--> %.2f%%\n", edu, edu * 100.0 / n);
System.out.printf("Reference books: %d\t--> %.2f%%\n", ref, ref * 100.0 / n);
System.out.printf(" Dictionaries: %d\t--> %.2f%%\n", dic, dic * 100.0 / n);
System.out.printf("\nNumber of remain books: %d\n", all);
System.out.printf("Remain education books: %d\t--> %.2f%%\n", eduRemain, eduRemain * 100.0 / all);
System.out.printf("Remain reference books: %d\t--> %.2f%%\n", refRemain, refRemain * 100.0 / all);
System.out.printf(" Remain dictionaries: %d\t--> %.2f%%\n", dicRemain, dicRemain * 100.0 / all);
}
@Override
public void readFile() {
try {
FileReader file = new FileReader("data\\books.txt");
BufferedReader reader = new BufferedReader(file);
String strLine;
while ((strLine = reader.readLine()) != null)
convertToObject(strLine);
reader.close();
} catch (Exception ignored) {}
}
@Override
public void writeFile(Object[] objects, boolean append) {
try {
FileWriter file = new FileWriter("data\\books.txt", append);
BufferedWriter writer = new BufferedWriter(file);
for (Object book : objects)
writer.write(book.toString() + "\n");
writer.close();
} catch (Exception ignored) {}
}
@Override
public void convertToObject(String line) throws Exception {
String[] object = line.split(", ");
Book book = null;
String status = object[1];
int id = Integer.parseInt(object[2]);
String name = object[3];
int remain = Integer.parseInt(object[4]);
double price = Double.parseDouble(object[5]);
Day publishDay = Day.parseDay(object[6]);
if (object[0].equals("EDU")) {
String publisher = object[7];
book = new EducationBook(status, id, name, remain, price, publishDay, publisher);
}
if (object[0].equals("REF")) {
String author = object[7];
book = new ReferenceBook(status, id, name, remain, price, publishDay, author);
}
if (object[0].equals("DIC")) {
String language = object[7];
book = new Dictionary(status, id, name, remain, price, publishDay, language);
}
books = Arrays.copyOf(books, books.length + 1);
books[books.length - 1] = book;
}
}