-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckout.java
More file actions
199 lines (157 loc) · 6.65 KB
/
Copy pathCheckout.java
File metadata and controls
199 lines (157 loc) · 6.65 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
import java.util.*;
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.*;
public class Checkout {
static Scanner scn = new Scanner(System.in);
public static void checkouts(ArrayList<Member> memberList) {
Member member = null;
System.out.print("Enter the ID of the member to checkout for: ");
String inputId = scn.nextLine().strip();
for(Member mem : memberList) {
if(inputId.equals(mem.getMemberID())) {
member = mem;
}
}
if(member == null) {
System.out.println("There was no member found with member ID " + inputId);
} else {
int id = Integer.parseInt(member.getMemberID());
boolean valid = false;
int[] total = {0};
boolean idExists = readCheckedOutItems(0, "-1", id, member, total);
if (idExists == true) {
System.out.println("The member's currently checked out items are: ");
for (int i = 0; i < 5; i++) {
String item = member.getCheckedOut(i);
if(!item.equals("0")) {
System.out.println(" *" + member.getCheckedOut(i));
} else {
System.out.println(" *Empty");
}
}
ArrayList<String> itemList = new ArrayList<>();
readCheckedOutItems(1, "-1", id, member, total);
if (total[0] < 5) {
int more = 5 - total[0];
System.out.printf("You can checkout %d more items.\n", more);
System.out.print("Would you like to checkout another item?(y/n) ");
String answer = scn.nextLine();
boolean validInput = false;
if (answer.toLowerCase().equals("y")) {
String newItemID = checkoutNewItem(scn);
readCheckedOutItems(1, newItemID, id, member, total);
} else {
File temp = new File("tempCheckedOut.txt");
temp.delete();
}
} else {
System.out.println("You already have the max amount of checked out items.");
}
} else {
System.out.println("The given member ID was not found: " + id);
}
}
}
private static String checkoutNewItem(Scanner scn) {
System.out.print("Enter the ISBN/ISSN of the item you want to checkout: ");
String itemID = scn.nextLine();
boolean valid = false;
File collections = new File("Collectiondatabase.txt");
String collectionID = "0";
try {
BufferedReader reader = new BufferedReader(new FileReader(collections));
String curr;
boolean found = false;
while ((curr = reader.readLine()) != null) {
String[] collectionInfo = curr.split(",");
if(collectionInfo.length < 1) {
continue;
}
collectionID = collectionInfo[0];
String isbn_sn = collectionInfo[1];
if (itemID.strip().equals(isbn_sn)) {
found = true;
break;
}
}
reader.close();
return collectionID;
} catch (IOException e) {
System.out.println("There was an error reading the checked out items.");
e.printStackTrace();
}
return collectionID;
}
private static boolean readCheckedOutItems(int toDo, String collectionID, int memberID, Member member, int[] total) {
total[0] = 0;
String curr;
int id = Integer.parseInt(member.getMemberID());
File temp = null;
File checkedOutItems = null;
boolean updated = false;
boolean returnValue = false;
try {
temp = new File("tempCheckedOut.txt");
checkedOutItems = new File("CheckedOutItems.txt");
BufferedReader reader = new BufferedReader(new FileReader(checkedOutItems));
BufferedWriter writer = new BufferedWriter(new FileWriter(temp));
while ((curr = reader.readLine()) != null) {
String[] currInfo = curr.split("\t");
int memID = Integer.parseInt(currInfo[0]);
if (toDo == 0 && id == memID) {
returnValue = true;
}
if (toDo == 1 && id == memID) {
for (int i = 1; i < 6; i++) {
if (!currInfo[i].equals("0")) {
total[0]++;
}
}
}
if (collectionID != "-1") {
if (memID == id) {
String content;
for (int i = 1; i < 6; i++) {
if (currInfo[i].equals("0")) {
currInfo[i] = collectionID;
member.setCheckedOut(i, collectionID);
for(String value : currInfo) {
writer.write(value + "\t");
}
updated = true;
writer.newLine();
break;
}
}
} else {
writer.write(curr);
writer.newLine();
}
}
}
reader.close();
writer.close();
performFileOperations(temp, checkedOutItems, updated);
} catch (IOException e) {
System.out.println("There was an error trying to update checked out items for member " + id + "\n");
e.printStackTrace();
}
return returnValue;
}
private static void performFileOperations(File temp, File checkedOutItems, boolean updated) {
if(updated == true) {
if (!checkedOutItems.delete()) {
System.out.println("Could not delete file: " + checkedOutItems.getName());
}
if (!temp.renameTo(checkedOutItems)) {
System.out.println("Could not rename file: " + temp.getName());
}
}
}
public static void closeScanner() {
scn.close();
}
}