79752873

Date: 2025-09-01 20:04:20
Score: 0.5
Natty:
Report link
class MetroRailway:
    def __init__(self):
        # Initialize the MetroRailway with an empty list of stations
        # Using list instead of queue to allow easier insertions/deletions
        self.stations = []

    # Method to add a new station
    # Special handling is done when adding the 4th station
    def addnewstation(self, station):
        if len(self.stations) < 3:
            # Directly add the station if fewer than 3 stations exist
            self.stations.append(station)
            print(f"Station '{station}' added successfully.")
        elif len(self.stations) == 3:
            # When adding the 4th station, user chooses position
            print("Insert at --")
            print("[1] First Station")
            print("[2] Middle Station")
            print("[3] Last Station")
            choice = input("Choose Position: ")

            # Insert based on user choice
            if choice == "1":
                self.stations.insert(0, station)  # Insert at the beginning
                print(f"Station '{station}' inserted as First Station.")
            elif choice == "2":
                self.stations.insert(2, station)  # Insert at the middle
                print(f"Station '{station}' inserted as Middle Station.")
            elif choice == "3":
                self.stations.append(station)     # Insert at the end
                print(f"Station '{station}' inserted as Last Station.")
            else:
                # Invalid choice defaults to adding at the end
                print("Invalid choice. Added to the Last Station by default.")
                self.stations.append(station)

            # Display the updated first 3 stations
            print("\nUpdated First 3 Stations:")
            for i, s in enumerate(self.stations[:3], 1):
                print(f"[{i}] {s}")

            # Ask user for preferred position (just an additional prompt)
            pos_choice = input("Enter position: ")
            print(f"You selected position {pos_choice}.")
        else:
            # If more than 4 stations, simply append at the end
            self.stations.append(station)
            print(f"Station '{station}' added successfully.")

    # Method to print all stations in the list
    def printallstations(self):
        if not self.stations:
            print("No stations available.")  # Handle empty station list
        else:
            print("Stations List:")
            # Print each station with numbering
            for i, station in enumerate(self.stations, 1):
                print(f"{i}. {station}")

    # Method to delete a specific station by name
    def deletestation(self):
        if not self.stations:
            print("No stations to delete!")  # Handle case when list is empty
            return

        station = input("Enter station name to delete: ")
        if station in self.stations:
            self.stations.remove(station)  # Remove the station if it exists
            print(f"Station '{station}' deleted successfully.")
        else:
            # Notify user if station is not found
            print(f"Station '{station}' not found in the list!")

    # Method to get the distance between two stations
    # Distance is calculated as the difference in their list indices
    def getdistance(self, station1, station2):
        if station1 in self.stations and station2 in self.stations:
            index1 = self.stations.index(station1)
            index2 = self.stations.index(station2)
            distance = abs(index1 - index2)  # Absolute difference
            print(f"Distance between {station1} and {station2} is {distance} station(s).")
        else:
            # If one or both stations are not in the list
            print("One or both stations not found!")

# Main function to run the MetroRailway program
def main():
    metro = MetroRailway()  # Create a MetroRailway instance

    # Display welcome message and available actions
    print("\nWelcome to LRT1 MetroRailway")
    print("Available Actions:")
    print(" help             - Show this menu")
    print(" printallstations - Show all stations")
    print(" addnewstation    - Add a new station (special choice on 4th input)")
    print(" deletestation    - Delete a specific station by name")
    print(" getdistance      - Measure distance between two stations")
    print(" stop             - Stop current run")
    print(" exit             - Exit the program completely")

    # Infinite loop to keep program running until user chooses to stop/exit
    while True:
        action = input("\nChoose Action: ").lower()

        if action == "help":
            # Show help menu again
            print(" help             - Show this menu")
            print(" printallstations - Show all stations")
            print(" addnewstation    - Add a new station (special choice on 4th input)")
            print(" deletestation    - Delete a specific station by name")
            print(" getdistance      - Measure distance between two stations")
            print(" stop             - Stop current run")
            print(" exit             - Exit the program completely")

        elif action == "printallstations":
            # Show all stations
            metro.printallstations()

        elif action == "addnewstation":
            # Prompt for station name then add
            station = input("Enter station name: ")
            metro.addnewstation(station)

        elif action == "deletestation":
            # Delete station
            metro.deletestation()

        elif action == "getdistance":
            # Prompt user for two stations and calculate distance
            station1 = input("Enter first station: ")
            station2 = input("Enter second station: ")
            metro.getdistance(station1, station2)

        elif action == "stop":
            # Stop current run but not exit the whole program
            print("Stopping current run...")
            break

        elif action == "exit":
            # Exit the program completely
            print("Exiting program completely. Goodbye!")
            exit()

        else:
            # Handle invalid actions
            print("Invalid action. Type 'help' to see available actions.")

# Run the program only if file is executed directly
if __name__ == "__main__":
    main()
Reasons:
  • Blacklisted phrase (1): help me
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Shaheed