Question

In: Computer Science

You have been providedwith 2 classes TicketPrinter and TicketMachine.The classTicketMachine describes a TicketMachine...

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);
  
}

}

Solutions

Expert Solution

 

// 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.


Related Solutions

You have been providedwith 2 classes Stock and Portfolio.The Stock classdescribes the template...
You have been provided with 2 classes Stock and Portfolio.The Stock class describes the template for stocks. Stocks have 3 fields/properties String stockName, int numberOfShares, double priceOfShare The constructor of Stock takes 3 parameters corresponding to the fields/properties of the Stock class. There are a variety of accessor/getter methods and mutator/setter methods for Stock.The Portfolio class does not have any fields/properties and has a main method. Within the main method of Portfolio, a double variable portfolioValue is declared and set...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The coin class represents a coin. Any object made from it will have a 1) name, 2) weight and 3) value. As of now, the instance variables in Coin.java are all public, and the main function is calling these variables directly for the one coin made in it. Your goal is to enforce information hiding principles in this project. Take Coin.java, make all instance variables...
Think about different groups you have been a part of (classes, family gatherings, office meetings). Discuss...
Think about different groups you have been a part of (classes, family gatherings, office meetings). Discuss with the class how the process and content of each type of group differ. Which concept was more important in which group? Why?
In this question, use your new GUI classes to create a small application. You have 2...
In this question, use your new GUI classes to create a small application. You have 2 options: Write any small application of your choice, as long as it follows the rules that follow. Write the small UnitConversion application, which will be described for you. Option 1: Write your own application You can write any small application that you like, as long as: It uses your GUI classes, including: at least one checkbox, at least one group of 3 radio buttons,...
Chapter 2 should be a review of previous economics classes you have taken. What do we...
Chapter 2 should be a review of previous economics classes you have taken. What do we expect to happen to market equilibrium price and quantity when demand increases? What about when supply decreases? (In other words, make sure you are familiar with the basic supply and demand model. You may want to play with the graphs on your own)
1. Suppose that you only have 2 classes this semester: intermediate microeconomics and nuclear physics. Your...
1. Suppose that you only have 2 classes this semester: intermediate microeconomics and nuclear physics. Your grade in each class is positively related to the number of hours you spend studying for that class each week. Your grade in the economics class is given by GE = 2 + 0.2E - 0.005E2 where GE is your grade on a 4-point scale and E is the number of hours you study for the economics class. Your grade in the physics class...
2). a). You have been recently appointed as a CEO of a distressed Bankand you are...
2). a). You have been recently appointed as a CEO of a distressed Bankand you are to produce a 5-Year Strategic Plan to position the Bank as a market leader. Discuss the process of producing a strategic plan for the Bank for next 5 years as a sustainable Bank Leader. The following are your KPIs from your Board of Directors: Return on Equity (ROE) of more than 25% Market share of Assets & Deposits of more than 12% (Current market...
You have been following Mr. Miller for the past 2 years. You have worked with him,...
You have been following Mr. Miller for the past 2 years. You have worked with him, trying to get him to lose weight by changing his lifestyle and nutritional habits. He continues to be overweight (body mass index [BMI] of 33) and has tried to cut down his carbohydrate and fat intake, but now presents with polyuria, polydipsia, and an elevated random blood sugar of 164. You have ordered an HbA1c test in the past, as he is at high...
Some of you have taken online classes before, and some have not. Those of you who...
Some of you have taken online classes before, and some have not. Those of you who are online class veterans, what advice can you give to those who are new to online learning? Do you have any tips, tricks or hints? What are the good, the bad and the ugly of online learning?
1) how has covid lead to online classes 2) how have online classes impacted mainstream education?...
1) how has covid lead to online classes 2) how have online classes impacted mainstream education? 3) how are students being affected by this drastic change? 4)is it fare for the fee structure to remain unchanged even though all classes are virtual? 5) is it possible to continue on this path for a prolonged period of time?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT