icse-promo

Question

Design a class RailwayTicket 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.
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 methods.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
 
public class RailwayTicket
{
    String name;
    String coach;
    long mobno;
    int amt;
    int totalamt;
    public RailwayTicket()
    {
        name="";
        coach="";
        mobno=0;
        amt=0;
        totalamt=0;
    }
    public void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter name of passenger: ");
        name = sc.nextLine();
        System.out.print("Enter coach:(Type 1A for first class, 2A for second class, 3A for third class & SL for Sleeper class) ");
        coach = sc.nextLine();
        System.out.print("Enter mobno of passenger: ");
        mobno = sc.nextLong();
        System.out.print("Enter amount: ");
        amt = sc.nextInt();
    }
 
    public void update()
    {
        if (coach.equalsIgnoreCase("1A"))
        {
            totalamt = amt + 700;
        }
        else if (coach.equalsIgnoreCase("2A"))
        {
            totalamt = amt + 500;
        }
        else if (coach.equalsIgnoreCase("3A"))
        {
            totalamt = amt + 250;
        }
        else if (coach.equalsIgnoreCase("SL"))
        {
            totalamt = amt;
        }
        else
        {
            System.out.println("Invalid coach number");
        }
    }
 
    public void display() 
    {
        System.out.println("Name: " + name);
        System.out.println("Coach: " + coach);
        System.out.println("Mobile Number: " + mobno);
        System.out.println("Amount: " + amt);
        System.out.println("Total Amount: " + totalamt);
    }

    public static void main(String args[])
    {
        RailwayTicket ob1 = new RailwayTicket();
        ob1.accept();
        ob1.update();
        ob1.display();
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *