-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBookstore.java
More file actions
660 lines (599 loc) · 22.1 KB
/
Copy pathBookstore.java
File metadata and controls
660 lines (599 loc) · 22.1 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package src;
/**
***********************************************
@Author : Khathab Hamed
@First Created: 2/28/2020
@Last Modified: 3/10/2020
@Description: This program allow the user in input book record from a binary or ASCII file and allows them to maintain it
by editing, sort, adding and deleting record while constantly storing the record
***********************************************
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class Bookstore {
/*
* Method Name: inputNonBinData()
*Author: Khathab Hamed
* Creation Date; 2 28, 2020
* Modified Date: 3 2, 2020
*Description: inputs record from ASCII file
* @Parameters: String fileName, int numBooks
* @Return Value: Book[]
* Data Type: example: record of books
* Dependencies: n/a
*Throws/Exceptions: n/a
*/
public static Book[] inputNonBinData (String fileName, int numBooks) {
int i=0;
Book[] bookShelf = new Book[numBooks];
try{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
while(in.ready()==true){
bookShelf[i] = new Book();
bookShelf[i].setBookName(in.readLine());
bookShelf[i].setAuthor(in.readLine());
bookShelf[i].setPublisher(in.readLine());
bookShelf[i].setISBN(Long.parseLong(in.readLine()));
bookShelf[i].setPrice(Double.parseDouble(in.readLine()));
bookShelf[i].setQuantity(Integer.parseInt(in.readLine()));
bookShelf[i].setRecordNumber(i);
i++;
}
in.close();
} catch (IOException e) {};
return bookShelf;
}
/*
* Method Name: findBookRec()
*Author: Khathab Hamed
* Creation Date; 3 2, 2020
* Modified Date: 3 4, 2020
*Description: finds number of record in an ASCII file
* @Parameters: String fileName, int recSize
* @Return Value: integer
* Data Type: example: size of recs
* Dependencies: n/a
*Throws/Exceptions: n/a
*/
public static int findBookRec (String fileName, int recSize) {
int i=0;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
while(in.ready()==true){
in.readLine();
i++;
}
in.close();
} catch (IOException e) {};
i = i/recSize;
return i;
}
/*
* Method Name: findBookRecBin()
*Author: Khathab Hamed
* Creation Date; 2 30, 2020
* Modified Date: 3 1, 2020
*Description: finds number of record in a binary file
* @Parameters: String fileName, int recSize
* @Return Value: integer
* Data Type: example: size of recs
* Dependencies: n/a
*Throws/Exceptions: IOException
*/
public static int findBookRecBin (String fileName, int recSize) throws IOException {
long i=0;
RandomAccessFile raf = new RandomAccessFile(fileName,"rw");
i = raf.length();
i = i/recSize;
raf.close();
return (int)i;
}
/*
* Method Name: addRAF()
*Author: Khathab Hamed
* Creation Date; 3 1, 2020
* Modified Date: 3 3, 2020
*Description: adds to a record
* @Parameters: String filename, Book bookShelf[], Book bookTemp
* @Return Value: n/a
* Data Type: example: n/a
* Dependencies: n/a
*Throws/Exceptions: IOException
*/
public static void addRAF(String filename, Book bookShelf[], Book bookTemp) throws IOException{
bookShelf[bookShelf.length-1] = new Book();
bookShelf[bookShelf.length-1].setAuthor(bookTemp.getAuthor()) ;
bookShelf[bookShelf.length-1].setBookName(bookTemp.getBookName()) ;
bookShelf[bookShelf.length-1].setISBN(bookTemp.getISBN()) ;
bookShelf[bookShelf.length-1].setPrice(bookTemp.getPrice()) ;
bookShelf[bookShelf.length-1].setPublisher(bookTemp.getPublisher()) ;
bookShelf[bookShelf.length-1].setQuantity(bookTemp.getQuantity()) ;
bookShelf[bookShelf.length-1].setRecordNumber(bookShelf.length-1);
RandomAccessFile raf = new RandomAccessFile(filename,"rw");
bookShelf[bookShelf.length-1].writeBinFile(raf);
raf.close();
}
/*
* Method Name: printToConsole()
*Author: Khathab Hamed
* Creation Date; 3 2, 2020
* Modified Date: 3 10, 2020
*Description: prints an array to the console
* @Parameters: Book bookShelf[], int numrecs
* @Return Value: n/a
* Data Type: example: n/a
* Dependencies: n/a
*Throws/Exceptions: n/a
*/
public static void printToConsole (Book bookShelf[], int numrecs) {
System.out.format("%2s %60s %20s %20s %16s %10s %10s %20s", "#","Title" ,"Author" , "Publisher", "ISBN", "Price $" , "Quantity", "Total");
System.out.println();
for (int f= 0; f< numrecs; f++)
if(bookShelf[f].getBookName().compareTo("#1#") == 0) {
}else {
System.out.format("%2s %60s %20s %20s %16s %10s %10s %20s", f+1 +".", bookShelf[f].getBookName() ,bookShelf[f].getAuthor() , bookShelf[f].getPublisher(), bookShelf[f].getISBN(), bookShelf[f].getPrice() , bookShelf[f].getQuantity(), bookShelf[f].getQuantity()*bookShelf[f].getPrice());
System.out.println();
}
}
/*
* Method Name: writeNewBinFile()
*Author: Khathab Hamed
* Creation Date; 2 29, 2020
* Modified Date: 3 1, 2020
*Description: writes an array of record to a binary file
* @Parameters: String filename, Book bookShelf[], int numrecs
* @Return Value: n/a
* Data Type: example: n/a
* Dependencies: n/a
*Throws/Exceptions: IOException
*/
public static void writeNewBinFile(String filename, Book bookShelf[], int numrecs) throws IOException{
RandomAccessFile raf = new RandomAccessFile(filename,"rw");
for (int i=0; i < numrecs; i++) {
bookShelf[i].writeBinFile(raf);
}
raf.close();
}
/*
* Method Name: readNewBinFile()
*Author: Khathab Hamed
* Creation Date; 2 29, 2020
* Modified Date: 3 1, 2020
*Description: reads an array of record from a binary file
* @Parameters: String filename,int numrecs
* @Return Value: Book[]
* Data Type: example: array of books
* Dependencies: n/a
*Throws/Exceptions: IOException
*/
public static Book[] readNewBinFile(String filename,int numrecs) throws IOException{
RandomAccessFile raf = new RandomAccessFile(filename,"rw");
Book[] bookShelf = new Book[numrecs];
for (int i=0; i < numrecs; i++) {
bookShelf[i] = new Book();
bookShelf[i].readBinFile(raf, i);
} //end for
raf.close();
return bookShelf;
}
/*
* Method Name: searchISBN()
*Author: Khathab Hamed
* Creation Date; 3 6, 2020
* Modified Date: 3 6, 2020
*Description: searches for an ISBN from an array of Books
* @Parameters: Book bookShelf[], long ISBN
* @Return Value: integer
* Data Type: example: location of record with same ISBN
* Dependencies: n/a
*Throws/Exceptions: -1
*/
public static int searchISBN(Book bookShelf[], long ISBN) {
int i = -1;
for(int p = 0; p < bookShelf.length; p++) {
if(bookShelf[p].getISBN()== ISBN) {
i = p;
break;
}
}
return i;
}
/*
* Method Name: searchAuthor()
*Author: Khathab Hamed
* Creation Date; 3 7, 2020
* Modified Date: 3 9, 2020
*Description: searches for an author's books
* @Parameters: Book bookShelf[], String author
* @Return Value: int[]
* Data Type: example: location of books by authors
* Dependencies: n/a
*Throws/Exceptions: -1
*/
public static int[] searchAuthor(Book bookShelf[], String author) {
int count = 0;
int booksWritten = 0;
for(int j = 0;j<bookShelf.length;j++) {
if(bookShelf[j].getAuthor().compareTo(author)==0) {
booksWritten++;
}
}
if(booksWritten>0) {
int[] holdPositions = new int[booksWritten];
for(int p = 0; p < bookShelf.length; p++) {
if(bookShelf[p].getAuthor().compareTo(author)== 0) {
holdPositions[count] = p;
count++;
if(count==booksWritten) {
break;
}
}
}
return holdPositions;
}else {
int[] negative = new int[1];
negative[0] = -1;
return negative;
}
}
/*
* Method Name: sortBooks()
*Author: Khathab Hamed
* Creation Date; 3 2, 2020
* Modified Date: 3 2, 2020
*Description: sorts books by ISBN
* @Parameters: Book bookShelf[],int numBooks
* @Return Value: int[]
* Data Type: example: sorted array of Books
* Dependencies: n/a
*Throws/Exceptions: n/a
*/
public static Book[] sortBooks(Book bookShelf[],int numBooks) {
int n = numBooks;
for (int i = 1; i < n; ++i) {
long keyISBN = bookShelf[i].getISBN();
Book holdbook = bookShelf[i];
int j = i - 1;
while (j >= 0 && (bookShelf[j].getISBN() >= keyISBN)) {
bookShelf[j+1] = bookShelf[j];
j = j - 1;
}
bookShelf[j+1] = holdbook;
}
return bookShelf;
}
/*
* Method Name: ReAlignBooks()
*Author: Khathab Hamed
* Creation Date; 3 10, 2020
* Modified Date: 3 10, 2020
*Description: puts deleted records that are empty at the bottom of the array
* @Parameters: Book bookShelf[]
* @Return Value: Book[]
* Data Type: example: array with empty record at the bottom
* Dependencies: n/a
*Throws/Exceptions: n/a
*/
public static Book[] ReAlignBooks(Book bookShelf[]) {
for (int i = 0; i < bookShelf.length; i++){
for (int j = 0; j < bookShelf.length-1; j++){
if (bookShelf[j].getBookName().compareTo("#1#") == 0){
Book holdbook = bookShelf[j+1];
bookShelf[j+1] = bookShelf[j];
bookShelf[j] = holdbook;
}
}
}
return bookShelf;
}
/*
* Method Name: printARecord()
*Author: Khathab Hamed
* Creation Date; 3 1, 2020
* Modified Date: 3 1, 2020
*Description: prints a single record
* @Parameters: Book bookShelf[], int i
* @Return Value: n/a
* Data Type: example: n/a
* Dependencies: n/a
*Throws/Exceptions: n/a
*/
public static void printARecord(Book bookShelf[], int i) {
System.out.println (bookShelf[i].getBookName() + "\t" + bookShelf[i].getAuthor() + "\t" + bookShelf[i].getPublisher()+ "\t" + bookShelf[i].getISBN()+ "\t" + bookShelf[i].getPrice() + "\t" + bookShelf[i].getQuantity());
}
/*
* Method Name: deleteRecord()
*Author: Khathab Hamed
* Creation Date; 3 2, 2020
* Modified Date: 3 2, 2020
*Description: deletes a record from array and replaces the bin file by blanks
* @Parameters: Book bookShelf[], int i,String filename
* @Return Value: n/a
* Data Type: example: n/a
* Dependencies: n/a
*Throws/Exceptions: IOException
*/
public static void deleteRecord(Book bookShelf[], int i,String filename) throws IOException {
System.out.println(bookShelf[i-1].getBookName()+" has been Deleted!!");
RandomAccessFile raf = new RandomAccessFile(filename,"rw");
bookShelf[i-1].deleteRec(raf);
raf.close();
}
/*
* Method Name: editRecord()
*Author: Khathab Hamed
* Creation Date; 3 2, 2020
* Modified Date: 3 2, 2020
*Description: changes record with edited record and replaces it in the bin file
* @Parameters: Book bookShelf[], int i,String filename
* @Return Value: n/a
* Data Type: example: n/a
* Dependencies: n/a
*Throws/Exceptions: IOException
*/
public static void editRecord(Book bookShelf[], int i,String filename) throws IOException {
System.out.println(filename + " " +bookShelf[i].getRecordNumber());
RandomAccessFile raf = new RandomAccessFile(filename,"rw");
i = bookShelf[i].getRecordNumber();
bookShelf[i].writeBinFile(raf);
raf.close();
}
/*
* Method Name: addNewRec()
*Author: Khathab Hamed
* Creation Date; 3 1, 2020
* Modified Date: 3 2, 2020
*Description: copies array records to new array record with a 1 higher array length
* @Parameters: Book bookShelf[], int i,String filename
* @Return Value: Book[]
* Data Type: example: array of record with bigger size
* Dependencies: n/a
*Throws/Exceptions: n/a
*/
public static Book[] addNewRec(Book[] tempBooks) {
Book[] bookShelf = new Book[tempBooks.length+1];
for(int i=0; i<tempBooks.length;i++) {
bookShelf[i] = tempBooks[i];
}
return bookShelf;
}
public static void main(String[] args) throws IOException {
int lengthrecord = 260; // constant that specifies the recordlength
int numBooks = 0; // stores the number of record that exist
int recAt = 0; // stores a record current location
long ISBNHold = 0; //stores the ISBN number to be searched for
String response; // stores user response
boolean end = false; // stores if the user is exiting
boolean canStore = false; // stores if the user inputed data
int option = 0; // stores selected menu option by user
String txtFileName, binFileName="BinFile"; // stores bin and ascii file names
Scanner scan = new Scanner(System.in); // scanner initialized
Book[] books = null; // stores book record
Book tempBook = new Book(); // stores temporary book record
do {
// menu options
System.out.println("1. to read an ASCII file\n" +
"2. to read a binary file.\n" +
"3. to store the array in a binary file.\n" +
"4. to sort the array by the ISBN.\n" +
"5. to print the data on the books.\n" +
"6. to add a new record.\n" +
"7. to delete a record.\n" +
"8. to edit a field of a record.\n" +
"9. to search by ISBN\n" +
"10. to search by Author\n" +
"11. to quit");
response = scan.nextLine(); // stores user response
// try catch for vetting option number
try {
if((Integer.parseInt(response)>0)&&(Integer.parseInt(response)<12)) {
option = Integer.parseInt(response);
}
}catch(NumberFormatException e) {
}
// case statment for all menu options
switch(option) {
// if user didn't properly input option menu
case 0: System.out.println("This option number doesn't exist! Try Again");
break;
// reading ascii file
case 1: System.out.println("Please enter an ASCII file name to read");
txtFileName= scan.nextLine(); // input file name
numBooks = findBookRec(txtFileName,6); // find number of record in the ascii file
books = inputNonBinData(txtFileName, numBooks); // store book record from ascii file in books
printToConsole(books, numBooks); // print records
canStore = true; // user can user other menu options
break;
case 2:
// reading bin file
System.out.println("Please enter a Binary file name to read");
binFileName= scan.nextLine(); // input file name
numBooks = findBookRecBin(binFileName,lengthrecord); // find number of record in bin file
books = readNewBinFile(binFileName, numBooks); //store book record from bin file in books
books = ReAlignBooks(books); // moves empty record to the top of the record in books
printToConsole(books, numBooks); // print records
canStore = true; // user can user other menu options
break;
case 3:
// if data has been inputed
if(canStore) {
System.out.println("Please enter a Binary file name to write to");
binFileName= scan.nextLine(); // input bin file name
writeNewBinFile(binFileName, books, numBooks); // write record to bin file
}else {
System.out.println("Data Base Empty, Please Read in any File First");
}
break;
//sorts ISBN
case 4:
// if data has been inputed
if(canStore) {
books = sortBooks(books,numBooks); // sorts books using ISBN
books = ReAlignBooks(books); // moves empty record to the top of the record in books
System.out.println("Sorted!!");
printToConsole(books, numBooks); // print record
}else {
System.out.println("Data Base Empty, Please Read in any File First");
}
break;
//prints records
case 5:
// if data has been inputed
if(canStore) {
printToConsole(books, numBooks); // print records
}else {
System.out.println("Data Base Empty, Please Read in any File First");
}
break;
// adds record
case 6:
// if data has been inputed
if(canStore) {
numBooks++;
// inputs record fields one by one
books = addNewRec(books);
System.out.println("Enter Title");
tempBook.setBookName(scan.nextLine());
System.out.println("Enter Author");
tempBook.setAuthor(scan.nextLine());
System.out.println("Enter Publisher");
tempBook.setPublisher(scan.nextLine());
System.out.println("Enter ISBN");
tempBook.setISBN(Long.parseLong(scan.nextLine()));
System.out.println("Enter Quantity");
tempBook.setQuantity(Integer.parseInt(scan.nextLine()));
System.out.println("Enter Price");
tempBook.setPrice(Double.parseDouble(scan.nextLine()));
addRAF(binFileName,books,tempBook); //writes new book record to bin file
books = ReAlignBooks(books); // // moves empty record to the top of the record in books
}else {
System.out.println("Data Base Empty, Please Read in any File First");
}
break;
// deletes record
case 7:
// if data has been inputed
if(canStore) {
printToConsole(books, numBooks); //prints record
System.out.println("Please enter the record number you want to delete from the list?");
recAt = Integer.parseInt(scan.nextLine()); // record number to delete
deleteRecord(books,recAt,binFileName); // deletes record by replacing it with blanks in bin file
books = ReAlignBooks(books); // moves empty record to the top of the record in books
printToConsole(books, numBooks); // prints records
}else {
System.out.println("Data Base Empty, Please Read in any File First");
}
break;
// edit record
case 8:
// if data has been inputed
if(canStore) {
printToConsole(books, numBooks); // prints records
System.out.println("Please enter the record number you want to edit from the list?");
recAt = Integer.parseInt(scan.nextLine()); // usr inputs what record they want to edit
recAt--;
// one by one checks if user wants to edit a field and what they want to edit it to
System.out.println("Do you want to edit the Title?(yes/no)");
response = scan.nextLine();
if (response.charAt(0)=='y') {
System.out.println("Enter title");
response = scan.nextLine();
books[recAt].setBookName(response);
}
System.out.println("Do you want to edit the Author Name?(yes/no)");
response = scan.nextLine();
if (response.charAt(0)=='y') {
System.out.println("Enter Author Name");
response = scan.nextLine();
books[recAt].setAuthor(response);
}
System.out.println("Do you want to edit the Publisher Name?(yes/no)");
response = scan.nextLine();
if (response.charAt(0)=='y') {
System.out.println("Enter Publisher Name");
response = scan.nextLine();
books[recAt].setPublisher(response);
}
System.out.println("Do you want to edit the ISBN?(yes/no)");
response = scan.nextLine();
if (response.charAt(0)=='y') {
System.out.println("Enter ISBN");
response = scan.nextLine();
books[recAt].setISBN(Long.parseLong(response));
}
System.out.println("Do you want to edit the Price?(yes/no)");
response = scan.nextLine();
if (response.charAt(0)=='y') {
System.out.println("Enter Price");
response = scan.nextLine();
books[recAt].setPrice(Double.parseDouble(response));
}
System.out.println("Do you want to edit the Quantity?(yes/no)");
response = scan.nextLine();
if (response.charAt(0)=='y') {
System.out.println("Enter Quantity");
response = scan.nextLine();
books[recAt].setQuantity(Integer.parseInt(response));
}
editRecord(books,recAt,binFileName); // changes record in bin file
printToConsole(books, numBooks); // prints records
}else {
System.out.println("Data Base Empty, Please Read in any File First");
}
break;
//search using ISBN
case 9:
// if data has been inputed
if(canStore) {
System.out.println("Please enter the ISBN?");
response= scan.nextLine();
ISBNHold = Long.parseLong(response); //stores user inputed ISBN
recAt = searchISBN(books, ISBNHold); // searches if ISBN matches any others
// if found prints rest of record for ISBN
if(recAt >-1) {
printARecord(books, recAt);
}else {
System.out.println("The Book you are looking for doesn't exist!!");
}
}else {
System.out.println("Data Base Empty, Please Read in any File First");
}
break;
// searches using Author
case 10:
// data has been inputed
if(canStore) {
System.out.println("Please enter the Author's full name?");
response= scan.nextLine(); // stores inputed author name
int[] holdPosition = null; // stores positions of books written by author
holdPosition = searchAuthor(books, response); // checks if author has written any books
// if author books it prints all of them
if(holdPosition[0] >-1) {
System.out.println("Book written by "+response+":");
for(int q=0;q<holdPosition.length;q++) {
printARecord(books, holdPosition[q]);
}
}else {
System.out.println("The Book you are looking for doesn't exist!!");
}
}else {
System.out.println("Data Base Empty, Please Read in any File First");
}
break;
//exits
case 11:
System.out.println("Are you sure you want to Quit?(yes/no)"); // if user confirmation prompt
response = scan.nextLine();
if(response.charAt(0)=='y') {
end = true;
}
break;
}
}while(end== false); // user didn't exit then go again
scan.close(); //Close the Text File
}
}