question archive Create a class Person with the following private member variables String name char gender int age Include appropriate getters and setters method Create a class BusTicket with the following private member variables int ticketNo float ticketPrice float totalAmount Person person Include appropriate getters and setters method Write the following method in the BusTicket class void calculateTotal()—this method should calculate the total and set it based on the discount given below: For Children whose age is less than 16, give 50% discount For Senior citizen whose age is greater than or equal to 60 give 25% discount For Ladies give 10% discount No discount for remaining category
Subject:Computer SciencePrice:5.87 Bought7
Create a class Person with the following private member variables
Include appropriate getters and setters method Create a class BusTicket with the following private member variables
Include appropriate getters and setters method
Write the following method in the BusTicket class
void calculateTotal()—this method should calculate the total and set it based on the discount given below:
Based on above condition calculate total price. Create TestMain class which has main method and do the following input and output process .
To get the input write a method
public static BusTicket getTicketDetails() - Get the inputs relevant to BusTicket in this method and return the BusTicket object. Call this method in the main method, using the returned object invoke the method calculateTotal and print the output accordingly
Note: Define all the attributes as private and methods as public.
Sample input 1:
Enter the passenger name:
vivek
Enter the gender(M or F / m or f):
M
Enter the age:
16
Enter the ticket no:
123
Enter the ticket price:
100.0
Answer:
Below is the code attached
Person class
public class Person { private String name; private char gender; private int age; public void setName(String name){ this.name=name; } public void setGender(char gender){ this.gender=gender; } public void setAge(int age){ this.age=age; } public String getName(){ return name; } public char getGender(){ return gender; } public int getAge(){ return age; } }
BusTicket class
public class BusTicket { private int ticketNo; private float ticketPrice; private float totalAmount; Person person=new Person(); public void setTicketNo(int ticketNo){ this.ticketNo=ticketNo; } public void setTicketPrice(float ticketPrice){ this.ticketPrice=ticketPrice; } public int getTicketNo(){ return ticketNo; } public float getTicketPrice(){ return ticketPrice; } public float getTotalAmount(){ return totalAmount; } public void calculateTotal(){ if(person.getAge()<16){ totalAmount=ticketPrice/2; } else if(person.getAge()>=60){ totalAmount=ticketPrice*0.75f; }else if(person.getGender()=='F'||person.getGender()=='f'){ totalAmount=ticketPrice*0.90f; }else{ totalAmount=ticketPrice; } } }
TestMain class
import java.util.Scanner; public class TestMain { public static BusTicket getTicketDetails(){ Scanner sc=new Scanner(System.in); System.out.println("Enter the passenger name:"); String name=sc.nextLine(); System.out.println("Enter the Gender (M or F / m or f):"); char gender=sc.next().charAt(0); System.out.println("Enter age: "); int age=sc.nextInt(); System.out.println("Enter ticket no: "); int tno=sc.nextInt(); System.out.println("Enter the ticket price"); float price=sc.nextFloat(); BusTicket ticket=new BusTicket(); ticket.person.setName(name); ticket.person.setAge(age); ticket.person.setGender(gender); ticket.setTicketNo(tno); ticket.setTicketPrice(price); return ticket; } public static void main(String[] args) { BusTicket ticket=getTicketDetails(); ticket.calculateTotal(); System.out.println("Total amount of ticket is : "+ticket.getTotalAmount()); } }
Step-by-step explanation
i have attached all the code in answer section
all code and its structure is same as it was described in question
all methods are working properly
output screenshot
in this output final price is same because age not less than 16
PFA