-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryManager.java
More file actions
278 lines (266 loc) · 7.39 KB
/
Copy pathLibraryManager.java
File metadata and controls
278 lines (266 loc) · 7.39 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
/**
* Main method to perform function based on user input and store changes to file
* @author Kelly Chen
* @version 0.1
* CSE 017 Project 1
* Date of creation: February 28, 2022
* Last Date Modified: February 28, 2022
*/
package Project1;
import java.util.*;
public class LibraryManager {
public static void main(String[] args)
{
Catalog myLibrary = new Catalog();
//Prompt User Input
Scanner scan = new Scanner(System.in);
int input = 0;
//Read in the text file
myLibrary.readFile();
//Operate until exit or error
while(input != 9)
{
//Menu
System.out.println("\nSelect an operation:");
System.out.println("1. View all titles");
System.out.println("2. Search by call number");
System.out.println("3. Search by title");
System.out.println("4. Search by year");
System.out.println("5. Add new title");
System.out.println("6. Remove title");
System.out.println("7. Sort titles by year");
System.out.println("8. View titles due for restoration");
System.out.println("9. Exit");
//take in user input
if(scan.hasNextInt())
{
input = scan.nextInt();
//check for user input and execute corresponding actions
switch(input)
{
case 1: //Print all titles
myLibrary.viewTitles();
break;
case 2: //Search By Call Number
try
{
System.out.println("Enter a Call Number:");
System.out.println(myLibrary.searchByCallNumber(scan.next()));
}
catch (InvalidDataException e)
{
System.out.println(e.getMessage());
}
break;
case 3: //Search by Title
System.out.println("Enter a Title:");
scan.nextLine(); //buffer
myLibrary.searchByTitle(scan.nextLine());
break;
case 4: //Search by Year
try
{
System.out.println("Enter a year:");
if(scan.hasNextInt()) //Check if it's integer
myLibrary.searchByYear(scan.nextInt());
else
{
System.out.println("Must be a valid integer");
scan.next();
break;
}
}
catch (InvalidDataException e) //catch if it's invalid year
{
System.out.println(e.getMessage());
}
break;
case 5: //Add Title
System.out.println("Enter the title:");
scan.nextLine(); //buffer
String title = scan.nextLine();
System.out.println("Enter the publisher:");
String publisher = scan.nextLine();
//Check and validate year
int year = 0;
try
{
System.out.println("Enter the year of publication:");
if(scan.hasNextInt()) //check if it's integer
{
year = scan.nextInt();
myLibrary.checkYear(year);
}
else
{
System.out.println("Must be a valid integer");
scan.next();
break;
}
}
catch (InvalidDataException e)
{
System.out.println(e.getMessage());
break;
}
//Check and validate that copies is an integer
System.out.println("Enter the number of copies:");
int copies = 0;
if(scan.hasNextInt())
{
copies = scan.nextInt();
}
else
{
System.out.println("Must be an integer!");
scan.next();
break;
}
//Check that type can only be book or periodical
String type = "";
scan.nextLine();
try
{
System.out.println("Enter the type of title (book/periodical):");
type = scan.nextLine();
myLibrary.checkType(type);
}
catch (InvalidDataException e)
{
System.out.println(e.getMessage());
break;
}
//Check and validate call number
String callNumber = "";
if(type.equalsIgnoreCase("book")) //Book
{
try
{
System.out.println("Enter the call number (B-ddd-ddd-ddd):");
callNumber = scan.nextLine();
myLibrary.checkBookCallNumber(callNumber);
}
catch (InvalidDataException e)
{
System.out.println(e.getMessage());
break;
}
}
else //Periodical
{
try
{
System.out.println("Enter the call number (P-ddd-ddd-ddd):");
callNumber = scan.nextLine();
myLibrary.checkPeriodicalCallNumber(callNumber);
}
catch (InvalidDataException e)
{
System.out.println(e.getMessage());
break;
}
}
//Depending on type, determine what other inputs are needed
if(type.equalsIgnoreCase("book")) //Book
{
System.out.println("Enter the author:");
String author = scan.nextLine();
String ISBN = "";
//Validate ISBN
try
{
System.out.println("Enter ISBN (10 digits):");
ISBN = scan.nextLine();
myLibrary.checkISBN(ISBN);
}
catch (InvalidDataException e)
{
System.out.println(e.getMessage());
break;
}
myLibrary.addBook(callNumber, title, publisher, year, copies, author, ISBN);
System.out.println("Title added successfully.");
}
else //periodical
{
int month = 0;
scan.nextLine();
//Validate periodical
try
{
System.out.println("Enter the month:");
if(scan.hasNextInt())
{
month = scan.nextInt();
myLibrary.checkMonth(month);
}
else
{
System.out.println("Must be a valid integer!");
scan.next();
break;
}
}
catch (InvalidDataException e)
{
System.out.println(e.getMessage());
break;
}
int issueNumber = 0;
if(scan.hasNextInt())
{
System.out.println("Enter the issue number:");
if(scan.hasNextInt())
{
issueNumber = scan.nextInt();
}
}
else
{
System.out.println("Must be a valid interger!");
scan.next();
break;
}
myLibrary.addPeriodical(callNumber, title, publisher, year, copies, month, issueNumber);
System.out.println("Title added successfully.");
}
break;
case 6: //Remove
try
{
System.out.println("Enter the call number (B-ddd-ddd-ddd or P-ddd-ddd-ddd):");
String check = scan.next();
if(check.charAt(0) == 'B')
myLibrary.checkBookCallNumber(check);
else
myLibrary.checkPeriodicalCallNumber(check);
myLibrary.remove(check);
}
catch (InvalidDataException e)
{
System.out.println(e.getMessage());
}
break;
case 7: //Sort by year
myLibrary.sortByYear();
myLibrary.viewTitles();
break;
case 8: //check and print all restorables
System.out.println(myLibrary.restoration());
break;
case 9: //Write and save the file
myLibrary.writeFile();
System.out.println("Thank you for using my program. Bye!");
System.exit(0);
default: //wrong input
System.out.println("Invalid operation. Try again.");
}
}
else //Invalid input choices
{
System.out.println("Invalid operation. Try again.");
scan.next();
}
}
}
}