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

Technical Support AI

Technical Support - Artificial Intelligence (*From now on, I'll try my best to explain about all of my Project using English) For this project, I made 3 classes to control the Bot Mechanism (Referencing to Objects-First-with-Java-A-Practical-Introduction-Using-BlueJ-David-J-Barnes pages 155) : InputReader Class (Used for detect or to take any user input) SupportSystem Class (As a main function connecting the user input and database) Responder Class (Handling AI-Output and Its role as 'mini' database) Current Version : 1.2 In the future, I'm expecting to have a bigger Responder's Database and would be separated from the Class itself, so it will be easier to make some modification inside the AI's Database and easier to manage. And then i'll try to improve the UX Design inside, so that the user can have a feeling about how 'Human' this AI. InputReader Class In this class, pretty much the same as how the name of the class is writt...

Fox And Rabbit

Implementasi Fox and Rabbit berupa simulasi hubungan antara predator (dalam hal ini Fox) dan prey (dalam hal ini Rabbit), digunakan 9 class : Simulator (sebagai class utama yang digunakan untuk melakukan simulasi) SimulatorView (sebagai pengatur tampilan yang diimplementasikan pada class Simulator) Field (sebagai class yang mengatur dan memanipulasi bidang simulasi yang telah ditentukan) FieldStats (sebagai class berupa value holder yang menyimpan kondisi dari Field) Counter (sebagai class penghitung seperti increment dan reset value) Location (sebagai class yang memiliki informasi posisi pada Field) Randomizer (sebagai class yang mengatur nilai random) Fox (sebagai class simulator yang bertindak sebagai predator) Rabbit (sebagai class simulator yang bertindak sebagai prey) Source Code Simulator import java.util.Random; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.awt.Color; import java.util.concurrent.TimeUnit; impo...