Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ATM2 {
  4. public static void main(String[] args){
  5.  
  6. int userCount = 0;
  7. int userPosition = 0;
  8. int menuChoice = 0;
  9. boolean loop = true;
  10. user[] userArray = new user[10];
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. while(loop != false){
  14.  
  15. menuPrint();
  16. menuChoice = scanner.nextInt();
  17.  
  18. if(menuChoice==1){
  19. System.out.print("Please input desired username: ");
  20. String tempUser = scanner.next();
  21. System.out.print("Please input desired 4 digit PIN ");
  22. int tempPin = scanner.nextInt();
  23. if(tempPin < 0 || tempPin > 9999){
  24. System.out.println("Please input valid 4 (0-9) digit PIN!");
  25. }
  26. else if(tempPin >= 0 && tempPin <= 9999 && userCount < 10){
  27. userArray[userCount] = user.createUser(tempUser,tempPin);
  28. userCount++;
  29. System.out.println("User " + tempUser + " account created!");
  30. }
  31. else if(userCount==10){
  32. System.out.println("Too many users!");
  33. }
  34. }
  35.  
  36. else if(menuChoice==2){
  37. if(userCount > 0){
  38. System.out.println(userPosition);
  39. System.out.print("Please input username: ");
  40. String tempUser = scanner.next();
  41. System.out.print("Please input 4 digit PIN ");
  42. int tempPin = scanner.nextInt();
  43. userPosition = user.login(tempUser,tempPin,userCount,userArray);
  44. System.out.println(userPosition);
  45. }
  46. else if(userCount==0){
  47. System.out.println("No users are registered.");
  48. }
  49. else if(userArray[userPosition].loginToken = true){
  50. System.out.println("You are now logged in.");
  51. }
  52.  
  53. }
  54.  
  55. else if(menuChoice==3){
  56. System.out.println("How much would you like to deposit?: ");
  57. int depositRequest = scanner.nextInt();
  58. user.Deposit(depositRequest, userPosition,userArray);
  59. }
  60.  
  61. else if(menuChoice==4){
  62. System.out.println("How much would you like to withdraw?: ");
  63. int withdrawRequest = scanner.nextInt();
  64. user.Withdraw(withdrawRequest,userPosition,userArray);
  65. }
  66. else if(menuChoice==5){
  67. user.checkBalance(userPosition, userArray);
  68. }
  69. else if(menuChoice==6){
  70. System.out.println("You are now logged out.");
  71. userArray[userPosition].loginToken = false;
  72. }
  73. else{
  74. System.out.println("Please make a valid selection (1-6).");
  75. }
  76.  
  77.  
  78. }
  79.  
  80.  
  81. }
  82.  
  83. static void menuPrint(){
  84. System.out.println("Please make a selection: ");
  85. System.out.println("1 - Register");
  86. System.out.println("2 - Login");
  87. System.out.println("3 - Deposit");
  88. System.out.println("4 - Withdraw");
  89. System.out.println("5 - Check Balance");
  90. System.out.println("6 - Log Out");
  91. }
  92. }
  93.  
  94. class user{
  95. private
  96. int pin;
  97. String id;
  98. float balance = 0;
  99.  
  100. public
  101. boolean loginToken;
  102.  
  103. public static user createUser(String inputUser, int inputPin){
  104. user newUser = new user();
  105. newUser.id = inputUser;
  106. newUser.pin = inputPin;
  107. return newUser;
  108. }
  109.  
  110. public static int login(String inputUser, int inputPin, int userCount, user[] userArray){
  111. int x = 0;
  112. int userPosition = 0;
  113.  
  114. while(x < userCount){
  115. userArray[x].loginToken = false;
  116. x++;
  117. }
  118. x=0;
  119.  
  120. while(x < userCount){
  121. if(userArray[x].id.equals(inputUser) && (userArray[x].pin == inputPin)) {
  122. userArray[x].loginToken = true;
  123. System.out.println("You are now logged in!");
  124. userPosition = x;
  125. }
  126. x++;
  127. }
  128. return userPosition;
  129. }
  130.  
  131.  
  132. public static void Deposit(int depositRequest,int userPosition, user[] userArray){
  133. if(userArray[userPosition].loginToken = true){
  134. userArray[userPosition].balance += depositRequest;
  135. System.out.println("Your new balance is: $ "+ userArray[userPosition].balance);
  136. }
  137. if(userArray[userPosition].loginToken = false){
  138. System.out.println("You need to login.");
  139. }
  140. }
  141. public static void Withdraw(int withdrawRequest,int userPosition, user[] userArray){
  142. if(userArray[userPosition].loginToken = true && userArray[userPosition].balance >= withdrawRequest){
  143. userArray[userPosition].balance -= withdrawRequest;
  144. System.out.println("Take your requested $ " + withdrawRequest);
  145. System.out.println("Your new balance is: $ " + userArray[userPosition].balance);
  146. }
  147. if(userArray[userPosition].loginToken = false){
  148. System.out.println("You need to login.");
  149. }
  150. if(userArray[userPosition].balance < withdrawRequest){
  151. System.out.println("You have insufficient funds!");
  152. }
  153.  
  154.  
  155.  
  156. }
  157.  
  158. public static void checkBalance(int userPosition,user[] userArray){
  159. if(userArray[userPosition].loginToken = true){
  160. System.out.println("Your balance is: $" + (userArray[userPosition].balance));
  161. }
  162. if(userArray[userPosition].loginToken = false){
  163. System.out.println("You need to login.");
  164. }
  165. }
  166.  
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement