In: Computer Science
You have been provided with 2 classes TicketPrinter and TicketMachine.
The class TicketMachine describes a TicketMachine that has 2 destinations (destination1 and destination2) and 2 corresponding prices (price1 and price2) as attributes. It also has moneyCollections and balance where money collections is the money collected based on ticket purchases and balance is the amount of money the customer has inserted in the ticket machine.
The TicketMachine class also has a constructor that is used to make the ticket machine object. Please inspect it to understand its functioning. There are several methods insertMoney, printTicket and printCollections. The insertMoney method is used to provide the TicketMachine with money so that users can subsequently print tickets. The printCollections method is used to print out the money collected by the TicketMachine. The printTicket method takes a parameter destinationNumber that is used to determine which destination to print a ticket for. You will be primarily working in this method.
The class TicketPrinter has a main method that declares a TicketMachine object and sets 2 destinations for that ticket machine. It declares and creates a scanner object as well that is used to take inputs for money that is passed to the insertMoney method of the ticket machine. Inputs are also take for the destination that the user wishes to print tickets for. Your assignment has to do with making sure that the tickets are printed out for the correct destination and also that balances are updated and collections are maintained appropriately in the TicketMachine.
Your task in this assignment is as follows:
a. Work within the printTicket method of TicketMachine. This method now takes a parameter input called destination Number. Your goal is to make sure that if a user types in 1 then they get printed out a ticket to destination1 and if they type in a 2 they get a ticket printed out to destination2. Examples below.
Destination 1
************************ ********MARTA******** ******RED LINE****** ****Cost : 100 dollars **** ****Destination : New York **** *************************
Destination 2
************************ ********MARTA******** ******RED LINE****** ****Cost : 200 dollars **** ****Destination : SanFrancisco **** *************************
b. Start by checking the destination that the user wishes to travel to. If they wish to travel to destination1 then check if the balance in the machine covers the cost of a ticket to destination1. If it doesn't then inform them as to how much additional money they have to put in. If they wish to travel to destination2 again check if the balance in the machine covers the cost of the ticket to destination2. If it doesn't then inform them as to how much additional money they have to put in.
Example below
Please insert 200 more dollars.
c. Make sure the destination and price are printed out on the ticket corresponding to destination1/price1 and destination2/price2 as shown previously.
d. If the user enters a destinationNumber other than 1 or 2, please inform them of their mistake and tell them to enter 1 or 2.
Example below
Please enter a destination number between 1 and 2.
-------------TicketMachine.java
public class TicketMachine { //class signature, name of the class
int price1;
String destination1;
int price2;
String destination2;
int moneyCollections;
int balance;
public TicketMachine(int dest1Price, String dest1Location, int
dest2Price, String dest2Location)
{
price1 = dest1Price;
destination1 = dest1Location;
price2 = dest2Price;
destination2 = dest2Location;
moneyCollections = 0;
balance = 0;
}
/**
* @return the moneyCollections
*/
public int getMoneyCollections() {
return moneyCollections;
}
/**
* @return the balance
*/
public int getBalance() {
return balance;
}
/**
* Insert money methods takes amount and add's it to balance
* @param amount
*/
public void insertMoney(int amount)
{
if(amount > 0)
{
balance = balance + amount;
}
else
{
System.out.println("Insert a positive value for
money.");
}
}
/**
* Takes a parameter destinationNumber and prints a ticket to the
right destination
* after making sure that the right amount is present for the
ticket.
* @param destinationNumber
*/
public void printTicket(int destinationNumber) //method signature,
method name, public - accessibility
// of the method from external classes
{
System.out.println("************************");
System.out.println("********MARTA********");
System.out.println("******RED
LINE******");
System.out.println("****Cost :
dollars ****");
System.out.println("****Destination
: ****");
System.out.println("*************************");
// fix this moneyCollections =
moneyCollections + price;
//fix this balance = balance - price;
}
/**
* Prints out the collections of the ticket machine.
*/
public void printCollections()
{
System.out.println("Money Collected : " +
moneyCollections);
}
}
----------------------------------TicketPrinter.java.-
import java.util.Scanner;
public class TicketPrinter {
public static void main(String[] args) {
//int collections = 0; //move to TicketMachine since it
can keep track of tickets sold
//int price = 10;// move this to TicketMachine as it
should know the price of a ticket
Scanner scan = new Scanner(System.in);
TicketMachine tm1;
tm1 = new TicketMachine(100,"New York", 200,
"SanFrancisco");
System.out.println("How much money would you like to
insert in the ticket machine? ");
int amountInserted = scan.nextInt();
tm1.insertMoney(amountInserted);
System.out.println("Which destination would you like to
travel to, 1 or 2 ? ");
int destinationNumber = scan.nextInt();
tm1.printTicket(destinationNumber);
System.out.println("Which is the next destination would
you like to travel to, 1 or 2 ? ");
destinationNumber = scan.nextInt();
tm1.printTicket(destinationNumber);
tm1.printTicket(destinationNumber);
}
}
// TicketMachine.java
public class TicketMachine { // class signature, name of the class
int price1;
String destination1;
int price2;
String destination2;
int moneyCollections;
int balance;
public TicketMachine(int dest1Price, String dest1Location, int dest2Price,
String dest2Location) {
price1 = dest1Price;
destination1 = dest1Location;
price2 = dest2Price;
destination2 = dest2Location;
moneyCollections = 0;
balance = 0;
}
/**
* @return the moneyCollections
*/
public int getMoneyCollections() {
return moneyCollections;
}
/**
* @return the balance
*/
public int getBalance() {
return balance;
}
/**
* Insert money methods takes amount and add's it to balance
*
* @param amount
*/
public void insertMoney(int amount) {
if (amount > 0) {
balance = balance + amount;
} else {
System.out.println("Insert a positive value for money.");
}
}
/**
* Takes a parameter destinationNumber and prints a ticket to the right
* destination after making sure that the right amount is present for the
* ticket.
*
* @param destinationNumber
*/
public void printTicket(int destinationNumber) // method signature, method
// name, public -
// accessibility
// of the method from external classes
{
int price=0,flag=0;
String des="";
if(destinationNumber==1 || destinationNumber==2)
{
if(destinationNumber==1)
{
moneyCollections = moneyCollections + price1;
if(balance>=price1)
{
balance = balance - price1;
price=price1;
des=destination1;
System.out.println("Destination 1");
flag=1;
}
else
{
System.out.println("Please insert "+(price1-balance)+" more dollars.");
}
}
else if(destinationNumber==2)
{
moneyCollections = moneyCollections + price2;
if(balance>=price1)
{
balance = balance - price2;
price=price2;
des=destination2;
System.out.println("Destination 2");
flag=1;
}
else
{
System.out.println("Please insert "+(price2-balance)+" more dollars.");
}
}
if(flag==1)
{
System.out.println("************************");
System.out.println("********MARTA********");
System.out.println("******RED LINE******");
System.out.println("****Cost :"+price+" dollars ****");
System.out.println("****Destination :"+des+" ****");
System.out.println("*************************");
}
}
else
{
System.out.println("Please enter a destination number between 1 and 2.");
}
}
/**
* Prints out the collections of the ticket machine.
*/
public void printCollections() {
System.out.println("Money Collected : " + moneyCollections);
}
}
____________________________
// TicketPrinter.java
import java.util.Scanner;
public class TicketPrinter {
public static void main(String[] args) {
int collections = 0; //move to TicketMachine since it can keep track
// of tickets sold
int price = 10;// move this to TicketMachine as it should know the
// price of a ticket
Scanner scan = new Scanner(System.in);
TicketMachine tm1;
tm1 = new TicketMachine(100, "New York", 200, "SanFrancisco");
System.out.println("How much money would you like to insert in the ticket machine? ");
int amountInserted = scan.nextInt();
tm1.insertMoney(amountInserted);
System.out.println("Which destination would you like to travel to, 1 or 2 ? ");
int destinationNumber = scan.nextInt();
tm1.printTicket(destinationNumber);
System.out.println("Which is the next destination would you like to travel to, 1 or 2 ? ");
destinationNumber = scan.nextInt();
tm1.printTicket(destinationNumber);
tm1.printTicket(destinationNumber);
}
}
_________________________________
Output:
How much money would you like to insert in the ticket machine?
300
Which destination would you like to travel to, 1 or 2 ?
1
Destination 1
************************
********MARTA********
******RED LINE******
****Cost :100 dollars ****
****Destination :New York ****
*************************
Which is the next destination would you like to travel to, 1 or 2 ?
2
Destination 2
************************
********MARTA********
******RED LINE******
****Cost :200 dollars ****
****Destination :SanFrancisco ****
*************************
Please insert 200 more dollars.