Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2. * Author : YogaOcean
  3. * Date : 04-Des-2016
  4. * Program : Withdrawal class (part of ATM case study)
  5. */
  6. public class Withdrawal extends Transaction
  7. {
  8. private int amount; // amount to withdraw
  9. private Keypad keypad; // references to keypad
  10. private CashDispenser cashDispenser; // references to cash dispenser
  11.  
  12. // constant corresponding to menu option to cancel
  13. private final static int CANCELED = 6;
  14.  
  15. // Withdrawal constructor
  16. public Withdrawal(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase, Keypad atmKeypad, CashDispenser atmCashDispenser){
  17. // initializes superclass variables
  18. super(userAccountNumber, atmScreen, atmBankDatabase);
  19.  
  20. // initializes references to keypad and cash dispenser
  21. keypad = atmKeypad;
  22. cashDispenser = atmCashDispenser;
  23. } // end Withdrawal constructor
  24.  
  25. // perform transaction
  26. @Override
  27. public void execute(){
  28. boolean cashDispensed = false; // cash was not dispensed yet
  29. double availableBalance; // amount available for withdrawal
  30.  
  31. // get references to bank database and screen
  32. BankDatabase bankDatabase = getBankDatabase();
  33. Screen screen = getScreen();
  34.  
  35. // loop until cash is dispensed or the user cancels
  36. do{
  37. // obtain a chosen withdrawal amount from the user
  38. amount = displayMenuOfAmounts();
  39.  
  40. // check whether user choose a withdrawal amount or canceled
  41. if(amount != CANCELED){
  42. // get available balance of account involved
  43. availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
  44.  
  45. // check whether the user has enough money in the account
  46. if(amount <= availableBalance){
  47.  
  48. // check whether the cash dispenser has enough money
  49. if(cashDispenser.isSufficientCashAvailable(amount)){
  50.  
  51. // update the account involved to reflect the withdrawal
  52. bankDatabase.debit(getAccountNumber(), amount);
  53.  
  54. cashDispenser.dispenseCash(amount); // dispense cash
  55. cashDispensed = true; // cash was dispensed
  56.  
  57. // instructs user to take cash
  58. screen.displayMessageLine("\nYour cash has been dispensed. Please take your cash now.");
  59. } // end if
  60. else{
  61. // cash dispenser does not have enough cash
  62. screen.displayMessageLine("\nInsufficient cash available in the ATM.");
  63. screen.displayMessageLine("\nPlease choose a smaller amount.");
  64. } // end if
  65. } // end if
  66. else{
  67. // not enough money available in user's account
  68. screen.displayMessageLine("\nInsufficient funds in your account.");
  69. screen.displayMessageLine("\nPlease choose a smaller amount.");
  70. } // end if
  71. } // end if
  72. else{
  73. // user choose cancel menu option
  74. screen.displayMessageLine("\nCancelling transactions...");
  75. return; // return to main menu because user canceled
  76. } // end if
  77. } while(!cashDispensed);
  78. } // end method execute
  79.  
  80. // display a menu of withdrawal amounts and the options to cancel
  81. // return the chosen amount or 0 if the user chooses to cancel
  82. private int displayMenuOfAmounts(){
  83. int userChoice = 0; // local variable to store return value
  84.  
  85. Screen screen = getScreen(); // get screen references
  86.  
  87. // array of amounts to correspond to menu numbers
  88. int[] amounts = {0, 20, 40, 60, 100, 200};
  89.  
  90. // loop while no valid choice has been made
  91. while(userChoice == 0){
  92. // display the withdrawal menu
  93. screen.displayMessageLine("\nWithdrawal Menu : ");
  94. screen.displayMessageLine("1 - $20");
  95. screen.displayMessageLine("2 - $40");
  96. screen.displayMessageLine("3 - $60");
  97. screen.displayMessageLine("4 - $100");
  98. screen.displayMessageLine("5 - $200");
  99. screen.displayMessageLine("6 - Cancel Transaction");
  100. screen.displayMessage("\nChoose a withdrawal amount : ");
  101.  
  102. int input = keypad.getInput(); // get user input through keypad
  103.  
  104. // determine how to proceed based on the input value
  105. switch(input){
  106. // if the user choose a withdrawal amount
  107. // i.e choose option 1, 2, 3, 4 or 5
  108. // return the corresponding amount from amounts's array
  109. case 1 :
  110. case 2 :
  111. case 3 :
  112. case 4 :
  113. case 5 :
  114. userChoice = amounts[input]; // save user's choice
  115. break;
  116.  
  117. case CANCELED :
  118. // the user choose to cancel
  119. userChoice = CANCELED; // save user's choice
  120. break;
  121.  
  122. default :
  123. // the user did not enter value between 1-6
  124. screen.displayMessageLine("\nInvalid selection.");
  125. screen.displayMessageLine("Try again.");
  126. } // end switch
  127. } // end while
  128.  
  129. return userChoice; // return withdrawal amount or CANCELED
  130. } // end method displayMenuOfAmounts
  131. } // end class Withdrawal