PBO-3 Mengenal BlueJ
Laporan mata kuliah Pemrograman Berorientasi Objek (A) - Pertemuan 3
Laporan
Membuat kode untuk studi kasus Ticket Machine
Pada pertemuan 3 telah dijelaskan salah satu contoh penggunaan kasus Ticket Machine. Ticket Machine ini menjual suatu tiket dengan harga yang tetap. Selain itu, Ticket Machine dapat menerima uang dan dapat menyimpan balance di dalamnya.
public class TicketMachine {
// Harga tiket dari machine.
private int price;
// Balance yang terdapat dalam machine.
private int balance;
// Total balance dalam machine.
private int total;
}
Selain itu juga terdapat constructor method yang menerima argumen price, yang merupakan harga tiket pada mesin tersebut. Apabila argumen yang diberikan > 0, maka harga tiket ditetapkan. Apabila harga yang dimasukkan tidak valid, akan ada warning dan ditetapkan balance dan total menjadi 0.
// method untuk memperoleh price
public int getPrice() {
return price;
}
// method untuk memperoleh balance
public int getBalance() {
return balance;
}

Method untuk memasukkan uang ke machine.
public void insertMoney(int amount) {
if (amount > 0) {
balance = balance + amount;
} else {
System.out.println("Use a positive amount rather than: " + amount);
}
}
Method untuk mencetak tiket dari machine.
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 + price;
balance = balance - price;
} else {
System.out.println("You must insert at least: " + (price - balance) + " cents.");
}
}

Method untuk refund balance.
public int refundBalance() {
int amountToRefund = balance;
balance = 0;
return amountToRefund;
}

Full source code:
/**
* TicketMachine models a ticket machine that issues
* flat-fare tickets.
* The price of a ticket is specified via the constructor.
* Instances will check to ensure that a user only enters
* sensible amounts of money, and will only print a ticket
* if enough money has been input.
* @author David J. Barnes and Michael Kölling
* @version 2011.07.31
*/
public class TicketMachine {
// 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.
* @param cost The cost of a single ticket.
*/
public TicketMachine(int cost) {
price = cost;
balance = 0;
total = 0;
}
/**
* Return the price of a ticket.
* @return The price of a ticket for this machine.
*/
public int getPrice() {
return price;
}
/**
* Return the amount of money already inserted for the next ticket.
* @return The balance of money inserted.
*/
public int getBalance() {
return balance;
}
/**
* Receive an amount of money from a customer.
* Only positive amounts are accepted; others are ignored with a message.
* @param amount The amount of money to add to the balance.
*/
public void insertMoney(int amount) {
if (amount > 0) {
balance = balance + amount;
} else {
System.out.println("Use a positive amount rather than: " + amount);
}
}
/**
* Print a ticket if enough money has been inserted.
* If not enough, shows how much more is required.
*/
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 + price;
balance = balance - price;
} else {
System.out.println("You must insert at least: " + (price - balance) + " cents.");
}
}
/**
* Refund the balance and reset it to zero.
* @return The amount refunded.
*/
public int refundBalance() {
int amountToRefund = balance;
balance = 0;
return amountToRefund;
}
}
Done.
Last updated