Repository navigation
Expand file tree
/
Copy pathLibrary.java
More file actions
225 lines (217 loc) · 8.15 KB
/
Copy pathLibrary.java
File metadata and controls
225 lines (217 loc) · 8.15 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
import java.io.*;
import java.util.LinkedList;
class Author {
public String name;
public BookList books = new BookList();
public Author() {
}
public boolean equals(Object node) {
return name.equals(((Author) node).name);
}
public void display() {
System.out.println(name);
books.display();
}
}
class Book {
public String title;
public Patron patron = null;
public Book() {
}
public boolean equals(Object node) {
return title.equals(((Book) node).title);
}
public String toString() {
return " * " + title +
(patron != null ? " - checked out to " + patron.name : "") + "\n";
}
}
class CheckedOutBook {
public Author author = null;
public Book book = null;
public CheckedOutBook() {
}
public boolean equals(Object node) {
return book.title.equals(((CheckedOutBook) node).book.title) &&
author.name.equals(((CheckedOutBook) node).author.name);
}
public String toString() {
return " * " + author.name + ", " + book.title + "\n";
}
}
class Patron {
public String name;
public BookList books = new BookList();
public Patron() {
}
public boolean equals(Object node) {
return name.equals(((Patron) node).name);
}
public void display() {
if (!books.isEmpty()) {
System.out.println(name + " has the following books:");
books.display();
}
else System.out.print(name + " has no books");
}
}
class AuthorList extends LinkedList {
public AuthorList() {
super();
}
public void display() {
Object[] authors = toArray();
for (int i = 0; i < authors.length; i++)
((Author)authors[i]).display();
}
}
class BookList extends LinkedList {
public BookList() {
super();
}
public void display() {
for (int i = 0; i < size(); i++)
System.out.print(get(i));
}
}
class PatronList extends LinkedList {
public PatronList() {
super();
}
public void display() {
for (java.util.Iterator it = iterator(); it.hasNext(); )
((Patron)it.next()).display();
}
}
class Library {
private AuthorList[] catalog = new AuthorList[(int)('Z'+1)];
private PatronList[] people = new PatronList[(int)('Z'+1)];
private String input;
private BufferedReader buffer = new BufferedReader(
new InputStreamReader(System.in));
public Library() {
for (int i = 0; i <= (int) 'Z'; i++) {
catalog[i] = new AuthorList();
people[i] = new PatronList();
}
}
private String getString(String msg) {
System.out.print(msg + " ");
System.out.flush();
try {
input = buffer.readLine();
} catch(IOException io) {
}
return input.substring(0,1).toUpperCase() + input.substring(1);
}
private void status() {
System.out.println("Library has the following books:\n ");
for (int i = (int) 'A'; i <= (int) 'Z'; i++)
if (!catalog[i].isEmpty())
catalog[i].display();
System.out.println("\nThe following people are using the library:\n ");
for (int i = (int) 'A'; i <= (int) 'Z'; i++)
if (!people[i].isEmpty())
people[i].display();
}
private void includeBook() {
Author newAuthor = new Author();
int oldAuthor;
Book newBook = new Book();
newAuthor.name = getString("Enter author's name:");
newBook.title = getString("Enter the title of the book:");
oldAuthor = catalog[(int) newAuthor.name.charAt(0)].indexOf(newAuthor);
if (oldAuthor == -1) {
newAuthor.books.add(newBook);
catalog[(int) newAuthor.name.charAt(0)].add(newAuthor);
}
else ((Author)catalog[(int) newAuthor.name.charAt(0)].get(oldAuthor)).
books.add(newBook);
}
private void checkOutBook() {
Patron patron = new Patron(), patronRef;
Author author = new Author(), authorRef = new Author();
Book book = new Book();
int patronIndex, bookIndex = -1, authorIndex = -1;
patron.name = getString("Enter patron's name:");
while (authorIndex == -1) {
author.name = getString("Enter author's name:");
authorIndex = catalog[(int) author.name.charAt(0)].indexOf(author);
if (authorIndex == -1)
System.out.println("Misspelled author's name");
}
while (bookIndex == -1) {
book.title = getString("Enter the title of the book:");
authorRef = (Author) catalog[(int) author.name.charAt(0)].get(authorIndex);
bookIndex = authorRef.books.indexOf(book);
if (bookIndex == -1)
System.out.println("Misspelled title");
}
Book bookRef = (Book) authorRef.books.get(bookIndex);
CheckedOutBook bookToCheckOut = new CheckedOutBook();
bookToCheckOut.author = authorRef;
bookToCheckOut.book = bookRef;
patronIndex = people[(int) patron.name.charAt(0)].indexOf(patron);
if (patronIndex == -1) { // a new patron in the library;
patron.books.add(bookToCheckOut);
people[(int) patron.name.charAt(0)].add(patron);
bookRef.patron = (Patron) people[(int) patron.name.charAt(0)].getFirst();
}
else {
patronRef = (Patron) people[(int) patron.name.charAt(0)].get(patronIndex);
patronRef.books.add(bookToCheckOut);
bookRef.patron = patronRef;
}
}
private void returnBook() {
Patron patron = new Patron();
Book book = new Book();
Author author = new Author(), authorRef = new Author();
int patronIndex = -1, bookIndex = -1, authorIndex = -1;
while (patronIndex == -1) {
patron.name = getString("Enter patron's name:");
patronIndex = people[(int) patron.name.charAt(0)].indexOf(patron);
if (patronIndex == -1)
System.out.println("Patron's name misspelled");
}
while (authorIndex == -1) {
author.name = getString("Enter author's name:");
authorIndex = catalog[(int) author.name.charAt(0)].indexOf(author);
if (authorIndex == -1)
System.out.println("Misspelled author's name");
}
while (bookIndex == -1) {
book.title = getString("Enter the title of the book:");
authorRef = (Author) catalog[(int) author.name.charAt(0)].get(authorIndex);
bookIndex = authorRef.books.indexOf(book);
if (bookIndex == -1)
System.out.println("Misspelled title");
}
CheckedOutBook checkedOutBook = new CheckedOutBook();
checkedOutBook.author = authorRef;
checkedOutBook.book = (Book) authorRef.books.get(bookIndex);
((Book)authorRef.books.get(bookIndex)).patron = null;
((Patron)people[(int) patron.name.charAt(0)].get(patronIndex)).
books.remove(checkedOutBook);
}
public void run() {
while (true) {
char option = getString("\nEnter one of the following options:\n" +
"1. Include a book in the catalog\n" +
"2. Check out a book\n" +
"3. Return a book\n4. Status\n5. Exit\n" +
"Your option:").charAt(0);
switch (option) {
case '1': includeBook(); break;
case '2': checkOutBook(); break;
case '3': returnBook(); break;
case '4': status(); break;
case '5': return;
default: System.out.println("Wrong option, try again.");
}
}
}
public static void main(String args[]) {
(new Library()).run();
}
}