App2 Ticket Machine - AtishAppadu/BlueJay Wiki

App02 TicketMachine

Description

This app is a simple simulation of a ticket machine where the user enter real money and is able to purchase a ticket

Requirements

Edit your copy of App02-ticketmachine as a starting point and make it more realistic by offering tickets to three local stations

  1. Aylesbury costing £2.20
  2. Amersham costing £3.00
  3. High Wycombe costing £3.30

Features

  1. Each ticket should have a destination, a cost and the date purchased.
  2. Print all available Tickets
  3. Select one of the Tickets to buy
  4. Add methods to the TicketMachine so that a user can insert 10p, 20, £1.00 and £2.00 coins.
  5. After each coin is inserted the machine should display the amount entered and the current balance.

Required Testing

Create a TicketMachine and use it to purchase all three tickets. Make sure that your tests include trying to purchase a ticket with too little money inserted, and also with too much money inserted. Show that excess money can be refunded. Use the terminal window to record your tests.

Design

App02 Class Diagram

Additional Java Concepts

YOU CAN GET A FIRST CLASS MARK (80%) FOR FUNCTIONALITY WITHOUT USING THE CONCEPTS BELOW IN App02.
However the following concepts are a better way of implementing some of the TicketMachine's functionality, and if used will gain you a further 20%.

Constants

Whenever you have values in a program that are not going to be changed, it is better if they are defined as constants, in Java that can be done by defining them as static final

  public static final Ticket AYLESBURY_TICKET = new Ticket("Aylesbury", 200);

This concept is not introduced until Chapter 5 in BlueJ, and you can use a variable instead. In the next Chapter 3 they use literals, 60 and 24. Although in this case they may be instantly recognised, constants like MINUTES_IN_HOUR and HOURS_IN_DAY make the program clearer and easier to read.

Dates

There is a Date data type in Java in the java.util package which can be used as shown below, but for simplicity you can simply use a String e.g. "15/Sep/2020" as this concepts is not covered in BlueJ.

    import java.util.Date;
    ...
    private Date today = new Date();

By creating a new date, the variable will contain today's date. In order to print out a date in the correct local date form you will need a SimpleDateFormat

    import java.text.SimpleDateFormat;
    ...
    private SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy");
    ...
    String date = formatter.format(currentTicket.getDate());

Enumerations

Whenever you have a piece of data that only has a limited range of values, an enumeration is the best way to go. So for example you could define Coin as where P10 has a value of 10, and P20 a value of 20. Without the values in parenthases the values are numbered 0,1,2,3 etc.

    public enum Coin
    {
        P10 (10),
        P20 (20),
        P100 (100),
        P200 (200);

        private final int value;

        private Coin(int value)
        {
            this.value = value;
        }

        public int getValue()
        {
            return value;
        }
     }

This concept is introduced in Chapter 6 of BlueJ

Testing

To be completed by the student

Evaluation

To be completed by the student