Ticket Machine
Ticket machine berikut merupakan sistem mesin tiket, yang mana dalam hal ini ialah mesin tiket handshake JKT48. Dalam kasus ini, tiket handshake JKT48 memiliki besaran harga yang berbeda-beda, tergantung dari timnya, yang terdiri dari Team J, Team K-III, Team T, dan Academy. Sehingga nantinya, tiket yang keluar sebagai output merupakan tiket dari salah satu tim yang dipilih.
Berikut merupakan diagram classnya.
1.TicketMachine.java
Source Code :
- /**
- * @author Sabrina Lydia S
- * @version 02/11/2020
- */
- public class TicketMachine
- {
- private int price;
- private int balance;
- private int total;
- public TicketMachine(int cost)
- {
- price = cost;
- 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 from a customer
- */
- public void insertMoney(int amount)
- {
- balance = balance + amount;
- }
- /**
- * Print a ticket
- * Update the total collected
- * reduce the balnce to zero.
- */
- public void printTicket1()
- {
- //Simulate the printing of a ticket
- System.out.println("###########################");
- System.out.println("# JKT48 Handshake Ticket #");
- System.out.println("# Team J #");
- System.out.println("# Rp200.000,- #");
- System.out.println("# Enjoy your handshake! #");
- System.out.println("###########################");
- // Update the total collected with the balance.
- total = total + balance;
- // Clear the balance.
- balance = 0;
- }
- public void printTicket2()
- {
- //Simulate the printing of a ticket
- System.out.println("###########################");
- System.out.println("# JKT48 Handshake Ticket #");
- System.out.println("# Team K-III #");
- System.out.println("# Rp200.000,- #");
- System.out.println("# Enjoy your handshake! #");
- System.out.println("###########################");
- // Update the total collected with the balance.
- total = total + balance;
- // Clear the balance.
- balance = 0;
- }
- public void printTicket3()
- {
- //Simulate the printing of a ticket
- System.out.println("###########################");
- System.out.println("# JKT48 Handshake Ticket #");
- System.out.println("# Team T #");
- System.out.println("# Rp180.000,- #");
- System.out.println("# Enjoy your handshake! #");
- System.out.println("###########################");
- // Update the total collected with the balance.
- total = total + balance;
- // Clear the balance.
- balance = 0;
- }
- public void printTicket4()
- {
- //Simulate the printing of a ticket
- System.out.println("###########################");
- System.out.println("# JKT48 Handshake Ticket #");
- System.out.println("# Academy #");
- System.out.println("# Rp150.000,- #");
- System.out.println("# Enjoy your handshake! #");
- System.out.println("###########################");
- // Update the total collected with the balance.
- total = total + balance;
- // Clear the balance.
- balance = 0;
- }
- }
2. TicketMenu.java
Source code:
- /**
- * @author Sabrina Lydia S
- * @version 02/11/20
- */
- import java.util.Scanner;
- public class TicketMenu
- {
- public static void main(String args[])
- {
- Scanner scan = new Scanner(System.in);
- int cost = 0;
- int team;
- int menu;
- System.out.println("JKT48 Handshake Ticket \n");
- System.out.println("1.Team J = Rp.200.000");
- System.out.println("2.Team K-III = Rp.200.000");
- System.out.println("3.Team T = Rp.180.000");
- System.out.println("4.Academy = Rp.150.000");
- System.out.println("Please enter the number of the team");
- team = scan.nextInt();
- switch(team)
- {
- case 1:
- cost = 200000;
- break;
- case 2:
- cost = 200000;
- break;
- case 3:
- cost = 180000;
- break;
- case 4:
- cost = 150000;
- break;
- }
- TicketMachine ticket = new TicketMachine(cost);
- while(true)
- {
- System.out.println("1. Get Price");
- System.out.println("2. Get Balance");
- System.out.println("3. Insert Money");
- System.out.println("4. Print Ticket");
- menu = scan.nextInt();
- switch(menu)
- {
- case 1:
- cost= ticket.getPrice();
- System.out.println(cost);
- break;
- case 2:
- System.out.println(ticket.getBalance());
- break;
- case 3:
- int money = scan.nextInt();
- ticket.insertMoney(money);
- break;
- case 4:
- switch(team)
- {
- case 1:
- ticket.printTicket1();
- break;
- case 2:
- ticket.printTicket2();
- break;
- case 3:
- ticket.printTicket3();
- break;
- case 4:
- ticket.printTicket4();
- break;
- }
- System.out.println("Thank You for Your Purchased!");
- break;
- }
- }
- }
- }
OUTPUT
Berikut merupakan hasil output dari program.
Komentar
Posting Komentar