Skip to content

Commit 9659a53

Browse files
committed
feat: implemented librarian, Magazine,Newspaper,Exceptions
1 parent e08c2b6 commit 9659a53

File tree

14 files changed

+825
-0
lines changed

14 files changed

+825
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.codedifferently.lesson10.jadaslibrary;
2+
3+
import com.codedifferently.lesson10.jadaslibrary.exceptions.WrongLibraryException;
4+
import java.util.List;
5+
import java.util.Objects;
6+
7+
public class Book extends MediaItem {
8+
private String isbn;
9+
private List<String> authors;
10+
private int numberOfPages;
11+
private Jadaslibrary library;
12+
private Patron patron;
13+
private String author;
14+
15+
// Constructor
16+
public Book(String title, String isbn, List<String> authors, int numberOfPages) {
17+
super(title, isbn, numberOfPages);
18+
this.isbn = isbn;
19+
this.authors = authors;
20+
this.numberOfPages = numberOfPages;
21+
this.author = author;
22+
}
23+
24+
// Set the library for the book
25+
public void setLibrary(Jadaslibrary library) {
26+
if (library != null && !library.hasBook(this)) {
27+
throw new WrongLibraryException("Book " + isbn + " is not in the provided library");
28+
}
29+
this.library = library;
30+
}
31+
32+
// Getters and setters
33+
public String getIsbn() {
34+
return isbn;
35+
}
36+
37+
public List<String> getAuthors() {
38+
return authors;
39+
}
40+
41+
public String getAuthor() {
42+
return author;
43+
}
44+
45+
public void setAuthors(List<String> authors) {
46+
this.authors = authors;
47+
}
48+
49+
public int getNumberOfPages() {
50+
return numberOfPages;
51+
}
52+
53+
public Jadaslibrary getLibrary() {
54+
return library;
55+
}
56+
57+
public String getId() {
58+
return isbn;
59+
}
60+
61+
public Patron getPatron() {
62+
return patron;
63+
}
64+
65+
public void setPatron(Patron patron) {
66+
this.patron = patron;
67+
}
68+
69+
// Overridden methods
70+
@Override
71+
public boolean equals(Object o) {
72+
if (this == o) return true;
73+
if (!(o instanceof Book)) return false;
74+
Book book = (Book) o;
75+
return Objects.equals(getIsbn(), book.getIsbn());
76+
}
77+
78+
@Override
79+
public int hashCode() {
80+
return Objects.hash(getId());
81+
}
82+
83+
@Override
84+
public String toString() {
85+
return "Book{" + "id='" + getId() + '\'' + ", title='" + getTitle() + '\'' + '}';
86+
}
87+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.codedifferently.lesson10.jadaslibrary;
2+
3+
public class Dvd extends MediaItem {
4+
private String director;
5+
private int releaseYear;
6+
private boolean checkedOut;
7+
private Patron patron;
8+
9+
public Dvd(String title, String director, int releaseYear) {
10+
super(title, "N/A", 0);
11+
this.director = director;
12+
this.releaseYear = releaseYear;
13+
this.patron = null;
14+
}
15+
16+
// Getters and setters
17+
public String getDirector() {
18+
return director;
19+
}
20+
21+
public void setDirector(String director) {
22+
this.director = director;
23+
}
24+
25+
public int getReleaseYear() {
26+
return releaseYear;
27+
}
28+
29+
public void setReleaseYear(int releaseYear) {
30+
this.releaseYear = releaseYear;
31+
}
32+
33+
public String getId() {
34+
return getTitle();
35+
}
36+
37+
@Override
38+
public Patron getPatron() {
39+
return patron;
40+
}
41+
42+
@Override
43+
public void setPatron(Patron patron) {
44+
this.patron = patron;
45+
}
46+
47+
@Override
48+
public boolean isCheckedOut() {
49+
return checkedOut;
50+
}
51+
52+
@Override
53+
public void setCheckedOut(boolean checkedOut) {
54+
this.checkedOut = checkedOut;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "DVD{"
60+
+ "title='"
61+
+ getTitle()
62+
+ '\''
63+
+ ", director='"
64+
+ director
65+
+ '\''
66+
+ ", releaseYear="
67+
+ releaseYear
68+
+ ", patron="
69+
+ (patron != null ? patron.getName() : "None")
70+
+ '}';
71+
}
72+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.codedifferently.lesson10.jadaslibrary;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Jadaslibrary {
7+
private String id;
8+
private List<Book> books;
9+
private List<Patron> patrons;
10+
private List<MediaItem> mediaItems;
11+
private List<Librarian> librarians;
12+
public static final int MAX_CHECKED_OUT_MEDIA_ITEMS = 3;
13+
14+
public Jadaslibrary(String id) {
15+
this.id = id;
16+
this.books = new ArrayList<>();
17+
this.patrons = new ArrayList<>();
18+
this.mediaItems = new ArrayList<>();
19+
this.librarians = new ArrayList<>();
20+
}
21+
22+
public void addBook(Book book) {
23+
books.add(book);
24+
}
25+
26+
public void removeBook(Book book) {
27+
books.remove(book);
28+
}
29+
30+
public void addMediaItem(MediaItem mediaItem) {
31+
mediaItems.add(mediaItem);
32+
if (mediaItem instanceof Book) {
33+
books.add((Book) mediaItem);
34+
}
35+
}
36+
37+
public void removeMediaItem(MediaItem mediaItem) {
38+
mediaItems.remove(mediaItem);
39+
if (mediaItem instanceof Book) {
40+
books.remove(mediaItem);
41+
}
42+
}
43+
44+
public void registerPatron(Patron patron) {
45+
patrons.add(patron);
46+
}
47+
48+
public void registerLibrarian(Librarian librarian) {
49+
librarians.add(librarian);
50+
}
51+
52+
public void checkOutMediaItem(MediaItem mediaItem, Patron patron) {
53+
if (!mediaItem.isCheckedOut()
54+
&& patron.getCheckedOutMediaItems().size() < MAX_CHECKED_OUT_MEDIA_ITEMS) {
55+
mediaItem.setCheckedOut(true);
56+
mediaItem.setPatron(patron);
57+
}
58+
}
59+
60+
public void returnMediaItem(MediaItem mediaItem) {
61+
if (mediaItem.isCheckedOut()) {
62+
mediaItem.setCheckedOut(false);
63+
mediaItem.setPatron(null);
64+
}
65+
}
66+
67+
public List<Book> getBooks() {
68+
return new ArrayList<>(books);
69+
}
70+
71+
public List<MediaItem> getMediaItems() {
72+
return new ArrayList<>(mediaItems);
73+
}
74+
75+
public List<Patron> getPatrons() {
76+
return new ArrayList<>(patrons);
77+
}
78+
79+
public void addPatron(Patron patron) {
80+
patrons.add(patron);
81+
}
82+
83+
public boolean hasBook(Book book) {
84+
return books.contains(book);
85+
}
86+
87+
public boolean hasPatron(Patron patron) {
88+
return patrons.contains(patron);
89+
}
90+
91+
public String getId() {
92+
return id;
93+
}
94+
95+
public List<Book> getCheckedOutBooksByPatron(Patron patron) {
96+
List<Book> checkedOutBooks = new ArrayList<>();
97+
for (Book book : books) {
98+
if (book.isCheckedOut() && book.getPatron().equals(patron)) {
99+
checkedOutBooks.add(book);
100+
}
101+
}
102+
return checkedOutBooks;
103+
}
104+
105+
public List<MediaItem> getCheckedOutMediaItemsByPatron(Patron patron) {
106+
List<MediaItem> checkedOutMediaItems = new ArrayList<>();
107+
for (MediaItem mediaItem : mediaItems) {
108+
if (mediaItem.isCheckedOut() && mediaItem.getPatron().equals(patron)) {
109+
checkedOutMediaItems.add(mediaItem);
110+
}
111+
}
112+
return checkedOutMediaItems;
113+
}
114+
115+
public boolean isCheckedOut(Book book) {
116+
return getCheckedOutBooks().contains(book);
117+
}
118+
119+
private List<Book> getCheckedOutBooks() {
120+
List<Book> checkedOutBooks = new ArrayList<>();
121+
for (Book book : books) {
122+
if (book.isCheckedOut()) {
123+
checkedOutBooks.add(book);
124+
}
125+
}
126+
return checkedOutBooks;
127+
}
128+
129+
public void removePatron(Patron patron) {
130+
patrons.remove(patron);
131+
}
132+
133+
public void checkOutBook(Book book, Patron patron) {
134+
if (books.contains(book) && patrons.contains(patron)) {
135+
book.setCheckedOut(true);
136+
book.setPatron(patron);
137+
}
138+
}
139+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codedifferently.lesson10.jadaslibrary;
2+
3+
public class Librarian {
4+
private String name;
5+
6+
public Librarian(String name) {
7+
this.name = name;
8+
}
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void addBookToLibrary(Book book, Jadaslibrary library) {
15+
library.addBook(book);
16+
}
17+
18+
public void removeBookFromLibrary(Book book, Jadaslibrary library) {
19+
library.removeBook(book);
20+
}
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.codedifferently.lesson10.jadaslibrary;
2+
3+
public class Magazine extends MediaItem {
4+
private String title;
5+
private String publisher;
6+
private int issueNumber;
7+
8+
public Magazine(String title, String isbn, String publisher, int numberOfPages, int issueNumber) {
9+
super(title, isbn, numberOfPages);
10+
this.title = title;
11+
this.publisher = publisher;
12+
this.issueNumber = issueNumber;
13+
}
14+
15+
// Getters and setters
16+
public String getTitle() {
17+
return title;
18+
}
19+
20+
public void setTitle(String title) {
21+
this.title = title;
22+
}
23+
24+
public String getPublisher() {
25+
return publisher;
26+
}
27+
28+
public void setPublisher(String publisher) {
29+
this.publisher = publisher;
30+
}
31+
32+
public int getIssueNumber() {
33+
return issueNumber;
34+
}
35+
36+
public void setIssueNumber(int issueNumber) {
37+
this.issueNumber = issueNumber;
38+
}
39+
}

0 commit comments

Comments
 (0)