-
Notifications
You must be signed in to change notification settings - Fork 14
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
Group #4 #14
Open
dejavu9127
wants to merge
13
commits into
fortiss-cce:main
Choose a base branch
from
dejavu9127:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Group #4 #14
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a7c3ed9
Updated Author class
44043b9
Merge branch 'dejavu9127'
9502249
Updated publisher clas
99cda73
library: Book: refactor serializer and introduce BookGenre
denishoornaert 201162f
library: book: book type
denishoornaert c55b108
removed
denishoornaert dc83cd7
Updated Booktype fee calculation and otehr things using static class …
673ad8e
Updated use of booktype enum
ed6554c
removed
denishoornaert d336ba1
Updated imports
dd1e917
minor updates
8df1a65
Updated references to booktype Paper
63c3789
library: user: change summary output
denishoornaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,23 +1,24 @@ | ||
class Author: | ||
|
||
firstname: str | ||
lastname: str | ||
_firstname: str | ||
_lastname: str | ||
|
||
def __init__(self, firstname: str, lastname: str): | ||
self.firstname = firstname | ||
self.lastname = lastname | ||
self._firstname = firstname | ||
self._lastname = lastname | ||
self._uid=f"{self._firstname} {self._lastname}" | ||
|
||
def get_firstname(self) -> str: | ||
return self.firstname | ||
return self._firstname | ||
|
||
def get_lastname(self) -> str: | ||
return self.lastname | ||
return self._lastname | ||
|
||
def get_fullname(self) -> str: | ||
return f"{self.firstname} {self.lastname}" | ||
return self._uid | ||
|
||
def __eq__(self, other): | ||
"""Overrides the default implementation""" | ||
if isinstance(other, Author): | ||
return self.get_fullname() == other.get_fullname() | ||
return self._uid == other._uid | ||
return NotImplemented |
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 |
---|---|---|
|
@@ -3,24 +3,24 @@ | |
from library.model.author import Author | ||
from library.model.genre import Genre | ||
from library.model.publisher import Publisher | ||
from library.model.booktype import BookType, BookTypeProcessor | ||
|
||
import xml.etree.ElementTree as et | ||
|
||
from library.persistence.storage import LibraryRepository | ||
|
||
|
||
class Book: | ||
title: str | ||
authors: list[Author] | ||
publisher: Publisher | ||
publication_date: datetime | ||
genres: list[Genre] | ||
genres: set[Genre] | ||
pages: int | ||
isbn: str | ||
|
||
existing_items: int | ||
borrowed_items: int | ||
|
||
_book_type: str | ||
_book_type: BookType | ||
duration: int = 0 | ||
|
||
def __init__(self, title, authors, publisher, pub_date, genres, pages, isbn, type, duration=0, existing_items=1, borrowed_items=0): | ||
|
@@ -54,38 +54,19 @@ def from_borrowed_book(cls, borrowed_book: "BorrowedBook") -> "Book": | |
return book | ||
|
||
def can_borrow(self) -> bool: | ||
if self._book_type == "Paper": | ||
return self.existing_items - self.borrowed_items > 0 | ||
elif self._book_type == "Electronic": | ||
return True | ||
elif self._book_type == "Audio": | ||
return True | ||
else: | ||
raise AttributeError("No such book type...") | ||
return BookTypeProcessor.can_borrow(self) | ||
|
||
|
||
def get_approximate_duration(self) -> int: | ||
if self._book_type == "Paper": | ||
return self.pages * 3 * 60 | ||
elif self._book_type == "Electronic": | ||
return self.pages * 5 * 60 | ||
elif self._book_type == "Audio": | ||
return self.duration | ||
else: | ||
raise AttributeError("No such book type...") | ||
return BookTypeProcessor.get_approximate_duration(self) | ||
|
||
|
||
def get_weekly_fee(self) -> int: | ||
if self._book_type == "Paper": | ||
return 5 | ||
elif self._book_type == "Electronic": | ||
return 2 | ||
elif self._book_type == "Audio": | ||
return 2 | ||
else: | ||
raise AttributeError("No such book type...") | ||
|
||
return BookTypeProcessor.get_weekly_fee(self) | ||
|
||
def borrow_book(self) -> "BorrowedBook": | ||
if self.can_borrow(): | ||
if self._book_type == "Paper": | ||
if BookTypeProcessor.book_check_reqrd(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, the readability of the if statement decreased here. You could follow the prefix pattern ( |
||
self.borrowed_items += 1 | ||
LibraryRepository.update_book(self) | ||
borrowed_book = BorrowedBook.from_book(self) | ||
|
@@ -131,7 +112,7 @@ def renew_rental(self) -> "BorrowedBook": | |
return self | ||
|
||
def return_book(self) -> Book: | ||
if self._book_type == "Paper": | ||
if self._book_type == BookType.PAPER: | ||
self.borrowed_items -= 1 | ||
book = Book.from_borrowed_book(self) | ||
LibraryRepository.update_book(book) | ||
|
@@ -145,26 +126,36 @@ def __eq__(self, other): | |
|
||
|
||
class BookSerializer: | ||
def serialize(self, book: Book, format: str): | ||
if format == "JSON": | ||
book_info = { | ||
"id": book.isbn, | ||
"title": book.title, | ||
"authors": [author.get_fullname() for author in book.authors], | ||
"available_items": book.existing_items - book.borrowed_items, | ||
"borrowed_items": book.borrowed_items, | ||
} | ||
return json.dumps(book_info) | ||
elif format == "XML": | ||
book_info = et.Element("book", attrib={"id": book.isbn}) | ||
title = et.SubElement(book_info, "title") | ||
title.text = book.title | ||
authors = et.SubElement(book_info, "authors") | ||
authors.text = ", ".join([author.get_fullname() for author in book.authors]) | ||
avail = et.SubElement(book_info, "available") | ||
avail.text = str(book.existing_items - book.borrowed_items) | ||
authors = et.SubElement(book_info, "borrowed") | ||
authors.text = str(book.borrowed_items) | ||
return et.tostring(book_info, encoding="Unicode") | ||
|
||
@staticmethod | ||
def _serialize_to_json(book: Book): | ||
book_info = { | ||
"id": book.isbn, | ||
"title": book.title, | ||
"authors": [author.get_fullname() for author in book.authors], | ||
"available_items": book.existing_items - book.borrowed_items, | ||
"borrowed_items": book.borrowed_items, | ||
} | ||
return json.dumps(book_info) | ||
|
||
@staticmethod | ||
def _serialize_to_xml(book: Book): | ||
book_info = et.Element("book", attrib={"id": book.isbn}) | ||
title = et.SubElement(book_info, "title") | ||
title.text = book.title | ||
authors = et.SubElement(book_info, "authors") | ||
authors.text = ", ".join([author.get_fullname() for author in book.authors]) | ||
avail = et.SubElement(book_info, "available") | ||
avail.text = str(book.existing_items - book.borrowed_items) | ||
authors = et.SubElement(book_info, "borrowed") | ||
authors.text = str(book.borrowed_items) | ||
return et.tostring(book_info, encoding="Unicode") | ||
|
||
@staticmethod | ||
def serialize(book: Book, format: str): | ||
if (format == "JSON"): | ||
return BookSerializer._serialize_to_json(book) | ||
elif (format == "XML"): | ||
return BookSerializer._serialize_to_xml(book) | ||
else: | ||
raise ValueError(format) |
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,51 @@ | ||
from enum import Enum | ||
from model.book import Book | ||
seconds_in_a_minute = 60 | ||
|
||
class BookType(Enum): | ||
PAPER = "Paper" | ||
ELECTRONIC = "Electronic" | ||
AUDIO = "Audio" | ||
|
||
class BookTypeProcessor(): | ||
|
||
@staticmethod | ||
def book_check_reqrd(book:Book): | ||
if book._book_type == BookType.PAPER: | ||
return True | ||
else: | ||
return False | ||
|
||
@staticmethod | ||
def can_borrow(book): | ||
if (book._book_type == BookType.PAPER): | ||
return book.existing_items - book.borrowed_item > 0 | ||
elif (book._book_type == BookType.ELECTRONIC): | ||
return True | ||
elif (book._book_type == BookType.AUDIO): | ||
return True | ||
else: | ||
raise AttributeError("No such a book...") | ||
|
||
@staticmethod | ||
def get_approximate_duration(book): | ||
if (book._book_type == BookType.PAPER): | ||
return book._pages * 3 * seconds_in_a_minute | ||
elif (book._book_type == BookType.ELECTRONIC): | ||
return book._pages * 5 * seconds_in_a_minute | ||
elif (book._book_type == BookType.AUDIO): | ||
return book._duration | ||
else: | ||
raise AttributeError("No such a book...") | ||
|
||
@staticmethod | ||
def get_weekly_fee(book): | ||
if (book._book_type == BookType.PAPER): | ||
return 5 | ||
elif (book._book_type == BookType.ELECTRONIC): | ||
return 2 | ||
elif (book._book_type == BookType.AUDIO): | ||
return 2 | ||
else: | ||
raise AttributeError("No such a book...") | ||
|
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
class Publisher: | ||
name: str | ||
_name: str | ||
|
||
def __init__(self, name): | ||
self.name = name | ||
self._name = name | ||
|
||
def __eq__(self, other): | ||
"""Overrides the default implementation""" | ||
if isinstance(other, Publisher): | ||
return self.name == other.name | ||
return self._name == other._name | ||
return NotImplemented |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the code symmetric, you could additionally declare _uid as a member variable here.