Disini saya membuat dua class yaitu :
1. IntMain
2. TicketMachine
Berikut adalah sourcecodenya:
/**
* IntMain sebagai fungsi utama
*
* @author (Lutfiyanti)
* @version (17-09-2018)
*/
import java.util.Scanner;
public class IntMain
{
public static boolean isPrinted;
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();
TicketMachines ticket=new TicketMachines(cost);
System.out.println("1. Cek harga");
System.out.println("2. Cek Saldo");
System.out.println("3. Masukkan uang");
System.out.println("4. Cetak Tiket");
System.out.println("5. Keluar Menu");
while (isPrinted!=true){
menu = scan.nextInt();
switch(menu)
{
case 1:
cost = ticket.getPrice();
System.out.println(cost);
break;
case 2:
ticket.getBalance();
break;
case 3:
int money = scan.nextInt();
ticket.insertMoney(money);
break;
case 4 :
ticket.printTicket();
break;
case 5 :
isPrinted=true;
break;
}
}
}
}
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikYulFNUdA3-YJddXEtvzeM-gEvY87bLlhMSkND245UMcg3jJEB5CWiefXC1USUZPlzMvXABQH37EZaXPngk_ZjRbOzTF7OJvUPquZpiWzvFKhkDPvdD7rcQrtxBauk7AzvamAufhPgXLK/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> /**
* Ticket Machines
*
* @author (Lutfiyanti)
* @version (17-09-2018)
*/
public class TicketMachines
{
// The price of a ticket from this machine.
private int price;
// The amount of money entered by a customer so far.
private int balance;
// The total amount of money collected by this machine.
private int total;
/**
* create a machine that issues tickets of the given price.
* Note that the price must be greater than zero, and
* there are no check to ensure this.
*/
public TicketMachines(int ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
}
/**
* *Return the price of a ticket.
*/
public int getPrice()
{
System.out.println("Harga Tiket Anda = "+price);
return price;
}
/**
* Return the amount of money already inserted for the
* next ticket
*/
public int getBalance()
{
System.out.println("Saldo anda = "+balance);
return balance;
}
/**
* Receave an amount of money in cents from a customer.
*/
public void insertMoney(int amount)
{ if (amount>=price){
balance = balance +amount;
System.out.println("Sisa uang anda "+(balance-price)+" cents");
}
else {
System.out.println("Masukkan jumlah uang sama dengan atau melebihi " +(price)+" cents");
}
}
/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
public void printTicket()
{
if(balance>price)
{
//simulate the printing of ticket.
System.out.println("#############");
System.out.println("# The BlueJ Line");
System.out.println("#Ticket");
System.out.println("# "+ price + " cents.");
System.out.println("#############");
System.out.println();
}
else
{
System.out.println("Anda harus memiliki saldo setidaknya setidaknya : "+(price)+" cents");
}
}
}
</code></pre>
Berikut adalah outputnya dengan dua case yaitu uang yang dimasukkan kurang dan berlebih :
Nama : Lutfiyanti
NRP : 05111740000036
Kelas : PBO-B
Tidak ada komentar:
Posting Komentar