TUGAS TICKET MACHINE

1. Class window


2. Sample output 1

2. Sample output 2

3. ticket_machine Class
/**
 * Ticket Machine Program
 *
 * @author (ARINO JENYNOF)
 * @version (20180917)
 */
public class ticket_machine
{
    private int price;
    private int balance;
    private int total;
    
    /**
     * Constructor, set ticket_machine initial state
     */
    public ticket_machine(int cost)
    {
        price = cost;
        balance = 0;
        total = 0;
    }
    
    public int get_price()
    {
        return price;
    }
    
    public int get_balance()
    {
        return balance;
    }
    
    public void set_balance(int amount)
    {
        balance += amount;
    }
    
    public void print_ticket()
    {
        if (balance < price)
        {
            System.out.println("You need to pay " + (price-balance) + " more!");
        }
        else
        {
            System.out.println("=====================");
            System.out.println("    BLUEJ TICKET");
            System.out.println("#PRICE  : " + price);
            System.out.println("#CHANGE : " + (balance-price));
            System.out.println("=====================");
            total += balance;
            balance = 0;
        }
    }
}


4. main Class
/**
 * main Class
 *
 * @author (ARINO JENYNOF)
 * @version (20180917)
 */
import java.util.Scanner;
import java.util.Random;
public class main
{
    public static void main(String[] args)
    {
        Scanner inp = new Scanner(System.in);
        Random rand = new Random();
        
        //initialise ticket price with random number
        ticket_machine tm = new ticket_machine(rand.nextInt(100000));
        
        System.out.println("1. Get Ticket Price");
        System.out.println("2. Check Balance");
        System.out.println("3. Pay Ticket");
        System.out.println("4. Print Ticket");
        System.out.println("0. Exit");
        System.out.println("Select options");
        
        int menu = inp.nextInt();
        while (menu != 0)
        {
            switch (menu)
            {
                case 1:
                    System.out.println("Ticket price is " + tm.get_price());
                    break;
                case 2:
                    System.out.println("Your balance is " + tm.get_balance());
                    break;
                case 3:
                    System.out.println("Insert money");
                    int inserted = inp.nextInt();
                    tm.set_balance(inserted);
                    System.out.println("Your inserted " + inserted);
                    break;
                case 4:
                    tm.print_ticket();
                    break;
                default:
                    System.out.println("Wrong options!");
            }
            System.out.println("Select options");
            menu = inp.nextInt();
        }
    }
}

Comments