Design a class Railway Ticket with following description: Instance variables/data members: String name: To store the name of the customer
Advertisements
Advertisements
Question
Design a class Railway Ticket with 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 mob no: To store customer’s mobile number
int amt: To store a basic amount of ticket
int total amt: To store the amount to be paid after updating the original amount
Member methods
void accept (): To take input for a 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 a name, coach, total amount and mobile number.
Write a main method to create an object of the class and call the above member methods.
Advertisements
Solution 1
import java.io.*;
import java.util.Scanner; class RailwayTicket {
String name, coach;
long mobno;
int amt, totalamt;
void accept( ) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter Passenger’s Name: “);
name = sc.next( );
System.out.print(“Enter Mobile Number:”);
mobno = sc.nextlnt( );
Systein.out.print(“Enter Coach (FirstAC/SecondAC/ThirdAC/sleeper):”);
coach = sc.next( );
System.out.print(“Enter basic amount of ticket:”);
amt = sc.nexdnt( );
}
void update!) {
if (coach.equals(“First_AC”))
totalamt = amt + 700;
else
if (coach.equals(“Second_AC”))
totalamt = amt + 500; .
else
if (coach.equals!”Third_AC”))
totalamt = amt + 250;
else
totalamt = amt;
}
void display() {
System.out.println(“\n\n Name :” +name);
System.out.println(“Coach :” +coach);
System.out.prindn(”Total Amount:” +totalaint);
System.out.prindn(“Mobile No.:” +name);
}
public static void main (String args[ ]) throws IOException {
RailwayTicket t = new RailwayTicket!);
t.accept();
t.update();
t.display();
}
}
Solution 2
import java.util.Scanner;
public class RailwayTicket
{String name, coach;
long mobno;
int amt, totalamt;
Scanner ob = new Scanner(System.in);
void accept()
{ System.out.println("Enter the Passenger Details");
System.out.print("Name: ");
name = ob.next();
System.out.print("Coach: ");
coach = ob.next().toLowerCase();
System.out.print("Mobile number: ");
mobno = ob.nextLong();
System.out.print("Amount: ");
amt = ob.nextInt();
}
void update()
{ if (coach.equals("first_ac"))
totalamt = amt + 700;
else if (coach.equals("second_ac"))
totalamt = amt + 500;
else if (coach.equals("third_ac"))
totalamt = amt + 250;
else if (coach.equals("sleeper"))
totalamt = amt;
}
void display()
{ System.out.println("\n***RAILWAY TICKET***");
System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Total Amount: " + totalamt);
System.out.println("Mobile Number: " + mobno);
}
public static void main(String args[])
{ RailwayTicket obj = new Railway Ticket();
obj.accept();
obj.update();
obj.display();
}}