Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. package one;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class JavaBikes1 {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9.  
  10. System.out.println("WELCOME");
  11.  
  12. Scanner input = new Scanner(System.in);
  13.  
  14. // Prompts user's names
  15. System.out.print("Enter your first and last name: ");
  16. String name = input.nextLine();
  17.  
  18. // Prompts post code
  19. System.out.print("Enter your postal code: ");
  20. int postCode = input.nextInt();
  21. while (postCode > 2501 || postCode < 1000) {
  22. System.out.println("Please enter a valid 4 digit postal code for the Copenhagen area: ");
  23. postCode = input.nextInt();
  24. }
  25.  
  26. // Prompts phone number
  27. System.out.print("Enter your phone number: ");
  28. int phoneNumber = input.nextInt();
  29. String phoneNumber1 = Integer.toString(phoneNumber);
  30. int length = phoneNumber1.length();
  31. while (length != 8) {
  32. System.out.println("Please enter a valid 8 digit phone number: ");
  33. phoneNumber = input.nextInt();
  34. }
  35.  
  36. // Prompts date of birth
  37. System.out.print("Enter your date of birth (i.e. dd-mm-yyyy): ");
  38. String dateOfBirth = input.nextLine();
  39. int dobLenght = dateOfBirth.length();
  40. input.nextLine();
  41. while (dobLenght != 9) {
  42. System.out.println("Please enter a valid date of birth in the format mm-dd-yyyy: ");
  43. dateOfBirth = input.nextLine();
  44. }
  45.  
  46. // Prompts CPR
  47. System.out.print("Enter your CPR (i.e. XXXXXX-XXXX): ");
  48. String cpr = input.nextLine();
  49. int cprLenght = cpr.length();
  50. while (cprLenght < 11 || cprLenght < 10) {
  51. System.out.println("Please enter a valid CPR in the format XXXXXX-XXXX: ");
  52. cpr = input.nextLine();
  53. }
  54.  
  55. // Creates and displays username and password
  56. String username = createUsername(name);
  57. String password = createPassword(name, cpr);
  58.  
  59. System.out.println("Your username is \"" + username + "\" and your password is \"" + password + "\"");
  60.  
  61. System.out.print("Your profile is stored. Press 1 for exit and 2 for login: ");
  62. int menu = input.nextInt();
  63.  
  64. switch (menu) {
  65. case 1:
  66. System.exit(0);
  67. break;
  68. case 2:
  69. System.out.println("You chose too login");
  70. break;
  71. }
  72.  
  73. int count1 = 1;
  74. int count2 = 0;
  75.  
  76. // Returns to the top when bottom is reached
  77. while (true) {
  78.  
  79. if (true) {
  80. System.out.println("Enter your username: ");
  81. String usernameAttempt = input.nextLine();
  82. input.nextLine();
  83. System.out.println("Enter your password: ");
  84. String passwordAttempt = input.nextLine();
  85.  
  86. while (usernameAttempt != username || passwordAttempt != password) {
  87. count2++;
  88. int triesLeft = 3 - count2;
  89. System.out.println("Wrong username or password. " + triesLeft + " tries left.");
  90. System.out.println("Enter your username: ");
  91. usernameAttempt = input.nextLine();
  92. System.out.print("Enter your password: ");
  93. passwordAttempt = input.nextLine();
  94. if (count2 == 3) {
  95. break;
  96. }
  97. }
  98. }
  99.  
  100. // Ends loop at three failed login attempts
  101. if (count2 >= 3) {
  102. System.out.println("Too many login attempts. Try again later.");
  103. break;
  104. } else if (count2 <= 3) {
  105. System.out.println("You are now logged in!");
  106. }
  107.  
  108. count1++;
  109.  
  110. }
  111. }
  112.  
  113. // Creates method for generating username (initials)
  114. public static String createUsername(String name) {
  115. String[] parts = name.split(" ");
  116. String firstName = parts[0];
  117. String lastName = parts[1];
  118. char a = firstName.charAt(0);
  119. char b = lastName.charAt(0);
  120. char c = lastName.charAt(1);
  121. char d = lastName.charAt(2);
  122. String username = Character.toString(a) + Character.toString(b) + Character.toString(c) + Character.toString(d);
  123. return username;
  124. }
  125.  
  126. // Crates method for generating password (first three last name chars + last
  127. // four cpr digits)
  128. public static String createPassword(String name, String cpr) {
  129. String[] parts = name.split(" ");
  130. String firstName = parts[0];
  131. String lastName = parts[1];
  132. char a = lastName.charAt(0);
  133. char b = lastName.charAt(1);
  134. char c = lastName.charAt(2);
  135. char d = cpr.charAt(7);
  136. char e = cpr.charAt(8);
  137. char f = cpr.charAt(9);
  138. char g = cpr.charAt(10);
  139.  
  140. String password = Character.toString(a) + Character.toString(b) + Character.toString(c) + Character.toString(d)
  141. + Character.toString(e) + Character.toString(f) + Character.toString(g);
  142. return password;
  143. }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement