Question 4:
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.
Solutions:
import java.util.*;
class RailwayTicket
{
String name;
String coach =
" ";
long mobno;
int amt;
int totalamt =
0 ;
void accept()
{
Scanner sk
= new Scanner(System.in);
System.out.print("Enter name: ");
name = sk.nextLine();
System.out.print("Enter coach: ");
coach =
sk.nextLine();
System.out.print("Enter mobno: ");
mobno =
sk.nextLong();
System.out.print("Enter amt: ");
amt =
sk.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 Details of the customer is : ");
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 arg[])
{
RailwayTicket ob = new RailwayTicket();
ob.accept();
ob.update();
ob.display();
}
}
Thank u sir afroz
ReplyDelete