E-TOLL with JAVA
program ini mensimulasikan cara kerja mesin GTO pada gerbang tol
program akan mencek saldo kartu, bisa juga menambah saldo
jika saldo mencukupi maka gerbang tol akan terbuka
Account Class
Screen Class
Database Class
Tapscan Class
GTO Class
Main
output
program akan mencek saldo kartu, bisa juga menambah saldo
jika saldo mencukupi maka gerbang tol akan terbuka
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Account | |
{ | |
private int accountNumber; | |
private double availableBalance; | |
public Account(int theAccountNumber,double theAvailableBalance) | |
{ | |
accountNumber=theAccountNumber; | |
availableBalance=theAvailableBalance; | |
} | |
public double getAvailableBalance() | |
{ | |
return availableBalance; | |
} | |
public void getTotalBalance(double Amount) | |
{ | |
availableBalance -= Amount; | |
} | |
public void getAddBalance(double Amount) | |
{ | |
availableBalance += Amount; | |
} | |
public int getAccountNumber() | |
{ | |
return accountNumber; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
public class Screen | |
{ | |
public void displayMessage(String message) | |
{ | |
System.out.print(message); | |
} | |
public void displayMessageLine(String message) | |
{ | |
System.out.println(message); | |
} | |
public void displayRPAmount(double amount) | |
{ | |
System.out.printf("$%,.2f",amount); | |
} | |
public void displayCurrentTime() { | |
Calendar cal = Calendar.getInstance(); | |
SimpleDateFormat sdf = new SimpleDateFormat("EEE , dd/MM/yyyy HH:mm:ss"); | |
System.out.println( sdf.format(cal.getTime()) ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Database | |
{ | |
private Account[] accounts; // array of Accounts | |
// no-argument BankDatabase constructor initializes accounts | |
public Database(){ | |
accounts = new Account[2]; // just 2 accounts for testing | |
accounts[0] = new Account(12345, 12000.0); | |
accounts[1] = new Account(56789, 2000.0); | |
} // end no-argument BankDatabase constructor | |
// retrieve Account object containing specified account number | |
public Account getAccount(int accountNumber){ | |
// loop through accounts searching for matching account number | |
for(Account currentAccount : accounts){ | |
// return current account if match found | |
if(currentAccount.getAccountNumber() == accountNumber) return currentAccount; | |
} // end for | |
return null; // if no matching account was found, return null | |
} // end method | |
} // end class BankDatabase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class TapScan | |
{ | |
private Scanner input; | |
public TapScan() | |
{ | |
input=new Scanner(System.in); | |
} | |
public int getInput() | |
{ | |
return input.nextInt(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GTO | |
{ | |
private Account currentAccount; | |
private TapScan TapScan; | |
private Screen screen; | |
private double Fare; | |
private Database gtoDatabase; | |
private double TopUp; | |
public GTO() | |
{ | |
currentAccount = null; | |
TapScan = new TapScan(); | |
screen = new Screen(); | |
Fare = 10000; | |
gtoDatabase = new Database(); | |
} | |
public void run() { | |
while (true) | |
{ | |
displayGetAccount(); | |
if (currentAccount==null){ | |
screen.displayMessage("Account Invalid\n"); | |
continue; | |
} | |
screen.displayMessageLine(""); | |
displayAddBalance(); | |
if (currentAccount.getAvailableBalance() >= Fare) | |
{ | |
currentAccount.getTotalBalance(Fare); | |
printStruk(); | |
} else { | |
screen.displayMessageLine("Saldo anda tidak mencukupi.\n"); | |
screen.displayMessage("\nSaldo anda: \n"); | |
screen.displayMessage("\n"); | |
screen.displayRPAmount(currentAccount.getAvailableBalance()); | |
} | |
currentAccount = null; | |
} | |
} | |
public void displayGetAccount() | |
{ | |
screen.displayMessageLine("\n\nWelcome!"); | |
screen.displayMessage("\nToll Fare: "); | |
screen.displayRPAmount(Fare); | |
screen.displayMessageLine("\nInput Your Acc Number :"); | |
currentAccount = gtoDatabase.getAccount(TapScan.getInput()); | |
} | |
public void displayAddBalance() | |
{ | |
screen.displayMessage("\nSaldo anda: \n"); | |
screen.displayMessage("\n"); | |
screen.displayRPAmount(currentAccount.getAvailableBalance()); | |
screen.displayMessage("\nApakah anda ingin menambah saldo?"); | |
screen.displayMessage("\ntekan 0 untuk melewati"); | |
TopUp=TapScan.getInput(); | |
currentAccount.getAddBalance(TopUp); | |
} | |
public void printStruk() | |
{ | |
screen.displayMessageLine("\nPT. Semen Padang"); | |
screen.displayCurrentTime(); | |
screen.displayMessage("\nBiaya Tol: "); | |
screen.displayRPAmount(Fare); | |
screen.displayMessage("\nSisa saldo: "); | |
screen.displayRPAmount(currentAccount.getAvailableBalance()); | |
screen.displayMessageLine("\nTerima Kasih. Selamat menikmati perjalanan anda.\n"); | |
screen.displayMessage(" "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GTOMain | |
{ | |
// main method creates and runs the ATM | |
public static void main(String[] args){ | |
GTO gto = new GTO(); | |
gto.run(); | |
} // end main | |
} |

Komentar
Posting Komentar