Tugas 3 PBO

Ditugas ke-3 kali ini. Saya akan membuat sebuah program untuk melakukan pembelian ticket menggunakan java.
Program ini bertugas untuk melakukan pencetakan serta pencatatan transaksi saat pembelian ticket.

contoh source code program saya:

1. Main


 /**  
  * Write a description of class IntMain here.  
  * @Krisna Badru W (your name)  
  * @NRP : 05111740000048  
  * @Kelas : PBO-B  
  * @1 (a version number or a date)  
  */  
 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;  
     System.out.println("Masukkan harga tiket \n");  
     cost = scan.nextInt();  
     TicketMachine ticket = new TicketMachine(cost);  
     while(true){  
     System.out.println("1. Lihat harga");  
     System.out.println("2. Lihat saldo uang");  
     System.out.println("3. Masukkan Uang");  
     System.out.println("4. Beli dan Print Ticket");  
     System.out.println("5. Stop this program");  
     menu=scan.nextInt();  
     if(menu==5){  
       break;}  
     else{   
     switch(menu)  
     {  
       case 1:  
       cost=ticket.getPrice();  
       System.out.println(cost);  
       break;  
       case 2:  
       ticket.getBalance();  
       break;  
       case 3:  
       int money=scan.nextInt();  
       ticket.insertMoney(money);  
       break;  
       case 4:  
       ticket.printTicket();  
       break;  
     }  
   }  
   }  
   }  
   }  

2. TicketMachine


 /**  
  * Ticket Machine  
  *  
  * @Krisna Badru W (your name)  
  * @1 (a version number or a date)  
  */  
 public class TicketMachine  
 {  
   // instance variables - replace the example below with your own  
   private int price;  
   private int balance;  
   private int total;  
   private int kembalian;  
   public TicketMachine(int ticketCost)  
   {  
     price = ticketCost;  
     balance = 0;  
     total = 0;  
   }  
   public int getPrice()  
   {  
     // put your code here  
     return price;  
   }  
   public int getBalance()  
   {  
     System.out.println("#"+balance+"\n");  
     return balance;  
   }  
   public void insertMoney(int amount)  
   {  
     balance = balance + amount;  
   }  
   public void printTicket()  
   {  
     if(balance<price){  
       System.out.println("Maaf uang anda tidak mencukupi untuk membeli ticket\n");}  
     else{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;}  
   }  
 }  

Dan berikut merupakan output dari program yang saya buat:


Komentar

Postingan populer dari blog ini

Tugas 2 PBKK