Advertisement
hRygold

Untitled

Feb 2nd, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. package loginsystem;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. /**
  8. *
  9. * @author sebastian
  10. */
  11. public class Customer {
  12.  
  13.  
  14.  
  15. private ArrayList<Account> accountList;
  16. ArrayList<Account>userList;
  17. Scanner input = new Scanner(System.in);
  18. boolean exit = false;
  19. LogInSystem view;
  20.  
  21. Customer(LogInSystem log) {
  22. this.accountList = new ArrayList();
  23. this.view = log;
  24. this.userList = new ArrayList();
  25. while(!exit){
  26. view.printLoggedOutMenu();
  27. int choice = input.nextInt();
  28. performAction(choice);
  29.  
  30. }
  31.  
  32.  
  33. }
  34.  
  35.  
  36.  
  37. private void performAction(int choice) {
  38. switch(choice){
  39. case 1:
  40. logInScreen();
  41. break;
  42. case 2:
  43. createUser();
  44. break;
  45. case 0:
  46. System.exit(0);
  47. break;
  48. default:
  49. System.out.println("Ett okänt fel inträffade.");
  50. }
  51. }
  52.  
  53. private void logInScreen() {
  54.  
  55. view.printUsernameQuestion();
  56. String username = input.next();
  57. view.printPasswordQuestion();
  58. String password = input.next();
  59. if(getUserAccountFromAccountList(username,password)==null){
  60. System.out.println("Inloggningen misslyckades");
  61. return;
  62. }
  63. getUserAccountFromAccountList(username,password);
  64. while(!exit){
  65. view.printLoggedInMenu();
  66. int choice = input.nextInt();
  67. performActionTwo(choice);
  68. }
  69. }
  70.  
  71. private void createUser() {
  72.  
  73. System.out.println("Personnummer: ");
  74. String ssn = input.next();
  75. System.out.println("Välj lösenord");
  76. String password = input.next();
  77. if(getSsnFromAccountList(ssn)!= null){
  78. System.out.println("Det finns redan en användare med det personnummret");
  79. }
  80. Account a = new Account(ssn,password);
  81. addUserAccount(a);
  82. System.out.println("Du har skapat en ny användare.");
  83. System.out.println("Personnummer: "+a.getSsn());
  84. System.out.println("Lösenord: "+a.getPassword());
  85.  
  86. }
  87.  
  88. public void addUserAccount(Account a){
  89. this.userList.add(a);
  90. }
  91. private Account getSsnFromAccountList(String ssn){
  92. for(Account a : userList){
  93. if(a.getSsn().equals(ssn)){
  94. return a;
  95. }
  96. }
  97.  
  98. return null;
  99. }
  100.  
  101.  
  102.  
  103. private Account getUserAccountFromAccountList(String ssn,String password){
  104. for(Account a: userList){
  105. if(a.getSsn().equals(ssn)&&a.getPassword().equals(password)){
  106. return a;
  107. }
  108. }
  109. return null;
  110. }
  111.  
  112.  
  113. public ArrayList<Account> getAccountList() {
  114. return accountList;
  115. }
  116.  
  117.  
  118. public void setAccountList(ArrayList<Account> accountList) {
  119. this.accountList = accountList;
  120. }
  121.  
  122. private void performActionTwo(int choice) {
  123. switch(choice){
  124. case 1:
  125. accountDetails();
  126. break;
  127. case 2:
  128. deposit();
  129. break;
  130. case 3:
  131. withdraw();
  132. break;
  133. case 4:
  134. createNewAccount();
  135. break;
  136. case 0:
  137. System.exit(0);
  138. break;
  139. default:
  140. System.out.println("Okänt fel inträffade");
  141. }
  142. }
  143.  
  144. private void accountDetails() {
  145. System.out.println("Kontoinformation");
  146. System.out.println("Kontonummer: Saldo:");
  147. System.out.println("---------------------------------------");
  148. for(int i = 0;i < accountList.size();i++){
  149.  
  150. System.out.printf("%1d %19d\n",accountList.get(i).getAccountNumber(),accountList.get(i).getBalance(),"\n");
  151. System.out.println("-----------------------------------");
  152. }
  153. }
  154.  
  155. private void deposit() {
  156. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  157. }
  158.  
  159. private void withdraw() {
  160. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  161. }
  162.  
  163. private void createNewAccount() {
  164. int accountNumber = 0;
  165. System.out.println("Hur mycket vill du sätta in på kontot?");
  166. int deposit = input.nextInt();
  167. Account a = new Account();
  168. accountNumber = createAccountNumber(accountNumber);
  169. a.setBalance(deposit);
  170. a.setAccountNumber(accountNumber);
  171. addNewAccount(a);
  172. System.out.println("Du har skapat ett nytt bankkonto!");
  173. System.out.println("Kontonummer: "+accountNumber);
  174. System.out.println("Saldo: "+a.getBalance());
  175.  
  176. }
  177. private int createAccountNumber(int accountNumber){
  178.  
  179. Random random = new Random();
  180. accountNumber = random.nextInt(9999+1-1000)+1000;
  181. return accountNumber;
  182. }
  183. private void addNewAccount(Account account){
  184. this.accountList.add(account);
  185. }
  186.  
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement