-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryUI.java
More file actions
202 lines (156 loc) · 4.85 KB
/
LibraryUI.java
File metadata and controls
202 lines (156 loc) · 4.85 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
import java.util.Scanner;
public class LibraryUI {
private static final String WELCOME_MESSAGE = "Welcome to our Library";
private String[] mainMenuOptions = {"Create Account", "Login", "Find Item","Checkout Item","Rate an Item","Pay a Fine","Logout"};
private Scanner scanner;
private Library library;
LibraryUI(){
scanner = new Scanner(System.in);
library = new Library();
}
public void run() {
System.out.println(WELCOME_MESSAGE);
//Loop as long as we want to keep interacting with the library
while(true) {
displayMainMenu();
int userCommand = getUserCommand(mainMenuOptions.length);
if(userCommand == -1) {
System.out.println("Not a valid command");
continue;
}
//if they picked the last option then log them out
if(userCommand == mainMenuOptions.length -1) {
library.logout();
break;
}
switch(userCommand) {
case(0):
createAccount();
break;
case(1):
login();
break;
case(2):
findItem();
break;
case(3):
checkoutItem();
break;
case(4):
rateItem();
break;
case(5):
payFine();
break;
}
}
System.out.println("Good bye, and have a nice day");
}
private void displayMainMenu() {
System.out.println("\n************ Main Menu *************");
for(int i=0; i< mainMenuOptions.length; i++) {
System.out.println((i+1) + ". " + mainMenuOptions[i]);
}
System.out.println("\n");
}
//get the users command number, if it's not valid, return -1
private int getUserCommand(int numCommands) {
System.out.print("What would you like to do?: ");
String input = scanner.nextLine();
int command = Integer.parseInt(input) - 1;
if(command >= 0 && command <= numCommands -1) return command;
return -1;
}
private void createAccount() {
String userName = getField("Username");
String firstName = getField("First Name");
String lastName = getField("Last Name");
int age = Integer.parseInt(getField("Age"));
String phoneNumber = getField("Phone Number");
if(library.createAccount(userName, firstName, lastName, age, phoneNumber)) {
System.out.println("You have successfully created an account");
} else {
System.out.println("Sorry an account with that username already exists");
}
}
private void login() {
String userName = getField("Username");
if(library.login(userName)) {
User currentUser = library.getCurrentUser();
System.out.println("Welcome " + currentUser.getFirstName() + " " + currentUser.getLastName() + "!");
} else {
System.out.println("Sorry, invalid username ");
}
}
private String getField(String prompt) {
System.out.print(prompt + ": ");
return scanner.nextLine();
}
private void findItem() {
System.out.println("\n-----Searching the Library-----");
String item = getUserItem();
if(item == null)return;
if(!library.findItem(item)) {
System.out.println("Sorry we couldn't find your item\n");
return;
}
System.out.println("YAY your item is in the library\n");
}
private void checkoutItem() {
System.out.println("\n-----Checking out an item-----");
String item = getUserItem();
if(item == null)return;
if(!library.checkout(item)) {
System.out.println("Sorry we couldn't checkout your item\n");
return;
}
System.out.println("Your item was successfully checked out\n");
}
private void rateItem() {
System.out.println("\n-----Searching the Library-----");
String item = getUserItem();
if(item == null)return;
//get rating
System.out.print("Enter rating: ");
int rating = Integer.parseInt(scanner.nextLine());
if(rating < 0 || rating > 5) {
System.out.println("Not a valid rating\n");
return;
}
if(!library.rateItem(item, rating)) {
System.out.println("Sorry we couldn't rate your item\n");
return;
}
System.out.println("Item was successfully rated\n");
}
private void payFine() {
System.out.println("-----Paying a fine-----");
//get amount
System.out.print("Enter amount: ");
int amount = Integer.parseInt(scanner.nextLine());
if(amount < 0) {
System.out.println("Not a valid amount\n");
return;
}
if(!library.payFine(amount)) {
System.out.println("Sorry, you were not able to pay this fine.\n");
return;
}
System.out.println("Fine paid\n");
}
private String getUserItem() {
System.out.print("Enter Item Name: ");
while(true) {
String itemName = scanner.nextLine().trim().toLowerCase();
if(!itemName.contentEquals("")) return itemName;
System.out.println("You need to actually enter content");
System.out.print("Would you like to enter item again (y) or return to main menu (n): ");
String command = scanner.nextLine().trim().toLowerCase();
if(command == "n") return null;
}
}
public static void main(String[] args) {
LibraryUI libraryInterface = new LibraryUI();
libraryInterface.run();
}
}