Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
393 lines (315 sloc) 16.1 KB
import json as js
import csv as cs
class Author:
titles = []
def __init__(this, name, age, *titles):
this.name = name
this.age = age
this.titles = titles
def __repr__(this):
return str(this.name)
def display_titles(this):
print(this.titles)
class Book:
def __init__(this, author, country, image_link, language, link, pages, title, year):
this.author = author
this.country = country
this.imageLink = image_link
this.language = language
this.link = link
this.pages = pages
this.title = title
this.year = year
def create_js_book(this):
string = {"author": this.author, "country": this.country, "imageLink": this.imageLink,
"language": this.language,
"link": this.link, "pages": this.pages, "title": this.title, "year": this.year}
json_string = js.dumps(string)
json_data = js.loads(json_string)
return json_data
class LoanItem(Book):
def __init__(this, is_loaned, author, country, image_link, language, link, pages, title, year):
super().__init__(author, country, image_link, language, link, pages, title, year)
this.loanee = None
this.is_loaned = is_loaned
def print(this):
print(this.is_loaned, this.author, this.country, this.imageLink, this.language, this.link, this.pages, this.title, this.year)
class Person:
def __init__(this, gender, given_name, surname, street_address, zip_code, city, telephone):
this.gender = gender
this.given_name = given_name
this.surname = surname
this.street_address = street_address
this.zip_code = zip_code
this.city = city
this.telephone = telephone
class Customer(Person):
def __init__(this, number, gender, given_name, surname, street_address, zip_code, city, email, username, telephone):
super().__init__(gender, given_name, surname, street_address, zip_code, city, telephone)
this.number = number
this.email = email
this.username = username
this.customer_data = [{'number': this.number, 'gender': this.gender, 'given_name': this.given_name, 'surname': this.surname, 'street_address': this.street_address,
'zip_code': this.zip_code, 'city': this.city, 'email': this.email, 'username': this.username, 'telephone': this.telephone, 'loaned_books': []}]
def display_cutomer_data(this):
for data in this.customer_data:
print(data)
def return_book(this, loan_item=None, loan_item_title=None):
if loan_item is not None:
book_list = this.customer_data[0]['loaned_books']
if isinstance(loan_item, LoanItem):
for data in book_list:
if loan_item.title in data:
return book_list.pop(book_list.index(data))
elif loan_item_title is not None:
book_list = this.customer_data[0]['loaned_books']
if isinstance(loan_item_title, str):
for data in book_list:
if loan_item_title in data:
return book_list.pop(book_list.index(data))
def display_loaned_books(this):
for data in this.customer_data:
print(data['loaned_books'])
class Catalog:
def __init__(this):
this.authors = []
this.titles = []
this.books = []
this.book_count = 0
def add_loan_item(this, title):
for data in this.books:
if data['title'] == title:
print(data)
return data
def add_book(this, book_obj):
this.books.append(book_obj.create_js_book())
this.titles.append(book_obj.title)
def add_author(this, author_obj):
this.authors.append(author_obj)
for title in author_obj.titles:
this.titles.append(title)
def add_author_books(this, author):
for title in author.titles:
this.titles.append(title)
return sorted(this.titles)
def import_books(this, method=None):
if method is not None:
data = method
this.books += data
else:
with open('booksset1.json', 'r', 1, 'utf-8') as f:
data = js.load(f)
this.books += data
for authors in data:
this.authors.append(authors['author'])
this.titles.append(authors['title'])
this.book_count += len(this.books)
print("Our catalog currently contains " + str(len(this.books)) + " books")
def display_catalog(this):
print(js.dumps(this.books, indent=4, sort_keys=True))
print('\n')
def display_authors(this): # straightforward function, displays all of the authors titles which the catalog contains
no_dup = set() # I've cheated here by porting authors to a set which automatically gets rid of duplicates
for authors in this.authors:
no_dup.add(authors)
print('Our catalog contains the following authors: ' + '\n' + '\n'.join(sorted(no_dup)))
def display_titles(this): # straightforward function, displays all of the book titles which the catalog contains
sorted_titles = sorted(this.titles)
for titles in sorted_titles:
print(titles)
def search_book(this, title=None, author=None, country=None, language=None):
if title is not None:
for books in this.books:
if books['title'] == title:
print(books)
if author is not None:
for books in this.books:
if books['author'] == author:
print(books)
if country is not None and author is not None:
for books in this.books:
if books['country'] == country:
print(books)
if language is not None and title is not None:
for books in this.books:
if books['language'] == language:
print(books)
if title is not None and country is not None:
for books in this.books:
if books['title'] == title and books['country'] == country:
print(books)
if country is not None and language is not None:
for books in this.books:
if books['country'] == country and books['language'] == language:
print(books)
class LoanAdministration:
def __init__(this):
this.customers = []
this.loaned_books = []
def loan_to_customer(this, customer_obj, loan_obj):
if isinstance(loan_obj, LoanItem):
for data in this.loaned_books:
if loan_obj.title in data[2]:
print('That item has already been loaned out')
exit()
loan = []
loan += customer_obj.number, customer_obj.surname, loan_obj.title
loan_obj.loanee = customer_obj.number
this.loaned_books.append(loan)
for data in customer_obj.customer_data:
data['loaned_books'].append(loan)
elif isinstance(loan_obj, dict):
for data in this.loaned_books:
if loan_obj['title'] in data[2]:
print('That item has already been loaned out')
exit()
loan = []
loan += customer_obj.number, customer_obj.surname, loan_obj['title']
this.loaned_books.append(loan)
for data in customer_obj.customer_data:
data['loaned_books'].append(loan)
else:
print('We can\'t loan anything aside from books or that title is not inside the catalog')
def loan_is_returned(this, loan_obj):
print(loan_obj)
for data in this.loaned_books:
if loan_obj[2] in data[2]:
this.loaned_books.remove(loan_obj)
print(this.loaned_books)
def import_customers(this, method=None):
if method is not None:
data = method
this.customers += data
else:
with open('FakeNameSet20.csv', 'r', encoding='utf-8-sig') as f:
data = cs.reader(f)
for i in data:
this.customers.append(i)
def import_loans(this, method):
data = method
this.loaned_books += data
def display_customers(this):
for data in this.customers:
print(data)
def display_loaned_books(this):
print('\n' + 'Loaned books, beginning with customer given name, surname, loaned title and whether it\'s loaned or not:')
for data in this.loaned_books:
print(data)
def add_customer(this, customer_data):
this.customers.append(customer_data)
def customers_to_json(this):
json_string = js.dumps([this.customers])
json_data = js.loads(json_string)
return json_data
def loans_to_json(this):
json_string = js.dumps([this.loaned_books])
json_data = js.loads(json_string)
return json_data
def export_customers(this):
csv_data = this.customers
with open('customers.csv', 'w', encoding='utf-8-sig', newline='') as f:
write = cs.writer(f)
write.writerows(csv_data)
class BackUp:
@staticmethod
def create_json_backup():
catalog_data = PLS.Catalog.books
loan_admin_data = PLS.LoanAdministration.customers_to_json()
loan_admin_loans = PLS.LoanAdministration.loans_to_json()
combined_data = catalog_data+loan_admin_data+loan_admin_loans
with open('PLS backup.json', 'w', encoding='utf-8-sig') as f:
js.dump(combined_data, f)
@staticmethod
def restore_json_backup_books():
with open('PLS backup.json', 'r', encoding='utf-8-sig') as f:
loaded_backup_file = js.load(f)
return loaded_backup_file[1:-2] # Books
@staticmethod
def restore_json_backup_customers():
with open('PLS backup.json', 'r', encoding='utf-8-sig') as f:
loaded_backup_file = js.load(f)
return loaded_backup_file[-2] # Customers
@staticmethod
def restore_json_loaned_books():
with open('PLS backup.json', 'r', encoding='utf-8-sig') as f:
loaded_backup_file = js.load(f)
return loaded_backup_file[-1] # Loaned books
# Customer objects
c1 = Customer(21, 'male', 'Tommy Hieu', 'ding', 'Tommy address 21', ' 6969 xx', 'Spijkenisse', 'hotvietboy@gmail.com', 'imaginaryuser', 6123456789)
c2 = Customer(24, 'male', 'Danyel ', 'Feiten', 'Imaginary street 5', '7787 dd', 'Rotterdam', 'gheboi69@gmail.com', 'destroyedcunt7', 684756389)
# LoanItem objects
l1 = LoanItem(False, "Tommy Hieu Ding", "Vietnam", "https://i.imgur.com/dkJ4iuQ.png", "Vietnamese", "nhentai.net", 777, "Word Art", 2019)
l2 = LoanItem(False, "Daan", "Netherlands", "https://i.imgur.com/wPdIKkT.png", "Dutch", "reddit.com", 123, "UML for dummies", 2018)
l3 = LoanItem(False, "Mikhail Gorbachev", "Russia", "https://i.imgur.com/dHi1BnZ.png", "English", "rt.com", 300, "On My Country and the World", 2000)
# Book objects
b1 = Book("Tommy Hieu Ding", "Vietnam", "https://i.imgur.com/dkJ4iuQ.png", "Vietnamese", "nhentai.net", 777, "Word Art", 2019)
b2 = Book("Dino Cosic", "Bosnia", "https://i.imgur.com/1uOrlTw.png", "Bosnian", "uplay.com", 566, "How to R6S", 2018)
b1.create_js_book()
b2.create_js_book()
class PublicLibrarySystem:
def __init__(this):
this.LoanAdministration = LoanAdministration()
this.Catalog = Catalog()
# This first section of function/instance calls is dedicated to the criteria "filling the system with books from a file of books in JSON format (file with books)"
this.Catalog.import_books() # WARNING! The function must have a valid path to the JSON file in order to import it!
this.Catalog.display_catalog()
print('\n')
# Up next is "filling the system with customers from a file of people ( file)."
this.LoanAdministration.import_customers() # WARNING! The function must have a valid path to the CSV file in order to import it!
this.LoanAdministration.display_customers()
print('\n')
# Now we look at assessment "adding a book item."
this.Catalog.add_book(b1) # b1 is defined above with it's corresponding function call to make this possible
this.Catalog.add_book(b2) # Same goes for b2
this.Catalog.display_catalog()
print('\n')
# Up next "adding a customer."
this.LoanAdministration.add_customer(['21', 'transcended', 'Dutch', 'John', 'Doe', 'Imaginary street 777', '9999 ZZ', 'Johndam', 'imaginary@email.e', 'test_user123', 'Neverland', '06-123456789'])
this.LoanAdministration.add_customer(['22', 'Unknown', 'Dutch', '¿áßðå', '©æ©þü©', 'ð®äåðæ', '0001 AA', 'ø©ñµµó', 'áßðå@email.e', 'test_user321', 'Unknown', '06-123456789'])
this.LoanAdministration.display_customers()
print('\n')
# Up next is "searching a book."
# This criteria was a bit more tricky for me as I did not know how to make approximate searches, the search is based on full and correct booktitles
# In order to come around this, I first display the catalog and randomly choose a book title. Erroneous searches show up as nothing in the console
# Otherwise there will be a single line (or multiple) with the dictionary data of the corresponding book(s)
this.Catalog.search_book('Memoirs of Hadrian')
print('\n')
this.Catalog.search_book(None, 'Virginia Woolf')
print('\n')
this.Catalog.search_book(None, None, 'United States', 'English')
print('\n')
this.Catalog.search_book('random name to show how it does nothing when it cant find anything')
print('\n')
# Now we look at "making a book loan."
this.LoanAdministration.loan_to_customer(c2, l1)
this.LoanAdministration.loan_to_customer(c2, b1)
this.LoanAdministration.loan_to_customer(c2, l2)
this.LoanAdministration.loan_to_customer(c2, this.Catalog.add_loan_item('Memoirs of Hadrian'))
this.LoanAdministration.loan_is_returned(c2.return_book(None, 'Memoirs of Hadrian'))
this.LoanAdministration.loan_is_returned(c2.return_book(l2, None))
this.LoanAdministration.loan_is_returned(c2.return_book(l1, None))
# Customer.display_loaned_books(c2)
# this.LoanAdministration.loan_to_customer(c2, l2)
# this.LoanAdministration.display_loaned_books()
print('\n')
# And finally, here we restore our data from the PLS backup.json
# !IMPORTANT! uncomment line 327 first and run the program to create a backup first!
# !IMPORTANT! comment line 327 again to prevent it from making an empty bogus backup
# After having done that, comment (or delete) all the lines from 306 up to and including 262
# Run the program again to see that there's absolutely nothing going on
# Now uncomment the lines 316 up to and including 321 and run the program to see the result
# this.Catalog.import_books(BackUp.restore_json_backup_books())
# this.LoanAdministration.import_customers(BackUp.restore_json_backup_customers())
# this.LoanAdministration.import_loans(BackUp.restore_json_loaned_books())
# this.Catalog.display_catalog()
# this.LoanAdministration.display_customers()
# this.LoanAdministration.display_loaned_books()
print('\n')
PLS = PublicLibrarySystem()
#BackUp.create_json_backup()
# Author objects
# a1 = Author('Abdel Majid ', 21, 'How to joint venture I', 'How to P', 'How to drop table Cihat', 'How to Ortam I', 'How to churros')
# a2 = Author('Caglar', 22, 'How to BSA', 'How to joint venture II', 'How to kms')
# a3 = Author('Muhammad', 21, 'How to security', 'How to Zoetermeer', 'How to Erdogan', 'How to Turkse', 'How to Ortam III', 'How to waarom doe je zo')
# a4 = Author('Milad', 70, 'How to shitpost', 'how to BSA', 'How to eat', 'How to Coke', 'How to piratenpartij', 'How to sleutelbeen', 'How to Ortam II')
# a5 = Author('Zakaria', 24, 'How to tablet', 'How to op de plaat', 'How to 2 euro voor kraanwater', 'How to nene çay')
You can’t perform that action at this time.