【Class Report】 Introduction to System Development Week 21 – Extending with Inheritance & Building a Console UI!
Week 21’s lesson further enhanced last week’s library lending system by extending functionality using inheritance and introducing a console-based menu UI.
■ Teacher’s Introduction: “A user-friendly interface shows consideration for the user”
Teacher Tanaka: “It’s not enough to just add features; creating an intuitive interface for users is equally important. This time, let’s add new book types via inheritance and tidy up the operation screen.”
■ Exercise ①: Creating ReferenceBook
/ Magazine
Classes
First, we inherit from the Book
class to add book types with usage restrictions and different return rules.
from book import Book
class ReferenceBook(Book):
def lend(self):
# Reference books cannot be lent out
print(f"{self.title} is a reference book and cannot be lent out.")
return False
class Magazine(Book):
def __init__(self, title, author, issue):
super().__init__(title, author)
self.issue = issue
def lend(self):
# Only the latest issue of magazines can be lent
if self.is_available and self.issue == "Latest Issue":
self.is_available = False
return True
print(f"{self.title} ({self.issue}) cannot be lent out.")
return False
Student A: “It’s fun to treat reference books specially!”
Student B: “Only lending the latest issue really feels like a real library!”
■ Exercise ②: Implementing a Simple Console Menu
Next, we create a menu UI that’s easy to operate. We added methods to the Library
class to display options in a loop.
def show_menu():
print("\n=== Library Menu ===")
print("1. Search for a book")
print("2. Borrow a book")
print("3. Return a book")
print("4. Exit")
def run_console(library, member):
while True:
show_menu()
choice = input("Please enter a number: ")
if choice == "1":
title = input("Title to search: ")
results = library.find_book(title)
print([b.title for b in results] or "No matching books found")
elif choice == "2":
title = input("Title to borrow: ")
results = library.find_book(title)
if results and member.borrow(results[0]):
print("Borrowing successful!")
else:
print("Failed to borrow.")
elif choice == "3":
title = input("Title to return: ")
book = next((b for b in member.borrowed if b.title == title), None)
if book and member.give_back(book):
print("Return successful!")
else:
print("Failed to return.")
elif choice == "4":
print("Thank you for using the system!")
break
else:
print("Invalid input. Please try again.")
Student C: “Having a menu really makes it feel like an app!”
Student D: “Handling input errors gives me peace of mind!”
■ Silent Coding Time: Tweaking the UI and Testing
In the latter half, everyone refined the menu text and error handling. They tested repeatedly in their own environments, focusing on usability improvements:
- Numbered menu options
- Enhanced guidance messages for invalid input
- Displaying magazines and reference books in search results
Student E: “Adding more guidance made it less confusing!”
Student F: “It’s nice that reference books also show up in the list.”
■ Teacher’s Final Note
“Inheritance helps organize functionality, and a good UI enhances usability. Balancing code structure and user experience is key. The concepts you learned today apply to any app.”
■ Next Week’s Preview: Preparing for External API Integration
Starting next week, ahead of second-year courses, we’ll learn the basics of simple external API integration. We’ll challenge ourselves by connecting to external services like weather information and map data!
Through extending with inheritance and implementing a console UI, the first-year students experienced the joy and depth of writing “practical code.”