Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added Librarian Concept + Media support #294

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,98 @@
package com.codedifferently.lesson10.library;

import com.codedifferently.lesson10.library.media.Dvd;
import java.util.HashSet;
import java.util.Set;

/** Represents a librarian of a library. */
public class Librarian extends Patron {
00USER1X marked this conversation as resolved.
Show resolved Hide resolved
private Set<Dvd> dvds = new HashSet<>(); // Added for DVDs
private Set<Dvd> checkedOutDvds = new HashSet<>(); // Added for checked out DVDs

/**
* Create a new librarian with the given id.
*
* @param id The id of the librarian.
*/
public Librarian(String id) {
super(id, "Librarian"); // Updated constructor call with role
}

/**
* Add a DVD to the librarian's collection.
*
* @param dvd The DVD to add.
*/
public void addDvd(Dvd dvd) {
this.dvds.add(dvd);
}

/**
* Remove a DVD from the librarian's collection.
*
* @param dvd The DVD to remove.
*/
public void removeDvd(Dvd dvd) {
this.dvds.remove(dvd);
}

/**
* Check out a DVD to a patron.
*
* @param dvd The DVD to check out.
* @param patron The patron to check out the DVD to.
* @return True if the DVD was checked out, false otherwise.
*/
public boolean checkOutDvd(Dvd dvd, Patron patron) {
if (!canCheckOutDvd(dvd, patron)) {
return false;
}
this.checkedOutDvds.add(dvd);
return true;
}

private boolean canCheckOutDvd(Dvd dvd, Patron patron) {
if (!this.hasDvd(dvd)) {
return false;
}
if (!this.hasPatron(patron)) {
return false;
}
return true;
}

/**
* Check in a DVD from a patron.
*
* @param dvd The DVD to check in.
* @param patron The patron returning the DVD.
* @return True if the DVD was returned, false otherwise.
*/
public boolean checkInDvd(Dvd dvd, Patron patron) {
if (!this.checkedOutDvds.contains(dvd)) {
return false;
}
this.checkedOutDvds.remove(dvd);
return true;
}

/**
* Check if the librarian has the given DVD.
*
* @param dvd The DVD to check for.
* @return True if the librarian has the DVD, false otherwise.
*/
public boolean hasDvd(Dvd dvd) {
return this.dvds.contains(dvd);
}

/**
* Check if the librarian has the given patron.
*
* @param patron The patron to check for.
* @return True if the librarian has the patron, false otherwise.
*/
public boolean hasPatron(Patron patron) {
return this.getId().equals(patron.getId());
}
}
Expand Up @@ -12,6 +12,7 @@ public class Library {
private Set<String> checkedOutIsbns = new HashSet<>();
private Map<String, Set<Book>> checkedOutBooksByPatron = new HashMap<>();
private Set<String> patronIds = new HashSet<>();
private Set<String> dvdIds = new HashSet<>(); // Added for DVDs
private String id;

/**
Expand Down Expand Up @@ -176,6 +177,6 @@ public String toString() {
+ checkedOutBooksByPatron
+ ", patronIds="
+ patronIds
+ '}';
+ "}";
}
}
Expand Up @@ -59,6 +59,36 @@ public Set<Book> getCheckedOutBooks() throws LibraryNotSetException {
return this.library.getCheckedOutByPatron(this);
}

/**
* Check out a book from the library.
*
* @param book The book to check out.
* @return True if the book was successfully checked out, false otherwise.
*/
public boolean checkOutBook(Book book) {
try {
return this.library.checkOutBook(book, this);
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

/**
* Return a book to the library.
*
* @param book The book to return.
* @return True if the book was successfully returned, false otherwise.
*/
public boolean checkInBook(Book book) {
try {
return this.library.checkInBook(book, this);
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
@@ -0,0 +1,10 @@
package com.codedifferently.lesson10.library.media;

import com.codedifferently.lesson10.library.Book;
import java.util.List;

public class Dvd extends Book {
public Dvd(String id, String title, List<String> authors, int year) {
super(id, title, authors, year); // Updated constructor call with four arguments
}
}
@@ -0,0 +1,25 @@
package com.codedifferently.lesson10.library.media;

public class Magazine {
private String title;
private String issueNumber;
private String publicationDate;

public Magazine(String title, String issueNumber, String publicationDate) {
this.title = title;
this.issueNumber = issueNumber;
this.publicationDate = publicationDate;
}

public String getTitle() {
return title;
}

public String getIssueNumber() {
return issueNumber;
}

public String getPublicationDate() {
return publicationDate;
}
}
@@ -0,0 +1,25 @@
package com.codedifferently.lesson10.library.media;

public class Newspaper {
private String title;
private String edition;
private String publicationDate;

public Newspaper(String title, String edition, String publicationDate) {
this.title = title;
this.edition = edition;
this.publicationDate = publicationDate;
}

public String getTitle() {
return title;
}

public String getEdition() {
return edition;
}

public String getPublicationDate() {
return publicationDate;
}
}
@@ -0,0 +1 @@

@@ -0,0 +1 @@

@@ -0,0 +1 @@