Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <“moibrahi7@gmail.com”>
- Loading branch information
Showing
10 changed files
with
683 additions
and
0 deletions.
There are no files selected for viewing
206 changes: 206 additions & 0 deletions
206
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/mohamedibrahim/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package com.codedifferently.lesson9.mohamedibrahim; | ||
|
||
import com.google.common.base.Objects; | ||
import java.util.ArrayList; | ||
|
||
public class Book { | ||
private String name; | ||
private int isbn; | ||
private ArrayList<String> authors = new ArrayList<String>(); | ||
private int pageNums; | ||
private boolean checkedOut; | ||
private Library origin; | ||
|
||
/** | ||
* Takes in all of the elaments of the book class exept for the boolean checked out. This is due | ||
* to all the books made will be by defult checked in. | ||
* | ||
* @param name The title of each book. | ||
* @param isbn A unique id for each book. | ||
* @param author Takes in the name of ONE author in the form of a string. | ||
* @param pageNums The number of pages in each book. | ||
*/ | ||
public Book(String name, int isbn, String author, int pageNums, Library origin) { | ||
this.name = name; | ||
this.isbn = isbn; | ||
this.authors.add(author); | ||
this.pageNums = pageNums; | ||
this.checkedOut = false; | ||
this.origin = origin; | ||
} | ||
|
||
/** | ||
* Same as above however takes in a list of names. | ||
* | ||
* @param name The title of each book. | ||
* @param isbn A unique id for each book. | ||
* @param author Takes in the name of MANY author in the form of a Arraylist. | ||
* @param pageNums The number of pages in each book. | ||
*/ | ||
public Book(String name, int isbn, ArrayList<String> authors, int pageNums, Library origin) { | ||
this.name = name; | ||
this.isbn = isbn; | ||
this.authors = authors; | ||
this.pageNums = pageNums; | ||
this.checkedOut = false; | ||
this.origin = origin; | ||
} | ||
|
||
/** | ||
* Returns the author arraylists. | ||
* | ||
* @return ArrayList<String> | ||
*/ | ||
public ArrayList<String> getAuthors() { | ||
return authors; | ||
} | ||
|
||
/** | ||
* Returns the unique isbn number. | ||
* | ||
* @return int | ||
*/ | ||
public int getIsbn() { | ||
return isbn; | ||
} | ||
|
||
/** | ||
* Returns the book name. | ||
* | ||
* @return String | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Returns the page number. | ||
* | ||
* @return int | ||
*/ | ||
public int getPageNums() { | ||
return pageNums; | ||
} | ||
|
||
/** | ||
* Tells the user if the book is checked in or out. | ||
* | ||
* @return | ||
*/ | ||
public boolean getStatus() { | ||
return checkedOut; | ||
} | ||
|
||
public Library getOrigin() { | ||
return origin; | ||
} | ||
|
||
/** | ||
* Adds authors to the author arrayList. | ||
* | ||
* @param author | ||
*/ | ||
public void addAuthors(ArrayList<String> author) { | ||
this.authors.addAll(author); | ||
} | ||
|
||
/** | ||
* Adds AN author to the author arrayList. | ||
* | ||
* @param author | ||
*/ | ||
public void addAuthor(String author) { | ||
this.authors.add(author); | ||
} | ||
|
||
/** | ||
* Removes AN author from a book. | ||
* | ||
* @param author | ||
*/ | ||
public void removeAuthor(String author) { | ||
if (this.authors.contains(author)) { | ||
this.authors.remove(author); | ||
} | ||
} | ||
|
||
/** | ||
* Removes many author from a book. | ||
* | ||
* @param author | ||
*/ | ||
public void removeAuthors(ArrayList<String> author) { | ||
if (this.authors.containsAll(author)) { | ||
this.authors.removeAll(author); | ||
} | ||
} | ||
|
||
/** Sets the boolean to checkedout. */ | ||
public void checkOut() { | ||
this.checkedOut = true; | ||
} | ||
|
||
/** Sets the boolean to false meaning the book is returned. */ | ||
public void checkIn() { | ||
this.checkedOut = false; | ||
} | ||
|
||
/** | ||
* Takes in a new isbn and sets it as the isbn. | ||
* | ||
* @param isbn | ||
*/ | ||
public void setIsbn(int isbn) { | ||
this.isbn = isbn; | ||
} | ||
|
||
/** | ||
* Takes in a new string name and makes it name of the object. | ||
* | ||
* @param name | ||
*/ | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
/** | ||
* Takes in a new number that becomes the page number of that book. | ||
* | ||
* @param pageNums | ||
*/ | ||
public void setPageNums(int pageNums) { | ||
this.pageNums = pageNums; | ||
} | ||
|
||
/** | ||
* Sets the origin of the book to a Library. | ||
* | ||
* @param origin The library the book came from. | ||
*/ | ||
public void setOrigin(Library origin) { | ||
this.origin = origin; | ||
} | ||
|
||
/** | ||
* Overrites the .equals fuction so that it can work better with book. | ||
* | ||
* @return boolean | ||
*/ | ||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
else if (o == null || getClass() != o.getClass()) return false; | ||
Book book = (Book) o; | ||
return Objects.equal(isbn, book.isbn); | ||
} | ||
|
||
/** | ||
* Overrites the hashcode function to make the isbn its hash code. | ||
* | ||
* @return int | ||
*/ | ||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(isbn); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...p_app/src/main/java/com/codedifferently/lesson9/mohamedibrahim/BookNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.codedifferently.lesson9.mohamedibrahim; | ||
|
||
public class BookNotFoundException extends RuntimeException { | ||
/** | ||
* @param error | ||
*/ | ||
BookNotFoundException(String error) { | ||
super(error); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...c/main/java/com/codedifferently/lesson9/mohamedibrahim/BookOfDifferentOrginException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.codedifferently.lesson9.mohamedibrahim; | ||
|
||
public class BookOfDifferentOrginException extends RuntimeException { | ||
BookOfDifferentOrginException(String error) { | ||
super(error); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
lesson_09/oop/oop_app/src/main/java/com/codedifferently/lesson9/mohamedibrahim/Library.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package com.codedifferently.lesson9.mohamedibrahim; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class Library { | ||
private HashMap<Integer, Book> shelves = new HashMap<>(); | ||
private HashMap<Integer, Patron> currentPatronsById = new HashMap<>(); | ||
|
||
public Library() {} | ||
|
||
/** | ||
* Creates a library object and alowing the input of a Array of books | ||
* | ||
* @param shelves | ||
*/ | ||
public Library(ArrayList<Book> books) { | ||
|
||
for (Book i : books) { | ||
this.shelves.put(i.hashCode(), i); | ||
} | ||
} | ||
|
||
/** | ||
* Retreives current patrons and ids in the library. | ||
* | ||
* @return Arraylist<Patrons> | ||
*/ | ||
HashMap<Integer, Patron> getCurrentPatronsById() { | ||
return currentPatronsById; | ||
} | ||
|
||
/** | ||
* Retrieves the current books and isbn in the library. | ||
* | ||
* @return | ||
*/ | ||
HashMap<Integer, Book> getShelves() { | ||
return shelves; | ||
} | ||
|
||
/** | ||
* Retrieves the current books in the library. | ||
* | ||
* @return | ||
*/ | ||
|
||
/** | ||
* Takes a book and adds it to the shelves array list and the used isbns arraylist. | ||
* | ||
* @param book | ||
*/ | ||
public void addBook(Book book) { | ||
shelves.put(book.hashCode(), book); | ||
} | ||
|
||
/** | ||
* Same as add books but changes the boolen is checked out for the book to false. | ||
* | ||
* @param book | ||
*/ | ||
public void checkInBook(Book book, Library library) { | ||
if (!(book.getOrigin().equals(library))) { | ||
throw new BookOfDifferentOrginException("Book Not From This Library"); | ||
} | ||
addBook(book); | ||
book.checkIn(); | ||
} | ||
|
||
/** | ||
* Takes in a patron and checkes if the user id is already registered. If it is, the code will | ||
* produce an error stating the id is in use. | ||
* | ||
* @param patron | ||
*/ | ||
public void registerPatron(Patron patron) { | ||
if (currentPatronsById.containsKey(patron.getId())) { | ||
throw new UserAlreadyRegisteredException("This User ID is In Use!"); | ||
} | ||
currentPatronsById.put(patron.getId(), patron); | ||
} | ||
|
||
/** | ||
* Removes a patron from the patronbyid hash map. | ||
* | ||
* @param patron | ||
*/ | ||
public void unRegisterPatron(Patron patron) { | ||
if (!currentPatronsById.containsKey(patron.getId())) { | ||
throw new UserNotRegisteredException("USER NOT REGISTERED!"); | ||
} | ||
currentPatronsById.remove(patron.getId(), patron); | ||
} | ||
|
||
/** | ||
* This is for checking a book out. It takes in the book you are checking out in. After it | ||
* checkes, if the book is on the shelves it is checked out and tbe book is removed from the list. | ||
* | ||
* @param book | ||
* @return Book | ||
*/ | ||
public Book checkOutBook(Book book) { | ||
if (!(shelves.containsKey(book.hashCode()))) { | ||
throw new BookNotFoundException("Error Book not Found!"); | ||
} else { | ||
book.checkOut(); | ||
shelves.remove(book.hashCode()); | ||
return book; | ||
} | ||
} | ||
|
||
/** | ||
* Same as the code above just uses an array to check out more books at once. | ||
* | ||
* @param books | ||
* @return ArrayList<Books> | ||
*/ | ||
public ArrayList<Book> checkOutBooks(ArrayList<Book> books) { | ||
|
||
int i = 0; | ||
for (Book b : books) { | ||
if (!(this.shelves.containsValue(b))) { | ||
i++; | ||
} else { | ||
b.checkOut(); | ||
this.shelves.remove(b.getIsbn(), b); | ||
} | ||
} | ||
if (i + 1 == books.size()) { | ||
throw new BookNotFoundException("Error Book not found"); | ||
} | ||
return books; | ||
} | ||
} |
Oops, something went wrong.