Define a class RailwayTicket 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 mobno : to store customers 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 :
RailwayTicket( ) - default constructor to initialize the data members with default values.
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 the main method to create an object of the class and call the above member methods.
in java
Answers
Answer:
import java.io.*;
class RailwayTicket{
String name;
String coach;
long mobno;
int amt;
int totalamt;
public void accept()throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Name: ");
name = br.readLine();
System.out.print("Coach: ");
coach = br.readLine();
System.out.print("Mobile number: ");
mobno = Long.parseLong(br.readLine());
System.out.print("Amount: ");
amt = Integer.parseInt(br.readLine());
}
public void update(){
if(coach.equalsIgnoreCase("First_AC"))
totalamt = amt + 700;
else if(coach.equalsIgnoreCase("Second_AC"))
totalamt = amt + 500;
else if(coach.equalsIgnoreCase("Third_AC"))
totalamt = amt + 250;
}
public void display(){
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[])
throws IOException{
RailwayTicket obj = new RailwayTicket();
obj.accept();
obj.update();
obj.display();
}
}