Skip to content

Commit e97c24c

Browse files
Moibrahi7Mohamed
andauthored
feat: adds Mohamed's Book.java, Library.java, Patron.java, tests, and custom exceptions (#243)
* Feat: Added Book.java, Library.java, Patron.java, tests, and custom exeptions * Chore: Added Java Docs to code * Chore: ran spotless apply * Feat: Added more tests to Library and changed Checkoutbooks * Feat: added more tests to BookTest and library test, cover wrote .equals and hashcode for book * Fix: Removed not needed test statement * Chore: removed not need import * Feat: Added more test to patron test * Feat: gave patorn the ability to check in array lists * Feat: added tests to PatronTest * Fix; ran spotlessapply * Feat: added patron removal and exeption for patron removal. --------- Co-authored-by: Mohamed <“[email protected]”>
1 parent 68e317d commit e97c24c

File tree

10 files changed

+683
-0
lines changed

10 files changed

+683
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package com.codedifferently.lesson9.mohamedibrahim;
2+
3+
import com.google.common.base.Objects;
4+
import java.util.ArrayList;
5+
6+
public class Book {
7+
private String name;
8+
private int isbn;
9+
private ArrayList<String> authors = new ArrayList<String>();
10+
private int pageNums;
11+
private boolean checkedOut;
12+
private Library origin;
13+
14+
/**
15+
* Takes in all of the elaments of the book class exept for the boolean checked out. This is due
16+
* to all the books made will be by defult checked in.
17+
*
18+
* @param name The title of each book.
19+
* @param isbn A unique id for each book.
20+
* @param author Takes in the name of ONE author in the form of a string.
21+
* @param pageNums The number of pages in each book.
22+
*/
23+
public Book(String name, int isbn, String author, int pageNums, Library origin) {
24+
this.name = name;
25+
this.isbn = isbn;
26+
this.authors.add(author);
27+
this.pageNums = pageNums;
28+
this.checkedOut = false;
29+
this.origin = origin;
30+
}
31+
32+
/**
33+
* Same as above however takes in a list of names.
34+
*
35+
* @param name The title of each book.
36+
* @param isbn A unique id for each book.
37+
* @param author Takes in the name of MANY author in the form of a Arraylist.
38+
* @param pageNums The number of pages in each book.
39+
*/
40+
public Book(String name, int isbn, ArrayList<String> authors, int pageNums, Library origin) {
41+
this.name = name;
42+
this.isbn = isbn;
43+
this.authors = authors;
44+
this.pageNums = pageNums;
45+
this.checkedOut = false;
46+
this.origin = origin;
47+
}
48+
49+
/**
50+
* Returns the author arraylists.
51+
*
52+
* @return ArrayList<String>
53+
*/
54+
public ArrayList<String> getAuthors() {
55+
return authors;
56+
}
57+
58+
/**
59+
* Returns the unique isbn number.
60+
*
61+
* @return int
62+
*/
63+
public int getIsbn() {
64+
return isbn;
65+
}
66+
67+
/**
68+
* Returns the book name.
69+
*
70+
* @return String
71+
*/
72+
public String getName() {
73+
return name;
74+
}
75+
76+
/**
77+
* Returns the page number.
78+
*
79+
* @return int
80+
*/
81+
public int getPageNums() {
82+
return pageNums;
83+
}
84+
85+
/**
86+
* Tells the user if the book is checked in or out.
87+
*
88+
* @return
89+
*/
90+
public boolean getStatus() {
91+
return checkedOut;
92+
}
93+
94+
public Library getOrigin() {
95+
return origin;
96+
}
97+
98+
/**
99+
* Adds authors to the author arrayList.
100+
*
101+
* @param author
102+
*/
103+
public void addAuthors(ArrayList<String> author) {
104+
this.authors.addAll(author);
105+
}
106+
107+
/**
108+
* Adds AN author to the author arrayList.
109+
*
110+
* @param author
111+
*/
112+
public void addAuthor(String author) {
113+
this.authors.add(author);
114+
}
115+
116+
/**
117+
* Removes AN author from a book.
118+
*
119+
* @param author
120+
*/
121+
public void removeAuthor(String author) {
122+
if (this.authors.contains(author)) {
123+
this.authors.remove(author);
124+
}
125+
}
126+
127+
/**
128+
* Removes many author from a book.
129+
*
130+
* @param author
131+
*/
132+
public void removeAuthors(ArrayList<String> author) {
133+
if (this.authors.containsAll(author)) {
134+
this.authors.removeAll(author);
135+
}
136+
}
137+
138+
/** Sets the boolean to checkedout. */
139+
public void checkOut() {
140+
this.checkedOut = true;
141+
}
142+
143+
/** Sets the boolean to false meaning the book is returned. */
144+
public void checkIn() {
145+
this.checkedOut = false;
146+
}
147+
148+
/**
149+
* Takes in a new isbn and sets it as the isbn.
150+
*
151+
* @param isbn
152+
*/
153+
public void setIsbn(int isbn) {
154+
this.isbn = isbn;
155+
}
156+
157+
/**
158+
* Takes in a new string name and makes it name of the object.
159+
*
160+
* @param name
161+
*/
162+
public void setName(String name) {
163+
this.name = name;
164+
}
165+
166+
/**
167+
* Takes in a new number that becomes the page number of that book.
168+
*
169+
* @param pageNums
170+
*/
171+
public void setPageNums(int pageNums) {
172+
this.pageNums = pageNums;
173+
}
174+
175+
/**
176+
* Sets the origin of the book to a Library.
177+
*
178+
* @param origin The library the book came from.
179+
*/
180+
public void setOrigin(Library origin) {
181+
this.origin = origin;
182+
}
183+
184+
/**
185+
* Overrites the .equals fuction so that it can work better with book.
186+
*
187+
* @return boolean
188+
*/
189+
@Override
190+
public boolean equals(Object o) {
191+
if (this == o) return true;
192+
else if (o == null || getClass() != o.getClass()) return false;
193+
Book book = (Book) o;
194+
return Objects.equal(isbn, book.isbn);
195+
}
196+
197+
/**
198+
* Overrites the hashcode function to make the isbn its hash code.
199+
*
200+
* @return int
201+
*/
202+
@Override
203+
public int hashCode() {
204+
return Objects.hashCode(isbn);
205+
}
206+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codedifferently.lesson9.mohamedibrahim;
2+
3+
public class BookNotFoundException extends RuntimeException {
4+
/**
5+
* @param error
6+
*/
7+
BookNotFoundException(String error) {
8+
super(error);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson9.mohamedibrahim;
2+
3+
public class BookOfDifferentOrginException extends RuntimeException {
4+
BookOfDifferentOrginException(String error) {
5+
super(error);
6+
}
7+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.codedifferently.lesson9.mohamedibrahim;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
6+
public class Library {
7+
private HashMap<Integer, Book> shelves = new HashMap<>();
8+
private HashMap<Integer, Patron> currentPatronsById = new HashMap<>();
9+
10+
public Library() {}
11+
12+
/**
13+
* Creates a library object and alowing the input of a Array of books
14+
*
15+
* @param shelves
16+
*/
17+
public Library(ArrayList<Book> books) {
18+
19+
for (Book i : books) {
20+
this.shelves.put(i.hashCode(), i);
21+
}
22+
}
23+
24+
/**
25+
* Retreives current patrons and ids in the library.
26+
*
27+
* @return Arraylist<Patrons>
28+
*/
29+
HashMap<Integer, Patron> getCurrentPatronsById() {
30+
return currentPatronsById;
31+
}
32+
33+
/**
34+
* Retrieves the current books and isbn in the library.
35+
*
36+
* @return
37+
*/
38+
HashMap<Integer, Book> getShelves() {
39+
return shelves;
40+
}
41+
42+
/**
43+
* Retrieves the current books in the library.
44+
*
45+
* @return
46+
*/
47+
48+
/**
49+
* Takes a book and adds it to the shelves array list and the used isbns arraylist.
50+
*
51+
* @param book
52+
*/
53+
public void addBook(Book book) {
54+
shelves.put(book.hashCode(), book);
55+
}
56+
57+
/**
58+
* Same as add books but changes the boolen is checked out for the book to false.
59+
*
60+
* @param book
61+
*/
62+
public void checkInBook(Book book, Library library) {
63+
if (!(book.getOrigin().equals(library))) {
64+
throw new BookOfDifferentOrginException("Book Not From This Library");
65+
}
66+
addBook(book);
67+
book.checkIn();
68+
}
69+
70+
/**
71+
* Takes in a patron and checkes if the user id is already registered. If it is, the code will
72+
* produce an error stating the id is in use.
73+
*
74+
* @param patron
75+
*/
76+
public void registerPatron(Patron patron) {
77+
if (currentPatronsById.containsKey(patron.getId())) {
78+
throw new UserAlreadyRegisteredException("This User ID is In Use!");
79+
}
80+
currentPatronsById.put(patron.getId(), patron);
81+
}
82+
83+
/**
84+
* Removes a patron from the patronbyid hash map.
85+
*
86+
* @param patron
87+
*/
88+
public void unRegisterPatron(Patron patron) {
89+
if (!currentPatronsById.containsKey(patron.getId())) {
90+
throw new UserNotRegisteredException("USER NOT REGISTERED!");
91+
}
92+
currentPatronsById.remove(patron.getId(), patron);
93+
}
94+
95+
/**
96+
* This is for checking a book out. It takes in the book you are checking out in. After it
97+
* checkes, if the book is on the shelves it is checked out and tbe book is removed from the list.
98+
*
99+
* @param book
100+
* @return Book
101+
*/
102+
public Book checkOutBook(Book book) {
103+
if (!(shelves.containsKey(book.hashCode()))) {
104+
throw new BookNotFoundException("Error Book not Found!");
105+
} else {
106+
book.checkOut();
107+
shelves.remove(book.hashCode());
108+
return book;
109+
}
110+
}
111+
112+
/**
113+
* Same as the code above just uses an array to check out more books at once.
114+
*
115+
* @param books
116+
* @return ArrayList<Books>
117+
*/
118+
public ArrayList<Book> checkOutBooks(ArrayList<Book> books) {
119+
120+
int i = 0;
121+
for (Book b : books) {
122+
if (!(this.shelves.containsValue(b))) {
123+
i++;
124+
} else {
125+
b.checkOut();
126+
this.shelves.remove(b.getIsbn(), b);
127+
}
128+
}
129+
if (i + 1 == books.size()) {
130+
throw new BookNotFoundException("Error Book not found");
131+
}
132+
return books;
133+
}
134+
}

0 commit comments

Comments
 (0)