Edgar Andrew Hutauruk
05111740000166
PBO B
Sorce Code:
1.Main
2.Mesin Tiket
05111740000166
PBO B
Sorce Code:
1.Main
/**
* Write a description of class Tiket here.
*
* @author (Edgar Andrew Hutauruk)
* @version (17/09/2018)
*/
import java.util.Scanner;
public class Mymain
{
public static void main(){
Scanner scan= new Scanner(System.in);
int price,menu,balance;
System.out.println("Masukkan harga tiket : ");
price = scan.nextInt();
TicketMachine ticket = new TicketMachine(price);
balance= ticket.balance;
System.out.println("1. Get Price");
System.out.println("2. Get Balance");
System.out.println("3. Insert Money");
System.out.println("4. Print Ticket");
while(true)
{
menu=scan.nextInt();
if(menu==1)
{
ticket.getPrice();
}
else if(menu==2)
{
ticket.getBalance();
}
else if(menu==3)
{
System.out.println("Masukkan uang anda : ");
int money=scan.nextInt();
ticket.insertMoney(money);
}
else if(menu==4)
{
ticket.printTicket();
break;
}
}
}
}
2.Mesin Tiket
/**
* Write a description of class TicketMachine here.
*
* @author (Edgar Andrew Hutauruk)
* @version (17/09/2018)
*/
public class TicketMachine
{
//harga tiket
public int price;
//uang yang dimasukkan
public int balance;
//uang yang telah terkumpul
public int total;
public int sementara;
//masukkan harga tiket
public TicketMachine(int ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
sementara = 0;
}
//print harganaya
public int getPrice(){
System.out.println("Harga Tiket : Rp."+price);
return price;
}
//print jumalah uang
public int getBalance(){
System.out.println("Uang Anda : Rp."+balance);
sementara = price - balance;
if(sementara>=0)
{
System.out.println("Anda perlu menambahkan : Rp."+(price-balance));
return balance;
}
else
{
System.out.println("Anda memiliki kembalian : Rp."+(balance-price));
return balance;
}
}
//memasukkan uang
public void insertMoney(int amount){
if(amount > 0)
{
balance = balance + amount;
}
else
{
System.out.println("Telah menambahkan Rp."+amount);
}
}
//print tiker
public void printTicket(){
if(balance >= price){
System.out.println("####################");
System.out.println("# PT. Kereta Kuda");
System.out.println("# Disimpan dengan baik");
System.out.println("# Harga tiket : Rp."+price);
System.out.println("#Selamat bersenang-senang");
System.out.println("####################");
System.out.println();
total = total + balance;
balance = 0;
}
else{
System.out.println("####################");
System.out.println("Tiket tidak dapat dicetak");
System.out.println("Anda harus memasukkan uang sebesar : Rp."+(price-balance));
System.out.println("####################");
}
}
}
Komentar
Posting Komentar