Dani Zaeem 480307 Python First Assignment
Dani Zaeem 480307 Python First Assignment
# Author
class Author:
def __init__(self, name, nationality):
self.name = name
self.nationality = nationality
def __str__(self):
return f"{self.name} ({self.nationality})"
# Book
class Book:
def __init__(self, title, author, publication_date):
self.title = title
self.author = author # Author example
self.publication_date = publication_date
self.is_loaned = False # the book is loaned out or not
def __str__(self):
return f"'{self.title}' by {self.author.name} (Published: {self.
↪publication_date})"
# Patron
class Patron:
def __init__(self, name):
self.name = name
self.borrowing_history = [] # borrowed books and dates
1
print(f"{self.name} borrowed '{book.title}' until {due_date.
↪strftime('%Y-%m-%d')}.")
else:
print(f"Sorry, '{book.title}' is already loaned out.")
def __str__(self):
return f"Patron: {self.name}, Borrowing History: {[book.title for book,␣
↪_, _ in self.borrowing_history]}"
def query_books(self):
for book in self.books:
status = "Available" if not book.is_loaned else "Loaned Out"
print(f"{book} - Status: {status}")
2
def query_authors(self):
for author in self.authors:
print(author)
def query_patrons(self):
for patron in self.patrons:
print(patron)
# Example
if __name__ == "__main__":
library = Library()
# Add Authors
author1 = library.add_author("Naguib Mahfouz", "Egyptian")
author2 = library.add_author("Mahmoud Darwish", "Jordanian")
# Add Books
book1 = library.add_book("Palace Walk", author1, "1956")
book2 = library.add_book("Memory for Forgetfulness", author2, "1987")
# Add Patrons
patron1 = library.add_patron("Ahmed")
patron2 = library.add_patron("Layla")
# Query Library
print("\nLibrary Books:")
library.query_books()
print("\nLibrary Authors:")
library.query_authors()
print("\nLibrary Patrons:")
library.query_patrons()
3
Ahmed borrowed 'Palace Walk' until 2025-01-15.
Layla borrowed 'Memory for Forgetfulness' until 2025-01-15.
Ahmed returned 'Palace Walk'.
Library Books:
'Palace Walk' by Naguib Mahfouz (Published: 1956) - Status: Available
'Memory for Forgetfulness' by Mahmoud Darwish (Published: 1987) - Status: Loaned
Out
Library Authors:
Naguib Mahfouz (Egyptian)
Mahmoud Darwish (Jordanian)
Library Patrons:
Patron: Ahmed, Borrowing History: ['Palace Walk']
Patron: Layla, Borrowing History: ['Memory for Forgetfulness']
[ ]:
4
Dani_Zaeem_First_Assignment_480307_
# Cars
class Car:
def __init__(self, manufacturer, model, rental_cost):
self.manufacturer = manufacturer
self.model = model
self.rental_cost = rental_cost
self.rental_income = 0 # income generated by this car
self.is_rented = False # the car is currently rented or not
def __str__(self):
return f"{self.manufacturer} {self.model} (Cost per day: ${self.
↪rental_cost})"
# Customers
class Customer:
def __init__(self, name, contact_info, credit_card):
self.name = name
self.contact_info = contact_info
self.credit_card = credit_card
self.rental_history = [] # Tracks past rentals
def __str__(self):
return f"Customer: {self.name}, Contact: {self.contact_info}"
# Rental Records
class RentalRecord:
def __init__(self, customer, car, rental_date, rental_days, rental_cost):
self.customer = customer
self.car = car
self.rental_date = rental_date
self.rental_days = rental_days
self.rental_cost = rental_cost
def __str__(self):
1
return (f"Rental Record: {self.customer.name} rented {self.car.
↪manufacturer} {self.car.model} "
f"on {self.rental_date.strftime('%Y-%m-%d')} for {self.
↪rental_days} days "
if not customer:
print(f"Customer '{customer_name}' not found.")
return
if not car:
print(f"Car '{car_model}' is either not available or already rented.
↪")
return
2
car.rental_income += rental_cost
customer.rental_history.append(record)
self.rental_records.append(record)
if car:
car.is_rented = False
print(f"Car '{car.model}' has been returned.")
else:
print(f"Car '{car_model}' is not currently rented.")
def calculate_income(self):
# Calculate and display total income for all cars
total_income = sum(car.rental_income for car in self.cars)
print(f"Total income for all cars: ${total_income}")
# income
for car in self.cars:
print(f"Total income for {car.manufacturer} {car.model}: ${car.
↪rental_income}")
if not customer:
print(f"Customer '{customer_name}' not found.")
return
print(f"Rental history for {customer.name}:")
for record in customer.rental_history:
print(record)
# Example
if __name__ == "__main__":
# Initialize the system
rental_system = CarRentalSystem()
# Add Cars
rental_system.add_car("Mercedes", "S500", 150)
rental_system.add_car("Nissan", "Sunny", 40)
# Add Customers
3
rental_system.add_customer("Dani", "[email protected]", "5342-4356-7536-3467")
rental_system.add_customer("Florian", "[email protected]",␣
↪"6456-3673-5673-4578")
[ ]:
4
Untitled7
def calculate_average_grade(self):
grades = [info["grade"] for info in self.courses.values() if␣
↪info["grade"] is not None]
def calculate_total_credit_hours(self):
return sum(info["course"].credit_hours for info in self.courses.
↪values())
class Course:
def __init__(self, name, credit_hours):
self.name = name
self.credit_hours = credit_hours
class GradeSystem:
def __init__(self):
self.students = []
self.courses = []
1
course = Course(name, credit_hours)
self.courses.append(course)
print(f"Course {name} added.")
# Example usage
if __name__ == "__main__":
system = GradeSystem()
# Add students
system.add_student("Dani", 20, "S001")
system.add_student("Müller", 22, "S002")
2
# Add courses with updated credit hours
system.add_course("Music", 6)
system.add_course("Programming", 12)
system.add_course("Physics", 9)
# Calculate and display grades and total credit hours for each student
dani = system._find_student("S001")
print(f"{dani.name}'s Average Grade: {dani.calculate_average_grade()}")
print(f"{dani.name}'s Total Credit Hours: {dani.
↪calculate_total_credit_hours()}")
mueller = system._find_student("S002")
print(f"{mueller.name}'s Average Grade: {mueller.
↪calculate_average_grade()}")
3
Dani's Total Credit Hours: 18
Müller's Average Grade: 82.5
Müller's Total Credit Hours: 15
[ ]: