this is my assignment
https://ucsb-cs9.github.io/s23/lab/lab05/
this is my code:
testFile.py
from Book import Book
def test_book_details():
b = Book("Ready Player One", "Cline, Ernest", 2011)
assert b.getBookDetails() == "Title: Ready Player One, Author: Cline, Ernest, Year: 2011"
from BookCollection import BookCollection
def test_get_books_by_author():
b0 = Book("Cujo", "King, Stephen", 1981)
b1 = Book("The Shining", "King, Stephen", 1977)
b2 = Book("Ready Player One", "Cline, Ernest", 2011)
b3 = Book("Rage", "King, Stephen", 1977)
bc = BookCollection()
bc.insertBook(b0)
bc.insertBook(b1)
bc.insertBook(b2)
bc.insertBook(b3)
print(bc.getBooksByAuthor("KING, Stephen"))
expected_output = ("Title: Rage, Author: King, Stephen, Year: 1977\n"
"Title: The Shining, Author: King, Stephen, Year: 1977\n"
"Title: Cujo, Author: King, Stephen, Year: 1981")
assert bc.getBooksByAuthor("KING, Stephen") == expected_output
def test_get_all_books_in_collection():
b0 = Book("Cujo", "King, Stephen", 1981)
b1 = Book("The Shining", "King, Stephen", 1977)
b2 = Book("Ready Player One", "Cline, Ernest", 2011)
b3 = Book("Rage", "King, Stephen", 1977)
bc = BookCollection()
bc.insertBook(b0)
bc.insertBook(b1)
bc.insertBook(b2)
bc.insertBook(b3)
expected_output = ("Title: Ready Player One, Author: Cline, Ernest, Year: 2011\n"
"Title: Rage, Author: King, Stephen, Year: 1977\n"
"Title: The Shining, Author: King, Stephen, Year: 1977\n"
"Title: Cujo, Author: King, Stephen, Year: 1981")
assert bc.getAllBooksInCollection() == expected_output
def test_remove_author():
b0 = Book("Cujo", "King, Stephen", 1981)
b1 = Book("The Shining", "King, Stephen", 1977)
b2 = Book("Ready Player One", "Cline, Ernest", 2011)
b3 = Book("Rage", "King, Stephen", 1977)
bc = BookCollection()
bc.insertBook(b0)
bc.insertBook(b1)
bc.insertBook(b2)
bc.insertBook(b3)
bc.removeAuthor("king, stephen")
assert bc.recursiveSearchTitle("Cujo", bc.head) == False
assert bc.recursiveSearchTitle("The Shining", bc.head) == False
assert bc.recursiveSearchTitle("Rage", bc.head) == False
assert bc.recursiveSearchTitle("Ready Player One", bc.head) == True
def test_recursive_search_title():
b0 = Book("Cujo", "King, Stephen", 1981)
b1 = Book("The Shining", "King, Stephen", 1977)
bc = BookCollection()
bc.insertBook(b0)
bc.insertBook(b1)
assert bc.recursiveSearchTitle("CUJO", bc.head) == True
assert bc.recursiveSearchTitle("Twilight", bc.head) == False
test_book_details()
test_get_books_by_author()
test_get_all_books_in_collection()
test_remove_author()
test_recursive_search_title()
BookCollection.py
from BookCollectionNode import BookCollectionNode
class BookCollection:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def getNumberOfBooks(self):
count = 0
current = self.head
while current != None:
count += 1
current = current.getNext()
return count
def insertBook(self, book):
node = BookCollectionNode(book)
if self.isEmpty():
self.head = node
elif node > self.head:
node.setNext(self.head)
self.head = node
else:
current = self.head
while current.getNext() is not None and current.getNext() > node:
current = current.getNext()
node.setNext(current.getNext())
current.setNext(node)
def getBooksByAuthor(self, author):
result = []
current = self.head
while current != None:
if current.getData().getAuthor().lower() == author.lower():
result.append(current.getData())
current = current.getNext()
return result
def getAllBooksInCollection(self):
result = []
current = self.head
while current != None:
result.append(current.getData())
current = current.getNext()
def removeAuthor(self, author):
author = author.lower()
current = self.head
previous = None
while current is not None:
if current.getData().getAuthor().lower() == author:
if previous is None:
# Removing the head node
self.head = current.getNext()
else:
previous.setNext(current.getNext())
# Move the current pointer to the next node
current = current.getNext()
else:
# Move the previous and current pointers to the next node
previous = current
current = current.getNext()
def recursiveSearchTitle(self, title, bookNode):
if bookNode is None:
return False
elif bookNode.getData().getTitle().lower() == title.lower():
return True
else:
return self.recursiveSearchTitle(title, bookNode.getNext())
BookCollectionNode.py
class BookCollectionNode:
def __init__(self, data):
self.data = data
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, data):
self.data = data
def setNext(self, next):
self.next = next
Book.py
class Book:
def __init__(self, title, author, year):
self.title = title
self.author = author
self.year = year
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getYear(self):
return self.year
def getBookDetails(self):
return f"Title: {self.title}, Author: {self.author}, Year: {self.year}"
def __gt__(self, other):
return self.title.upper() > other.title.upper()