assignment 1 files
A1.py file
A1_functions.py
book.py
users.py
please do the assignment content as in the requrments using
python only
assignment 1 code that you need to work with
assignment 1 files
a1.py file
from typing import List
from Assignments.User import *
from Assignments.Book import *
from A1_functions import *
# Creating the two lists: one for books and another for users
# initializing the book list to some preliminary values (should be changed to reading from a file in A2
bookList = [Book("python 4everyone", "Horstmann", '978-1-119-49853-7', 2, 0, 0, 15, []),
Book("absolute Java", "Savitch", '9780134041674', 3, 0, 0, 15, [])]
# initializing the user list to some preliminary values and passwords
userList = [[User("Ali", 1010, '[email protected]'), '1234'],
[NormalUser("Ahmed", 00000, '[email protected]', []), '4532'],
[NormalUser("Shadi", 1111, '[email protected]', []), '5678'],
[Librarian("Morris", 2222, '[email protected]', ["UPEI", "Ryerson"]), 'lglg'],
[Admin("David", 3333, '[email protected]', ["UPEI", "Ryerson"]), 'pppp']]
# Authenticating the user
username, password = input("Please enter your username and password?").split()
# search the user and display his menu based on his type
foundIndex = -1
for i, user in enumerate(userList):
# check if the user exists in the DB
# print(user)
if user[0].name == username:
# check if his password matches the password
if user[1] == password:
foundIndex = i
print(f"found at {foundIndex}")
# display menus based on the user type and whether he is found or not
if foundIndex >= 0:
if isinstance(userList[foundIndex][0], NormalUser):
print("found again")
userChoice = input('Dear user your options are:\n'
'1) Borrow a book\n'
'2) return a book\n'
'3) search for a book\n')
elif isinstance(userList[foundIndex][0], Librarian):
userChoice = input('Dear Librarian your options are:\n'
'1) Add a book\n'
'2) Remove a book\n'
'3) search for a book\n')
elif isinstance(userList[foundIndex][0], Admin):
userChoice = input('Dear Admin your options are:\n'
'1) Add a user\n'
'2) Remove a user\n')
# Normal user operations
if isinstance(userList[foundIndex][0], NormalUser):
# borrow a book choice
if int(userChoice) == 1:
# ask the user for the bookname he wants to borrow
bookToBorrow = input("enter the title of the book to borrow?")
# seacrh for the book in the bookList
bookIndex = searchForBook(bookToBorrow, bookList)
# add the book to the list of books the user borrowing, if available
if bookIndex >=0:
# add the book to the user account
userList[foundIndex][0].borrowBook(bookList[bookIndex])
print("Book added to user account successfully!")
#print(userList[foundIndex][0])
# update book record
bookList[bookIndex].noOfCpsBorrowed += 1
bookList[bookIndex].borrowers.append(userList[foundIndex][0])
print(bookList[bookIndex].borrowers)
else:
print("sorry book not found!")
# return a book choice
if int(userChoice) == 2:
# ask the user for the bookname he wants to return
bookToReturn = input("enter the title of the book to return?")
# remove the book from the user account
try:
# remove it from the user list of borrowed books
userList[foundIndex][0].returnBook(bookList[bookIndex])
# update the number of borrowed copies for this book
bookList[bookIndex].noOfCpsBorrowed -= 1
except:
print("Book is not borrowed by this user! Cannot be returned")
# search for a book choice
if int(userChoice) == 3:
# ask the user for the bookname he wants to borrow
bookToSearch = input("enter the title of the book to search for?")
# seacrh for the book in the bookList
bookIndex = searchForBook(bookToSearch, bookList)
if bookIndex >= 0:
print("the requested book details:\n")
print(bookList[bookIndex])
else:
print("Sorry, this book is not found in the library!")
# Librarian user operations
if isinstance(userList[foundIndex][0], Librarian):
# Add a book choice
if int(userChoice) == 1:
bookDetails: List[str] = input("please enter the details of the book in CSV format").split(',')
bookList.append(Book(bookDetails[0],bookDetails[1],bookDetails[2],bookDetails[3],
bookDetails[4],bookDetails[5],bookDetails[6],bookDetails[7]))
# Remove a book choice
if int(userChoice) == 2:
# ask the user for the bookname he wants to borrow
bookToRemove = input("enter the title of the book to remove from the list?")
# search for the book in the bookList
bookIndex = searchForBook(bookToRemove, bookList)
try:
userList[foundIndex][0].remove(bookList[bookIndex], bookList)
except:
print("the book is not in the books list! Can't be removed!")
# search for a book choice
if int(userChoice) == 3:
# ask the user for the bookname he wants to borrow
bookToSearch = input("enter the title of the book to search for?")
# seacrh for the book in the bookList
bookIndex = searchForBook(bookToSearch, bookList)
if bookIndex >= 0:
print("the requested book details:\n")
print(bookList[bookIndex])
else:
print("Sorry, this book is not found in the library!")
# Admin user operations
if isinstance(userList[foundIndex][0], Admin):
# add user to the list
if int(userChoice) == 1:
userDetails = input("please enter the new user details in CSV format:").split(',')
userType = int(input("what type of user: 1) normal, 2) Librarian, 3) Admin"))
if userType == 1:
userList.append(NormalUser(userDetails[0],userDetails[1],userDetails[2]))
elif userType == 2:
userList.append(Librarian(userDetails[0],userDetails[1],userDetails[2]))
elif userType == 3:
userList.append(Admin(userDetails[0],userDetails[1],userDetails[2]))
print(userList[-1])
# remove user to the list
if int(userChoice) == 2:
# ask the admin for the user he wants to remove
userToRemove = input("enter the name of the user to remove from the list?")
# search for the book in the bookList
userIndex = searchForUser(userToRemove, userList)
try:
userList[foundIndex][0].remove(userList[userIndex], userList)
except:
print("the user is not in the users list! Can't be removed!")
A1.functions.py file
def searchForBook(title, bookList):
bookIndex = -1
for i, book in enumerate(bookList):
if book.title == title:
bookIndex = i
return bookIndex
def searchForUser(name, userList):
userIndex = -1
for i, user in enumerate(userList):
if user.name == name:
userIndex = i
return userIndex
book.py file
class Book:
def __init__(self, title, authors, ISBN, noOfCps=1, noOfCpsBorrowed=0, physOrOnline=0, loanPeriod=30,
borrowers=list()):
self._title = title
self._authors = authors
self._ISBN = ISBN
self._noOfCps = noOfCps
self._noOfCpsBorrowed = noOfCpsBorrowed
self._physOrOnline = physOrOnline
self._loanPeriod = loanPeriod
self._borrowers = borrowers
# getters
@property
def title(self):
return self._title
@property
def authors(self):
return self._authors
@property
def ISBN(self):
return self._ISBN
@property
def noOfCps(self):
return self._noOfCps
@property
def noOfCpsBorrowed(self):
return self._noOfCpsBorrowed
@property
def physOrOnline(self):
return self._physOrOnline
@property
def loanPeriod(self):
return self._loanPeriod
@property
def borrowers(self):
return self._borrowers
# setters
@title.setter
def title(self, title):
self._title = title
@authors.setter
def authors(self, authors):
self._authors = authors
@ISBN.setter
def ISBN(self, ISBN):
self._ISBN = ISBN
@noOfCps.setter
def noOfCps(self, noOfCps):
self._noOfCps = noOfCps
@noOfCpsBorrowed.setter
def noOfCpsBorrowed(self, noOfCpsBorrowed):
self._noOfCpsBorrowed = noOfCpsBorrowed
@physOrOnline.setter
def physOrOnline(self, physOrOnline):
self._physOrOnline = physOrOnline
@loanPeriod.setter
def loanPeriod(self, loanPeriod):
self._loanPeriod = loanPeriod
@borrowers.setter
def borrowers(self, borrowers):
self._borrowers = borrowers
def __eq__(self, other):
if isinstance(other, type(self)):
if self.ISBN == other.ISBN:
return True
elif (self.title == other.title) and (self.authors == other.authors):
return True
else:
return False
else:
return False
def __repr__(self):
return f'Book Details are:\n' \
f'Title: {self.title}\n' \
f'Authors: {self.authors}\n' \
f'ISBN: {self.ISBN}\n' \
f'Total number of copies: {self.noOfCps}\n' \
f'{"This book is available as a hard copy only" if (self.physOrOnline == 0) else None}' \
f'{"This book is available as an online copy only" if (self.physOrOnline == 1) else None}' \
f'{"This book is available as a hard copy as well as online" if (self.physOrOnline == 2) else None}' \
f'\nLoan duration for this book is: {self.loanPeriod}\n' \
f'Currently the following patrons are borrowing the book:{self.borrowers}\n'
users.py file
from Assignments import Book
class User:
def __init__(self, name, ID, email):
self._name = name
self._ID = ID
self._email = email
# getters
@property
def name(self):
return self._name
@property
def ID(self):
return self._ID
@property
def email(self):
return self._email
# Setters
@name.setter
def name(self, name):
self._name = name
@ID.setter
def ID(self, ID):
self._ID = ID
@email.setter
def email(self, email):
self._email = email
def __repr__(self):
return f'User name: {self.name}\n' \
f'User ID: {self.ID}\n' \
f'User email: {self.email}\n'
def __eq__(self, other):
if isinstance(other, type(self)):
if self.name == other.name and self.ID == other.ID:
return True
else:
return False
else:
return False
class NormalUser(User):
def __init__(self, name, ID, email, borrowedBooks=None):
super().__init__(name, ID, email)
self._borrowedBooks = borrowedBooks
@property
def borrowedBooks(self):
return self._borrowedBooks
@borrowedBooks.setter
def borrowedBooks(self, borrowedBooks):
self._borrowedBooks = borrowedBooks
def borrowBook(self, book: Book):
self.borrowedBooks.append(book)
def returnBook(self, book: Book):
try:
self.borrowedBooks.remove(book)
except ValueError:
return "Book not in the borrow list! Can't be removed!"
# def searchBook(self, book: Book, booksList: list):
# try:
# return booksList.index(book)
# except ValueError:
# return "Book not found!"
def __repr__(self):
return super().__repr__() + f'list of books borrowed is: {self.borrowedBooks}'
class Librarian(User):
def __init__(self, name, ID, email, affLibrs: list):
super().__init__(name, ID, email)
self.affLibrs = affLibrs
def addBook(self, book: Book, booksList: list):
booksList.add(book)
def removeBook(self, book: Book, booksList: list):
try:
self.booksList.remove(book)
except ValueError:
return "the book is not in the books list! Can't be removed!"
# def searchBook(self, book: Book, booksList: list):
# try:
# return booksList.index(book)
# except ValueError:
# return "Book not found!"
def __repr__(self):
return super().__repr__() + f'list of libraries he is affiliated with is: {self.affLibrs}'
class Admin(User):
def __init__(self, name, ID, email, affLibrs: list):
super().__init__(name, ID, email)
self.affLibrs = affLibrs
def addUser(self, user: User, userList: list):
userList.add(user)
def removeUser(self, user: User, userList: list):
try:
userList.remove(user)
except ValueError:
return "the User is not in the users list! Can't be removed!"
def __repr__(self):
return super().__repr__() + f'list of libraries he is affiliated with is: {self.affLibrs}'
please continue this assignment as shown in the assignment explanation and use python only like assignment 1
that all things needed
please i need to passs
Introduction This assignment is designed to test your understanding of inheritance, polymorphism and File 10 in Python. No prior knowledge is required however attendance and understanding of the content of the lectures given and posted on D2L so far is absolutely required to finish this assignment. The ideal way to do this assignment is to study the lectures, do the labs, jot down any questions and ask your instructor or TA, plan the implementation and then finally code the assignment. Objective To test your understanding of the following: Review of basic OOP programming basics done in assignment 1 • Inheritance child-parent relationships Polymorphism and method overriding Working with files to save and retrieve class objects using CSV and JSON formats. General Guidelines When Writing Programs At the top of your program you should write your name, student ID and information about your assignment # Assignment (number) # Written by: (your name and student id) # In a comment, give a general explanation of what your program is doing. The more complicated the program is the more elaborative your comments should be. Using comments explain every major step in the program as you implement it. • All user prompts MUST be clear and informative for the user whether you are asking for input or displaying outputs. Always start and end the program with descriptive and informative messages to the users.
Assignment explanation Building upon assignment 1, we want to extend this program to be an online portal supporting multi libraries with different requirements and different types of users. The additional functionality will be as follows: Three library types will be supported: o Online school library Online municipal library o Online national library All three libraries will share some properties and methods. You can deduct those properties and methods from what you have done in assignment 1. Likely, you didn't implement the library as a separate class in A1 given that you only had a single library and there was no point of instantiating more than library object from a class. Nevertheless, here you should consider having a class that contains all common behaviour and properties and then add to it any special extensions. A non-exhaustive list of the properties and methods are as follows: (note: feel free to add any additional property or method you deem is necessary) o Properties: Library name Library type • Allowed list of user types (contains the types of users that are allowed exist in this library) • List of already registered users • List of restricted users • List of books in this library · Borrowing policy in terms of: loan period, grace period, number of times to extend loan before default. List of active book loans o Methods: Constructor that takes all properties as parameters and have default values set in case the programmer didn't enter all the fields when constructing an object. Add a user: to add new user to the library. It should accept all user details and the user type to create a new user in case the user doesn't exist in the list of users, OR add an existing user object to the list of library users in case the user already exists but wasn't a member of this specific library. • Add a book to add a new book to the library. Again, it will accept all the book details and create a new book object in case it didn't exist in the list of books, OR just add existing book object to the list of library books in case the book already exists but wasn't a part of this library's collection. Remove a user to take off the user from the list of library users and return all his borrowed books to the list of available books
Restrict a user: applying some restrictions on the user; such as temporarily banning a user from borrowing library books due to excessive abuse of the loan period. Remove a book: to take off the book from the library's list • Add/change borrowing policy: to adjust the details of the borrowing policy to which any new book rentals should comply. • Borrow a book: to check out a book to a user and take it off the library's list of available books. Before checking out, it checks if the patron has any past due amounts or is restricted from using the library for any reason. Also after, it should update different lists with the new loan details. . Return a book: return the book from the user back to the library inventory and checks if it's past due and apply any charges or penalty on the user. · Search for a book: search inside the list of library books using: title or author names and return a reference pointing to the book of interest. Get library stats: return useful statistics such as the frequently borrowed books, highest borrowing users and books pending returns. This could help library managers to decide which books they should buy additional copies for, extending or reducing loan period/policy and any other decision regarding the return process. Additionally, each library will have its own additional methods or its own implementation of the above methods as follows: (NOTE: again feel free to add any additional property or method you deem is necessary) School library: will have few changes from above as follows: Add user: should check that the user to be added is a: student, librarian or admin only. Borrow a book: works only for students, so validating if a student before checking out is important. • Municipal library: will have the following changes: Add user: should check that the user lives in the same municipality in order to be added. o Borrow a book: should ensure that the user has a credit card registered on file to ensure that any dues will be charged automatically. National library: will have the following changes: Add user: should check that the user an adult and has a social security number in order to be added. o Borrow a book: should ensure that the user has a credit card registered on file to ensure that any dues will be charged automatically. • The following simple UML diagram explains the relationship between different classes mentioned above. Again, this is a simple and not an exhaustive UML with all possible relationships so feel free to add any additional relationship you deem necessary for your code. Additionally, neither properties nor behaviour is illustrated in this UML so you need to update it
to include all properties and methods from A1 in addition to the new properties and methods added in this assignment. has Libran aburrows from User returns to has Scho Municbat.ba Nationary C Back @Regular Student Ammin Librarian • All the data that you store inside the lists of books, users or libraries MUST be saved to a JSON file before you exit the program and MUST reloaded when you start the program. This will ensure that you always keep your data and never have to start from scratch every time you run the program. Finally, you MUST include a readme file for your program explaining how it operates, what sort of prompts the user will face and what he should do to achieve different functionalities. Important things to consider when building this program: • The type of classes that you create. You should clearly specify which are supposed to be classes and which should be class members and not a class in its own. • All necessary methods for every class including all setters, getters and other mutator methods. • Constructors and what they should initialize. • Differentiate clearly between instance variable and class variables. Justify the need for class variables in case they are needed. • Storing users and books references can be done inside a list or similar data structures before saving them to a JSON file before exiting.
Introduction This assignment is designed to test your understanding of inheritance, polymorphism and File 10 in Python. No prior knowledge is required however attendance and understanding of the content of the lectures given and posted on D2L so far is absolutely required to finish this assignment. The ideal way to do this assignment is to study the lectures, do the labs, jot down any questions and ask your instructor or TA, plan the implementation and then finally code the assignment. Objective To test your understanding of the following: Review of basic OOP programming basics done in assignment 1 • Inheritance child-parent relationships Polymorphism and method overriding Working with files to save and retrieve class objects using CSV and JSON formats. . General Guidelines When Writing Programs At the top of your program you should write your name, student ID and information about your assignment . # # Assignment (number) # Written by: (your name and student id) # In a comment, give a general explanation of what your program is doing. The more complicated the program is the more elaborative your comments should be. Using comments explain every major step in the program as you implement it. All user prompts MUST be clear and informative for the user whether you are asking for input or displaying outputs. Always start and end the program with descriptive and informative messages to the users. .
Assignment explanation Building upon assignment 1, we want to extend this program to be an online portal supporting multi libraries with different requirements and different types of users. The additional functionality will be as follows: . . Three library types will be supported: Online school library o Online municipal library o Online national library All three libraries will share some properties and methods. You can deduct those properties and methods from what you have done in assignment 1. Likely, you didn't implement the library as a separate class in A1 given that you only had a single library and there was no point of instantiating more than library object from a class. Nevertheless, here you should consider having a class that contains all common behaviour and properties and then add to it any special extensions. A non-exhaustive list of the properties and methods are as follows: (note: feel free to add any additional property or method you deem is necessary) Properties Library name Library type Allowed list of user types (contains the types of users that are allowed exist in this library) List of already registered users List of restricted users - List of books in this library Borrowing policy in terms of: loan period, grace period, number of times to -extend loan before default . Cist of active book loans Methods: Constructor that takes all properties as parameters and have default values set in case the programmer didn't enter all the fields when constructing an object. Add a user to add new user to the library. It should accept all user details and the user type to create a new user in case the user doesn't exist in the list of users, OR add an existing user object to the list of library users in case the user already exists but wasn't a member of this specific library Add a book to add a new book to the library. Again, it will accept all the book ads.g.doubleclick.net. object in case it didn't exist in the list of books, o .
OR just add existing book object to the list of library books in case the book already exists but wasn't a part of this library's collection. Remove a user to take off the user from the list of library users and return all his borrowed books to the list of available books Restrict a user: applying some restrictions on the user, such as temporarily banning a user from borrowing library books due to excessive abuse of the loan period. Remove a book: to take off the book from the library's list Add/change borrowing policy: to adjust the details of the borrowing policy to which any new book rentals should comply. Borrow a book: to check out a book to a user and take it off the library's list of available books. Before checking out, it checks if the patron has any past due amounts or is restricted from using the library for any reason. Also after, it should update different lists with the new loan details, Return a book: return the book from the user back to the library inventory and checks if it's past due and apply any charges or penalty on the use Search for a book: search inside the list of library books using title or author names and return a reference pointing to the book of interest. Get library stats, return useful statistics such as the frequently borrowed books, highest borrowing users and books pending retums. This could help library managers to decide which books they should buy additional copies for, extending or reducing loan period/policy and any other decision regarding the return process Additionally, each library will have its own additional methods or its own implementation of the above methods as follows: (NOTE again feel free to add any additional property or method you deem is necessary School library will have few changes from above as follows: Add user should check that the user to be added is a: student, librarian or admin only. Borrow a book works only for students, so validating if a .
student before checking out is important. Municipal library: will have the following changes: @ Add users should check that the user lives in the same municipality in order to be added. Borrow a book: should ensure that the user has a credit card registered on file to ensure that any dues will be charged automatically National library: will have the following changes: Add user: should check that the user an adult and has a social security number in order to be added. Borrow a book: should ensure that the user has a credit card registered on file to ensure that any dues will be charged automatically The following simple UML diagram explains the relationship between different classes mentioned above. Again, this is a simple and not an exhaustive UML with all possible relationships so feel free to add any additional relationship you deem necessary for your code. Additionally, neither properties nor behaviour is illustrated in this UML so you need to update it to include all properties and methods from Al in addition to the new properties and methods added in this assignment. ch All the data that you store inside the lists of books, users or libraries MUST be saved to a JSON file before you exit the program and MUST reloaded when you start the program. This will ensure that you always keep your data and never have to start from scratch every time you run the program Finally, you MUST include a readme file for your program explaining how it operates, what sort of prompts the user will face and what he should do to achieve different functionalities.
Important things to consider when building this program: The type of classes that you create. You should clearly specify which are supposed to be classes and which should be class members and not a class in its own. All necessary methods for every class including all setters, getters and other mutator methods. Constructors and what they should initialize. Differentiate clearly between instance variable and class variables. Justify the need for class variables in case they are needed. Storing users and books references can be done inside a list or similar data structures before saving them to a JSON file before exiting.
Introduction This assignment is designed to test your understanding of inheritance, polymorphism and File 10 in Python.
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Introduction This assignment is designed to test your understanding of inheritance, polymorphism and File 10 in Python.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!