-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.java
More file actions
754 lines (604 loc) · 19.4 KB
/
Data.java
File metadata and controls
754 lines (604 loc) · 19.4 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
/**
* A grouping of static methods used to manipulate and format data retrieved from the database.
*
* @author Joshua
* @version 1.0
*/
public class Data {
/**
* Gets a list of Member Objects from the database and puts their data into a nicely formated String.
*
* @return A String formated nicely to be printed to the screen
* @deprecated Replaced by listMembers(int order).
*/
public static String getCurrentFeeReport()
{
DB mydb = new DB();
mydb.updateDatabase();
ArrayList<Member> data = mydb.listMembers(0);
double totalOutstandingFees = 0;
int memberCount = data.size();
int residents = 0;
int nonResidents = 0;
Date date = new Date();
NumberFormat nFmt = NumberFormat.getCurrencyInstance();
StringBuilder report = new StringBuilder("The Library Of Yore\nMemberListing\n" + date.toString() + "\n\n");
for(int i = 0; i < 100; i++)
report.append("-");
report.append("\n");
report.append(String.format("%-10s | %-15s | %-15s | %-15s | %-10s | %-5s | %-15s\n",
"ID", "Last Name", "First Name", "Phone Number", "Fees Due", "Out", "Residency"));
for(int i = 0; i < 100; i++)
report.append("-");
report.append("\n");
for(Member m : data)
{
String first = m.getFirstName();
String last = m.getLastName();
String res;
String due = nFmt.format(m.getLateFeesDue());
if(first.length() > 15)
first = first.substring(0, 12) + "...";
if(last.length() > 15)
last = last.substring(0, 12) + "...";
if(m.isResident())
{
res = "resident";
residents ++;
}
else
{
res = "non-resident";
nonResidents ++;
}
totalOutstandingFees += m.getLateFeesDue();
report.append(String.format("%-10s | %-15s | %-15s | %-15s | %10s | %-5s | %-15s\n",
m.getMemberId(), last, first, m.getPhone(), due, m.getBooksOut(), res));
}
for(int i = 0; i < 100; i++)
report.append("-");
report.append("\n" + String.format("Residents: %-7s Non-Residents: %-7s Total Members: %-7s Total Fees Due: %-14s",
"" + residents, "" + nonResidents, "" + memberCount, nFmt.format(totalOutstandingFees)));
return report.toString();
}
/**
* Writes out the String s to a text file with a name beginning
* with the String str followed by _report_(Current Date in Milis)
*
* @param str
* The beginning of the file name
*
* @param s
* The String to be sent to a file
*
* @return A boolean signifying whether the file write was successful or not.
*/
public static boolean toFile(String str, String s)
{
boolean b = false;
try{
File f = new File(str + "_report_" + new Date().getTime() + ".txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f));
out.write(s);
b = true;
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
return b;
}
/**
* Gets a list of Book Objects from the database and puts their data into a nicely formated String.
* Designed for easier report generation than listBooks(int order, int limiter).
*
* @return A String formated nicely to be printed to the screen
*/
public static String getBookReport() {
DB mydb = new DB();
mydb.updateDatabase();
ArrayList<Book> data = mydb.listBooks(1);
int bookCount = data.size();
int available = 0;
int notAvailable = 0;
Date date = new Date();
StringBuilder report = new StringBuilder("The Library Of Yore\nBook Inventory Report\n" + date.toString() + "\n\n");
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
report.append(String.format("%-15s | %-20s | %-20s | %-10s | %15s\n",
"ID", "Title", "Author", "Editon", "Availability"));
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
for(Book bk : data)
{
String name = bk.getName();
String author = bk.getAuthor();
String edition = "N/A";
String availability = "";
if(bk.getEdition() != null)
{
edition = bk.getEdition();
if(edition.length() > 10)
edition = edition.substring(0, 7) + "...";
}
if(bk.isAvailable())
{
available++;
availability = "Available";
}
else
{
notAvailable++;
availability = "Not-Available";
}
if(name.length() > 20)
name = name.substring(0, 17) + "...";
if(author.length() > 20)
author = author.substring(0, 17) + "...";
if(edition.length() > 10)
author = author.substring(0, 7) + "...";
report.append(String.format("%-15s | %-20s | %-20s | %-10s | %15s\n",
bk.getBookId(), name, author, edition, availability));
}
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n" + String.format("Available: %-12s Not-Available: %-9s Total Books: %-15s",
"" + available, "" + notAvailable, "" + bookCount));
return report.toString();
}
/**
* Adds the specified Book Object to the data base and returns the updated book listing.
*
* @param book
* The Book Object to ad to the database
*
* @return The String generated by getBookReport() after the book is added.
*/
public static String add(Book book) {
DB mydb = new DB();
mydb.add(book);
mydb.updateDatabase();
return getBookReport();
}
/**
* Deletes the Book Object with the specified Book Id from the database and returns the updated book listing.
*
* @param id
* The Id of the book to be deleted
*
* @return The String generated by getBookReport() after the book is deleted.
*/
public static String delBook(int id) {
DB mydb = new DB();
mydb.delBook(id);
mydb.updateDatabase();
return getBookReport();
}
/**
* Gets a list of Book Objects that are found in the database based on the search parameters
* and puts their data into a nicely formated String.
*
* @param s
* The String to search for
*
* @param option
* Where to search: refer to DB.findBooks() for full details
*
* @return A String formated nicely to be printed to the screen
*/
public static String findBooks(String s, int option) {
DB mydb = new DB();
mydb.updateDatabase();
ArrayList<Book> data = mydb.findBooks(s, option);
int bookCount = data.size();
int available = 0;
int notAvailable = 0;
Date date = new Date();
StringBuilder report = new StringBuilder("The Library Of Yore\n" + "Search Results \"" + s + "\"\n" + date.toString() + "\n\n");
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
report.append(String.format("%-15s | %-20s | %-20s | %-10s | %15s\n",
"ID", "Title", "Author", "Editon", "Availability"));
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
for(Book bk : data)
{
String name = bk.getName();
String author = bk.getAuthor();
String edition = "N/A";
String availability = "";
if(bk.getEdition() != null)
{
edition = bk.getEdition();
if(edition.length() > 10)
edition = edition.substring(0, 7) + "...";
}
if(bk.isAvailable())
{
available++;
availability = "Available";
}
else
{
notAvailable++;
availability = "Not-Available";
}
if(name.length() > 20)
name = name.substring(0, 17) + "...";
if(author.length() > 20)
author = author.substring(0, 17) + "...";
report.append(String.format("%-15s | %-20s | %-20s | %-10s | %15s\n",
bk.getBookId(), name, author, edition, availability));
}
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n" + String.format("Available: %-12s Not-Available: %-9s Total Matches: %-15s",
"" + available, "" + notAvailable, "" + bookCount));
return report.toString();
}
/**
* Gets a list of the specified Book Objects from the database and puts
* their data into a nicely formated String in the specified order.
*
* @param order
* specifies order: refer to DB.listBooks() for full details
*
* @param limiter
* limits the listing: if limiter = 1, show Available;
* if limiter = 2, show non-available; if limit equals anything else show all.
*
* @return A String formated nicely to be printed to the screen
*/
public static String listBooks(int order, int limiter) {
DB mydb = new DB();
mydb.updateDatabase();
ArrayList<Book> data = mydb.listBooks(order);
int bookCount = data.size();
int available = 0;
int notAvailable = 0;
Date date = new Date();
String lmt, ord;
if(limiter == 1)
lmt = "Available";
else if(limiter == 2)
lmt = "Not-available";
else
lmt = "All";
if(order == 1)
ord = "Id";
else if(order == 2)
ord = "Title";
else
ord = "Author";
StringBuilder report = new StringBuilder("The Library Of Yore\n" + lmt + " Books Sorted by " + ord + "\n" + date.toString() + "\n\n");
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
report.append(String.format("%-15s | %-20s | %-20s | %-10s | %15s\n",
"ID", "Title", "Author", "Editon", "Availability"));
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
for(Book bk : data)
{
String name = bk.getName();
String author = bk.getAuthor();
String edition = "N/A";
String availability = "";
if(bk.getEdition() != null)
{
edition = bk.getEdition();
if(edition.length() > 10)
edition = edition.substring(0, 7) + "...";
}
if(name.length() > 20)
name = name.substring(0, 17) + "...";
if(author.length() > 20)
author = author.substring(0, 17) + "...";
if(edition.length() > 10)
author = author.substring(0, 7) + "...";
if(bk.isAvailable())
{
available++;
availability = "Available";
}
else
{
notAvailable++;
availability = "Not-Available";
}
if(limiter == 3 && !bk.isAvailable())
{
report.append(String.format("%-15s | %-20s | %-20s | %-10s | %15s\n",
bk.getBookId(), name, author, edition, availability));
}
else if(limiter == 2 && bk.isAvailable())
{
report.append(String.format("%-15s | %-20s | %-20s | %-10s | %15s\n",
bk.getBookId(), name, author, edition, availability));
}
else if(limiter == 1)
{
report.append(String.format("%-15s | %-20s | %-20s | %-10s | %15s\n",
bk.getBookId(), name, author, edition, availability));
}
}
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n" + String.format("Available: %-12s Not-Available: %-9s Total Books: %-15s",
"" + available, "" + notAvailable, "" + bookCount));
return report.toString();
}
/**
* Deletes the Member Object with the specified member id from the database and returns the updated member listing.
*
* @param id
* The Id of the member to be deleted
*
* @return A String formated nicely to be printed to the screen.
*/
public static String delMember(int id) {
DB mydb = new DB();
mydb.delMember(id);
mydb.updateDatabase();
return listMembers(0);
}
/**
* Gets a list of Member Objects that are found in the database based on the
* search parameters and puts their data into a nicely formated String.
*
* @param s
* The String to search for
*
* @param option
* @return Specifies search option: refer to DB.findMembers() for full details
*/
public static String findMembers(String s, int option) {
DB mydb = new DB();
mydb.updateDatabase();
ArrayList<Member> data = mydb.findMembers(s, option);
double totalOutstandingFees = 0;
int memberCount = data.size();
int residents = 0;
int nonResidents = 0;
Date date = new Date();
NumberFormat nFmt = NumberFormat.getCurrencyInstance();
StringBuilder report = new StringBuilder("The Library Of Yore\nOutstanding Fees Report\n" + date.toString() + "\n\n");
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
report.append(String.format("%-10s | %-15s | %-15s | %-15s | %-10s | %-5s | %-15s\n",
"ID", "Last Name", "First Name", "Phone Number", "Fees Due", "Out", "Residency"));
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
for(Member m : data)
{
String first = m.getFirstName();
String last = m.getLastName();
String res;
String due = nFmt.format(m.getLateFeesDue());
if(first.length() > 15)
first = first.substring(0, 12) + "...";
if(last.length() > 15)
last = last.substring(0, 12) + "...";
if(m.isResident())
{
res = "resident";
residents ++;
}
else
{
res = "non-resident";
nonResidents ++;
}
totalOutstandingFees += m.getLateFeesDue();
report.append(String.format("%-10s | %-15s | %-15s | %-15s | %-10s | %-5s | %-15s\n",
m.getMemberId(), last, first, m.getPhone(), due, m.getBooksOut(), res));
}
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n" + String.format("Residents: %-7s Non-Residents: %-7s Total Members: %-7s Total Fees Due: %-14s",
"" + residents, "" + nonResidents, "" + memberCount, nFmt.format(totalOutstandingFees)));
return report.toString();
}
/**
* Adds the specified Member Object to the data base and returns the updated member listing.
*
* @param member
* The Member Object to add to the database
*
* @return The String generated by listMembers(0) after adding the member.
*/
public static String add(Member member) {
DB mydb = new DB();
mydb.updateDatabase();
mydb.add(member);
return listMembers(0);
}
/**
* Gets a list of Member Objects from the database and puts their data into a nicely formated String in the specified order.
*
* @param order
* The order to list by: refer to DB.listMembers() for more details
*
* @return A nicely formated String in an order specified by the option parameter
*/
public static String listMembers(int order) {
DB mydb = new DB();
mydb.updateDatabase();
ArrayList<Member> data = mydb.listMembers(order);
double totalOutstandingFees = 0;
int memberCount = data.size();
int residents = 0;
int nonResidents = 0;
Date date = new Date();
NumberFormat nFmt = NumberFormat.getCurrencyInstance();
StringBuilder report = new StringBuilder("The Library Of Yore\nOutstanding Fees Report\n" + date.toString() + "\n\n");
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
report.append(String.format("%-10s | %-15s | %-15s | %-15s | %-10s | %-5s | %-15s\n",
"ID", "Last Name", "First Name", "Phone Number", "Fees Due", "Out", "Residency"));
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
for(Member m : data)
{
String first = m.getFirstName();
String last = m.getLastName();
String res;
String due = nFmt.format(m.getLateFeesDue());
if(first.length() > 15)
first = first.substring(0, 12) + "...";
if(last.length() > 15)
last = last.substring(0, 12) + "...";
if(m.isResident())
{
res = "resident";
residents ++;
}
else
{
res = "non-resident";
nonResidents ++;
}
totalOutstandingFees += m.getLateFeesDue();
report.append(String.format("%-10s | %-15s | %-15s | %-15s | %-10s | %-5s | %-15s\n",
m.getMemberId(), last, first, m.getPhone(), due, m.getBooksOut(), res));
}
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n" + String.format("Residents: %-7s Non-Residents: %-7s Total Members: %-7s Total Fees Due: %-14s",
"" + residents, "" + nonResidents, "" + memberCount, nFmt.format(totalOutstandingFees)));
return report.toString();
}
/**
* Executes the makePayment(int memberId, double payment) from the DB class and returns the change in a nicely formated String.
*
* @param memberId
* The Id of the member making the payment
*
* @param payment
* The amount being payed
*
* @return The change after payment in a nicely formated String.
*/
public static String makePayment(int memberId, double payment) {
DB mydb = new DB();
mydb.updateDatabase();
double chng;
String change;
NumberFormat nbf = NumberFormat.getCurrencyInstance();
chng = mydb.makePayment(memberId, payment);
change = nbf.format(chng);
return change;
}
/**
* Gets the member and book object specified from the database and uses
* them to construct a new Borrow Object. This new Borrow Object is then added to the database its self.
*
* @param memberId
* The Id of the member making the borrow
*
* @param bookId
* The Id of the book being borrowed
*
* @return The String generated by listBorrows(false) after the Borrow Object is added
*/
public static String add(int memberId, int bookId) {
DB mydb = new DB();
mydb.updateDatabase();
Member m = mydb.findMembers("" + memberId, 2).get(0);
Book b = mydb.findBooks("" + bookId, 2).get(0);
Borrow borrow = new Borrow(0, m, b, new Date());
mydb.borrowBook(borrow);
return listBorrows(false);
}
/**
* Executes the returnBook(int bookId) from the DB class passing it
* the specified id and returns an updated member listing
*
* @param bookId
* The Id of the book being returned.
*
* @return The String generated by listBorrows(false) after the return is complete
*/
public static String returnBook(int bookId)
{
DB mydb = new DB();
mydb.returnBook(bookId);
return listBorrows(false);
}
/**
* Gets an ArrayList of Borrow Objects from the database and lists them in a nicely formated string
* limited by the boolean passed in.
*
* @param b
* A limiter on what is being returned: if b = true, List all borrows; b = false, List only open borrows
* @return A String formated nicely to be printed to the screen
*/
public static String listBorrows(boolean b) {
DB mydb = new DB();
mydb.updateDatabase();
ArrayList<Borrow> data = mydb.listBorrows(1);
int allBorrows = data.size();
int open = 0;
int closed = 0;
String listType = "";
Date date = new Date();
if(b)
listType = "Listing All Borrows";
else
listType = "Listing Open Borrows";
StringBuilder report = new StringBuilder("The Library Of Yore\n" + listType + "\n" + date.toString() + "\n\n");
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
report.append(String.format("%-15s | %-25s | %-20s | %-10s | %10s\n",
"ID", "Member Name", "Book Name", "Borrowed", "Returned"));
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n");
for(Borrow br : data)
{
String memberName = br.getMember().getLastName() + ", " + br.getMember().getFirstName();
String bookName = br.getBook().getName();
String dateReturned = "Not Returned";
if(memberName.length() > 25)
memberName = memberName.substring(0, 22) + "...";
if(bookName.length() > 20)
bookName = bookName.substring(0, 17) + "...";
if(br.getDateReturned() != null)
{
closed ++;
dateReturned = br.getDateReturned().toString();
if(b)
report.append(String.format("%-15s | %-25s | %-20s | %-10s | %10s\n",
br.getBorrowId(), memberName, bookName, br.getDateBorrowed(), dateReturned));
}
else
{
open++;
report.append(String.format("%-15s | %-25s | %-20s | %-10s | %10s\n",
br.getBorrowId(), memberName, bookName, br.getDateBorrowed(), dateReturned));
}
}
for(int i = 0; i < 95; i++)
report.append("-");
report.append("\n" + String.format("Closed: %-12s Open: %-9s Total Borrows To Date: %-15s",
"" + closed, "" + open, "" + allBorrows));
return report.toString();
}
}