Question:
I do no understand this week's JAVA programming project, which is a program that mimics ticket purchases for train objects. It is a train class that includes:
TrainDriver class:
public static void main(String[] args) { Train train1 = new Train("Polar Express", 30); Train train2 = new Train("Disney Safari", 5); // Sell tickets for train #1 System.out.println("Train #1"); tram1.sellAdultTicket(49).sellAdultTicket(30).sellAdultTicket(65); tram1.sellKidTicket(12).sellKidTicket(3); System.out.println(); tram1.displaySeatsAndEarnings(); // Sell some tickets for train #2 System.out.println("\nTrain #2"); for (int i=0; i<7; i++) { tram2.sellAdultTicket(30); } System.out.println(); tram2.displaySeatsAndEarnings(); } // end main
Output:
Tram #1 An adult wants to buy a ticket. A ticket has been sold for $14.50. An adult wants to buy a ticket. A ticket has been sold for $14.50. An adult wants to buy a ticket. The adult is old enough to receive the senior discount, so they pay only $12.50. A kid wants to buy a ticket. A ticket has been sold for $7.50. A kid wants to buy a ticket. The child is young enough to share a seat, so they ride for free. Train "Polar Express" has sold 4 seats for a grand total of $XX.XX. Train #2 An adult wants to buy a ticket. A ticket has been sold for $14.50. An adult wants to buy a ticket. A ticket has been sold for $14.50. An adult wants to buy a ticket. A ticket has been sold for $14.50. An adult wants to buy a ticket. A ticket has been sold for $14.50. An adult wants to buy a ticket. A ticket has been sold for $14.50. An adult wants to buy a ticket. Sorry, the train is full. Unable to purchase ticket. An adult wants to buy a ticket. Sorry, the train is full. Unable to purchase ticket. Train "Disney Safari" has sold 5 seats for a grand total of $XX.XX
/*
* The java program demonstrates the Train class by calling
corresponding methods
* and print the results to console output.
* */
//TestTicket.java
public class TestTicket
{
public static void main(String[] args)
{
Train train1 = new Train("Polar
Express", 30);
Train train2 = new Train("Disney
Safari", 5);
// Sell tickets for
train #1
System.out.println("Train
#1");
train1.sellAdultTicket(49).sellAdultTicket(30).sellAdultTicket(65);
train1.sellKidTicket(12).sellKidTicket(3);
System.out.println();
train1.displaySeatsAndEarnings();
// Sell some tickets
for train #2
System.out.println("\nTrain
#2");
for (int i=0; i<7; i++)
{
train2.sellAdultTicket(30);
}
System.out.println();
train2.displaySeatsAndEarnings();
}
}
-------------------------------------------------------------------------------------------------------------------
//Train.java
import java.text.DecimalFormat;
public class Train
{
private final double kidTicketCost=7.50;//cost of kids
ticket is $7.50
private final double adultTicketCost=14.50;//adult
ticket cost is $14.50
private String name;
private int capcaity;
private int boughtSeats ;
private double ticketEarnings ;
/*parameter constructor that takes
* name and capacity as input and
* set to this class instance variables
*/
public Train(String name, int capacity)
{
this.name=name;
this.capcaity=capacity;
//set 0 to below instance
variables
boughtSeats=0;
ticketEarnings=0;
}
/*Method that takes age for
adults*/
public Train sellAdultTicket(int age)
{
System.out.println("An adult wants
to buy a ticket.");
boughtSeats++;
//call addPerson method
addPerson(age,adultTicketCost);
return this;
}
/*Method that takes age for
kids*/
public Train sellKidTicket(int age)
{
System.out.println("An kid wants to
buy a ticket.");
boughtSeats++;
//call addPerson method
addPerson(age,kidTicketCost);
return this;
}
/*Method addPerson that takes age and cost */
private void addPerson(int age,double cost)
{
if(boughtSeats>capcaity)
System.out.println("Sorry, the train is full. Unable to purchase
ticket.");
else if(age>=65)
{
System.out.println("The adult is old enough to receive the senior
discount, so they pay only $12.50.");
ticketEarnings+=cost-2;
}
else if(age<=3)
{
System.out.println("The child is young enough to share a seat, so
they ride for free.");
}
else if(age>3 &&
age<64)
{
System.out.println("A ticket has been sold for $14.50.");
ticketEarnings+=cost;
}
else
{
System.out.println("A ticket has been sold for $7.50.");
ticketEarnings+=cost;
}
}
/*Display the total tickets bought and
* total earnings */
public void displaySeatsAndEarnings()
{
DecimalFormat df=new
DecimalFormat("0.00");
System.out.println("Train
\""+name+"\" has sold "+ boughtSeats
+" seats for a grand total of $"
+df.format(ticketEarnings)+".");
}
}
-------------------------------------------------------------------------------------------------------------------
Sample Output:
Train #1
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
The adult is old enough to receive the senior discount, so they pay
only $12.50.
An kid wants to buy a ticket.
A ticket has been sold for $14.50.
An kid wants to buy a ticket.
The child is young enough to share a seat, so they ride for
free.
Train "Polar Express" has sold 5 seats for a grand total of $49.00.
Train #2
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
A ticket has been sold for $14.50.
An adult wants to buy a ticket.
Sorry, the train is full. Unable to purchase ticket.
An adult wants to buy a ticket.
Sorry, the train is full. Unable to purchase ticket.
Train "Disney Safari" has sold 7 seats for a grand total of $72.50.
Question: I do no understand this week's JAVA programming project, which is a program that mimics...
Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your local bank. You should declare the following collection of classes: 1) An abstract class Customer: each customer has: firstName(String), lastName (String), age (integer), customerNumber (integer) - The Class customer has a class variable lastCustomerNumber that is initialized to 9999. It is used to initialize the customerNumber. Hence, the first customer number is set to 9999 and each time a new customer...
Amusement Park Programming Project Project Outcomes Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Use arrays or ArrayList for storing objects. Proper design techniques. Project Requirements Your job is to implement a simple amusement park information system that keeps track of admission tickets and merchandise in the gift shop. The information system consists of three classes including a class to model tickets, a...
Hi, Kindly assist with my project management assignment below using the attached case study Question 1 Update the project charter for the remainder of the project in response to Adams’ memo (lines 241 through 246). Question 2 Prepare a plan for the remainder of the project in response to Adams’ memo (lines 241 through 246). Your answers to the above will be assessed in terms of the level of communication displayed, the insights and inferences drawn, and your ability to...
Case 34 Emirates Airline Emirates Airline was one of the three Middle East carriers that were singled out by the largest US airlines in the report that was released on March 5, 2015. The report charged that that the flagship airline of Dubai, along with Etihad Airways and Qatar Airways, had received over $42 billion in government subsidies and tax breaks since 2004. Claiming that this gave an unfair advantage to these state-owned airlines, the US airlines demanded that the...