English

Railway reservation System → Declare the base class Train with train number, name, Starting station, destination, departure time, arrival time, etc. → Define and declare the function getdata()

Advertisements
Advertisements

Question

Railway reservation System

→ Declare the base class Train with train number, name, Starting station, destination, departure time, arrival time, etc.

→ Define and declare the function getdata() and putdata() to get the train details and print the details.

→ Define search() function to search train detail using train number.

→ Declare the derived class passenger with ticket number, PNR name of the passenger, gender, age, address, phone number, etc.

→ Declare and define the function getdata1() to call search() function to get the train details and get the passenger's information.

→ Define the function display() to call putdata() and display passenger's details.

→ Create base class object.

→ Read the number of trains.

→ Create the derived class object.

→ Read the number of passengers.

→ Call the function getdata1() to each passenger

→ Call the display() function.

Code Writing
Advertisements

Solution

class Train(object):
    def getdata(self):
        self.train_no = input("Train number: ")
        self.train_name = input("Train name: ")
        self.start = input("Starting station: ")
        self.destination = input("Destination: ")
        self.departure = input("Departure time: ")
        self.arrival = input("Arrival time: ")

    def putdata(self):
        print("Train number:", self.train_no)
        print("Train name:", self.train_name)
        print("From:", self.start)
        print("To:", self.destination)
        print("Departure:", self.departure)
        print("Arrival:", self.arrival)

    @staticmethod
    def search(trains, train_no):
        for train in trains:
            if train.train_no == train_no:
                return train
        return None

class Passenger(Train):
    def getdata1(self, trains):
        required_train = input("Enter train number to book: ")
        self.train = Train.search(trains, required_train)

        if self.train is None:
            print("Train not found.")
            return False

        self.ticket_no = input("Ticket number: ")
        self.pnr = input("PNR number: ")
        self.name = input("Passenger name: ")
        self.gender = input("Gender: ")
        self.age = input("Age: ")
        self.address = input("Address: ")
        self.phone = input("Phone number: ")
        return True

    def display(self):
        print("\n--- Train Details ---")
        self.train.putdata()
        print("\n--- Passenger Details ---")
        print("Ticket number:", self.ticket_no)
        print("PNR:", self.pnr)
        print("Name:", self.name)
        print("Gender:", self.gender)
        print("Age:", self.age)
        print("Address:", self.address)
        print("Phone:", self.phone)

trains = []
n = int(input("Enter number of trains: "))
for i in range(n):
    train = Train()
    train.getdata()
    trains.append(train)

p = int(input("Enter number of passengers: "))
for i in range(p):
    passenger = Passenger()
    if passenger.getdata1(trains):
        passenger.display()
shaalaa.com
  Is there an error in this question or solution?
Chapter 4: Inheritance - EXERCISE [Page 85]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 4 Inheritance
EXERCISE | Q 23. | Page 85
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×