Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ICSE JAVA PROGRAMS 2018: SECTION B QUESTION 4

Wednesday, October 6, 2021

ICSE JAVA PROGRAMS 2018: SECTION B QUESTION 4

 

ICSE JAVA PROGRAMS 2018: SECTION B QUESTION 4

Programming for school students ( upper to middle/secondary school) PYTHON AND JAVA PROGRAMS

Design a Railway Ticket Class with the following description:

Instance Variables/Data Members:

String name                     : To store the name of the customer

String coach                     : To store the type of coach customer wants to travel

long mobno                     : To store customer’s mobile number

int amt                             : To store basic amount of ticket

int totalamt                     : To store the amount to be paid after updating the original amount

Member methods :

void accept () – To take input for name, coach, mobile number and amount.

void update() – To update the amount as per the coach selected

(extra amount to be added in the amount as follows)

Type of Coaches Amount

First_AC                  700

Second_AC              500

Third_AC                 250

sleeper                     None

void display() – To display all details of a customer such as name, coach, total amount and mobile number.

Write a main method to create an object of the class and call the above member

methods.


import java.util.Scanner;
public class ICSEJava{
    public static void main(String[] args) {
        RailwayTicket obj = new RailwayTicket();
        obj.accept();
        obj.update();
        obj.display();

    }
}
class RailwayTicket {
    public void display() {
        System.out.println("Passenger's name:"+name);    
        System.out.println("Travelling coach:"+coach); 
        System.out.println("Passenger's mobile no:"+mobileNumber); 
        System.out.println("Intial Amount:"+amount); 
        ifamount < totalAmount)
            System.out.println("Amount after upgradation:"+totalAmount); 
        else
            System.out.println("You have not opted for upgradation:");
    }
    public void update() {
        Scanner in = new Scanner(System.in);
        String choice;
        String temp;
        int counter = 0;
        System.out.println("Do you want to update your coach(Yes,No):");    
        choice = in.nextLine();
        if(choice.toUpperCase().equals("NO"))
            System.out.println("You have not opted for upgradation:");
        else if(choice.toUpperCase().equals("YES")){
            if (coach.equals("First_AC"))
                System.out.println("Cannot be upgraded:");
            else {
                int currentCoachPos = 0;
                int upgradeCoachPos = 0;
                System.out.println("Enter the coach name you want to upgrade:");
                temp = in.nextLine();
                while(!coach.equals(coachType[currentCoachPos++]));
                while(!temp.equals(coachType[upgradeCoachPos++]));
                if (currentCoachPos <= upgradeCoachPos)
                    if(currentCoachPos < upgradeCoachPos)
                        System.out.println("You are already in a higher class:");
                    else
                        System.out.println("You have opted to upgrade in the same class!:");
                else {
                    counter = 0;
                    while(!temp.equals(coachType[counter++]) && counter < 4);   
                    if(counter == 4 )
                        System.out.println("Invalid choice:");
                    else {
                        coach = coachType[counter-1];
                        totalAmount = amount + chargesForCoach[counter-1];
                    }   
                } 
            }
        }
        else {
            System.out.println("Invalid choice:");
        }
        in.close();
    }
    public void accept() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter your name:");
        name = in.nextLine();
        System.out.println("Enter your coach type:(First_AC/Second_AC/Third_AC/Sleeper):");
        coach = in.nextLine();
        System.out.println("Enter your mobile number:");
        mobileNumber = in.nextLong();
        System.out.println("Enter your fare:");
        amount = in.nextInt();
        //in.close();
    }
    String name;
    String coach="Sleeper";
    long mobileNumber;
    int amount;
    int totalAmount;
    String[] coachType = {"First_AC","Second_AC","Third_AC","Sleeper"};
    int[] chargesForCoach = {700,500,250,0};
}

PYTHON CODE

class RailwayTicket:
    def display(self):
        print("Passenger's name:",self.__name)   
        print("Travelling coach:",self.__coach)
        print("Passenger's mobile no:",self.__mobileNumber)
        print("Intial Amount:",self.__amount)
        ifself.__amount < self.__totalAmount):
            print("Amount after upgradation:",self.__totalAmount); 
        else:
            print("You have not opted for upgradation:")
    def update(self):
        choiceOption = ""
        temp = ""
        choiceOption = str(input("Do you want to update your coach(Yes,No):"))
        if choiceOption.upper() == "NO":
            print("You have not opted for upgradation:")
        elif choiceOption.upper() == "YES":
            if self.__coach == "First_AC":
                print("Cannot be upgraded:")
            else:
                currentCoachPos = 0
                upgradeCoachPos = 0
                temp = str(input("Enter the coach name you want to upgrade:"))
                upgradeCoach = temp
                while(self.__coach != self.coachType[currentCoachPos]):
                    currentCoachPos +=1
                while(temp != self.coachType[upgradeCoachPos]):
                    upgradeCoachPos +=1
                if (currentCoachPos <= upgradeCoachPos):
                    if(currentCoachPos < upgradeCoachPos):
                        print("You are already in a higher class:")
                    else:
                        print("You have opted to upgrade in the same class!:")
                else:
                    counter = 0
                    while((temp != self.coachType[counter]) and counter < 4):
                        counter +=1
                    if counter == 4:
                        print("Invalid choice")
                    else:
                        self.__coach = self.coachType[counter-1]
                        self.__totalAmount = self.__amount + self.chargesForCoach[counter-1]
        else:
            print("Invalid choice:")

    def accept(self):
        self.__namestr(input("Enter your name:"))
        self.__coachstr(input("Enter your coach type:(First_AC/Second_AC/Third_AC/Sleeper):"))
        self.__mobileNumber = int(input("Enter your mobile number:"))
        self.__amount = float(input("Enter your fare:"))
    __name = ""
    __coach""
    __mobileNumber = 0
    __amount = float(0)
    __totalAmount = float(0)
    coachType = ["First_AC","Second_AC","Third_AC","Sleeper"]
    chargesForCoach = [700,500,250,0]
obj = RailwayTicket()
obj.accept()
obj.update()
obj.display()

1 comment: