Skip to main content

Ticketing Machine

Pembuatan simulasi Ticketing Machine menggunakan OOP

Class yang digunakan di dalam Project terdiri atas :

  1. Main class
  2. TicketMachine class
  3. Destination class
Library yang digunakan di dalam Project adalah java.util.Scanner

Source code Destination class


/**
 * A class which describes Destination
 *
 * @author Satria Ade Veda Karuniawan
 * @version version 1.0
 */
public class Destination
{
    private String Station;
    private int PriceAdult;
    private int PriceChild;

    //Struct destination
 
    public Destination(String name)
    {
        Station = name;
        PriceAdult = 0;
        PriceChild = 0;

    }
 
    public void SetInfo(int pa, int pc)
    {
        PriceAdult = pa;
        PriceChild = pc;

    }
 
    public int GetPriceAdult()
    {
        return PriceAdult;
    }
 
    public int GetPriceChild()
    {
        return PriceChild;
    }
 
    public String GetName()
    {
        return Station;
    }
 

}


Source code TicketMachine class

import java.lang.String;
/**
 * Ticketing Mechanism
 *
 * @author Satria Ade Veda Karuniawan
 * @version version 1.0
 */
public class TicketMachine
{
    //Deklarasi Objek Destination
    Destination stationObj1 = new Destination("Tokyo Station");
    Destination stationObj2 = new Destination("Shibuya Station");
    Destination stationObj3 = new Destination("Shinjuku Station");
 
    private int MoneySpend;
    private int Change;
    private int Adult;
    private int Child;
 
    //Constructor set database
    public TicketMachine()
    {
        stationObj1.SetInfo(200,100);
        stationObj2.SetInfo(180,80);
        stationObj3.SetInfo(220,120);
    }
 
    //Input
    public void Input(int money, int adult, int child)
    {
        MoneySpend = money;
        Adult = adult;
        Child = child;
    }
 
    //Check keberhasilan transaksi
    public boolean BuyTicket(int choice)
    {
        boolean Success = false;
        if(Sufficient(choice, Adult, Child))
        {
            MoneySpend -= Change;
            Success = true;
        }
        return Success;
    }
 
    public int GetMoney()
    {
        return MoneySpend;
    }
 
    public int GetChange()
    {
        return Change;
    }
 
 
    //Cek cukup atau tidaknya uang
    public boolean Sufficient(int choice, int adult, int child)
    {
        boolean Cukup = false;
        switch(choice)
        {
            case 1:
            {
                if(MoneySpend >=(stationObj1.GetPriceAdult()*adult + stationObj1.GetPriceChild()*child))
                {
                    Change = MoneySpend - (stationObj1.GetPriceAdult()*adult + stationObj1.GetPriceChild()*child);
                 
                    Cukup = true;
                }
            }
            break;
            case 2:
            {
                if(MoneySpend >=(stationObj2.GetPriceAdult()*adult + stationObj2.GetPriceChild()*child))
                {
                    Change = MoneySpend - (stationObj2.GetPriceAdult()*adult + stationObj2.GetPriceChild()*child);
                    Cukup = true;
                }
            }
            break;
            case 3:
            {
                if(MoneySpend >=(stationObj3.GetPriceAdult()*adult + stationObj3.GetPriceChild()*child))
                {
                    Change = MoneySpend - (stationObj3.GetPriceAdult()*adult + stationObj3.GetPriceChild()*child);
                    Cukup = true;
                }
            }
            break;
        }
        return Cukup;
    }
 
 
}


Source code Main class

import java.util.Scanner;
/**
 * Main function
 *
 * @author Satria Ade Veda Karuniawan
 * @version version 1.0
 */
public class Main
{
    public static void main(String[] args)
    {
        int Money;
        int Adult;
        int Child;
        int Destination;
        String DestinationName = "";
        boolean Success;
     
        Scanner inputS = new Scanner(System.in);
     
        TicketMachine tmObj = new TicketMachine();
     
        //Tampilan menu ticket
        System.out.println("");
        System.out.println("Welcome to Ticket Machine");
        System.out.println("1. " + tmObj.stationObj1.GetName());
        System.out.println("Price");
        System.out.println("Adult : " + tmObj.stationObj1.GetPriceAdult());
        System.out.println("Child : " + tmObj.stationObj1.GetPriceChild());
        System.out.println("");
        System.out.println("2. " + tmObj.stationObj2.GetName());
        System.out.println("Price");
        System.out.println("Adult : " + tmObj.stationObj2.GetPriceAdult());
        System.out.println("Child : " + tmObj.stationObj2.GetPriceChild());
        System.out.println("");
        System.out.println("3. " + tmObj.stationObj3.GetName());
        System.out.println("Price");
        System.out.println("Adult : " + tmObj.stationObj3.GetPriceAdult());
        System.out.println("Child : " + tmObj.stationObj3.GetPriceChild());
        System.out.println("");
     
     
        //Tampilan input
        System.out.println("Please enter your destination (Choose 1 - 3)");
        Destination = inputS.nextInt();
     
        //Error handler
        while(Destination <= 0 || Destination >= 4)
        {
            System.out.println("Error Input, Please enter your destination again");
            Destination = inputS.nextInt();
        }
     
        switch(Destination)
        {
            case 1:
            {
                DestinationName = tmObj.stationObj1.GetName();
            }
            break;
         
            case 2:
            {
                DestinationName = tmObj.stationObj2.GetName();
            }
            break;
         
            case 3:
            {
                DestinationName = tmObj.stationObj3.GetName();
            }
        }
     
        System.out.println("Please input your money");
        Money = inputS.nextInt();
        System.out.println("Enter how many passengers");
        System.out.println("Adult :");
        Adult = inputS.nextInt();
        System.out.println("Child :");
        Child = inputS.nextInt();
     
        tmObj.Input(Money, Adult, Child);
        Success = tmObj.BuyTicket(Destination);
     
        if(Success)
        {
            //Transaksi berhasil
            System.out.println("-Transaction Succeded-");
            System.out.println("");
            System.out.println("=======================================");
            System.out.println("Departure Station : Akihabara Station");
            System.out.println("Destination : " + DestinationName);
            System.out.println("Money spended : " + tmObj.GetMoney());
            System.out.println("Money change : " + tmObj.GetChange());
            System.out.println("=======================================");
        }
        else
        {
            //Transaksi gagal
            System.out.println("-Transaction Failed-");
            System.out.println("Please try again");
        }
     
    }
}


Bagian workspace



Tampilan saat transaksi berhasil



Tampilan saat transaksi gagal



Comments

Popular posts from this blog

Clock

Pembuatan Clock menggunakan BlueJ Class yang digunakan sebagai berikut : Main Clock : digunakan sebagai penghubung antara NumberDisplay dan ClockMechanics NumberDisplay : digunakan sebagai format output number pada jam ClockMechanics : digunakan sebagai pengatur mekanisme jam Main Clock Source Code import java.util.concurrent.TimeUnit; /**  * MainClock class as connector NumberDisplay and ClockMechanics class  *  * @author Satria Ade Veda Karuniawan  * @version Version 1.0  */ public class MainClock {     NumberDisplay numberobj = new NumberDisplay();     ClockMechanics clockobj = new ClockMechanics();     int Marker = 0;     private String[] time_now = {"","","","","","",""};     private String[] converter = new String[7];     public MainClock()     {         while(true)         {            ...

Image Viewer GUI 4.0 (Final Exam Project)

Rancangan Interface (Workspace) Project lanjutan dari Image Viewer GUI 3.0 dengan implementasi menggunakan BlueJ, hal yang telah selesai diimplementasikan adalah : Frame Panel Button Label dll Dengan daftar class utama sebagai berikut : ImageViewer : merupakan class utama yang berfungsi untuk menampilkan dan menginisialisasi GUI (main class) OFImage : merupakan class yang mendefinisikan objek dari gambar ImagePanel : merupakan class yang salah satunya bertindak sebagai canvas gambar (berbeda dengan ImageViewer, ImagePanel hanya mengatur bagian panel foto saja) ImageFileManager : merupakan class yang bertindak sebagai file handler Dengan daftar inherited class ( Filter ) sebagai berikut : DarkerFilter : class Filter yang berfungsi untuk menggelapkan foto LighterFilter : class Filter yang berfungsi untuk menerangkan foto ThresholdFilter : class Filter yang berfungsi untuk merubah warna foto menjadi 3 tingkatan warna dari grayscale InvertFilter : class Fil...