
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 the customer wants to travel long mobNo - to store the 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 Member 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 to 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 member methods.
Topic: Class with conditional amount update
Answer
import java.util.Scanner;
class RailwayTicket { String name, coach; long mobNo; int amt, totalAmt;
void accept() { Scanner sc = new Scanner(System.in); System.out.print("Enter name : "); name = sc.nextLine(); System.out.print("Enter coach type : "); coach = sc.nextLine(); System.out.print("Enter mobile number : "); mobNo = sc.nextLong(); System.out.print("Enter basic amount : "); amt = sc.nextInt(); }
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; else totalAmt = amt; }
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[]) { RailwayTicket ob = new RailwayTicket(); ob.accept(); ob.update(); ob.display(); } }
A class question decided by a String comparison rather than a numeric range.
Compare Strings with equalsIgnoreCase(), never with ==. The == operator compares memory addresses, not the text, and would fail even when the words read identically. Using equalsIgnoreCase() also accepts "first_ac", "FIRST_AC" and "First_AC" alike, which is what a real booking system needs.
The sleeper coach adds nothing, so the final else simply sets totalAmt = amt. Writing else if(coach.equalsIgnoreCase("sleeper")) would leave totalAmt at 0 for any unexpected input — a plain else is safer.
Note the data type of mobNo: a ten-digit number exceeds the range of int (about 2.1 billion), so long is required. The question specifies it, and choosing int would be a genuine error.
Check: a Second_AC ticket with a basic amount of 1200 gives a total of Rs. 1700.
name String - name of the customer coach String - type of coach chosen mobNo long - mobile number (long, since it exceeds int range) amt int - basic amount of the ticket totalAmt int - amount payable after the coach charge is added sc Scanner - object used to read input ob RailwayTicket - object of the class used in main()