NRP: 05111740000097
Kelas: PBO-A
Ini adalah tugas saya pada pertemuan minggu ke-4 dengan membuat Ticket Machine dengan menggunakan class Java/bahasa java.
Berikut formatted souce code saya:
A)Class Main
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner scan= new Scanner(System.in);
int cost,option;
cost = 50;
System.out.println("Masukkan harga tiket \n");
cost=scan.nextInt();
TicketMachine ticket=new TicketMachine(cost);
while (true)
{
System.out.println("Menu Option:");
System.out.println("1.Get Price");
System.out.println("2. Get Balance");
System.out.println("3. Insert Money");
System.out.println("4. Print Ticket");
option=scan.nextInt();
if(option==1){
cost=ticket.getPrice();
System.out.println(cost);
}
if(option==2){
ticket.getBalance();
}
if(option==3){
int money=scan.nextInt();
ticket.insertMoney(money);
}
if(option==4){
ticket.printTicket();
}
}
}
}
B) Class TicketMachine
/**
* Ticket Machine
* @Author Muhammad Naufal Refadi
* @version date 17-09-2018
*/
public class TicketMachine
{
//The price of a ticket ftr=rom this machin
private int price;
private int balance;
private int total;
private int refund;
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)
{
if(amount>0){
balance=balance+amount;
}
else{
System.out.println("Masukkan Uang dengan nilai yang benar");
}
}
public void printTicket()
{
if(balance >= price){
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=0;
}
else{
System.out.println("Uang anda belum cukup, silahkan masukkan minimal"+(price-balance)+ " more cents");
}
}
}
Dan ini adalah bagan classnya dan screenshot hasil compilenya
No comments:
Post a Comment