Advertisement
Guest User

Untitled

a guest
Sep 10th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.05 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.BufferedWriter;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7.  
  8. public class Guerra {
  9.  
  10. private static final String FILENAME = "db.txt";
  11. public static void main(String args[]){
  12.  
  13. //Variables supplied by the user
  14. String firstName;
  15. String lastName;
  16. String address;
  17. String dob;
  18. String phoneNumber;
  19. String cprNumber;
  20.  
  21. //Variables the program will generate based on user input.
  22. //They will then be stored in the database along with all user data.
  23. String username;
  24. //first 3 letters of last name
  25. //last 4 digits of the cpr number
  26. String password;
  27. //this boolean will allow us to stop normal program execution
  28. //in order to make certain format checks on user-supplied input.
  29. boolean validityCheck = true;
  30. //Number of tries the user has to log in should he or she choose this option.
  31. int validityCounter = 2;
  32. //A string which will be used to guide the switch statement.
  33. //The user can either log in, create an account, or exit.
  34. //The input string is formatted into caps, following conventions.
  35. String decision = "";
  36.  
  37.  
  38. Scanner input = new Scanner(System.in);
  39. //***************************************
  40. //Program Start Here
  41. System.out.println("Welcome to Java Bikes!");
  42. System.out.println("-----------*-----------");
  43. System.out.println("----------***----------");
  44. System.out.println("---------*****---------");
  45. System.out.println("--------*******--------");
  46. System.out.println("-------*********-------");
  47. System.out.println("-----------------------");
  48. System.out.println("Please enter one of the following options using your keyboard(login, create account, exit)");
  49. decision = input.nextLine().toUpperCase();
  50. decision = decision.replaceAll("\\s", "");
  51.  
  52. switch (decision) {
  53. case "LOGIN":
  54. System.out.println("LOGIN");
  55. System.out.println("-----------------------");
  56. System.out.println("-----------------------");
  57. System.out.println("-----------------------");
  58.  
  59. //The username the user inputs into the keyboard
  60. String enteredUserName = "";
  61. //This variable will hold the line from our text file if that line contains the username.
  62. String userDetails = "";
  63. //This variable will contain the "password" part of the userDetails line. We will use it to compare against what the user entered.
  64. String correctPassword = "";
  65. //The password the user inputs into the keyboard. We will compare this against the enteredPassword variable.
  66. String enteredPassword = "";
  67.  
  68. //we set up our file reading variables
  69. //we also set validityCheck to true. This will ensure that the login loop only breaks if the user has succesfully logged in within 3 tries.
  70. validityCheck = true;
  71. FileReader fr = null;
  72. BufferedReader br = null;
  73.  
  74. while(validityCheck){
  75. System.out.println("Please enter your username");
  76.  
  77. //We do this in upper case to compare against what is in the file, which is in upper case
  78. //Design consideration: I decided to NOT make the username case sensitive.
  79. enteredUserName = input.nextLine().toUpperCase();
  80.  
  81. //This is where we open our text file database to extract the required information.
  82. //Step 1: Open the file and begin reading line by line
  83. //Step 2: If the entered username is found in a particular file line, store that entire line in the userDetails variable as a string
  84. //Step 3: Break out of the while loop.
  85.  
  86. try {
  87. String currentLine = "";
  88. fr = new FileReader(FILENAME);
  89. br = new BufferedReader(fr);
  90. while((currentLine = br.readLine()) != null){
  91. if(currentLine.indexOf(enteredUserName) != -1){
  92. userDetails = currentLine;
  93. //found the user. we exit the loop.
  94. break;
  95. }
  96. }
  97. } catch(IOException e) {
  98. e.printStackTrace();
  99. } finally {
  100. try {
  101. if(br != null){
  102. br.close();
  103. }
  104. if(fr != null){
  105. fr.close();
  106. }
  107. } catch (IOException ex){
  108. ex.printStackTrace();
  109. }
  110. }
  111.  
  112. //If the userDetails string remains empty after we've combed the file line by line, we assume the username is either incorrect or does not exist.
  113. //Thus, we maintain the correctPassword variable empty.
  114.  
  115. if(userDetails.length() > 0){
  116. correctPassword = userDetails.split("\\s")[1];
  117. } else {
  118. correctPassword = "";
  119. }
  120.  
  121. System.out.println("Please enter your password");
  122. enteredPassword = input.nextLine();
  123.  
  124. // Step 1: Compare the correctPassword and the enteredPassword for equality
  125. // Step 2: Ensure the correctPassword variable is of length more than 6.
  126. // This is because we know that our auto-generated passwords are of 7 characters
  127. // Step 3: We only allow a succesfull login if the conditions in Step 1 and Step 2 check out.
  128. // Step 4: Otherwise, we subtract 1 from the validityCounter and allow the user to try inputting his/her credentials again.
  129. // Step 5: If the user fails to correctly input his/her details, and the validityCounter is -1, the application exits for expired tries.
  130.  
  131. if(correctPassword.equals(enteredPassword) && correctPassword.length() > 6){
  132. System.out.println("You are now logged in...");
  133. validityCheck = false;
  134. } else if(validityCounter > -1){
  135. System.out.println("You pressed wrong password and/or username");
  136. validityCounter--;
  137. } else {
  138. System.out.println("Sorry. You have exceeded the number of tries. Please try again after a few hours");
  139. validityCheck = false;
  140. }
  141. }
  142.  
  143. break;
  144.  
  145. case "CREATEACCOUNT":
  146. System.out.println("CREATE ACCOUNT");
  147. System.out.println("-----------------------");
  148. System.out.println("-----------------------");
  149. System.out.println("-----------------------");
  150.  
  151.  
  152. System.out.println("Please enter your first name");
  153. firstName = input.nextLine().toUpperCase();
  154.  
  155. System.out.println("Please enter your last name");
  156. lastName = input.nextLine().toUpperCase();
  157.  
  158. // ZIP CODE
  159. //Step 1: Validate if the zip code supplied is 4 digits and below 2500.
  160. //Step 2. Validate the zip code only contains numbers and not letters or special characters
  161. System.out.println("Please enter your zipcode");
  162. address = input.nextLine();
  163. while(validityCheck){
  164. if(Integer.parseInt(address) > 2500 || Integer.parseInt(address) < 1000){
  165. System.out.println("Your address must be located in Copenhagen. Try again");
  166. address = input.nextLine();
  167. } else if(address.matches(".*[a-zA-Z].*")) {
  168. System.out.println("Your address must not contain letters. Try again");
  169. address = input.nextLine();
  170. } else if (address.matches(".*[!@#$%^&*()_~].*")) {
  171. System.out.println("Your address must not contain special characters. Try again");
  172. address = input.nextLine();
  173. } else {
  174. validityCheck = false;
  175. }
  176. }
  177.  
  178. // DATE OF BIRTH
  179. //Step 1: Validate if the date of birth is of length 8.
  180. //Step 2. Validate the input string contains only numbers.
  181. //Step 3. Validate the last 4 digits correspond to a year after 1900.
  182. System.out.println("Please enter your date of birth in the format ddmmyyyy");
  183. dob = input.nextLine();
  184. validityCheck = true;
  185. while(validityCheck){
  186. if(dob.length() > 8 || dob.length() < 8){
  187. System.out.println("Your DOB must be exactly 9 characters. Try again");
  188. dob = input.nextLine();
  189. } else if (dob.matches(".*[a-zA-Z].*")){
  190. System.out.println("Your DOB must not contain letters. Try again.");
  191. dob = input.nextLine();
  192. } else if(dob.matches(".*[!@#$%^&*()_~].*")){
  193. System.out.println("Your DOB must not contain illegal characters. Try again.");
  194. dob = input.nextLine();
  195. } else if (Integer.parseInt(dob.substring(4)) < 1900 ) {
  196. System.out.println("Your DOB must have a year after 1900 to be valid. Try again");
  197. System.out.println(Integer.parseInt(dob.substring(4)));
  198. dob = input.nextLine();
  199. } else {
  200. validityCheck = false;
  201. }
  202. }
  203.  
  204. //PHONE NUMBER
  205. //Step 1: Validate the phone number is of length 8.
  206. //Step 2. Validate the input string contains only numbers.
  207. //Step 3. Confirm the phone number has no special characters or letters.
  208. System.out.println("Please enter your phone number in the format XXXXXXXX");
  209. phoneNumber = input.nextLine();
  210. validityCheck = true;
  211. while(validityCheck){
  212. if(phoneNumber.length() < 8 || phoneNumber.length() > 8){
  213. System.out.println("Your phone number has to be exactly 8 digits");
  214. phoneNumber = input.nextLine();
  215. } else if(phoneNumber.matches(".*[a-zA-Z].*")){
  216. System.out.println("Your phone number cannot contain letters. Please try again.");
  217. phoneNumber = input.nextLine();
  218. } else if(phoneNumber.matches(".*[!@#$%^&*()_~].*")) {
  219. System.out.println("Your phone number cannot contain illegal characters. Please try again.");
  220. phoneNumber = input.nextLine();
  221. } else {
  222. validityCheck = false;
  223. }
  224. }
  225.  
  226. //CPR NUMBER
  227. //Step 1: Validate the user input contains the -.
  228. //Step 2. Validate the input string contains only numbers.
  229. //Step 3. Confirm the phone number has no special characters or letters.
  230. //Step 4. Validate the length of the CPR number is exactly 11 including the -
  231. //Step 5. We ensure the - is located just before the last 4 numbers.
  232. System.out.println("Please enter your CPR number in the format XXXXXX-XXXX");
  233. cprNumber = input.nextLine();
  234. validityCheck = true;
  235. while(validityCheck){
  236. if(!cprNumber.matches(".*[-].*")){
  237. System.out.println("Your CPR number must contain an -");
  238. cprNumber = input.nextLine();
  239. } else if(cprNumber.matches(".*[a-zA-Z].*")){
  240. System.out.println("Your CPR number cannot contain any letters");
  241. cprNumber = input.nextLine();
  242.  
  243. } else if(cprNumber.length() > 11 || cprNumber.length() < 11){
  244. System.out.println("Your CPR number must contain exactly 11 characters. This includes the -");
  245. cprNumber = input.nextLine();
  246.  
  247. } else if(cprNumber.indexOf("-") != 6){
  248. System.out.println("the - in your CPR must precede the last 4 digits of your CPR");
  249. cprNumber = input.nextLine();
  250. } else if(cprNumber.matches(".*[!@#$%^&*()_~].*")) {
  251. System.out.println("Your CPR number cannot contain illegal characters. Please try again.");
  252. cprNumber = input.nextLine();
  253. } else {
  254. validityCheck = false;
  255. }
  256. }
  257.  
  258. //PROGRAMMERS JUDGMENT CALL
  259. // I am assuming some people have last names with less than 3 characters.
  260. // I have thus implemented the following conditionals to cover this case.
  261. // This way usernames and passwords can be created for everyone.
  262.  
  263. if(lastName.length() > 3) {
  264. username = firstName.charAt(0) + lastName.substring(0, 3);
  265. password = lastName.substring(0,3) + cprNumber.substring(7);
  266. } else {
  267. username = firstName.charAt(0) + lastName;
  268. password = lastName + cprNumber.substring(7);
  269. }
  270.  
  271. //WRITING TO OUR DATABASE
  272. //For the purpose of this exercise, I decided to use a text file as my "database"
  273. //When the user creation process is completed, all information is recorded on a single line in a file, separated by whitespace.
  274. //The login functionality will read from this file.
  275. try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME, true))){
  276.  
  277. String content = username + " " + password + " " + firstName + " " + lastName + " " + address + " " + dob + " " + phoneNumber + " " + cprNumber + "\n";
  278.  
  279. bw.write(content);
  280. bw.close();
  281. System.out.println("New user has been created");
  282. } catch (IOException e){
  283. e.printStackTrace();
  284. }
  285.  
  286. break;
  287. case "EXIT":
  288. System.out.println("Bye Bye!");
  289. break;
  290. default:
  291. System.out.println("That is not a viable option");
  292. break;
  293. }
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement