Pembuatan simulasi Ticketing Machine menggunakan OOP
Class yang digunakan di dalam Project terdiri atas :
/**
* 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
Source code Main class
Bagian workspace
Tampilan saat transaksi berhasil
Tampilan saat transaksi gagal
Class yang digunakan di dalam Project terdiri atas :
- Main class
- TicketMachine class
- 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;
}
}
/**
* 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");
}
}
}
/**
* 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
Post a Comment