ilham_syamsuddin

Untitled

Oct 29th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. //ATM.java
  2. //represents an ATM
  3.  
  4. public class ATM
  5. {
  6. private boolean userAuthenticated;
  7. private int currentAccountNumber;
  8. private Screen screen;
  9. private Keypad keypad;
  10. private CashDispenser cashDispenser;
  11. private DepositSlot depositSlot;
  12. private BankDatabase bankDatabase;
  13.  
  14. //constant coresponding to main menu options
  15. private static final int BALANCE_INQUIRY=1;
  16. private static final int WITHDRAWAL=2;
  17. private static final int DEPOSIT=3;
  18. private static final int EXIT=4;
  19.  
  20. //no argument ATM constructor intitializes instancevariables
  21. public ATM()
  22. {
  23. userAuthenticated = false;
  24. currentAccountNumber = 0;
  25. screen = new Screen();
  26. keypad = new Keypad();
  27. cashDispenser = new CashDispenser();
  28. depositSlot = new DepositSlot();
  29. bankDatabase = new BankDatabase();
  30. }// end of no argument construct
  31.  
  32. //start ATM
  33. public void run()
  34. {
  35. // welcome authenticated user
  36. while(true)
  37. {
  38. //loop while user is not yet authenthicated
  39. while(!userAuthenticated)
  40. {
  41. screen.displayMessageLine("\nwelcome!!");
  42. authenticateUser();
  43. }//end while
  44.  
  45. performTransactions();
  46. userAuthenticated = false;
  47. currentAccountNumber = 0;
  48. screen.displayMessageLine("\nThank you Goodbye!");
  49. }//endwhile
  50. }//endmethodrun
  51.  
  52. //attemp to authentiate against database
  53. private void authenticateUser()
  54. {
  55. screen.displayMessage("\n please enter your account number: ");
  56. int accountNumber = keypad.getInput();
  57. screen.displayMessage( "\nEnter your PIN: " );
  58. int pin = keypad.getInput();
  59.  
  60. // set userAuthenticated to boolean value returned by database
  61. userAuthenticated =
  62. bankDatabase.authenticateUser(accountNumber,pin);
  63.  
  64. // check wether authentication succeeded
  65. if( userAuthenticated )
  66. {
  67. currentAccountNumber = accountNumber;
  68. }//end if
  69. else
  70. screen.displayMessageLine(
  71. "Invalid account number, or PIN,Please try again");
  72. }
  73.  
  74. //display Main menu and perform transaction;
  75. private void performTransactions()
  76. {
  77. //local variable to store transaction;
  78. Transaction currentTransaction = null;
  79.  
  80. boolean userExited = false;
  81.  
  82. //loop wile user still not exit
  83. while(!userExited)
  84. {
  85. //show main menu anfd get user selection
  86. int mainMenuSelection = displayMainMenu();
  87.  
  88. //decidevhow to proceed based on user selection
  89. switch ( mainMenuSelection )
  90. {
  91. //user choose one transaction type
  92. case BALANCE_INQUIRY:
  93. case WITHDRAWAL:
  94. case DEPOSIT:
  95.  
  96. //initialize as new object of chosen type;
  97. currentTransaction =
  98. createTransaction( mainMenuSelection );
  99.  
  100. currentTransaction.execute(); //execute transaction
  101. break;
  102. case EXIT:// user choose exit
  103. screen.displayMessageLine("\nExiting the system....");
  104. userExited = true;
  105. break;
  106. default: // user input integer other than 1-4
  107. screen.displayMessageLine(
  108. "\nYou did not enter a valid selection, try again");
  109. break;
  110. }//end switch
  111. }//end while
  112. }//end method performTransaction
  113.  
  114. //dislay main menu and return input selection
  115. private int displayMainMenu()
  116. {
  117. screen.displayMessageLine( "\nMain menu:" );
  118. screen.displayMessageLine( "1 - View my balance" );
  119. screen.displayMessageLine( "2 - Withdraw cash" );
  120. screen.displayMessageLine( "3 - Deposit funds" );
  121. screen.displayMessageLine( "4 - Exit\n" );
  122. screen.displayMessage( "Enter a choice: " );
  123. return keypad.getInput(); // return user's selection
  124. }//end method displayMainMenu
  125.  
  126. //return object of specified transaction subcalss
  127. private Transaction createTransaction( int type )
  128. {
  129. Transaction temp = null;
  130.  
  131. //determine wich type to create
  132. switch ( type )
  133. {
  134. case BALANCE_INQUIRY:
  135. temp = new BalanceInquiry(
  136. currentAccountNumber, screen, bankDatabase);
  137. break;
  138. case WITHDRAWAL: // create new Withdrawal transaction
  139. temp = new Withdrawal( currentAccountNumber, screen,
  140. bankDatabase, keypad, cashDispenser );
  141. break;
  142. case DEPOSIT: // create new Deposit transaction
  143. temp = new Deposit( currentAccountNumber, screen,
  144. bankDatabase, keypad, depositSlot );
  145. break;
  146. } // end switch
  147.  
  148. return temp;// return newly created object
  149. }//end method createTransaction
  150. }//end ATM
Add Comment
Please, Sign In to add comment