ilham_syamsuddin

Untitled

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