Feature Overview
The application handles these operations:
- Insert Record: Capture unique ID, full name, years, and sex too create a new entry.
- Erase Record: Locate and remove a specific entry using its unique identifier.
- Update Details: Change the attributes of an existing entry based on its ID.
- Lookup Info: Search for an entry using either the ID or the name.
- List All: Print every record currently held in memory.
- Persist Data: Write the current state of records to a text file.
- Terminate: Close the application loop.
Core Implementation
The logic relies on two main classes: one representing the individual record and another managing the collection of records.
class Scholar:
def __init__(self, uid, full_name, years, sex):
self.uid = uid
self.full_name = full_name
self.years = years
self.sex = sex
def print_details(self):
print(f"ID: {self.uid}")
print(f"Name: {self.full_name}")
print(f"Age: {self.years}")
print(f"Gender: {self.sex}")
class ScholarRegistry:
def __init__(self):
self.records = []
def insert(self, scholar_obj):
self.records.append(scholar_obj)
def erase(self, target_id):
initial_count = len(self.records)
self.records = [s for s in self.records if s.uid != target_id]
if len(self.records) < initial_count:
print("Removal complete.")
else:
print("ID not located.")
def edit(self, target_id, new_name, new_age, new_gender):
for s in self.records:
if s.uid == target_id:
s.full_name = new_name
s.years = new_age
s.sex = new_gender
print("Update successful.")
return
print("ID not located.")
def find(self, search_term):
found = False
for s in self.records:
if search_term == s.uid or search_term.lower() in s.full_name.lower():
s.print_details()
found = True
if not found:
print("No matches found.")
def show_all(self):
if not self.records:
print("Registry is empty.")
else:
for idx, s in enumerate(self.records, 1):
print(f"--- Record {idx} ---")
s.print_details()
def export_to_file(self, path):
try:
with open(path, 'w') as f:
for s in self.records:
f.write(f"{s.uid}|{s.full_name}|{s.years}|{s.sex}\n")
print("Data dumped to file.")
except IOError as e:
print(f"Write error: {e}")
def import_from_file(self, path):
self.records = []
try:
with open(path, 'r') as f:
for line in f:
parts = line.strip().split('|')
if len(parts) == 4:
self.records.append(Scholar(parts[0], parts[1], int(parts[2]), parts[3]))
print("Data loaded into memory.")
except FileNotFoundError:
print("Storage file missing; starting fresh.")
Usage Workflow
The following script demonstrates the interactive menu for controlling the registry.
app = ScholarRegistry()
app.import_from_file("scholars.txt")
while True:
print("\n--- Scholar Registry ---")
print("1. Insert Record")
print("2. Erase Record")
print("3. Edit Record")
print("4. Find Record")
print("5. List All Records")
print("6. Save to Disk")
print("7. Exit")
action = input("Select option: ")
if action == "1":
uid = input("Unique ID: ")
name = input("Full Name: ")
age = int(input("Age: "))
gender = input("Gender: ")
app.insert(Scholar(uid, name, age, gender))
print("Record added.")
elif action == "2":
target = input("ID to remove: ")
app.erase(target)
elif action == "3":
target = input("ID to edit: ")
name = input("New Name: ")
age = int(input("New Age: "))
gender = input("New Gender: ")
app.edit(target, name, age, gender)
elif action == "4":
term = input("Search by ID or Name: ")
app.find(term)
elif action == "5":
app.show_all()
elif action == "6":
app.export_to_file("scholars.txt")
elif action == "7":
break
else:
print("Invalid selection.")