Untuk mengakses ATM digunakan kartu plastik ATM yang diterbitkan oleh lembaga keuangan (bank) yang disebut dengan Kartu ATM. Kartu ATM yang dikeluarkan oleh pihak bank biasanya sudah menetapkan batas jumlah penarikan atau transasksi tunai maksimum perhari. Batas penarikan ATM ditetapkan untuk mengantisipasi kemungkinan adanya kerusakan pada perangkat ATM, selain itu batas jumlah penarikan diterapkan untuk mengantisipasi kelebihan penyediaan uang tunai dalam ATM.
Secara umum fungsi ATM adalah agar dapat melakukan penarikan uang tunai, namun selain itu masih banyak fungsi ATM yang dapat mempermudah kepentingan kita sebagai nasabah dalam melakukan aktivitas perbankan, seperti:
- Informasi Saldo
- Pembayaran Umum: tagihan telepon, kartu kredit, listrik, air, handphone, dan uang kuliah
- Pembelian: tiket penerbangan, isi ulang pulsa
- Pemindah bukuan (open transfer)
- Pengubahan PIN, dll
Saya akan membagikan Source Code dari buku "Java - How To Program - Ninth Edition (2012)" Bab 13 tentang OOP pada Mesin ATM
berikut table class dan relasinya
berikut adalah kelas - kelasnya
- class study_case atau Main
public class Study_Case
{
public static void main( String[] args )
{
ATM theATM = new ATM();
theATM.run();
}
}
- class ATM
public class ATM
{
private boolean userAuthenticated; // untuk cek ke otekntikan User
private int currentAccountNumber; //cek user sekarang
private Screen screen;
private Keypad keypad;
private CashDispenser cashDispenser;
private DepositSlot depositSlot;
private BankDatabase bankDatabase; // untuk informasi database akun
// menu
private static final int saldo = 1;
private static final int tarik_tunai = 2;
private static final int setoran = 3;
private static final int exit = 4;
public ATM()
{
userAuthenticated = false; // inisiasi
currentAccountNumber = 0; //inisiasi
screen = new Screen();
keypad = new Keypad();
cashDispenser = new CashDispenser();
depositSlot = new DepositSlot();
bankDatabase = new BankDatabase();
}
public void run()
{
while(true)
{
while(!userAuthenticated)
{
screen.displayMessageLine("WELCOME");
authenticateUser(); // mengotentikasi user
}
performTransactions();
userAuthenticated = false; // reset
currentAccountNumber = 0; // reset
screen.displayMessageLine("\nTHANK YOU");
}
}
private void authenticateUser()
{
screen.displayMessageLine("\nMASUKKAN ACCOUNT NUMBER: ");
int accountNumber = keypad.getInput(); // account
screen.displayMessageLine("MASUKKAN PIN: ");
int pin = keypad.getInput(); // pin
userAuthenticated = bankDatabase.authenticateUser (accountNumber, pin);
if(userAuthenticated)
{
currentAccountNumber = accountNumber; //save
}
else
screen.displayMessageLine("\nAKUN ATAU PIN SALAH, COBA LAGI");
}
private void performTransactions()
{
//untuk menyimpan transaksi sekarang
Transaction currentTransaction = null;
boolean userExited = false; // inisiasi
while(!userExited)
{
int mainMenuSelection = displayMainMenu();
switch(mainMenuSelection)
{
case saldo:
case tarik_tunai:
case setoran:
currentTransaction = createTransaction(mainMenuSelection);
currentTransaction.execute();
break;
case exit:
screen.displayMessageLine("\nKeluar");
userExited = true; // user keluar
break;
default: screen.displayMessageLine("INPUT SALAH, COBA LAGI");
break;
}
}
}
private int displayMainMenu()
{
screen.displayMessageLine("MENU : \n");
screen.displayMessageLine("1 - CEK SALDO\n");
screen.displayMessageLine("2 - TARIK TUNAI\n");
screen.displayMessageLine("3 - SETORAN TUNAI\n");
screen.displayMessageLine("4 - EXIT\n");
screen.displayMessageLine("MASUKKAN INPUT: ");
return keypad.getInput();// intup dari user
}
private Transaction createTransaction (int type)
{
Transaction temp = null; // inisiasi
switch(type)
{
case saldo:
temp = new Saldo(currentAccountNumber, screen, bankDatabase);
break;
case tarik_tunai:
temp = new Tarik_Tunai(currentAccountNumber, screen,bankDatabase, keypad, cashDispenser);
break;
case setoran:
temp = new Setoran(currentAccountNumber, screen, bankDatabase,keypad,depositSlot);
break;
}
return temp;
}
}
- class Screen
public class Screen
{
public void displayMessage(String message)
{
System.out.print(message);
}
public void displayMessageLine(String message)
{
System.out.print(message);
}
public void displayAmount(double amount)
{
System.out.printf("$%,.2f",amount);
}
}
- class Keypad
import java.util.Scanner;
public class Keypad
{
private Scanner input;
public Keypad()
{
input = new Scanner(System.in);
}
public int getInput()
{
return input.nextInt();
}
}
- class Cash Dispenser
public class CashDispenser
{
private final static int INITIAL_COUNT = 500;
private int count;
public CashDispenser()
{
count = INITIAL_COUNT;
}
public void dispenseCash(int amount)
{
int billsRequired = amount/20;
count -=billsRequired; //update
}
public boolean isSufficientCashAvailable(int amount)
{
int billsRequired = amount/20;
if (count>=billsRequired)//uang tersedia
return true;
else
return false; //uang tidak tersedia
}
}
- class Deposit Slot
public class DepositSlot
{
public boolean isEnvelopeReceived()
{
return true; //asumsi bahwa uang talah di masukkan
}
}
- class Account
public class Account
{
private int accountNumber;
private int pin;
private double availableBalance; // saldo yang dapat ditarik
private double totalBalance;
public Account(int theAccountNumber, int thePIN, double theAvailableBalance, double theTotalBalance)
{
accountNumber = theAccountNumber;
pin = thePIN;
availableBalance = theTotalBalance;
totalBalance = theTotalBalance;
}
// cek pin
public boolean validatePIN(int userPIN)
{
if(userPIN==pin)
return true;
else
return false;
}
public double getAvailableBalance()
{
return availableBalance;
}
public double getTotalBalance()
{
return totalBalance;
}
public void credit (double amount)
{
totalBalance+=amount;
}
public void debit(double amount)
{
availableBalance -= amount;
totalBalance -= amount;
}
public int getAccountNumber()
{
return accountNumber;
}
}
- class Bank Database
public class BankDatabase
{
private Account[] accounts;
public BankDatabase()
{
accounts = new Account[2];
accounts[0]= new Account(12345,54321,1000.0,1200.0);
accounts[1]= new Account(98765,56789,200.0,200.0);
}
public Account getAccount(int accountNumber)
{
for(Account currentAccount : accounts)
{
if(currentAccount.getAccountNumber()==accountNumber)
return currentAccount;
}
return null;//jika tidak ada akun yang cocok
}
public boolean authenticateUser(int userAccountNumber, int userPIN)
{
Account userAccount = getAccount(userAccountNumber);
if(userAccount!=null)
return userAccount.validatePIN(userPIN);
else
return false;
}
public double getAvailableBalance(int userAccountNumber)
{
return getAccount(userAccountNumber).getAvailableBalance();
}
public double getTotalBalance(int userAccountNumber)
{
return getAccount(userAccountNumber).getTotalBalance();
}
public void credit(int userAccountNumber, double amount)
{
getAccount(userAccountNumber).credit(amount);
}
public void debit(int userAccountNumber, double amount)
{
getAccount(userAccountNumber).debit(amount);
}
}
- class Transaction
public abstract class Transaction
{
private int accountNumber;
private Screen screen;
private BankDatabase bankDatabase;
public Transaction(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase)
{
accountNumber = userAccountNumber;
screen = atmScreen;
bankDatabase = atmBankDatabase;
}
public int getAccountNumber()
{
return accountNumber;
}
public Screen getScreen()
{
return screen;
}
public BankDatabase getBankDatabase()
{
return bankDatabase;
}
abstract public void execute();
}
- class Saldo
public class Saldo extends Transaction
{
public Saldo(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase)
{
super(userAccountNumber, atmScreen, atmBankDatabase);
}
@Override
public void execute()
{
// mengambil referensi dairi database dan screen
BankDatabase bankDatabase = getBankDatabase();
Screen screen = getScreen();
// mengambil saldo tersedia
double availableBalance = bankDatabase.getAvailableBalance( getAccountNumber() );
// mengambil total saldo
double totalBalance = bankDatabase.getTotalBalance(getAccountNumber());
//menampilkan informasi
screen.displayMessageLine("\nInfo Saldo:");
screen.displayMessage(" - Saldo Tersedia: ");
screen.displayAmount(availableBalance);
screen.displayMessage("\n - Total balance: ");
screen.displayAmount(totalBalance);
screen.displayMessageLine("");
}
}
- class Tarik Tunai
public class Tarik_Tunai extends Transaction
{
private int amount;
private Keypad keypad;
private CashDispenser cashDispenser;
private final static int batal = 6;
public Tarik_Tunai(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, CashDispenser atmCashDispenser)
{
super(userAccountNumber, atmScreen, atmBankDatabase);
keypad = atmKeypad;
cashDispenser = atmCashDispenser;
}
@Override
public void execute()
{
boolean cashDispensed = false; // uang belum keluar
double availableBalance;
BankDatabase bankDatabase = getBankDatabase();
Screen screen = getScreen();
do
{
amount = displayMenuOfAmounts();
if(amount!=batal)
{
availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
if(amount <= availableBalance)//cek apakah saldo di akun mencukupi
{
if(cashDispenser.isSufficientCashAvailable(amount))
{
bankDatabase.debit(getAccountNumber(),amount);
cashDispenser.dispenseCash(amount);// mengeluarkan uang
cashDispensed = true;
screen.displayMessageLine("\nSilahkan Ambil Uang Anda");
}
else
screen.displayMessageLine("\nUang di mesin ATM tidak mencukupi\nsilahkan pilih nominal yang lebih kecil");
}
else
screen.displayMessageLine("\nSaldo tidak mencukupi\nsilahkan pilih nominal yang lebih kecil");
}
else
{
screen.displayMessageLine("\nTransaksi Batal");
return;
}
}while(!cashDispensed);
}
private int displayMenuOfAmounts()
{
int userChoice =0;
Screen screen = getScreen();
int[] amounts={0,20,40,60,100,200};
while(userChoice==0)
{
screen.displayMessageLine( "\nTarikan Tunai:" );
screen.displayMessageLine( "\n1 - $20" );
screen.displayMessageLine( "\n2 - $40" );
screen.displayMessageLine( "\n3 - $60" );
screen.displayMessageLine( "\n4 - $100" );
screen.displayMessageLine( "\n5 - $200" );
screen.displayMessageLine( "\n6 - Cancel transaction" );
screen.displayMessage( "\nPilih Nominal: " );
int input = keypad.getInput(); // mengambil input user
switch ( input )
{
case 1: // pilihan untuk nominal tarik tunai
case 2:
case 3:
case 4:
case 5:
userChoice = amounts[input]; // pilihan user
break;
case batal: // jika batal
userChoice = batal;
break;
default:
screen.displayMessageLine(
"\nINPUT SALAH" );
}
}
return userChoice;
}
}
- class Setoran
public class Setoran extends Transaction
{
private double amount;
private Keypad keypad;
private DepositSlot depositSlot;
private final static int batal = 0;
public Setoran(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, DepositSlot atmDepositSlot)
{
super(userAccountNumber,atmScreen,atmBankDatabase);
keypad=atmKeypad;
depositSlot=atmDepositSlot;
}
@Override
public void execute()
{
BankDatabase bankDatabase = getBankDatabase();
Screen screen = getScreen();
amount = promptForDepositAmount();
if ( amount != batal )
{
screen.displayMessage("\nSilahkan masukkan uang " );
screen.displayAmount( amount );
screen.displayMessageLine( "." );
boolean envelopeReceived = depositSlot.isEnvelopeReceived();
if(envelopeReceived)
{
screen.displayMessageLine("\nUang telah di setorkan");
bankDatabase.credit(getAccountNumber(),amount);
}
else
screen.displayMessageLine("\nUang Belum Masuk, Transaksi Dibatalkan");
}
else
screen.displayMessageLine("\nTransaksi Batal");
}
private double promptForDepositAmount()
{
Screen screen = getScreen();
screen.displayMessage("\nMasukkan nilai Setoran, atau 0 untuk Batal");
int input = keypad.getInput();
if ( input == batal )
return batal;
else
{
return ( double ) input;
}
}
}
Tidak ada komentar:
Posting Komentar