Created
October 27, 2017 04:09
-
-
Save Rahandi/38a6e5046229e7e126d6e4fafb00351f to your computer and use it in GitHub Desktop.
E-Toll
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 pin; | |
private double totalBalance; | |
public Account(int thePin, double thetotalBalance){ | |
pin = thePin; | |
totalBalance = thetotalBalance; | |
} | |
public boolean validatePIN(int userPIN){ | |
if(userPIN == pin){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} | |
public double getTotalBalance(){ | |
return totalBalance; | |
} | |
public void debit(double amount){ | |
totalBalance -= amount; | |
} | |
public int getAccountNumber(){ | |
return pin; | |
} | |
} |
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 Cost{ | |
private final static double amount = 1500.0; | |
private EtollDatabase etollDatabase; | |
private Screen screen; | |
private Gate gate; | |
private double saldo; | |
private int accountnumb; | |
private Receipt struk; | |
public Cost(int userAccountNumber, EtollDatabase data, Screen scren){ | |
accountnumb = userAccountNumber; | |
etollDatabase = data; | |
screen = scren; | |
saldo = etollDatabase.getTotalBalance(accountnumb); | |
gate = new Gate(screen); | |
struk = new Receipt(screen, etollDatabase, accountnumb); | |
} | |
public void execute(){ | |
if(saldo >= amount){ | |
etollDatabase.debit(accountnumb, amount); | |
gate.OPEN(); | |
struk.cetak(); | |
} | |
else{ | |
screen.displayMessageLine("Saldo tidak cukup"); | |
} | |
return; | |
} | |
} |
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 Etoll{ | |
private boolean userAuthed; | |
private int currentAccountnumb; | |
private Screen screen; | |
private Keypad keypad; | |
private EtollDatabase etollDatabase; | |
private Cost cost; | |
public Etoll(){ | |
userAuthed = false; | |
currentAccountnumb = 0; | |
screen = new Screen(); | |
keypad = new Keypad(); | |
etollDatabase = new EtollDatabase(); | |
} | |
public void run(){ | |
while(true){ | |
while(!userAuthed){ | |
screen.displayMessageLine("\nSelamat Datang"); | |
authenticateUser(); | |
} | |
Transaksi(); | |
userAuthed = false; | |
currentAccountnumb = 0; | |
screen.displayMessageLine("\nSampai jumpa lagi"); | |
return; | |
} | |
} | |
private void authenticateUser(){ | |
screen.displayMessageLine("Masukkan nomor serial"); | |
int pin = keypad.getInput(); | |
userAuthed = etollDatabase.authenticateUser(pin); | |
if(userAuthed){ | |
currentAccountnumb = pin; | |
} | |
else{ | |
screen.displayMessageLine("Serial salah"); | |
screen.displayMessageLine("Coba lagi"); | |
} | |
} | |
private void Transaksi(){ | |
cost = new Cost(currentAccountnumb, etollDatabase, screen); | |
cost.execute(); | |
} | |
} |
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 EtollDatabase{ | |
private Account[] accounts; | |
public EtollDatabase(){ | |
accounts = new Account[2]; | |
accounts[0] = new Account(54321, 100.0); | |
accounts[1] = new Account(12345, 120000.0); | |
} | |
private Account getAccount(int accountNumber){ | |
for(Account currentAccount : accounts){ | |
if(currentAccount.getAccountNumber() == accountNumber){ | |
return currentAccount; | |
} | |
} | |
return null; | |
} | |
public boolean authenticateUser(int userPIN){ | |
Account userAccount = getAccount(userPIN); | |
if(userAccount != null){ | |
return userAccount.validatePIN(userPIN); | |
} | |
else{ | |
return false; | |
} | |
} | |
public double getTotalBalance(int userAccountNumber){ | |
return getAccount(userAccountNumber).getTotalBalance(); | |
} | |
public void debit(int userAccountNumber, double amount){ | |
getAccount(userAccountNumber).debit(amount); | |
} | |
} |
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 Gate{ | |
private Screen GateScreen; | |
public Gate(Screen scren){ | |
GateScreen = scren; | |
} | |
public void OPEN(){ | |
GateScreen.displayMessageLine("GATE OPEN"); | |
} | |
} |
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 Keypad{ | |
private Scanner input; | |
public Keypad(){ | |
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
import java.util.Date; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
public class Receipt{ | |
private Screen screen; | |
private EtollDatabase etollDatabase; | |
private int akun; | |
public Receipt(Screen scren, EtollDatabase data, int accountNumb){ | |
screen = scren; | |
etollDatabase = data; | |
akun = accountNumb; | |
} | |
public void cetak(){ | |
double saldo = etollDatabase.getTotalBalance(akun); | |
Date date = new Date(); | |
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy"); | |
screen.displayMessageLine("\nRECEIPT"); | |
screen.displayMessageLine(dateFormat.format(date)); | |
screen.displayMessageLine("JASA MARGA"); | |
screen.displayMessageLine("Biaya toll: Rp.1500"); | |
screen.displayMessage("Sisa saldo: Rp."); | |
screen.dislayRupiahAmount(saldo); | |
} | |
} |
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 Screen{ | |
public void displayMessage(String message){ | |
System.out.print(message); | |
} | |
public void displayMessageLine(String message){ | |
System.out.println(message); | |
} | |
public void dislayRupiahAmount(double amount){ | |
System.out.printf("Rp%,.2f", amount); | |
} | |
} |
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 Test{ | |
public static void main(String[] args){ | |
Etoll Etollnya = new Etoll(); | |
Etollnya.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment