-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw4.java
More file actions
474 lines (427 loc) · 17.4 KB
/
hw4.java
File metadata and controls
474 lines (427 loc) · 17.4 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Worked on by: Shade Rahman
import java.util.*;
public class hw4 {
public static void main(String[] args){
//variable initializations
Book[] array = new Book[100];
String input = "";
Scanner myScan = new Scanner(System.in);
boolean bookCreation = true;
boolean valid = false;
int index = -1;
//welcome message
System.out.println("\t\tWelcome to the book program!");
//creating books
do{
System.out.print("Would you like to create a book object? (yes/no): ");
//checks for valid input
do{
input = myScan.nextLine().toUpperCase();
if(!input.equals("YES") && !input.equals("NO")){
System.out.print("Oops! That's not a valid entry. Please try again: ");
valid = false;
}
else{
valid = true;
}
}while(valid == false);
//create a book object
if(input.equals("YES")){
//get author, title, and isbn from user, split entries by /, ensure no spaces at the end, ensure all entries are uppercase
System.out.print("Enter the author, title, and the isbn of the book separated by /: ");
input = myScan.nextLine();
String[] entries = input.split("/");
String author = entries[0].trim().toUpperCase();
String title = entries[1].trim().toUpperCase();
String isbn = entries[2].trim().toUpperCase();
System.out.println("Got it!");
//bookstore book or library book?
System.out.print("Now, tell me if it is a bookstore book or a library book (enter BB for bookstore book or LB for library book): ");
//checks for valid input
do{
input = myScan.nextLine().toUpperCase();
if(!input.equals("BB") && !input.equals("LB")){
System.out.print("Oops! That's not a valid entry. Please try again: ");
valid = false;
}
else{
System.out.println("Got it!");
valid = true;
}
}while(valid == false);
//getting bookstore book information
if(input.equals("BB")){
//initialize bookstore book variables
double price;
boolean onSale;
double saleRate;
//get price
System.out.print("Enter the list price of " + title + " by " + author + ": ");
price = myScan.nextDouble();
myScan.nextLine(); //grabs newline to prevent any errors
//get onSale and saleRate
System.out.print("Is it on sale? (yes/no): ");
//checks for valid input
do{
input = myScan.nextLine().toUpperCase();
if(!input.equals("YES") && !input.equals("NO")){
System.out.print("Oops! That's not a valid entry. Please try again: ");
valid = false;
}
else{
valid = true;
}
}while(valid == false);
//book is on sale
if(input.equals("YES")){
onSale = true;
System.out.print("Deduction percentage: ");
String rawPercent = myScan.nextLine();
//convert string into a mathematical value we can use to calculate discounted book price
rawPercent = rawPercent.replace("%", "");
saleRate = Double.parseDouble(rawPercent);
saleRate = saleRate / 100;
}
//book is not on sale
else{
onSale = false;
saleRate = 0.0;
}
System.out.println("Got it!\n");
//using all given info, add bookstore book to array
index++;
array[index] = new BookstoreBook(author, title, isbn, price, onSale, saleRate);
//print bookstore book toString
System.out.println("Here is your bookstore book information");
System.out.println(array[index]);
System.out.println("\n");
}
//getting library book information
else{
//initialize library book variables
String Subject;
String SubjectCode = "";
String callNumber;
//get subject
System.out.print("What's the subject: ");
//checks for valid input
do{
Subject = myScan.nextLine();
//find subject code
switch(Subject.toUpperCase()){
case "GENERAL": SubjectCode = "A"; break;
case "PHILOSOPHY": SubjectCode = "B"; break;
case "RELIGION": SubjectCode = "C"; break;
case "WORLD HISTORY": SubjectCode = "D"; break;
case "HISTORY OF THE AMERICAS": SubjectCode = "E"; break;
case "GEOGRAPHY": SubjectCode = "F"; break;
case "ANTHROPOLOGY": SubjectCode = "G"; break;
case "SOCIAL SCIENCES": SubjectCode = "H"; break;
case "INTERNET": SubjectCode = "I"; break;
case "POLITICAL SCIENCE": SubjectCode = "J"; break;
case "LAW": SubjectCode = "K"; break;
case "EDUCATION": SubjectCode = "L"; break;
case "MUSIC": SubjectCode = "M"; break;
case "FINE ARTS": SubjectCode = "N"; break;
case "LANGUAGE": SubjectCode = "P"; break;
case "SCIENCE": SubjectCode = "Q"; break;
case "MEDICINE": SubjectCode = "R"; break;
case "AGRICULTURE": SubjectCode = "S"; break;
case "TECHNOLOGY": SubjectCode = "T"; break;
case "MILITARY": SubjectCode = "U"; break;
default: SubjectCode = "";
}
if(SubjectCode.equals("")){
System.out.print("Oops! That's not a valid entry. Please try again: ");
valid = false;
}
else{
System.out.println("Got it!\n");
valid = true;
}
}while(valid == false);
//generate callNumber
int topFloor = 15;
int bottomFloor = 1;
Random rand = new Random();
int floor = rand.nextInt(topFloor - bottomFloor + 1) + bottomFloor;
callNumber = SubjectCode + "." + String.format("%02d", floor) + "." + author.substring(0,3) + "." + isbn.charAt(isbn.length() - 1);
//using all given info, add library book to array
index++;
array[index] = new LibraryBook(author, title, isbn, SubjectCode, callNumber);
//print library book toString
System.out.println("Here is your library book information");
System.out.println(array[index]);
System.out.println("\n");
}
}
//display all books
else{
//counts all library books and bookstore books
int LBcount = 0;
int BBcount = 0;
for (Book b : array){
if (b!= null && b.getBookType().equals("Library Book")){
LBcount++;
}
if (b!= null && b.getBookType().equals("Bookstore Book")){
BBcount++;
}
}
//general msg
System.out.println("Sure!");
System.out.println("Here are all the books you entered...");
//display all library books
System.out.println("Library Books (" + LBcount + ")");
for(Book b: array){
if (b != null && b.getBookType().equals("Library Book")){
System.out.println("\t" + b);
}
}
System.out.println("----");
//display all bookstore books
System.out.println("Bookstore Books (" + BBcount + ")");
for(Book b: array){
if (b != null && b.getBookType().equals("Bookstore Book")){
System.out.println("\t" + b);
}
}
System.out.println("----");
//condition to exit Book Creation do-while look
bookCreation = false;
}
}while(bookCreation == true);
//searching books
System.out.print("Would you like to search for a book? (yes/no): ");
//checks for valid input
do{
input = myScan.nextLine().toUpperCase();
if(!input.equals("YES") && !input.equals("NO")){
System.out.print("Oops! That's not a valid entry. Please try again: ");
valid = false;
}
else{
valid = true;
}
}while(valid == false);
//user wants to search for a book
if(input.equals("YES")){
System.out.print("Search by isbn, author, or title? ");
input = myScan.nextLine().toUpperCase();
int LBcount = 0;
int BBcount = 0;
if(input.equals("ISBN")){
System.out.print("Enter the last character of the isbn: ");
input = myScan.nextLine().toUpperCase();
//figure out # of library books and # of bookstore books this applies to
for(Book b : array){
if(b!= null && (b.getIsbn().charAt((b.getIsbn().length()) - 1)) == input.charAt(0)){
if((b.getBookType()).equals("Library Book")){
LBcount++;
}
else{
BBcount++;
}
}
}
//num of books found msg
System.out.println("We found " + LBcount + " Library Book(s) and " + BBcount + " Book Store Book(s):");
//print the books
for(Book b : array){
if(b!= null && (b.getIsbn().charAt((b.getIsbn().length()) - 1)) == input.charAt(0)){
System.out.println(b);
}
}
}
else if(input.equals("AUTHOR")){
System.out.print("Enter the first three letters of the author: ");
input = myScan.nextLine().toUpperCase();
//figure out # of library books and # of bookstore books this applies to
for(Book b : array){
if(b != null && ((b.getAuthor()).substring(0,3)).equals(input)){
if((b.getBookType()).equals("Library Book")){
LBcount++;
}
else{
BBcount++;
}
}
}
//num of books found msg
System.out.println("We found " + LBcount + " Library Book(s) and " + BBcount + " Book Store Book(s):");
//print books
for(Book b : array){
if(b != null && ((b.getAuthor()).substring(0,3)).equals(input)){
System.out.println(b);
}
}
}
else if(input.equals("TITLE")){
System.out.print("Enter the title of the book: ");
input = myScan.nextLine().toUpperCase();
//figure out # of library books and # of bookstore books this applies to
for(Book b : array){
if(b != null && (b.getTitle()).equals(input)){
if((b.getBookType()).equals("Library Book")){
LBcount++;
}
else{
BBcount++;
}
}
}
//num of books found msg
System.out.println("We found " + LBcount + " Library Book(s) and " + BBcount + " Book Store Book(s):");
//print books
for(Book b : array){
if(b != null && (b.getTitle()).equals(input)){
System.out.println(b);
}
}
}
//goodbye message
System.out.println("\nTake Care!");
return;
}
//user does not want to search for a book
else{
//goodbye message
System.out.println("\nTake Care!");
return;
}
}
}
abstract class Book{
//variable initializations
private String author;
private String title;
private String isbn;
//setters and getters
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
//constructors
public Book(String author, String title, String isbn){
this.author = author;
this.title = title;
this.isbn = isbn;
}
public Book(){
author = "xxx";
title = "xxx";
isbn = "xxx";
}
//toString override
@Override
public String toString(){
return "[" + isbn + "-" + title.toUpperCase() + " by " + author.toUpperCase();
}
//getBookType function
abstract public String getBookType();
}
class BookstoreBook extends Book{
//variable initializations
private double price;
private boolean onSale;
private double saleRate;
//setters and getters
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isOnSale() {
return onSale;
}
public void setOnSale(boolean onSale) {
this.onSale = onSale;
}
public double getSaleRate() {
return saleRate;
}
public void setSaleRate(double saleRate) {
this.saleRate = saleRate;
}
//constructors
public BookstoreBook(String author, String title, String isbn, double price, boolean onSale, double saleRate){
super(author, title, isbn);
this.price = price;
this.onSale = onSale;
this.saleRate = saleRate;
}
public BookstoreBook(double price, boolean onSale, double saleRate){
this.price = price;
this.onSale = onSale;
this.saleRate = saleRate;
}
//additional functions
private double calculateDiscountedPrice(){
return (price - (price*saleRate));
}
//toString override
@Override
public String toString(){
if(onSale)
return super.toString() + ", $" + String.format("%.2f", price) + " listed for $" + String.format("%.2f", calculateDiscountedPrice()) + "]";
else
return super.toString() + ", $" + String.format("%.2f", price) + "]";
}
//getBookType function
@Override
public String getBookType(){
return "Bookstore Book";
}
}
class LibraryBook extends Book{
//variable initializations
private String Subject; //this specifically holds the subject code, not the full subject name itself
private String callNumber;
//setters and getters
public String getSubject() {
return Subject;
}
public void setSubject(String subject) {
Subject = subject;
}
public String getCallNumber() {
return callNumber;
}
public void setCallNumber(String callNumber) {
this.callNumber = callNumber;
}
//constructors
public LibraryBook(String author, String title, String isbn, String Subject, String callNumber){
super(author, title, isbn);
this.Subject = Subject;
this.callNumber = callNumber;
}
public LibraryBook(String author, String title, String isbn){
super(author, title, isbn);
}
//toString override
@Override
public String toString(){
return super.toString() + "-" + callNumber + "]";
}
//getBookType function
@Override
public String getBookType(){
return "Library Book";
}
}