Ticket

Design a class with the following description.

Class Name: Ticket

Data Members:

  • String name – To store the name of the customer.
  • String coach – To store the type of coach
  • long mobile – To store consumer’s mobile number.
  • double amount – To store the amount of ticket.
  • double total – To store the amount to be paid after updating the original amount.

Member Methods:

  • Ticket() – Default constructor to initialize the data members.
  • void input() – To input customer name, mobile number, coach type and price of ticket.
  • void calculate() – To provide the amount as per the coach. Amount to be added in the amount as follows.
Type Of CoachAmount
ac-one-tyre₹1000
ac-second-tyre₹500
ac-third-tyre₹100
sleeper₹0
  • void display() – To print all details.

Write a main method to create an object of the class and call methods.

Java
import java.util.*;
public class Ticket
{
    String name,coach;
    long mobile;
    double amount,total;
    Ticket()
    {
        name="";
        coach="";
        mobile=0;
        amount=0.0;
        total=0.0;
    }
    public void input()
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Customer name: ");
        name=sc.nextLine();
        System.out.print("Type of coach: ");
        coach=sc.nextLine();
        coach=coach.toLowerCase();
        System.out.print("Customer's mobile number: ");
        mobile=sc.nextLong();
        System.out.print("Amount: ");
        amount=sc.nextDouble();
        System.out.println();
    }
    public void calculate()
    {
        if(coach.compareTo("ac-one-tyre")==0)
        {
            total=amount+1000;    
        }
        else if(coach.compareTo("ac-second-tyre")==0)
        {
            total=amount+500;    
        }
        else if(coach.compareTo("ac-third-tyre")==0)
        {
            total=amount+100;    
        }
        else if(coach.compareTo("sleeper")==0)
        {
            total=amount+0;    
        }
        else
        {
            System.out.println("Invalid input! No such coach");
        }
    }
    public void display()
    {
        System.out.println("Customer's Name: "+name);
        System.out.println("Customer's Mobile Number: "+mobile);
        System.out.println("Coach type: "+coach);
        System.out.println("Amount to be paid: "+total);
    }
    public static void main (String args[])
    {
        Ticket ob=new Ticket();
        ob.input();
        ob.calculate();
        ob.display();
    }
}
Java