TUGAS 3 PEMROGRAMAN BERORIENTASI OBJEK
MEMBUAT MESIN TIKET
Karina Soraya P
05111740000003
PBO - B
- Source Code Mesin Tiket
import java.util.Scanner;
/**
* Source Code TicketMachine.
*
* @author (Karina Soraya P)
* @version (7.1/17092018)
*/
public class TicketMachine
{
private int price;
private int balance;
private int total;
private int terbeli;
public TicketMachine(int cost)
{
price = cost;
balance = 0;
total = 0;
terbeli = 0;
}
/**
* @Return The price of a ticket.
*/
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("Use a positive amount rather than: " +
amount);
}
}
public void printTicket()
{
if(terbeli == 1) {
// Simulate the printing of a ticket.
System.out.println("====================");
System.out.println("= Early Ticket Sale");
System.out.println("= Reeva =");
System.out.println("====================");
System.out.println();
}
else {
System.out.println("Tiket Gagal Terbeli");
}
}
public void beliTicket()
{
if(balance >= price) {
System.out.println("Tiket Terbeli");
// Update the total collected with the price.
total = total + price;
// Reduce the balance by the prince.
balance = balance - price;
terbeli = 1;
}
else {
System.out.println("Tambahkan Saldo Sebesar : Rp. " +
(price - balance));
}
}
}
- Source Code Main
import java.util.Scanner;
/**
* Source Code Main
*
* @author (Karina Soraya P)
* @version (7.2/17092018)
*/
public class IntMain
{
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
int cost, menu;
boolean entry;
System.out.println("======Tiket Online Reeva======");
System.out.println("Masukkan harga tiket : ");
cost = scan.nextInt();
System.out.println("===============================");
TicketMachine ticket = new TicketMachine(cost);
entry = true;
while(entry) {
System.out.println("Daftar Menu :");
System.out.println("1. Harga Tiket");
System.out.println("2. Saldo Saat Ini");
System.out.println("3. Tambah Saldo");
System.out.println("4. Beli Tiket");
System.out.println("5. Cetak Tiket");
System.out.println("6. Exit");
System.out.println("Menu Yang Dipilih : ");
menu=scan.nextInt();
System.out.println("===============================");
switch(menu){
case 1:
cost=ticket.getPrice();
System.out.println("Harga Tiket : Rp. "+cost);
System.out.println("===============================");
break;
case 2:
System.out.println("Saldo Saat ini : Rp. "+ticket.getBalance());
System.out.println("===============================");
break;
case 3:
System.out.println("Masukkan Jumlah Uang : ");
int money=scan.nextInt();
ticket.insertMoney(money);
System.out.println("===============================");
break;
case 4:
ticket.beliTicket();
System.out.println("===============================");
break;
case 5:
ticket.printTicket();
System.out.println("===============================");
break;
case 6:
System.out.println("Terima Kasih Telah Menggunakan Layanan Kami");
System.out.println("===============================");
entry = false;
break;
}
}
}
}
- Hasil
Komentar
Posting Komentar