Implementasi dari mesin tiket.
Kode :
Untuk Main.java :
import java.util.Scanner;
public class Main
{
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. Dapatkan harga");
System.out.println("2. Dapatkan uang");
System.out.println("3. Masukkan uang");
System.out.println("4. Print tiket");
System.out.println("5. Keluar");
menu=scan.nextInt();
switch(menu)
{
case 1:
cost=ticket.getPrice();
System.out.println("Harga untuk tiket ini adalah Rp. "+cost);
break;
case 2:
System.out.println(ticket.getBalance());
break;
case 3:
System.out.println("Masukkan balance baru Anda.");
int money=scan.nextInt();
ticket.insertMoney(money);
break;
case 4:
ticket.printTicket();
break;
case 5:
ticket.exit();
break;
}
}
}
}
Untuk TicketMachine.java
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 exit()
{
System.exit(0);
}
public void printTicket()
{
System.out.println("##################");
System.out.println("# Ticket");
System.out.println("# Harga : Rp. " + price + ".");
System.out.println("##################");
System.out.println();
total = total + balance;
balance = 0;
}
}
Kode :
Untuk Main.java :
import java.util.Scanner;
public class Main
{
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. Dapatkan harga");
System.out.println("2. Dapatkan uang");
System.out.println("3. Masukkan uang");
System.out.println("4. Print tiket");
System.out.println("5. Keluar");
menu=scan.nextInt();
switch(menu)
{
case 1:
cost=ticket.getPrice();
System.out.println("Harga untuk tiket ini adalah Rp. "+cost);
break;
case 2:
System.out.println(ticket.getBalance());
break;
case 3:
System.out.println("Masukkan balance baru Anda.");
int money=scan.nextInt();
ticket.insertMoney(money);
break;
case 4:
ticket.printTicket();
break;
case 5:
ticket.exit();
break;
}
}
}
}
Untuk TicketMachine.java
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 exit()
{
System.exit(0);
}
public void printTicket()
{
System.out.println("##################");
System.out.println("# Ticket");
System.out.println("# Harga : Rp. " + price + ".");
System.out.println("##################");
System.out.println();
total = total + balance;
balance = 0;
}
}
Tidak ada komentar:
Posting Komentar