-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
418 lines (353 loc) · 10.8 KB
/
Driver.java
File metadata and controls
418 lines (353 loc) · 10.8 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
//-----------------------------------------------------
// Assignment 3
// Part: 3 (Driver class)
// Written by: Mira Alanzarouti 40195605
// Noor Kaddoura 40253572
//-----------------------------------------------------
package A3;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Vector;
import java.util.Scanner;
/**
* Driver class that contains the main method
*/
public class Driver {
public static void main(String[] args) {
//Scanner object
Scanner in = new Scanner(System.in);
// initializing and declaring the ArrayList and BookList of type Book
ArrayList<Book> arrLst = new ArrayList<Book>();
BookList bkLst = new BookList();
// initializing and declaring variables to be used in reading from the text file
String line = "";
BufferedReader reader = null;
double tempDouble = 0;
long tempLong = 0;
int tempInteger = 0;
boolean errorsExist = false;
String address = "Books.txt";
try
{
reader = new BufferedReader(new FileReader(address));
}
catch (FileNotFoundException e)
{
// If file is not found, end program
System.out.println("The file " + address + " does not exist.");
System.exit(0);
}
try
{
while ((line = reader.readLine()) != null)
{
// split line read from file into string array, separated by ","
String[] row = line.split(",", -1);
// trim every element in row
for (int i = 0; i < row.length; i++)
{
row[i] = row[i].trim();
}
// checking if the record has 6 fields
if (row.length == 6)
{
// parse the non string values and add them to constructor
try
{
tempDouble = Double.parseDouble(row[2]);
tempLong = Long.parseLong(row[3]);
tempInteger = Integer.parseInt(row[5]);
}
catch (NumberFormatException nfe)
{
System.out.println("Cannot add to Constructor! ");
continue;
}
// checking if year value is valid, if not, add the incorrect records to arrLst
if (tempInteger > 2023 || tempInteger < 1900)
{
arrLst.add(new Book(row[0], row[1], tempDouble, tempLong, row[4], tempInteger));
// set errorsExist to true, meaning an error does exist in the text file
errorsExist = true;
}
else
{
// if the year is valid, add the record to the BookList
bkLst.addtoEnd((new Book(row[0], row[1], tempDouble, tempLong, row[4], tempInteger)));
}
}
}
}
catch (IOException e)
{
// If file is not found, end program
System.out.println("Cannot find file. Ending program");
System.exit(0);
}
// if error exists, YearErr.txt is created and written to
if (errorsExist)
{
PrintWriter semErrorWriter = null;
try
{
semErrorWriter = new PrintWriter(new FileOutputStream("YearErr.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
}
// write the contents of arrLst to the error file named YearErr.txt, using the
// toString method on each Book
for (int i = 0; i < arrLst.size(); i++)
{
semErrorWriter.print(arrLst.get(i).toString() + "\n");
}
// close the writer
semErrorWriter.close();
}
// initializing and declaring variables that will be used in the menu
int userChoice = 0;
int year = 0;
long aISBN = 0;
long bISBN = 0;
long cISBN = 0;
String name = "";
String optionTitle = "";
String optionAuthor = "";
double optionPrice = 0;
long optionISBN = 0;
String optionGenre = "";
int optionYear = 0;
Book userInputtedBook = null;
// if user does not enter 7, loop will keep running
while (userChoice != 7)
{
display_menu();
System.out.print("Enter Choice: ");
try
{
userChoice = in.nextInt();
in.nextLine();
}
catch (InputMismatchException e)
{
in.nextLine();
System.out.println("Error! Please enter an option presented by the Menu!\n");
continue;
}
//option 1
if (userChoice == 1)
{
System.out.print("Please enter Year: ");
try
{
year = in.nextInt();
in.nextLine();
if(year > 2023 || year < 1900)
{
System.out.println("Error! Please enter a valid Year.\n");
continue;
}
}
catch (InputMismatchException e)
{
in.nextLine();
System.out.println("Error! Please enter a valid Year.\n");
continue;
}
bkLst.storeRecordsByYear(year);
System.out.println("Displaying contents of main list: ");
bkLst.displayContent();
}
//option 2
else if (userChoice == 2)
{
System.out.println("Deleting consecutive records now...\n");
bkLst.delConsecutiveRepeatedRecords();
System.out.println("Displaying contents of main list: ");
bkLst.displayContent();
}
//option 3
else if (userChoice == 3)
{
System.out.print("Enter Author's Name: ");
name = in.nextLine();
System.out.println("Here is your Authors List");
BookList authorsList = bkLst.extractAuthList(name);
System.out.println("Displaying new list with the records of author " + name);
authorsList.displayContent();
System.out.println("Displaying contents of main list: ");
bkLst.displayContent();
}
//option 4
else if (userChoice == 4)
{
try {
System.out.print("Enter ISBN of book you want to enter before: ");
aISBN = in.nextLong();
in.nextLine();
}
catch (InputMismatchException e)
{
in.nextLine();
System.out.println("Error! Please enter a valid ISBN!\n");
continue;
}
try
{
System.out.println("Entering Book Information: \n");
System.out.print("Enter Title: ");
optionTitle = in.nextLine();
System.out.print("Enter Author: ");
optionAuthor = in.nextLine();
System.out.print("Enter Price: ");
optionPrice = in.nextDouble();
in.nextLine();
System.out.println();
System.out.print("Enter ISBN: ");
optionISBN = in.nextLong();
in.nextLine();
System.out.println();
System.out.print("Enter Genre: ");
optionGenre = in.nextLine();
System.out.println();
System.out.print("Enter Year: ");
optionYear = in.nextInt();
in.nextLine();
// if year is out of range
if(optionYear > 2023 || optionYear < 1900)
{
System.out.println(year);
System.out.println("Error! Please enter a valid Yearrr.\n");
continue;
}
System.out.println();
}
//catch if inputmismatch is thrown
catch (InputMismatchException e)
{
in.nextLine();
System.out.println("Error! Please enter correct Book information!");
continue;
}
// add user inputed information to book constructor and insertBefore method
userInputtedBook = new Book(optionTitle, optionAuthor, optionPrice, optionISBN, optionGenre,
optionYear);
bkLst.insertBefore(aISBN, userInputtedBook);
System.out.println("Displaying contents of main list: ");
bkLst.displayContent();
}
//choice 5
else if (userChoice == 5)
{
try
{
System.out.print("Enter first ISBN: ");
bISBN = in.nextLong();
in.nextLine();
System.out.print("Enter second ISBN: " );
cISBN = in.nextLong();
in.nextLine();
}
catch (InputMismatchException e)
{
in.nextLine();
System.out.println("Error! Please enter a valid ISBN\n");
continue;
}
try
{
System.out.println("Entering Book Information: \n");
System.out.print("Enter Title: ");
optionTitle = in.nextLine();
System.out.print("Enter Author: ");
optionAuthor = in.nextLine();
System.out.print("Enter Price: ");
optionPrice = in.nextDouble();
System.out.println();
System.out.print("Enter ISBN: ");
optionISBN = in.nextLong();
in.nextLine();
System.out.print("Enter Genre: ");
optionGenre = in.nextLine();
System.out.print("Enter Year: ");
optionYear = in.nextInt();
in.nextLine();
// if year is out of range
if(optionYear > 2023 || optionYear < 1900)
{
System.out.println("Error! Please enter a valid Year.\n");
continue;
}
System.out.println();
}
catch (InputMismatchException e)
{
in.nextLine();
System.out.println("Error! Please enter correct Book information!\n");
continue;
}
// add user inputed information to book constructor and insertBetween method
userInputtedBook = new Book(optionTitle, optionAuthor, optionPrice, optionISBN, optionGenre,
optionYear);
bkLst.insertBetween(bISBN, cISBN, userInputtedBook);
System.out.println("Displaying contents of main list: ");
bkLst.displayContent();
}
//choice 6
else if (userChoice == 6)
{
try
{
System.out.print("Enter first ISBN: ");
bISBN = in.nextLong();
in.nextLine(); //extrtacting newline
System.out.print("Enter second ISBN: ");
cISBN = in.nextLong();
in.nextLine();
}
catch (InputMismatchException e)
{
in.nextLine();
System.out.println("Error! Please enter a valid ISBN!\n");
continue;
}
bkLst.swap(bISBN, cISBN);
System.out.println("Displaying contents of main list: ");
bkLst.displayContent();
}
//choice 7
else if (userChoice == 7)
{
System.out.println("Displaying contents of main list: ");
bkLst.displayContent();
break;
}
else
{
System.out.println("Please enter an option presented by the Menu\n");
continue;
}
}
System.out.println("End of Program");
}
/**
* Method that displays menu to user
*/
public static void display_menu() {
System.out.println(
"1) Give me a year # and I would extract all records of that year and store them in a file for that year;\r\n"
+ "2) Ask me to delete all consecutive repeated records;\n"
+ "3) Give me an author name and I will create a new list with the records of this author and display them;\r\n"
+ "4) Give me an ISBN number and a Book object, and I will insert Node with the book before the record with this ISBN;\r\n"
+ "5) Give me 2 ISBN numbers and a Book object, and I will insert a Node between them, if I find them!\r\n"
+ "6) Give me 2 ISBN numbers and I will swap them in the list for rearrangement of records; of course if they exist!\r\n"
+ "7) Tell me to STOP TALKING. \n\n");
}
}