|
| 1 | +# Class representing a Car |
| 2 | +class Car: |
| 3 | + |
| 4 | + def __init__(self, make, model, year): |
| 5 | + """ |
| 6 | + Constructor to initialize the car object with make, model, and year. |
| 7 | +
|
| 8 | + :param make: The make of the car (e.g., Toyota, Ford) |
| 9 | + :param model: The model of the car (e.g., Camry, Mustang) |
| 10 | + :param year: The manufacturing year of the car |
| 11 | + """ |
| 12 | + self.make = make |
| 13 | + self.model = model |
| 14 | + self.year = year |
| 15 | + self.speed = 0 # Initial speed is set to 0 |
| 16 | + |
| 17 | + def accelerate(self, acceleration): |
| 18 | + """ |
| 19 | + Method to accelerate the car. |
| 20 | +
|
| 21 | + :param acceleration: The amount by which the car should accelerate |
| 22 | + """ |
| 23 | + self.speed += acceleration |
| 24 | + print(f"The car is accelerating. Current speed: {self.speed} mph") |
| 25 | + |
| 26 | + def brake(self, deceleration): |
| 27 | + """ |
| 28 | + Method to apply brakes and decelerate the car. |
| 29 | +
|
| 30 | + :param deceleration: The amount by which the car should decelerate |
| 31 | + """ |
| 32 | + self.speed -= deceleration |
| 33 | + if self.speed < 0: |
| 34 | + self.speed = 0 |
| 35 | + print(f"The car is braking. Current speed: {self.speed} mph") |
| 36 | + |
| 37 | + |
| 38 | +# Example usage of the Car class |
| 39 | +my_car = Car("Toyota", "Camry", 2022) |
| 40 | +print(f"My car: {my_car.year} {my_car.make} {my_car.model}") |
| 41 | + |
| 42 | +my_car.accelerate(20) |
| 43 | +my_car.brake(10) |
| 44 | + |
| 45 | + |
| 46 | +# Class representing a Book |
| 47 | +class Book: |
| 48 | + |
| 49 | + def __init__(self, title, author, ISBN): |
| 50 | + """ |
| 51 | + Constructor to initialize the book object with title, author, and ISBN. |
| 52 | +
|
| 53 | + :param title: The title of the book |
| 54 | + :param author: The author of the book |
| 55 | + :param ISBN: The ISBN (International Standard Book Number) of the book |
| 56 | + """ |
| 57 | + self.title = title |
| 58 | + self.author = author |
| 59 | + self.ISBN = ISBN |
| 60 | + self.is_checked_out = False # Initial state: not checked out |
| 61 | + |
| 62 | + def __str__(self): |
| 63 | + """ |
| 64 | + String representation of the book. |
| 65 | +
|
| 66 | + :return: A string representation of the book |
| 67 | + """ |
| 68 | + return f"{self.title} by {self.author} (ISBN: {self.ISBN})" |
| 69 | + |
| 70 | + |
| 71 | +# Class representing a Library |
| 72 | +class Library: |
| 73 | + |
| 74 | + def __init__(self): |
| 75 | + """ |
| 76 | + Constructor to initialize the Library object with an empty book catalog. |
| 77 | + """ |
| 78 | + self.catalog = [] |
| 79 | + |
| 80 | + def add_book(self, book): |
| 81 | + """ |
| 82 | + Method to add a book to the library catalog. |
| 83 | +
|
| 84 | + :param book: The book object to be added |
| 85 | + """ |
| 86 | + self.catalog.append(book) |
| 87 | + print(f"Book added to the library: {book}") |
| 88 | + |
| 89 | + def check_out_book(self, ISBN): |
| 90 | + """ |
| 91 | + Method to check out a book from the library. |
| 92 | +
|
| 93 | + :param ISBN: The ISBN of the book to be checked out |
| 94 | + """ |
| 95 | + for book in self.catalog: |
| 96 | + if book.ISBN == ISBN and not book.is_checked_out: |
| 97 | + book.is_checked_out = True |
| 98 | + print(f"Book checked out successfully: {book}") |
| 99 | + return |
| 100 | + print(f"Book with ISBN {ISBN} not found or already checked out.") |
| 101 | + |
| 102 | + |
| 103 | +# Example usage of the Library Management System |
| 104 | +library = Library() |
| 105 | + |
| 106 | +book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "978-0743273565") |
| 107 | +book2 = Book("To Kill a Mockingbird", "Harper Lee", "978-0061120084") |
| 108 | + |
| 109 | +library.add_book(book1) |
| 110 | +library.add_book(book2) |
| 111 | + |
| 112 | +library.check_out_book("978-0743273565") |
| 113 | +library.check_out_book("978-0743273565") # Try to check out the same book again |
0 commit comments