-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataProvider.java
368 lines (326 loc) · 11.8 KB
/
DataProvider.java
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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/*
* Student name: Ross Petridis
* Student ID: 1080249
* LMS username: rpetridis
*/
/**
* This class allows an instation of the bills and member data into a datastore
* object, it also facilitates organisation and maintenance of data.
* Many methods in this class are used extensively by other classes and so are
* public - but not at the expense of data security.
* All direct modificaitons to the data are peformed internally to this class
* for security.
*
* @author Ross Petridis
*/
public class DataProvider {
private String memberFile;
private String billFile;
private ArrayList<Member> members;
private ArrayList<Bill> bills;
private static final int NUM_MEMBER_DATA = 3;
private static final int NUM_BILL_DATA = 4;
private static final int MEMBER_ID_IDX = 0;
private static final int MEMBER_NAME_IDX = 1;
private static final int MEMBER_ADRESS_IDX = 2;
private static final int BILL_ID_IDX = 0;
private static final int BILL_MEMBER_ID_IDX = 1;
private static final int BILL_TOTAL_IDX = 2;
private static final int BILL_USED_IDX = 3;
/**
*
* @param memberFile A path to the member file (e.g., members.csv)
* @param billFile A path to the bill file (e.g., bills.csv)
* @throws DataAccessException If a file cannot be opened/read
* @throws DataFormatException If the format of the the content is incorrect
*/
public DataProvider(String memberFile, String billFile) throws DataAccessException, DataFormatException {
// Add your code here
this.memberFile = memberFile;
this.billFile = billFile;
getMembersFromFile();
getBillsFromFile();
}
/**
* Copy constructor that returns new copies of objects rather than the original.
* These copy object would not be used for changing data, since they wont be
* changing the real database, but a copy.
* Instead, any copies would be purely for reading.
*
* @param data The data class to return a copy of.
*/
public DataProvider(DataProvider data) {
this.billFile = data.billFile;
this.memberFile = data.memberFile;
this.members = data.getMembers();
this.bills = data.getBills();
}
/**
* Given a member Id, retrieve this member
*
* @param memberID ID of the member to retrieve.
* @return The member
* @throws MemberDoesNotExist if caanot find a member with ID memberID
*/
public Member getMember(String memberID) throws MemberDoesNotExist {
Iterator<Member> iter = members.iterator();
Member thisMember;
while (iter.hasNext()) {
thisMember = iter.next();
if (thisMember.hasID(memberID)) {
return thisMember;
}
}
// scanned through all members and still here - member doesn't exist.
throw new MemberDoesNotExist(memberID);
}
/**
* Recieves a potential billID and retrns true if the bill exists
*
* @param billID
* @return
*/
public boolean billExists(String billID) {
Iterator<Bill> iter = bills.iterator();
while (iter.hasNext()) {
if (iter.next().hasId(billID)) {
return true;
}
}
return false;
}
/**
* Helper function that returns a bill with given BillID that is previously
* known to exist - hence no need for try catch of custom BillDoesNotExist
*
* @param billId
* @return The bill with ID billId
*/
private Bill getActualBillObject(String billId) {
Iterator<Bill> iter = bills.iterator();
Bill thisBill = null;
while (iter.hasNext()) {
thisBill = iter.next();
if (thisBill.hasId(billId)) {
return thisBill; // not returning copy as this is a private helper for this class only
}
}
System.out.println("failed to find bill that was assumed to exist. Exiting (1).");
System.exit(1);
return thisBill;
}
/**
* sets a bill to used
*
* @param billID the bill id to set to used
*/
public void setBillToUsed(String billID) {
getActualBillObject(billID).useBill();
}
/**
* Checks if a bill has been used or not
*
* @param billID
* @return
*/
public boolean billHasBeenUsed(String billID) {
Bill bill = getBillThatExists(billID);
return bill.hasBeenUsed();
}
/**
* Reads members from file into an arrray list of members
*
* @throws DataAccessException if function cannot access the file
* @throws DataFormatException if format of the file does not match the expected
* format
*/
private void getMembersFromFile() throws DataAccessException, DataFormatException {
// Set up connection
Scanner memberStream;
try {
memberStream = new Scanner(new FileInputStream(memberFile));
} catch (FileNotFoundException e) {
throw new DataAccessException(memberFile); // use default constructor
}
// read and parse members
members = new ArrayList<Member>();
String memberLine;
String[] memberData;
int lineNum = 0;
while (memberStream.hasNextLine()) { // while still members to add.
lineNum++;
memberLine = memberStream.nextLine();
memberData = memberLine.split(",");
if (memberData.length != NUM_MEMBER_DATA) {
throw new DataFormatException("Expecting exactly " + NUM_MEMBER_DATA +
" piececs of information per member. Got " + memberData.length + " in line " + lineNum +
". DataProvider requires a Member ID, name, and adress");
}
if (emptyFieldFound(memberData)) {
throw new DataFormatException("Empty field found in line " + lineNum + ". DataProvider" +
" requires a Member ID, name, and adress");
}
// else, got 3 strings.
members.add(new Member(
memberData[MEMBER_ID_IDX],
memberData[MEMBER_NAME_IDX], // member name
memberData[MEMBER_ADRESS_IDX] // member address
));
}
memberStream.close();
}
/**
* Reads bills from file and stores in arraylist of bills
*
* @throws DataAccessException if cannot access the bills file
* @throws DataFormatException if supplied format does match exapected format.
*/
private void getBillsFromFile() throws DataAccessException, DataFormatException {
// Set up connection
Scanner billStream;
try {
billStream = new Scanner(new FileInputStream(billFile));
} catch (FileNotFoundException e) {
throw new DataAccessException(billFile);
}
// read and parse bills
bills = new ArrayList<Bill>();
String billLine;
String[] billData;
while (billStream.hasNextLine()) {
// read this bill
billLine = billStream.nextLine();
billData = billLine.split(",");
if (billData.length != NUM_BILL_DATA) {
throw new DataFormatException("Expecting exactly " + NUM_BILL_DATA +
"piececs of information per bill. Got " + billData.length);
}
try { // try parse entry to make a bill.
bills.add(
new Bill(
billData[BILL_ID_IDX],
billData[BILL_MEMBER_ID_IDX], // by keeping string, can avoid errors of parsing into Int
// '""' and also
// might want t use chars in id
Float.parseFloat(billData[BILL_TOTAL_IDX]),
Boolean.parseBoolean(billData[BILL_USED_IDX])));
} catch (Exception e) { // weird format recieved
throw new DataFormatException(
"Error parsing Bill data. BillLine = " + billLine + "\nError:" + e.getMessage());
}
}
billStream.close();
}
/**
* retrive bill object that is previously known to exist.
*
* @param billID
* @return
*/
public Bill getBillThatExists(String billID) {
Bill bill = getActualBillObject(billID);
return new Bill(bill);
}
/**
* Given a bill ID, retrieve the assocaited bill object, or throw a
* Bill does not exist error
*
* @param billID The inputed ID to find the associated bill for
* @return The associated bill object
* @throws BillDoesNotExist if the bill being searched for does not exist
*/
public Bill getBill(String billID) throws BillDoesNotExist {
Iterator<Bill> iter = bills.iterator();
Bill thisBill;
while (iter.hasNext()) {
thisBill = iter.next();
if (thisBill.hasId(billID)) {
return new Bill(thisBill); // return a copy for security reasons.
}
}
// member doesnt exist if still here
throw new BillDoesNotExist(billID);
}
/**
* Returns
*
* @param billID
* @return true iff the inputted bill ID has a member associated with it
*/
public boolean billHasMember(String billID) {
Bill bill = null;
try {
bill = getBill(billID);
} catch (BillDoesNotExist e) {
System.out.println("Bill: " + billID + " does not exist");
}
return bill.hasMember();
}
/**
*
* @return a copy of the Bills
*/
public ArrayList<Bill> getBills() {
ArrayList<Bill> bills_copy = new ArrayList<Bill>();
Iterator<Bill> iter = bills.iterator();
while (iter.hasNext()) {
bills_copy.add(new Bill(iter.next()));
}
return bills_copy;
}
/**
*
* @return a copy of the members
*/
public ArrayList<Member> getMembers() {
ArrayList<Member> members_copy = new ArrayList<Member>();
Iterator<Member> iter = members.iterator();
while (iter.hasNext()) {
members_copy.add(new Member(iter.next()));
}
return members_copy;
}
/**
* Writes updates to file by re writing the entire file. The same file used for
* reading. This is to help
* kepe track of bills that has been used.
*/
public void updateBillsFile() {
PrintWriter billOut = null;
try {
billOut = new PrintWriter(new FileOutputStream(billFile));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
for (Bill bill : bills) { // re write each bill
try {
billOut.println(bill.getId() + "," + bill.getMemberId() + "," + bill.getTotalAmount() +
"," + (bill.hasBeenUsed() ? "true" : "false"));
} catch (Exception e) {
System.out.println("Failed to write bill of billID :" + bill.getId() + " Error: " + e.getMessage());
}
}
billOut.close();
}
/**
* Recieves an array of string and returns true if there is an empty field
*
* @param data array of string to check for empty fields in
* @return true iff theres an empty string in data.
*/
private boolean emptyFieldFound(String[] data) {
for (String str : data) {
if (str == "") {
return true;
}
}
return false;
}
}