Ticket Machine Simulator?! Ngoding pakai BlueJ lagi!

Pada kelas hari ini, saya belajar mengenali syntax yang ada dalam BlueJ. Untuk tugas kelas pada minggu ini, kelas saya diinstruksikan untuk memodifikasi source code yang ada dalam halaman web milik dosen saya. Berikut adalah hasil modifikasi saya yang terinspirasi oleh konser musik milik SMA saya dahulu.

Source Code

Ticket Machine

 /**  
  * A Classical Clarion (name of a music concert) online ticket simulator!  
  *  
  * @author Hazimi  
  * @version 1.0  
  */  
  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;   
  // The amount of tickets that can be bought.  
  private int tickets;  
  //The amount of money that can be refunded.  
  private int refund;  
  /**   
  * 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)   
  {   
  price = ticketCost;   
  balance = 0;   
  total = 0;  
  tickets = 0;  
  refund = 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;   
  }   
  /**   
  * 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("Please insert a sensible amount of money.");  
   }  
  }   
  /**   
  * Print a ticket.   
  * Update the total collected and   
  * reduce the balance to zero.   
  */   
  public void printTicket()   
  {   
   if(balance > 0)  
   {  
     if(balance >= price)  
     {  
       tickets = purchaseTickets();  
       int i = 1;  
       while(i <= tickets)  
       {  
         // Simulate the printing of a ticket.   
         System.out.println("##############################");   
         System.out.println("# Classical Clarion Tickets");   
         System.out.println("# Ticket #CC" + i);   
         System.out.println("# Price: Rp. " + price);  
         System.out.println("#");  
         System.out.println("# Open Space Grand City @3pm");  
         System.out.println("# 25 September 2018");  
         System.out.println("##############################");   
         System.out.println();  
         i++;  
       }  
       // Update the total collected with the balance.   
       total = total + balance;   
       refund = refundBalance();  
       if (refund == 0)  
       {  
         System.out.println("No change given.");  
       }  
       else  
       {  
         System.out.println("Amount to refund: Rp. " + refund);  
       }  
       balance = 0;  
     }  
     else  
     {  
       System.out.println("Please insert Rp. " + (price - balance) + " more.");  
     }  
   }  
   else  
   {  
     System.out.println("Please insert a positive amount of money.");  
   }  
  }  
  public int refundBalance()  
  {  
    int amountToRefund;  
    amountToRefund = balance - price * tickets;  
    balance = 0;  
    return amountToRefund;  
  }  
  public int purchaseTickets()  
  {  
    int numberOfTickets;  
    numberOfTickets = balance / price;  
    return numberOfTickets;  
  }  
 }  

Main (Execute Program)

 import java.util.Scanner;   
 public class IntMain   
 {   
  public static void main()   
  {   
    Scanner scan= new Scanner(System.in);   
    int cost,menu=0;   
    System.out.println("Input the ticket's price");   
    cost=scan.nextInt();   
    TicketMachine ticket=new TicketMachine(cost);  
    System.out.println("\n");  
    while(menu < 5)  
    {  
      System.out.println("==================================");  
      System.out.println("Classical Clarion Online Tickets Menu: ");  
      System.out.println();  
      System.out.println("1. Ticket Price");   
      System.out.println("2. Your Balance");   
      System.out.println("3. Insert Money");   
      System.out.println("4. Buy and Print Ticket (Refund Included)");   
      System.out.println("5. Exit");  
      System.out.println("==================================");  
      System.out.println("Selected Menu: ");  
      menu=scan.nextInt();   
      switch(menu)   
      {    
        case 1:   
        cost=ticket.getPrice();  
        System.out.println("Classical Clarion Ticket Price: ");  
        System.out.println("Rp. " + cost);   
        break;   
        case 2:   
        System.out.println("Your Balance: ");  
        System.out.println("Rp. " + ticket.getBalance());  
        break;   
        case 3:   
        System.out.println("Top up amount: ");  
        int money=scan.nextInt();  
        ticket.insertMoney(money);   
        break;   
        case 4:   
        ticket.printTicket();   
        System.out.println("You successfully bought the ticket(s)! We will be waiting for you at Classical Clarion!\n");  
        break;  
      }  
    }  
  }   
  }   

Output



Komentar

Postingan populer dari blog ini

Halaman Berita? HTML & CSS!

Tampilan Baru Pendaftaran Siswa dengan Bootstrap!

Rumah Sederhana dengan BlueJ!