Langsung ke konten utama

Tugas 3 PBO A

Hey guys! This time I have been working on making my own Ticket Machine using BlueJ.
I am using 2 classes, which is Main and TicketMachine.
  1. TicketMachine

     /*  
      * This class contains the declaration of the Ticket Machine we are going to use..  
      *  
      * @author (Elkana Hans Widersen)  
      * @version (17/9/2018)  
      */  
     import java.util.Scanner;  
       
     public class TicketMachine{   
       Scanner scan = new Scanner(System.in);  
       // 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 amount of tickets about to be printed.  
       private int ticket;  
       // 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){   
         price = ticketCost;   
         balance = 0;   
         total = 0;  
         ticket = 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 int insertMoney(int amount){  
         balance = balance + amount;  
         return balance;  
       }   
       /*   
        * Print a ticket.   
        * Update the total collected and   
        * reduce the balance to zero.   
        */   
       public int printTicket(int ticket){   
         // Simulate the printing of a ticket.  
         for(int i = 0; i < ticket; i++){  
           System.out.println("======================");   
           System.out.println("The BlueJ Line Ticket");    
           System.out.println("Price: Rp " + price + ",00");  
           System.out.println("======================\n");  
         }  
         // Update the total collected with the balance.   
         total = total + balance;   
         // Clear the balance and the ticket.   
         balance = balance - (ticket * price);  
         return balance;  
       }   
     }  
    
  2. Main

       
     /*  
      * This is where the main operation works.  
      *  
      * @author (Elkana Hans Widersen)  
      * @version (17/9/2018)  
      */  
     import java.util.Scanner;   
     public class main{   
       public static void main(String args[]){   
         Scanner scan = new Scanner(System.in);   
         int cost, menu, balance = 0;  
         int amount = 0;  
         boolean printed = false;  
         System.out.print("Input the ticket price: Rp ");   
         cost = scan.nextInt();  
         TicketMachine ticket = new TicketMachine(cost);  
         System.out.println();  
         while(true){   
           System.out.println("1. Get Price");  
           System.out.println("2. Get Balance");   
           System.out.println("3. Insert Money");   
           System.out.println("4. Print Ticket");  
           System.out.println("5. Exit\n");  
           System.out.print("Your input: ");  
           menu = scan.nextInt();   
           switch(menu){   
             case 1:   
             cost=ticket.getPrice();   
             System.out.println("Price: Rp " + cost + ",00\n");   
             break;   
             case 2:   
             balance = ticket.getBalance();  
             System.out.println("Balance: Rp " + balance + ",00\n");  
             break;   
             case 3:   
             int money = scan.nextInt();   
             balance = ticket.insertMoney(money);   
             break;   
             case 4:  
             System.out.print("Total tickets: ");  
             amount = scan.nextInt();  
             if(balance >= (amount * cost)){  
               balance = ticket.printTicket(amount);  
               printed = true;  
             }  
             else{  
               System.out.println("Balance insufficient. Please insert enough balance to proceed.\n");  
             }  
             break;  
             case 5: break;  
           }  
           if(menu == 5) break;  
         }  
         System.out.println("\nThank you for using BlueJ Line!\n");  
         System.out.println("Your balance: Rp " + balance + ",00");  
       }   
    }
    
    Using those codes above, it will look like this:


Thank you and see you next time!

Komentar

Postingan populer dari blog ini

Tugas PBO Remote AC

Hey guys! In this post I'm going to show you how to make an AC Remote using BlueJ. I'm going to use 2 classes, which are: Main ACRemote Main /* * Main function for the AC Remote * * Elkana Hans Widersen * 05111740000127 * 23 September 2018 */ import java.util.Scanner; public class Main{ public static void main(String args[]){ int menu; Scanner input = new Scanner(System.in); //Creates the remote ACRemote remote = new ACRemote(); while(true){ //displays the remote interface every loop remote.display(); //scans for menu number menu = input.nextInt(); switch(menu){ //changes the temperature case 1: remote.TempUp(); break; case 2: remote.TempDown(); break; //sets the AC swing mode case 3: remote.getSwing(); break; //terminates when the AC turned off ...

UAS GUI 3.1

Nama: Elkana Hans Widersen NRP: 05111740000127 Kelas: PBO-A 1. Inilah rancangan interface dari image editor yang saya buat: 2. Berikut diagram dari kelas-kelas penyusun image editor saya beserta relasinya: Image Viewer: Kelas utama yang mengatur fitur-fitur utama dari image editor. Image Panel: Kelas swing untuk menampilkan gambar dalam format OF. Image File Manager: Mengatur fungsi load dan save. OF Image: Mendefinisikan gambar dalam format OF. Filter: Abstraksi dari filter-filter yang tersedia. Darker Filter: Berisi settingan filter Darker. Lighter Filter: Berisi settingan filter Lighter . Threshold Filter:  Berisi settingan filter Threshold. Fish-eye Filter:  Berisi settingan filter Fish-eye. 3. Berikut implementasi dari kelas-kelas di atas: Image Viewer import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.swing.border.*; import java.io.File; import java.util...