Tugas 4 PBO B Ticket Machine

Faisal Reza Maulana 05111940000009

Berikut adalah source code dari program sederhana Ticket Machine Roket Falcon Heavy dengan tujuan Planet Mars.

  1. TicketMachine.java
/**
 * Class Ticket Machine
 *
 * @author Faisal 051119-009
 */
public class TicketMachine {

    //price of ticket from this machine
    private final int price;

    //amount of money entered by a customer so far
    private int balance;

    //total amount of money collected by this machine
    private int total;

    //constructor for object of class TicketMachine
    public TicketMachine(int ticketCost){
        price = ticketCost;
        balance = 0;
        total = 0;
    }

    //return the price of a ticket
    public int getPrice(){
        return price;
    }

    //return the amount of money already inserted for the next ticket
    public int getBalance() {
        return balance;
    }

    //receive an amount of money in cents from a customer
    public void insertMoney(int amount){
        balance = balance + amount;
    }

    //print a ticket
    //update the total collected
    //reduce the balance to zero

    public void printTicket(){
        if(balance<price){
            System.out.println("Not enough money");
        }else {
            //simulate the printing of a ticket
            System.out.println("################################");
            System.out.println("One way ticket to Mars by SpaceX");
            System.out.println("################################");
            System.out.println();

            //update the total collected with the balance
            total = total + balance;

            //clear the balance
            balance = balance - price;
        }
    }
}

2. IntMain.java

import java.util.Scanner;

/**
 * Class Main
 *
 * @author Faisal 051119-009
 */
public class IntMain {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int cost, menu;
        System.out.println("Welcome to Falcon Heavy ticket machine!");
        System.out.println("Here you can buy one way ticket to Mars");
        System.out.println("Don't worry you can order another ticket to Earth later\n");
        cost = 100000;
        System.out.println("The ticket cost is IDR 100000");
        TicketMachine ticket = new TicketMachine(cost);
        boolean flag = true;
        while(flag){
            System.out.println("1. Get Price");
            System.out.println("2. Get Balance");
            System.out.println("3. Insert Money");
            System.out.println("4. Print Ticket");
            System.out.println("0. Exit");
            System.out.println();
            menu = scan.nextInt();
            switch (menu) {
                case 1:
                    cost = ticket.getPrice();
                    System.out.println("The ticket cost is IDR " + cost);
                    break;
                case 2:
                    System.out.println("Your current balance is IDR " + ticket.getBalance());
                    break;
                case 3:
                    int money = scan.nextInt();
                    ticket.insertMoney(money);
                    break;
                case 4:
                    ticket.printTicket();
                    break;
                case 0:
                    flag = false;
                    System.out.println("Thank you for choosing SpaceX");
                    break;
            }
        }
    }
}

3. Class Diagram

4. Output

Leave a comment

Design a site like this with WordPress.com
Get started