document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class ATM
  2. {
  3. private boolean userAuthenticated; // whether user is authenticated
  4. private int currentAccountNumber; // current user's account number
  5. private Screen screen; // ATM's screen
  6. private Keypad keypad; // ATM's keypad
  7. private CashDispenser cashDispenser; // ATM's cash dispenser
  8. private DepositSlot depositSlot; // ATM's deposit slot
  9. private BankDatabase bankDatabase; // account information database
  10.  
  11. // constants corresponding to main menu options
  12. private static final int BALANCE_INQUIRY = 1;
  13. private static final int WITHDRAWAL = 2;
  14. private static final int DEPOSIT = 3;
  15. private static final int EXIT = 4;
  16.  
  17. // no-argument ATM constructor initializes instance variables
  18. public ATM(){
  19. userAuthenticated = false; // user is not authenticated to start
  20. currentAccountNumber = 0; // no current account number to start
  21. screen = new Screen(); // create screen
  22. keypad = new Keypad(); // create keypad
  23. cashDispenser = new CashDispenser(); // create cash dispenser
  24. depositSlot = new DepositSlot(); // create deposit slot
  25. bankDatabase = new BankDatabase(); // create account info database
  26. } // end no argument ATM constructor
  27.  
  28. // start ATM
  29. public void run(){
  30. // welcome and authenticate user; perform transactions
  31. while(true){
  32. // loop while user is not yet authenticated
  33. while(!userAuthenticated){
  34. screen.displayMessageLine("\\nWelcome!");
  35. authenticateUser(); // authenticate user
  36. } // end while
  37.  
  38. performTransactions(); // user is now authenticated
  39. userAuthenticated = false; // reset before next ATM session
  40. currentAccountNumber = 0; // reset before next ATM session
  41. screen.displayMessageLine("\\nThank You!\\nGoodbye!");
  42. } // end while
  43. } // end method run
  44.  
  45. // attempts to authenticate user against database
  46. private void authenticateUser(){
  47. screen.displayMessage("\\nPlease enter your account number : ");
  48. int accountNumber = keypad.getInput(); // input account number
  49. screen.displayMessage("\\nEnter your PIN : "); // prompt for PIN
  50. int pin = keypad.getInput(); // input PIN
  51.  
  52. // set userAuthenticated to boolean value returned by database
  53. userAuthenticated = bankDatabase.authenticateUser(accountNumber, pin);
  54.  
  55. // check whether authentication succeeded
  56. if(userAuthenticated){
  57. currentAccountNumber = accountNumber; // save user's account
  58. } // end if
  59. else{
  60. screen.displayMessageLine("Invalid Account Number or PIN.");
  61. screen.displayMessageLine("Please Try Again.");
  62. }
  63. } // end method authenticateUser
  64.  
  65. // display the main menu and perform transactions
  66. private void performTransactions(){
  67. // local variable to store transaction currently being processed
  68. Transaction currentTransaction = null;
  69. boolean userExited = false; // user has not chosen to exit
  70.  
  71. // loop while user has not chosen option to exit system
  72. while(!userExited){
  73. // show main menu and get user selection
  74. int mainMenuSelection = displayMainMenu();
  75.  
  76. // decide how to proceed based on user's menu selection
  77. switch(mainMenuSelection){
  78. // user choose to perform one of three transaction types
  79. case BALANCE_INQUIRY :
  80. case WITHDRAWAL :
  81. case DEPOSIT :
  82.  
  83. // initialize as new object choosen type
  84. currentTransaction = createTransaction(mainMenuSelection);
  85.  
  86. currentTransaction.execute(); // execute transaction
  87. break;
  88.  
  89. case EXIT :
  90. // user choose to terminate session
  91. screen.displayMessageLine("\\nExiting the system...");
  92. userExited = true; // this ATM session should end
  93. break;
  94.  
  95. default :
  96. // user did not enter an integer between 1-4
  97. screen.displayMessageLine("\\nYou did not enter a valid selection.");
  98. screen.displayMessageLine("Please try again.");
  99. break;
  100. } // end switch
  101. } // end while
  102. } // end method performTransactions
  103.  
  104. // display the main menu and return an input selection
  105. private int displayMainMenu(){
  106. screen.displayMessageLine("\\nMain Menu :");
  107. screen.displayMessageLine("1 - View my balance");
  108. screen.displayMessageLine("2 - Withdraw cash");
  109. screen.displayMessageLine("3 - Deposit funds");
  110. screen.displayMessageLine("4 - Exit\\n");
  111. screen.displayMessage("Enter a choice : ");
  112. return keypad.getInput(); // return user's selection
  113. } // end method of displayMainMenu
  114.  
  115. // return object of specified Transaction subclass
  116. private Transaction createTransaction(int type){
  117. Transaction temp = null; // temporary Transaction variable
  118.  
  119. // determine which type of Transaction to create
  120. switch(type){
  121. case BALANCE_INQUIRY :
  122. // create new BalanceInquiry transaction
  123. temp = new BalanceInquiry(currentAccountNumber, screen, bankDatabase);
  124. break;
  125.  
  126. case WITHDRAWAL :
  127. // create new Withdrawal transaction
  128. temp = new Withdrawal(currentAccountNumber, screen, bankDatabase, keypad, cashDispenser);
  129. break;
  130.  
  131. case DEPOSIT :
  132. // create new Deposit transaction
  133. temp = new Deposit(currentAccountNumber, screen, bankDatabase, keypad, depositSlot);
  134. break;
  135. } // end switch
  136.  
  137. return temp; // return newly created object
  138. } // end method createTransaction
  139. } // end class ATM
  140.  
  141. public class DepositSlot
  142. {
  143. // indicate whether envelope was received (always returns true)
  144. // because this is only a software simulation of a real deposit slot
  145. public boolean isEnvelopeReceived(){
  146. return true; // deposit envelope was received
  147. } // end method isEnvelopeReceived
  148. } // end class DepositSlot
  149.  
  150. public class Account
  151. {
  152. private int accountNumber; // account number
  153. private int pin; // PIN for authentication
  154. private double availableBalance; // funds available for withdrawal
  155. private double totalBalance; // funds available + pending deposits
  156.  
  157. // Account constructor initializes attributes
  158. public Account(int theAccountNumber, int thePIN, double theAvailableBalance, double theTotalBalance){
  159. accountNumber = theAccountNumber;
  160. pin = thePIN;
  161. availableBalance = theAvailableBalance;
  162. totalBalance = theTotalBalance;
  163. } // end Account constructor
  164.  
  165. // determines whether a user-specified PIN matches PIN in Account
  166. public boolean validatePIN(int userPIN){
  167. if(userPIN == pin){
  168. return true; // means the PIN input is match with the user's PIN
  169. }
  170. else{
  171. return false; // means the PIN input is not match with the user's PIN
  172. }
  173. } // end method validatePIN
  174.  
  175. // returns available balance
  176. public double getAvailableBalance(){
  177. return availableBalance;
  178. }
  179.  
  180. // returns the total balance
  181. public double getTotalBalance(){
  182. return totalBalance;
  183. }
  184.  
  185. // credits an amount to the account
  186. public void credit(double amount){
  187. totalBalance += amount; // add to total balance
  188. }
  189.  
  190. // debits an amount from the account
  191. public void debit(double amount){
  192. availableBalance -= amount; // substract from available balance
  193. totalBalance -= amount; // substract from total balance
  194. }
  195.  
  196. // returns account number
  197. public int getAccountNumber(){
  198. return accountNumber;
  199. }
  200. } // end class Account
  201.  
  202.  
  203. public class Screen
  204. {
  205. // display a message without a carriage return
  206. public void displayMessage(String message){
  207. System.out.print(message);
  208. } // end method displayMessage
  209.  
  210. // display a message with a carriage return
  211. public void displayMessageLine(String message){
  212. System.out.println(message);
  213. } // end method displayMessageLine
  214.  
  215. // displays a dollar amount
  216. public void displayDollarAmount(double amount){
  217. System.out.printf("$%,.2f", amount);
  218. } // end method displayDollarAmount
  219. } // end class Screen
  220.  
  221.  
  222. import java.util.Scanner;
  223.  
  224. public class Keypad
  225. {
  226. private Scanner input; // reads data from the command line
  227.  
  228. // no-argument constructor initializes the Scanner
  229. public Keypad(){
  230. input = new Scanner(System.in);
  231. } // end no-argument Keypad constructor
  232.  
  233. // return an integer value entered by user
  234. public int getInput(){
  235. return input.nextInt(); // we assume that user enters an integer
  236. } // end method getInput
  237. } // end class Keypad
  238.  
  239.  
  240. public class ATMCaseStudy
  241. {
  242. // main method creates and runs the ATM
  243. public static void main(String[] args){
  244. ATM theATM = new ATM();
  245. theATM.run();
  246. } // end main
  247. } // e
  248.  
  249.  
  250. public class CashDispenser
  251. {
  252. // the default initial number of bills in the cash dispenser
  253. private final static int INITIAL_COUNT = 500;
  254. private int count; // number of $20 bills remaining
  255.  
  256. // no-argument CashDispenser constructor initializes count to default
  257. public CashDispenser(){
  258. count = INITIAL_COUNT; // set count attribute to default
  259. } // end CashDispenser constructor
  260.  
  261. // simulates dispensing of specified amount of cash
  262. public void dispenseCash(int amount){
  263. int billsRequired = amount / 20; // number of $20 bills required
  264. count -= billsRequired; // update the count of bills
  265. } // end method dispenseCash
  266.  
  267. // indicates whether cash dispenser can dispense desired amount
  268. public boolean isSufficientCashAvailable(int amount){
  269. int billsRequired = amount / 20;
  270.  
  271. if(count >= billsRequired){
  272. return true; // enough bills required
  273. }
  274. else{
  275. return false; // not enough bills required
  276. }
  277. } // end method isSufficientCashAvailable
  278. } // end class CashDispenser
  279.  
  280.  
  281. public abstract class Transaction
  282. {
  283. private int accountNumber; // indicates account involved
  284. private Screen screen; // ATM's screen
  285. private BankDatabase bankDatabase; // account info database
  286.  
  287. // Transaction constructor invoked by subclasses using super()
  288. public Transaction(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase){
  289. accountNumber = userAccountNumber;
  290. screen = atmScreen;
  291. bankDatabase = atmBankDatabase;
  292. } // end Transaction constructor
  293.  
  294. // return account number
  295. public int getAccountNumber(){
  296. return accountNumber;
  297. } // end method
  298.  
  299. // return reference to screen
  300. public Screen getScreen(){
  301. return screen;
  302. } // end method
  303.  
  304. // return reference to bank database
  305. public BankDatabase getBankDatabase(){
  306. return bankDatabase;
  307. } // end method
  308.  
  309. // perform the transaction (overridden by each subclass)
  310. abstract public void execute();
  311. } // end class Transaction
  312.  
  313.  
  314. public class BankDatabase
  315. {
  316. private Account[] accounts; // array of Accounts
  317.  
  318. // no-argument BankDatabase constructor initializes accounts
  319. public BankDatabase(){
  320. accounts = new Account[2]; // just 2 accounts for testing
  321. accounts[0] = new Account(12345, 54321, 1000.0, 1200.0);
  322. accounts[1] = new Account(98765, 56789, 200.0, 200.0);
  323. } // end no-argument BankDatabase constructor
  324.  
  325. // retrieve Account object containing specified account number
  326. private Account getAccount(int accountNumber){
  327. // loop through accounts searching for matching account number
  328. for(Account currentAccount : accounts){
  329. // return current account if match found
  330. if(currentAccount.getAccountNumber() == accountNumber) return currentAccount;
  331. } // end for
  332.  
  333. return null; // if no matching account was found, return null
  334. } // end method
  335.  
  336. // determine whether user-specified account number and PIN match
  337. // those of an account in the database
  338. public boolean authenticateUser(int userAccountNumber, int userPIN){
  339. // attempt to retrieve the account with the account number
  340. Account userAccount = getAccount(userAccountNumber);
  341.  
  342. if(userAccount != null){
  343. return userAccount.validatePIN(userPIN);
  344. }
  345. else{
  346. return false; // account number not found, so return false
  347. }
  348. } // end method
  349.  
  350. // return available balance of Account with specified account number
  351. public double getAvailableBalance(int userAccountNumber){
  352. return getAccount(userAccountNumber).getAvailableBalance();
  353. } // end method
  354.  
  355. public double getTotalBalance(int userAccountNumber){
  356. return getAccount(userAccountNumber).getTotalBalance();
  357. } // end method
  358.  
  359. public void credit(int userAccountNumber, double amount){
  360. getAccount(userAccountNumber).credit(amount);
  361. } // end method
  362.  
  363. public void debit(int userAccountNumber, double amount){
  364. getAccount(userAccountNumber).debit(amount);
  365. } // end method
  366. } // end class BankDatabase
  367.  
  368.  
  369. public class Withdrawal extends Transaction
  370. {
  371. private int amount; // amount to withdraw
  372. private Keypad keypad; // references to keypad
  373. private CashDispenser cashDispenser; // references to cash dispenser
  374.  
  375. // constant corresponding to menu option to cancel
  376. private final static int CANCELED = 6;
  377.  
  378. // Withdrawal constructor
  379. public Withdrawal(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, CashDispenser atmCashDispenser){
  380. // initializes superclass variables
  381. super(userAccountNumber, atmScreen, atmBankDatabase);
  382.  
  383. // initializes references to keypad and cash dispenser
  384. keypad = atmKeypad;
  385. cashDispenser = atmCashDispenser;
  386. } // end Withdrawal constructor
  387.  
  388. // perform transaction
  389. @Override
  390. public void execute(){
  391. boolean cashDispensed = false; // cash was not dispensed yet
  392. double availableBalance; // amount available for withdrawal
  393.  
  394. // get references to bank database and screen
  395. BankDatabase bankDatabase = getBankDatabase();
  396. Screen screen = getScreen();
  397.  
  398. // loop until cash is dispensed or the user cancels
  399. do{
  400. // obtain a chosen withdrawal amount from the user
  401. amount = displayMenuOfAmounts();
  402.  
  403. // check whether user choose a withdrawal amount or canceled
  404. if(amount != CANCELED){
  405. // get available balance of account involved
  406. availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
  407.  
  408. // check whether the user has enough money in the account
  409. if(amount <= availableBalance){
  410.  
  411. // check whether the cash dispenser has enough money
  412. if(cashDispenser.isSufficientCashAvailable(amount)){
  413.  
  414. // update the account involved to reflect the withdrawal
  415. bankDatabase.debit(getAccountNumber(), amount);
  416.  
  417. cashDispenser.dispenseCash(amount); // dispense cash
  418. cashDispensed = true; // cash was dispensed
  419.  
  420. // instructs user to take cash
  421. screen.displayMessageLine("\\nYour cash has been dispensed. Please take your cash now.");
  422. } // end if
  423. else{
  424. // cash dispenser does not have enough cash
  425. screen.displayMessageLine("\\nInsufficient cash available in the ATM.");
  426. screen.displayMessageLine("\\nPlease choose a smaller amount.");
  427. } // end if
  428. } // end if
  429. else{
  430. // not enough money available in user's account
  431. screen.displayMessageLine("\\nInsufficient funds in your account.");
  432. screen.displayMessageLine("\\nPlease choose a smaller amount.");
  433. } // end if
  434. } // end if
  435. else{
  436. // user choose cancel menu option
  437. screen.displayMessageLine("\\nCancelling transactions...");
  438. return; // return to main menu because user canceled
  439. } // end if
  440. } while(!cashDispensed);
  441. } // end method execute
  442.  
  443. // display a menu of withdrawal amounts and the options to cancel
  444. // return the chosen amount or 0 if the user chooses to cancel
  445. private int displayMenuOfAmounts(){
  446. int userChoice = 0; // local variable to store return value
  447.  
  448. Screen screen = getScreen(); // get screen references
  449.  
  450. // array of amounts to correspond to menu numbers
  451. int[] amounts = {0, 20, 40, 60, 100, 200};
  452.  
  453. // loop while no valid choice has been made
  454. while(userChoice == 0){
  455. // display the withdrawal menu
  456. screen.displayMessageLine("\\nWithdrawal Menu : ");
  457. screen.displayMessageLine("1 - $20");
  458. screen.displayMessageLine("2 - $40");
  459. screen.displayMessageLine("3 - $60");
  460. screen.displayMessageLine("4 - $100");
  461. screen.displayMessageLine("5 - $200");
  462. screen.displayMessageLine("6 - Cancel Transaction");
  463. screen.displayMessage("\\nChoose a withdrawal amount : ");
  464.  
  465. int input = keypad.getInput(); // get user input through keypad
  466.  
  467. // determine how to proceed based on the input value
  468. switch(input){
  469. // if the user choose a withdrawal amount
  470. // i.e choose option 1, 2, 3, 4 or 5
  471. // return the corresponding amount from amounts's array
  472. case 1 :
  473. case 2 :
  474. case 3 :
  475. case 4 :
  476. case 5 :
  477. userChoice = amounts[input]; // save user's choice
  478. break;
  479.  
  480. case CANCELED :
  481. // the user choose to cancel
  482. userChoice = CANCELED; // save user's choice
  483. break;
  484.  
  485. default :
  486. // the user did not enter value between 1-6
  487. screen.displayMessageLine("\\nInvalid selection.");
  488. screen.displayMessageLine("Try again.");
  489. } // end switch
  490. } // end while
  491.  
  492. return userChoice; // return withdrawal amount or CANCELED
  493. } // end method displayMenuOfAmounts
  494. } // end class Withdrawal
  495.  
  496.  
  497. public class Deposit extends Transaction
  498. {
  499. private double amount; // amount to deposit
  500. private Keypad keypad; // references to keypad
  501. private DepositSlot depositSlot; // references to deposit slot
  502. private final static int CANCELED = 0; // constant for cancel option
  503.  
  504. // Deposit constructor
  505. public Deposit(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, DepositSlot atmDepositSlot){
  506. // initializes superclass variables
  507. super(userAccountNumber, atmScreen, atmBankDatabase);
  508.  
  509. // initialize references to keypad and deposit slot
  510. keypad = atmKeypad;
  511. depositSlot = atmDepositSlot;
  512. } // end Deposit constructor
  513.  
  514. // perform transaction
  515. @Override
  516. public void execute(){
  517. BankDatabase bankDatabase = getBankDatabase(); // get reference
  518. Screen screen = getScreen(); // get reference
  519.  
  520. amount = promptForDepositAmount(); // get deposit amount from user
  521.  
  522. // check whether the user entered a deposit amount or canceled
  523. if(amount != CANCELED){
  524. // request deposit envelope containing specified amount
  525. screen.displayMessage("\\nPlease insert a deposit envelope containing ");
  526. screen.displayDollarAmount(amount);
  527. screen.displayMessage(".");
  528.  
  529. // receive deposit envelope
  530. boolean envelopeReceived = depositSlot.isEnvelopeReceived();
  531.  
  532. // check whether deposit envelope was received
  533. if(envelopeReceived){
  534. screen.displayMessageLine("\\nYour envelope has been received.");
  535. screen.displayMessage("NOTE: The money just deposited will not be available until we verify the amount");
  536. screen.displayMessage("of any enclosed cash and your checks clear.");
  537.  
  538. // credit account to reflect the deposit
  539. bankDatabase.credit(getAccountNumber(), amount);
  540. } // end if
  541. else{
  542. // deposit envelope not received
  543. screen.displayMessageLine("\\nYou did not insert an envelope");
  544. screen.displayMessageLine("So, the ATM has canceled your transaction.");
  545. } // end else
  546. } // end if
  547. else{
  548. // user canceled instead of entering amount
  549. screen.displayMessageLine("\\nCanceling transaction...");
  550. } // end else
  551. } // end method execute
  552.  
  553. // prompt user to enter a deposit amount in cents
  554. private double promptForDepositAmount(){
  555. Screen screen = getScreen(); // get references to screen
  556.  
  557. // display the prompt
  558. screen.displayMessage("\\nPlease enter a deposit amount in CENTS (or 0 to cancel)");
  559. int input = keypad.getInput(); // receive input of deposit amount
  560.  
  561. // check whether the user canceled or entered a valid amount
  562. if(input == CANCELED) return CANCELED;
  563. else{
  564. return (double) input / 100; // return dollar amount
  565. } // end else
  566. } // end method
  567. } // end class Deposit
  568.  
  569.  
  570. public class BalanceInquiry extends Transaction
  571. {
  572. // BalanceInquiry constructor
  573. public BalanceInquiry(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase){
  574. super(userAccountNumber, atmScreen, atmBankDatabase);
  575. } // end BalanceInquiry constructor
  576.  
  577. // performs the transaction
  578. @Override
  579. public void execute(){
  580. // get references to bank database and screen
  581. BankDatabase bankDatabase = getBankDatabase();
  582. Screen screen = getScreen();
  583.  
  584. // get the available balance for the account involved
  585. double availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
  586.  
  587. // get the total balance for the account involved
  588. double totalBalance = bankDatabase.getTotalBalance(getAccountNumber());
  589.  
  590. // display the balance information on the screen
  591. screen.displayMessageLine("\\nBalance Information : ");
  592. screen.displayMessage(" - Available Balance : ");
  593. screen.displayDollarAmount(availableBalance);
  594. screen.displayMessage("\\n - Total Balance : ");
  595. screen.displayDollarAmount(totalBalance);
  596. screen.displayMessageLine("");
  597. } // end method execute
  598. } // end class BalanceInquiry
');