Kuliah minggu ke-4 PBO hari ini, kelas saya mendapatkan tugas untuk membuat ticket machine menggunakan BlueJ.
Berikut adalah source code nya:
Main:
TicketMachine:
Berikut adalah hasilnya
Berikut adalah source code nya:
Main:
/**
* Write a description of class Main here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner scan= new Scanner(System.in);
int cost,menu,balance;
menu=0;
System.out.println("Masukkan harga tiket \n");
cost=scan.nextInt();
TicketMachine ticket=new TicketMachine(cost);
while(menu!=5)
{
System.out.println("1. Get Price");
System.out.println("2. Get Balance");
System.out.println("3. Insert Money");
System.out.println("4. Print Ticket");
System.out.println("5. Exit");
menu=scan.nextInt();
switch(menu)
{
case 1:
cost=ticket.getPrice();
System.out.println(cost);
break;
case 2:
balance=ticket.getBalance();
System.out.println(balance);
break;
case 3:
int money=scan.nextInt();
ticket.insertMoney(money);
break;
case 4:
ticket.printTicket();
break;
case 5:
System.out.println("Thank you and Good Bye");
break;
}
}
}
}
TicketMachine:
/**
* Write a description of class TicketMachine here.
*
* @author (your name)
* @version (a version number or a date)
*/
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 printTicket()
{
System.out.println("##################");
System.out.println("# The BlueJ Line");
System.out.println("# Ticket");
System.out.println("# " + price + " cents.");
System.out.println("##################");
System.out.println();
total = total + balance;
balance = balance - price;
}
}
Berikut adalah hasilnya
Komentar
Posting Komentar