Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1.  
  2. [code]import java.util.Scanner;
  3.  
  4. class ATM {
  5.  
  6.  
  7. public static void main(String[] args) {
  8. //Declare and/or initialize variables used throughout
  9. int clear;
  10. int x = -1;
  11. int loop = 1;
  12. int menuChoice = 0;
  13. int arrayCount = 0;
  14. String loginID = null;
  15. user[] userArray = new user[10];
  16. Scanner scanner = new Scanner(System.in);
  17.  
  18. //Menu loop
  19. while (loop != 0) {
  20.  
  21. //Printed menu for user
  22. System.out.println("Please make a selection: ");
  23. System.out.println("1 - Register");
  24. System.out.println("2 - Login");
  25. System.out.println("3 - Deposit");
  26. System.out.println("4 - Withdraw");
  27. System.out.println("5 - Check Balance");
  28. System.out.println("6 - Log Out");
  29.  
  30. //Menu prompt for user input
  31. System.out.print("Make your selection (1-6): ");
  32. menuChoice = scanner.nextInt();
  33.  
  34. //Register
  35. if (menuChoice == 1) {
  36.  
  37. //Conditional to check for space in array
  38. if (arrayCount < 10) {
  39. //Creates new user obj, increments array position for next, reads user input for id, pin
  40. userArray[arrayCount] = new user();
  41. System.out.println("Please input your user ID:");
  42. userArray[arrayCount].id = scanner.next();
  43. System.out.println("Please input 4 digit PIN: ");
  44. userArray[arrayCount].pin = scanner.nextInt();
  45.  
  46. //arrayCount incrementer to keep track of # of users
  47. arrayCount++;
  48. }
  49. //Warning of full user array
  50. else {
  51. System.out.println("Too many users!");
  52. }
  53. }
  54.  
  55. //Login
  56. else if (menuChoice == 2) {
  57.  
  58. //Clears all loginTokens
  59. clear = 0;
  60. while (clear < arrayCount) {
  61. userArray[clear].loginToken = false;
  62. clear++;
  63. }
  64.  
  65. //User input for login, and PIN
  66. System.out.println("Please input your user ID (case insensitive): ");
  67. loginID = scanner.next();
  68. System.out.println("Please input your PIN: ");
  69. int loginPin = scanner.nextInt();
  70.  
  71. // Uses user input to check id string input versus stored user id to find the
  72. //array position of the user
  73. x = checkID(arrayCount, loginID, userArray);
  74.  
  75. // Checks the input PIN against the pin in stored instance variable in the
  76. // same array position as the id found in checkID
  77. userArray[x].loginToken = checkPin(x, loginPin, userArray);
  78.  
  79. }
  80.  
  81. //Deposit
  82. else if (menuChoice == 3) {
  83.  
  84. //Retrieving array position of user
  85. x = checkID(arrayCount, loginID, userArray);
  86.  
  87. if (x == -1 || userArray[x].loginToken == false) {
  88. System.out.println("Please login.");
  89. }
  90. if (x > -1 && userArray[x].loginToken == true) {
  91. System.out.print("How much would you like to deposit?: ");
  92. int deposit = scanner.nextInt();
  93. userArray[x].balance = userArray[x].balance + deposit;
  94. System.out.println("Your new balance is: $" + deposit);
  95. }
  96.  
  97. }
  98.  
  99. //Withdraw
  100. else if (menuChoice == 4) {
  101. if (x == -1 || userArray[x].loginToken == false) {
  102. System.out.println("Please login.");
  103. }
  104. if (x > -1 && userArray[x].loginToken == true) {
  105. System.out.print("How much would you like to withdraw?: ");
  106. int withdraw = scanner.nextInt();
  107. if (withdraw < userArray[x].balance) {
  108. userArray[x].balance = userArray[x].balance - withdraw;
  109. System.out.println("Your new balance is $" + userArray[x].balance);
  110. }
  111. if (withdraw > userArray[x].balance) {
  112. System.out.println("You have insufficient funds.");
  113. }
  114.  
  115. }
  116. }
  117.  
  118. //Check Balance
  119. else if(menuChoice==5){
  120. if (x == -1 || userArray[x].loginToken == false) {
  121. System.out.println("Please login.");
  122. }
  123. if (x > -1 && userArray[x].loginToken == true) {
  124. System.out.println("Your balance is: $"+ userArray[x].balance);
  125. }
  126. }
  127.  
  128. //Log Out
  129. else if (menuChoice==6) {
  130. //Clears all loginTokens
  131. clear = 0;
  132. while (clear < arrayCount) {
  133. userArray[clear].loginToken = false;
  134. clear++;
  135. }
  136. }
  137.  
  138. //Catches invalid entries at the menu
  139. else {
  140. System.out.println("Please make a valid selection!");
  141.  
  142. }
  143.  
  144. }
  145. }
  146.  
  147.  
  148. //Uses temporary variable idTemp to find array position of login credentials, if available.
  149.  
  150. static int checkID(int arrayCount, String idTemp, user[] userArray){
  151.  
  152. int x = 0;
  153. //Loops through all user objects to find match
  154. while(x < arrayCount) {
  155. if(userArray[x].id.equals(idTemp)) {
  156. return x;
  157. }
  158. if(idTemp != userArray[x].id){
  159. x++;
  160. }
  161. }
  162. if(x==userArray.length){
  163. System.out.println("User was not found!");
  164. }
  165. return x;
  166. }
  167.  
  168. //Accepts the int return of checkID to compare user input pin against the stored pin at that array position.
  169. static boolean checkPin(int x, int inputPin, user[] userArray) {
  170. boolean pinBool = false;
  171. if(userArray[x].pin == inputPin){
  172. System.out.println("User " + userArray[x].id + " has been successfully logged in!");
  173. pinBool = true;
  174. }
  175. else{
  176. System.out.println("There was an error!");
  177. }
  178. return pinBool;
  179. }
  180.  
  181.  
  182.  
  183. }
  184.  
  185. //User object
  186. class user{
  187. String id;
  188. int pin;
  189. int balance;
  190. boolean loginToken = false;
  191. };
  192. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement