In: Computer Science
QUESTION 13
Implement in Python and test the classes shown in the UML below. The LibraryItem class shown in the UML, represents an item that the library issues to students. The partial Python code is provided and It has the following attributes. Test the program by creating a list of books and a list of DVDs and printing the details of each element in the list.
Attribute |
Description |
ID |
The ID of the item. |
createdate |
The date the item was created as dd/mm/yyyy |
title |
The title of the item. |
class LibraryItem: def __init__(self,ID="",year=0,title=""): self.ID=ID self.year=year self.title=title def getLoanPeriod(self): return 0
A) Implement the Book class. It inherits from LibraryItem. Furthermore, it has this additional attribute
Attribute |
Description |
author |
The author of the book |
Electronic |
This tells whether the book is available as an electronic book (e book) |
Implement the getLoanPeriod function in this way:
- If the book was written before the year 2000, the loan period will be 14 days.
- If the book was written after or in 2000, the loan period will be 7 days.
B) Implement the DVD class. It inherits from LibraryItem. Furthermore, it has this additional attribute
Attribute |
Description |
length |
The length of the DVD. Example: “2 hours: 30 minutes” |
HD |
Whether the DVD is available in HD (High Definition) or not |
Implement the getLoanPeriod function in this way:
- If the DVD is available in HD format, the loan period will be 3 days. Otherwise, the loan period will be 7 days.
Here is the Python code:
import datetime
class LibraryItem:
def __init__ (self, ID=0, year = 2000, month = 1, day = 1, title = ""):
self.ID = ID
self.year = year
self.createDate = datetime.datetime (year, month, day)
self.title = title
def getLoanPeriod (self):
return 0
class Book (LibraryItem):
def __init__ (self, ID=0, year = 2000, month = 1, day = 1, title = "", Author = "", Electronic = 1):
self.Author = Author
self.Electronic = Electronic
LibraryItem.__init__ (self, ID, year, month, day, title)
def getLoanPeriod (self):
if self.year < 2000:
return 14
else:
return 7
class DVD (LibraryItem):
def __init__ (self, ID=0, year = 2000, month = 1, day = 1, title = "", length = "", HD = 1):
self.length = length
self.HD = HD
LibraryItem.__init__ (self, ID, year, month, day, title)
def getLoanPeriod (self):
if self.HD == 1:
return 3
else:
return 7
l1 = Book (1, 1998, 10, 20, "Book 1", "Auth 1", 1)
l2 = Book (2, 2018, 11, 21, "Book 2", "Auth 2", 0)
l3 = DVD (3, 2018, 12, 22, "DVD 1", "2 hours 30 mins", 1)
l4 = DVD (4, 2017, 3, 4, "DVD 2", "1 hours 25 mins", 0)
print (l1.createDate, l1.getLoanPeriod())
print (l2.Author, l2.getLoanPeriod())
print (l3.HD, l3.getLoanPeriod())
print (l4.length, l4.getLoanPeriod())