17 September 2018

Tugas 3 PBO B

Pada saat ini, saya akan membuat simulasi mesin tiket kereta sederhana yang terinspirasi dari banyaknya mesin tiket kereta di negara Jepang. Seperti biasa, saya akan menggunakan bahasa Java dalam IDE BlueJ untuk membuatnya.

Hanya ada 2 class yang diperlukan, yaitu class Main dan class TicketMachine.

Berikut ini adalah source codenya.

1. Main

 import java.util.Scanner;  
 /**  
  * Fungsi main.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180917)  
  */  
 public class IntMain   
 {   
   public static void main(String args[])   
   {   
     Scanner scan= new Scanner(System.in);   
     int cost,menu;   
     System.out.println("SET THE TICKET PRICE \n");   
     cost=scan.nextInt();   
     TicketMachine ticket=new TicketMachine(cost);  
       
     menu=0;  
     while(menu!=5)  
     {  
       System.out.println("");  
       System.out.println("Select Action:");  
       System.out.println("1. CHECK PRICE");   
       System.out.println("2. CHECK YOUR 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:   
         int 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 for using our services. Good bye.");  
         break;  
       }   
     }  
   }   
 }

2. TicketMachine
   
 /**  
  * Mengimplementasikan cara kerja mesin tiket.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180917)  
  */  
 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.   
    * Note that the price must be greater than zero, and there are no checks to ensure this.   
    */   
    public TicketMachine(int ticketCost)   
    {   
      if(ticketCost > 0)  
      {  
        price = ticketCost;   
      }  
      else  
      {  
        price = 100;  
        System.out.println("Price set to " + price + " as default price.");  
      }    
      balance = 0;   
      total = 0;   
    }   
    /**   
    * Return the price of a ticket.   
    */   
    public int getPrice()   
    {   
      return price;   
    }   
    /**   
    * Return the amount of money already inserted for the next ticket.   
    */   
    public int getBalance()   
    {   
      return balance;   
    }   
    /**  
    * Set balance manually.  
    */  
    public void setBalance(int amount)  
    {  
      if (amount > 0)   
      {  
        balance = amount;  
      }  
    }     
    /**   
    * Receive an amount of money in cents from a customer.   
    */   
    public void insertMoney(int amount)   
    {   
      if(amount > 0)  
      {  
        balance = balance + amount;   
      }  
      else  
      {  
        System.out.println("Use a positive amount. You cannot insert " + amount + " cents.");  
      }  
    }   
    /**   
    * Print a ticket.   
    * Update the total collected and reduce the balance to zero.   
    */   
    public void printTicket()   
    {   
      if(balance >= price)   
      {  
        // Simulate the printing of a ticket.   
        System.out.println("##################");   
        System.out.println("# The BlueJ Line #");   
        System.out.println("#   Ticket   #");   
        System.out.println("#  " + price + " cents.  #");   
        System.out.println("##################");   
        System.out.println();   
          
        // Update the total collected with the price.   
        total = total + price;   
        // Reduce the balance by the price.   
        balance = balance - price;   
      }  
      else {  
       System.out.println("You must insert at least: " + (price - balance) + " more cents.");  
      }  
    }   
 }   

Setelah di-compile, jalankan fungsi main pada class Main. Maka akan muncul simulasi pemesanan tiket pada terminal BlueJ.

Lebih jelasnya bisa disaksikan melalui video berikut.


Atau bisa dilihat dari screenshot di bawah ini.





Demikian tugas ketiga saya pada mata kuliah Pemrograman Berbasis Objek. Terima kasih atas kunjungannya.



Jangan lupa, semangat ketik 10 jari. :)

Tidak ada komentar:

Posting Komentar

Silahkan isi kolom komentar di bawah ini