Findryan

Untitled

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