Ticket Machine
Untuk projek yang akan saya lakukan sekarang adalah projek untuk membuat sebuah Ticket Machine dengan melakukan penggunaan java.util.scanner. Singkatnya scanner adalah fungsi untuk menginputkan sebuah nilai saat setelah program dijalankan.
Diagram kelas :
1. TicketMachineTest.java untuk menampilkan interface dari mesin tiket.
2. TicketMachine.java untuk mengimplementasikan program mesin tiket.
TicketMachineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class TicketMachineTest | |
{ | |
public static void main(String args[]) | |
{ | |
Scanner scan = new Scanner(System.in); | |
int cost, menu; | |
System.out.println("Masukkan harga tiket \n"); | |
cost = scan.nextInt(); | |
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: | |
ticket.printTicket(); | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TicketMachine | |
{ | |
private int price; | |
private int balance; | |
private int total; | |
public TicketMachine(int ticketCost) | |
{ | |
price = ticketCost; | |
balance = 0; | |
total = 0; | |
} | |
public int getPrice() | |
{ | |
return price; | |
} | |
public int getBalance() | |
{ | |
return balance; | |
} | |
public void insertMoney(int amount) | |
{ | |
balance = balance + amount; | |
} | |
public void printTicket() | |
{ | |
System.out.println("##################"); | |
System.out.println("# Movie"); | |
System.out.println("# Ticket"); | |
System.out.println("# " + price + " Rupiah."); | |
System.out.println("##################"); | |
System.out.println(); | |
total = total + balance; | |
balance = 0; | |
} | |
} |
Comments
Post a Comment