Tugas ketiga hari ini saya membuat ticket machine dengan 2 class yaitu:
- Ticket_Machine
- IntMain
Dibawah ini merupakan source codenya:
- Ticket_Machine
/**
* ini merupakan class ticket machinenya
*
* @author (Gede)
* @version (17-09-2018)
*/
public class Ticket_Machine
{
// instance variables - replace the example below with your own
private int price;
private int balance;
private int total;
private int kembali;
private int orang;
private int count;
/**
* Constructor for objects of class Ticket_Machine
*/
public Ticket_Machine(int ticketcost)
{
// initialise instance variables
price=ticketcost;
balance=0;
total=0;
kembali=0;
orang=0;
count=0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int getPrice()
{
return price;
}
public int getBalance()
{
return balance;
}
public void insertJumlahTiket(int jmlh)
{
orang=jmlh;
}
public void insertMoney(int amount)
{
balance=balance+amount;
}
public void printTicket()
{
if(balance>=price*orang)
{
int orang2=orang
while(orang2>0)
{
System.out.println("##################");
System.out.println("# The BlueJ Line");
System.out.println("# Ticket");
System.out.println("# "+ price+ " Rupiah.");
System.out.println();
orang2--;
count=1;
}
kembali=balance-price*orang;
if(kembali>0)
{
System.out.println("Uang Kembalian: Rp "+kembali );
}
// Update the total collected with the balance.
total = total + balance-kembali;
// Clear the balance.
balance = 0;
}
else
{
System.out.println("Uang tidak cukup");
}
}
public int looping()
{
return count;
}
}
- IntMain
/**
* ini merupakan class mainnya
*
* @author (Gede)
* @version (17-09-2018)
*/
import java.util.Scanner;
public class IntMain
{
// instance variables - replace the example below with your own
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
int cost, menu, balance, money, kembali, jumlah, count1;
System.out.println("Masukkan harga tiket ");
cost=scan.nextInt();
Ticket_Machine ticket=new Ticket_Machine(cost);
System.out.println("Masukkan jumlah tiket ");
jumlah=scan.nextInt();
ticket.insertJumlahTiket(jumlah);
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();
switch(menu)
{
case 1:
cost=ticket.getPrice();
System.out.println("Harga Tiket: Rp "+cost);
break;
case 2:
balance=ticket.getBalance();
System.out.println("Uang yang ditampung: Rp "+balance);
break;
case 3:
System.out.println("Masukkan jumlah uang");
money=scan.nextInt();
ticket.insertMoney(money);
break;
case 4:
ticket.printTicket();
break;
}
count1=ticket.looping();
if(count1==1)
{
break;
}
}
}
}
Dan di bawah ini merupakan hasil dari kode diatas
Tidak ada komentar:
Posting Komentar