# Class to represent a software application
class SoftwareApplication:
# Constructor to initialize the application with name, category, description, and version
def __init__(self, name, category, description, version):
self.name = name # The name of the application
self.category = category # The category of the application
self.description = description # The description of the application
self.version = version # Version as a tuple (version number, month, year)
# String representation of the application for easy display
def __str__(self):
# Format the application details as a string for display
return f"Name: {self.name}\nCategory: {self.category}\nDescription: {self.description}\nVersion: {self.version[0]} (Month: {self.version[1]}, Year: {self.version[2]})"
# List to store all the applications entered by the user
applications = []
# Start the main program loop
while True:
# Display the main menu to the user
print("\nMenu:")
print("1 - Add data of a new application")
print("2 - Show all stored applications")
print("3 - View all data for a certain application")
print("4 - Update a record")
print("5 - Delete a record")
print("6 - Sort data alphabetically by name")
print("7 - Fix redundant spaces in application names")
print("X - Exit")
# Get the user's choice from the menu
choice = input("Enter your choice: ").strip().lower()
# Option 1: Add a new application
if choice == '1':
# Get user input for the application name, ensuring it's not empty
name = input("Enter application name: ").strip()
if not name: # Check if the name is empty
print("Name cannot be empty.") # Display an error message
continue # Skip to the next iteration of the loop
# Get the category, ensuring its length is within the limit of 30 characters
category = input("Enter category (max 30 characters): ").strip()
while len(category) > 30: # Check if the category exceeds 30 characters
print("Category is too long. Please re-enter.") # Error message
category = input("Enter category (max 30 characters): ").strip() # Prompt again
# Get the description and truncate it to the first 100 characters
description = input("Enter description (max 100 characters): ").strip()
description = description[:100] # Truncate the description to 100 characters
# Get the version number, launch month, and launch year
version_number = input("Enter version number: ").strip()
launch_month = int(input("Enter launch month (1-12): ").strip())
launch_year = int(input("Enter launch year: ").strip())
# Create a new SoftwareApplication object and add it to the list
applications.append(SoftwareApplication(name, category, description, (version_number, launch_month, launch_year)))
# Option 2: Show all stored applications
elif choice == '2':
if not applications: # Check if there are no applications stored
print("No applications stored.") # Display a message if no applications exist
else:
counter = 1 # Initialize a counter to display application numbers
for app in applications: # Iterate through all stored applications
print(f"\n{counter}. {app}") # Display the application
counter += 1 # Increment the counter
if counter % 20 == 0: # Check if 20 applications have been displayed
input("\nPress Enter to continue...") # Pause and wait for user input
# Option 3: View all data for a certain application
elif choice == '3':
# Prompt the user for search text
search_text = input("Enter part of the name, category, or description to search: ").strip()
found = False # Flag to indicate if any applications were found
for app in applications: # Iterate through all stored applications
# Check if the search text is found in the name, category, or description (case-insensitive)
if (search_text.lower() in app.name.lower() or
search_text.lower() in app.category.lower() or
search_text.lower() in app.description.lower()):
print(f"\n{app}") # Display the application details if a match is found
found = True # Set the flag to True if a match is found
if not found: # If no matches were found
print("No matches found.") # Inform the user
# Option 4: Update an existing record
elif choice == '4':
try:
# Prompt the user for the record number to update
record_number = int(input("Enter the record number to update: ").strip()) - 1
if record_number < 0 or record_number >= len(applications): # Validate the record number
print("Invalid record number.") # Display an error message
continue # Skip to the next iteration of the loop
app = applications[record_number] # Get the application to update
# Display the current data of the application
print(f"\nCurrent data for application {record_number + 1}:")
print(f"Name: {app.name}\nCategory: {app.category}\nDescription: {app.description}\nVersion: {app.version[0]} (Month: {app.version[1]}, Year: {app.version[2]})")
# Prompt the user for new values, or allow them to press Enter to keep the current ones
new_name = input(f"Enter new name (current: {app.name}): ").strip()
if new_name: # If the user entered a new name
app.name = new_name # Update the name
new_category = input(f"Enter new category (current: {app.category}): ").strip()
if new_category: # If the user entered a new category
app.category = new_category # Update the category
new_description = input(f"Enter new description (current: {app.description}): ").strip()
if new_description: # If the user entered a new description
app.description = new_description[:100] # Update the description (truncate to 100 characters)
new_version_number = input(f"Enter new version number (current: {app.version[0]}): ").strip()
if new_version_number: # If the user entered a new version number
app.version = (new_version_number, app.version[1], app.version[2]) # Update the version number
new_month = input(f"Enter new launch month (current: {app.version[1]}): ").strip()
if new_month: # If the user entered a new launch month
app.version = (app.version[0], int(new_month), app.version[2]) # Update the launch month
new_year = input(f"Enter new launch year (current: {app.version[2]}): ").strip()
if new_year: # If the user entered a new launch year
app.version = (app.version[0], app.version[1], int(new_year)) # Update the launch year
except ValueError: # If the input is not a valid integer
print("Please enter a valid number.") # Display an error message
# Option 5: Delete a record
elif choice == '5':
try:
# Prompt the user for the record number to delete
record_number = int(input("Enter the record number to delete: ").strip()) - 1
if record_number < 0 or record_number >= len(applications): # Validate the record number
print("Invalid record number.") # Display an error message
continue # Skip to the next iteration of the loop
# Delete the application from the list
del applications[record_number]
print(f"Application {record_number + 1} deleted.") # Inform the user
except ValueError: # If the input is not a valid integer
print("Please enter a valid number.") # Display an error message
# Option 6: Sort data alphabetically by name
elif choice == '6':
applications.sort(key=lambda app: app.name.lower()) # Sort the applications by name (case-insensitive)
print("Applications sorted alphabetically by name.") # Inform the user
# Option 7: Fix redundant spaces in application names
elif choice == '7':
for app in applications: # Iterate through all applications
app.name = ' '.join(app.name.split()) # Replace multiple spaces with a single space
print("Redundant spaces fixed in application names.") # Inform the user
# Option X: Exit the program
elif choice == 'x':
print("Exiting the program...") # Inform the user
break # Exit the loop and end the program
else:
print("Invalid choice, please enter a valid option.") # Display an error message if the choice is invalid
Output
Menu:
1 - Add data of a new application
2 - Show all stored applications
3 - View all data for a certain application
4 - Update a record
5 - Delete a record
6 - Sort data alphabetically by name
7 - Fix redundant spaces