-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibraryCatalogue.java
108 lines (88 loc) · 3.48 KB
/
LibraryCatalogue.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
package librarycatalogue;
import java.util.HashMap;
import java.util.Map;
public class LibraryCatalogue {
Map<String,Book> bookCollection = new HashMap<String,Book>();
int currentDay = 0;
int lengthOfCheckoutPeriod = 7;
double initialLateFee = 0.50;
double feePerLateDay = 1.00;
// Constructors
public LibraryCatalogue(Map<String,Book> collection) {
this.bookCollection = collection;
}
public LibraryCatalogue(Map<String,Book> collection, int lengthOfCheckoutPeriod,
double initialLateFee, double feePerLateDay) {
this.bookCollection = collection;
this.lengthOfCheckoutPeriod = lengthOfCheckoutPeriod;
this.initialLateFee = initialLateFee;
this.feePerLateDay = feePerLateDay;
}
// Getters
public int getCurrentDay() {
return this.currentDay;
}
public Map<String,Book> getBookCollection() {
return this.bookCollection;
}
public Book getBook(String bookTitle) {
return getBookCollection().get(bookTitle);
}
public int getLengthOfCheckoutPeriod() {
return this.lengthOfCheckoutPeriod;
}
public double getInitialLateFee() {
return this.initialLateFee;
}
public double getFeePerLateDay() {
return this.feePerLateDay;
}
// Setters
public void nextDay() {
currentDay++;
}
public void setDay(int day) {
currentDay = day;
}
// Instance methods
public void checkOutBook(String title) {
Book book = getBook(title);
if (book.getIsCheckedOut()) {
sorryBookAlreadyCheckedOut(book);
} else {
book.setIsCheckedOut(true, currentDay);
System.out.println("You just checked out " + title + ". It is due on day "+
(getCurrentDay() + getLengthOfCheckoutPeriod())+ "." );
}
}
public void returnBook(String title) {
Book book = getBook(title);
int daysLate = currentDay - (book.getDayCheckedOut() + getLengthOfCheckoutPeriod());
if (daysLate > 0) {
System.out.println("You owe the library £" + (getInitialLateFee() + daysLate * getFeePerLateDay()) +
" because your book is " + daysLate + " days overdue.");
} else {
System.out.println("Book Returned. Thank you.");
}
book.setIsCheckedOut((false), -1);
}
public void sorryBookAlreadyCheckedOut(Book book) {
System.out.println("Sorry, " + book.getTitle() + " is already checked out. "
+ "It should be back on day " + (book.getDayCheckedOut() +
getLengthOfCheckoutPeriod())+ ".");
}
public static void main(String[] args) {
// TODO code application logic here
Map<String,Book> bookCollection = new HashMap<String,Book>();
Book harry = new Book("Harry Potter", 827132, 9999999);
bookCollection.put("Harry Potter", harry);
LibraryCatalogue lib = new LibraryCatalogue(bookCollection);
lib.checkOutBook("Harry Potter");
lib.nextDay();
lib.nextDay();
lib.checkOutBook("Harry Potter");
lib.setDay(17);
lib.returnBook("Harry Potter");
lib.checkOutBook("Harry Potter");
}
}