-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBorrowbook
More file actions
119 lines (103 loc) · 3.78 KB
/
Borrowbook
File metadata and controls
119 lines (103 loc) · 3.78 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
public class Library {
String address;
java.util.ArrayList<Book> books;
public static final String openTime = "9am";
public static final String closeTime = "5pm";
public Library(String libraryAddress) {
address = libraryAddress;
books = new java.util.ArrayList<Book>();
}
public void addBook(Book book) {
books.add(book);
}
public static void printOpeningHours() {
System.out.println("Libraries are open daily from " + openTime + " to " + closeTime);
}
public void printAddress() {
System.out.println(address);
}
public void printAvailableBooks() {
boolean bookPresent = false;
for (Book book : books) {
if (!book.isBorrowed()) {
System.out.println(book.getTitle());
bookPresent = true;
}
}
if (!bookPresent) {
System.out.println("No book in catalog");
}
}
public void borrowBook(String title) {
int found = 0;
for (Book book : books) {
if (book.getTitle().equals(title)) {
if (found == 0) {
found = 1;
}
if (!book.isBorrowed()) {
book.borrowed();
found = 2;
break;
};
}
}
if (found == 0) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (found == 1) {
System.out.println("Sorry, this book is already borrowed.");
} else if (found == 2) {
System.out.println("You successfully borrowed " + title);
}
}
public void returnBook(String title) {
boolean found = false;
for (Book book : books) {
if (book.getTitle().equals(title) && book.isBorrowed()) {
book.returned();
found = true;
break;
}
}
if (found) {
System.out.println("You successfully returned " + title);
}
}
public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
// Add four books to the first library
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));
// Print opening hours and the addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
System.out.println("Borrow Percyjackson s:");
firstLibrary.borrowBook("Percyjackson");
firstLibrary.borrowBook("Percyjackson");
secondLibrary.borrowBook("Percyjackson");
System.out.println();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();
// Return
System.out.println("Returning The Lord of the Rings:");
firstLibrary.returnBook("The Lord of the Rings");
System.out.println();
// Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
}
}