17 September 2018
TICKET MACHINE
Hari ini, kami mendapat tugas untuk membuat Ticket Machine.
Nama : Sherly Rosa Anggraeni
NRP : 05111740000018
Kelas : PBO-A
Nama : Sherly Rosa Anggraeni
NRP : 05111740000018
Kelas : PBO-A
1. Source Code
/*
* Nama : Sherly Rosa Anggraeni
* NRP : 05111740000018
* Kelas : PBO-A
*/
public class TicketMachine{
private int price;
private int balance;
private int total;
private int refund;
private int tickets;
public TicketMachine(int ticketCost){
price=ticketCost;
balance=0;
total=0;
refund=0;
tickets=0;
}
public int getPrice(){
return price;
}
public void insertMoney(int amount){
if(amount>0){
balance += amount;
}
else{
System.out.println("Masukkan Jumlah Uang");
}}
public void printTicket(){
System.out.println("Jumlah Uang = "+balance);
if(balance>0){
if(balance>price){
tickets=purchaseTickets();
int t;
for(t=1;t<=tickets;t++)
{
System.out.println("Tiket ="+t);
System.out.println("Total Harga="+price);
}
total+=balance;
refund=refundBalance();
if(refund==0){
System.out.println("Uang Pas");
}
else{
System.out.println("Kembalian="+refund);
}
balance=0;
}
else{
System.out.println("Tolong masukkan jumlah uang dengan benar"+(price-balance));
}
}}
public int refundBalance(){
int amountToRefund;
amountToRefund=balance-(price*tickets);
balance=0;
return amountToRefund;
}
public int purchaseTickets(){
int numberOfTickets;
numberOfTickets=balance/price;
return numberOfTickets;
}
}
2. Output
Terima Kasih
Tidak ada komentar:
Posting Komentar