-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnit5_HW3.java
414 lines (348 loc) · 13.3 KB
/
Unit5_HW3.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//Unit5_HW3
//Alexander Gershfeld and Gabriel Perez
//Hatim Boustique
//COP3330
//October 15, 2023
import java.util.Random;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
BookList Blist = new BookList(); //creates the list for the books to be held
Scanner input = new Scanner(System.in);
String bookEntry, bookType, author = null, title = null, isbnStr = null; //information that all books have
String [] bookInfo;
System.out.println("Welcome to the book program!");
//iterates as many times as the user would like, stopping when input is "no"
while(true) {
System.out.println("Would you like to create a book object? (yes/no): ");
String choice = input.nextLine();
//if the user inputs anything other than yes or no
while(choice.compareToIgnoreCase("yes") != 0 && choice.compareToIgnoreCase("no") != 0) {
System.out.println("I'm sorry but " + choice + " isn't a valid answer. Please enter either yes or no: ");
choice = input.nextLine();
}
//if user says yes, goes through the process of adding a book to the list
if(choice.compareToIgnoreCase("yes") == 0) {
System.out.println("Please enter the author, title and the isbn of the book separated by /: ");
//Gets the information for author title and isbn ---------------------------------
bookEntry = input.nextLine();
bookInfo = bookEntry.split("/");
author = bookInfo[0];
title = bookInfo[1];
isbnStr = bookInfo[2];
if(Blist.getBook(isbnStr) != null) {
System.out.println("Sorry this ISBN is already taken");
continue;
}
//--------------------------------------------------------------------------------
System.out.println("Got it!");
System.out.println("Now, tell me if it is a bookstore book or a library book" +
" (enter BB for bookstore book or LB for library book): ");
//input either bb or lb
bookType = input.nextLine();
//if input is not bb or lb
while(bookType.compareToIgnoreCase("bb") != 0 && bookType.compareToIgnoreCase("lb") != 0) {
System.out.println("Not a valid book type. Please enter the correct type: ");
bookType = input.nextLine();
}
System.out.println("Got it!");
//Takes in more information for bookstore books-----------------------------------
if(bookType.compareToIgnoreCase("bb") == 0) {
double percent; //pass this through
double price = 0; //pass this through
String sale, percentage;
String [] parsePercent; //gets rid of the percent symbol
System.out.println("Please enter the list price of "
+ title.toUpperCase() + " by " + author.toUpperCase() + ": ");
try {
price = Double.parseDouble(input.nextLine());
}
catch (NumberFormatException e) {
System.out.println("Please enter a valid price: ");
price = Double.parseDouble(input.nextLine());
}
System.out.println("Is it on sale? (y/n): ");
sale = input.nextLine();
//keeps asking input if not y or n
while(sale.compareToIgnoreCase("y") != 0 && sale.compareToIgnoreCase("n") != 0) {
System.out.println("I hope you're not messing up this to test our code, try again please: ");
sale = input.nextLine();
}
if(sale.compareToIgnoreCase("n") == 0) percent = 0;
else {
System.out.println("Deduction percentage: ");
percentage = input.nextLine();
try {
parsePercent = percentage.split("%");
percent = Integer.parseInt(parsePercent[0]);
}
catch (NumberFormatException e) {
System.out.println("That is not a number, try again: ");
percentage = input.nextLine();
parsePercent = percentage.split("%");
percent = Integer.parseInt(parsePercent[0]);
}
}
System.out.println("Got it!");
Blist.addBook(new BookstoreBook(author, title, isbnStr, percent, sale, price));
System.out.println("\nHere is your bookstore book information");
System.out.println(Blist.getBook(isbnStr));
}
//--------------------------------------------------------------------------------
//Now does the same but for library books; there are less modifiers for this type--
else if(bookType.compareToIgnoreCase("lb") == 0) {
Blist.addBook(new LibraryBook(author, title, isbnStr));
System.out.println("\nHere is your library book information");
System.out.println(Blist.getBook(isbnStr));
}
//--------------------------------------------------------------------------------
}
//When the user decides to exit, the full list of books will be printed and the loop
//will end
else if(choice.compareToIgnoreCase("no") == 0) {
System.out.println("Sure!");
Blist.printList();
break;
}
}
}
}
//_________________________________
abstract class Book {
private String author;
private String title;
private String isbn;
//get and set author
public String getAuthor() {
return author.toUpperCase();
}
public void setAuthor(String author) {
this.author = author;
}
//get and set title
public String getTitle() {
return title.toUpperCase();
}
public void setTitle(String title) {
this.title = title;
}
//get and set isbn
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
//constructors
public Book() {
author = "NO AUTHOR FOUND";
title = "TITLE NOT FOUND";
isbn = "ISBN NOT FOUND";
}
public Book(String isbn) {
this.author = "NO AUTHOR FOUND";
this.title = "TITLE NOT FOUND";
this.isbn = isbn;
}
public Book(String title, String isbn) {
this.author = "NO AUTHOR FOUND";
this.title = title;
this.isbn = isbn;
}
public Book (String author, String title, String isbn) {
this.author = author;
this.title = title;
this.isbn = isbn;
}
//to String but is overridden
public String toString() {
return "[" + "-" + getIsbn() + getTitle() + " by " + getAuthor() + "]";
}
}
//_________________________________
class BookstoreBook extends Book {
private double discount;
private String onSale;
private double listPrice;
//get and set discount
public double getDiscount() {
if(getOnSale().compareToIgnoreCase("n") == 0) {
return discount;
}
return discount/100;
}
public void setDiscount(double discount) {
this.discount = discount;
}
//get and set sale flag
public String getOnSale() {
return onSale;
}
public void setOnSale(String onSale) {
this.onSale = onSale;
}
//get and set price
public double getPrice() {
return listPrice;
}
public void setPrice(double listPrice) {
this.listPrice = listPrice;
}
//get the price after discount
public double getPriceReduction() {
return getPrice()-(getPrice()*getDiscount());
}
//constructors
public BookstoreBook () {
discount = 0.0;
listPrice = 0.0;
onSale = "SALE NOT FOUND";
}
public BookstoreBook (String isbn, double discount, String onSale, double listPrice) {
super(isbn);
setOnSale(onSale);
setDiscount(discount);
setPrice(listPrice);
}
public BookstoreBook (String title, String isbn, double discount, String onSale, double listPrice) {
super(title, isbn);
setOnSale(onSale);
setDiscount(discount);
setPrice(listPrice);
}
public BookstoreBook (String author, String title, String isbn, double discount, String onSale, double listPrice) {
super(author, title, isbn);
setOnSale(onSale);
setDiscount(discount);
setPrice(listPrice);
}
@Override
public String toString () {
return "[" + getIsbn() + "-" + getTitle() + " by " + getAuthor() + ", $" + String.format("%.2f", getPrice()) +
" listed for $" + String.format("%.2f", getPriceReduction()) + "]";
}
}
//_________________________________
class LibraryBook extends Book {
//random number variable
Random randInt = new Random();
//callNum itself
private String callNum;
//fields for callNum constituents
private int floorNumber;
private String firstThreeChar;
private char callIsbn;
//setters for constituents
public void setFloorNumber() {
this.floorNumber = randInt.nextInt(1, 100); //set random number each object
}
public void setFirstThreeChar() {
this.firstThreeChar = getAuthor().substring(0,3); //grabs the first three characters of String author
}
public void setCallIsbn() {
this.callIsbn = getIsbn().charAt(getIsbn().length() - 1); //takes the char value at the last index of Isbn
}
//piece together the fields into one
public void setCallNum() {
if(getFloorNumber() < 10)
this.callNum = "0%d.%s.%s".formatted(getFloorNumber(), getFirstThreeChar(), getCallIsbn());
else
this.callNum = "%d.%s.%s".formatted(getFloorNumber(), getFirstThreeChar(), getCallIsbn());
}
//getters for constituents
public int getFloorNumber() {
return floorNumber;
}
public String getFirstThreeChar() {
return firstThreeChar;
}
public char getCallIsbn() {
return callIsbn;
}
//getter for the callNum
public String getCallNum() {
return callNum;
}
//Constructors
public LibraryBook(String isbnStr) {
super(isbnStr);
setFloorNumber();
firstThreeChar = "null";
setCallIsbn();
setCallNum();
}
public LibraryBook(String title, String isbnStr) {
super(title, isbnStr);
setFloorNumber();
firstThreeChar = "null";
setCallIsbn();
setCallNum();
}
public LibraryBook(String author, String title, String isbnStr) {
super(author, title, isbnStr);
setFloorNumber();
setFirstThreeChar();
setCallIsbn();
setCallNum();
}
@Override
public String toString() {
return "[" + getIsbn() + "-" + getTitle() + " by " + getAuthor() + "-" + getCallNum() + "]";
}
}
//_________________________________
class BookList {
private Book[] list;
//Constructor method for the list of books, setting every index to null
public BookList() {
list = new Book[100];
for(int i = 0; i < 100; i++) {
list[i] = null;
}
}
//adds the book of its respective book type into the list
public void addBook(Book b) {
boolean full = true;
for(int i = 0; i < 100; i++) {
if(list[i] == null) {
list[i] = b;
full = false;
break;
}
}
if(full) {
System.out.println("Sorry, no more books can be added to your list.");
}
}
//returns the Book object at the index of list that matches the parameter
public Book getBook(String isbnStr) {
for(int i = 0; i < 100; i++) {
if (list[i] == null) return null;
if (isbnStr.compareToIgnoreCase(list[i].getIsbn()) == 0)
return list[i];
}
return null;
}
//prints out the full list of books across all types
public void printList() {
int lb = 0, bb = 0;
System.out.println("\nHere are all your books...");
for(int i = 0; i < 100; i++) {
if(list[i] instanceof LibraryBook) lb++;
else if(list[i] instanceof BookstoreBook) bb++;
}
System.out.println("Library Books (" + lb + ")");
for(Book b: list) {
if(b instanceof LibraryBook)
System.out.println("\t\t" + b.toString());
}
System.out.println("\n----");
System.out.println("Bookstore Books (" + bb + ")");
for(Book b: list) {
if(b instanceof BookstoreBook)
System.out.println("\t\t" + b.toString());
}
System.out.println("\n----");
System.out.println("Take care now!");
}
}