Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package one;
- import java.util.Scanner;
- public class JavaBikes1 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println("WELCOME");
- Scanner input = new Scanner(System.in);
- // Prompts user's names
- System.out.print("Enter your first and last name: ");
- String name = input.nextLine();
- // Prompts post code
- System.out.print("Enter your postal code: ");
- int postCode = input.nextInt();
- while (postCode > 2501 || postCode < 1000) {
- System.out.println("Please enter a valid 4 digit postal code for the Copenhagen area: ");
- postCode = input.nextInt();
- }
- // Prompts phone number
- System.out.print("Enter your phone number: ");
- int phoneNumber = input.nextInt();
- String phoneNumber1 = Integer.toString(phoneNumber);
- int length = phoneNumber1.length();
- while (length != 8) {
- System.out.println("Please enter a valid 8 digit phone number: ");
- phoneNumber = input.nextInt();
- }
- // Prompts date of birth
- System.out.print("Enter your date of birth (i.e. dd-mm-yyyy): ");
- String dateOfBirth = input.nextLine();
- int dobLenght = dateOfBirth.length();
- input.nextLine();
- while (dobLenght != 9) {
- System.out.println("Please enter a valid date of birth in the format mm-dd-yyyy: ");
- dateOfBirth = input.nextLine();
- }
- // Prompts CPR
- System.out.print("Enter your CPR (i.e. XXXXXX-XXXX): ");
- String cpr = input.nextLine();
- int cprLenght = cpr.length();
- while (cprLenght < 11 || cprLenght < 10) {
- System.out.println("Please enter a valid CPR in the format XXXXXX-XXXX: ");
- cpr = input.nextLine();
- }
- // Creates and displays username and password
- String username = createUsername(name);
- String password = createPassword(name, cpr);
- System.out.println("Your username is \"" + username + "\" and your password is \"" + password + "\"");
- System.out.print("Your profile is stored. Press 1 for exit and 2 for login: ");
- int menu = input.nextInt();
- switch (menu) {
- case 1:
- System.exit(0);
- break;
- case 2:
- System.out.println("You chose too login");
- break;
- }
- int count1 = 1;
- int count2 = 0;
- // Returns to the top when bottom is reached
- while (true) {
- if (true) {
- System.out.println("Enter your username: ");
- String usernameAttempt = input.nextLine();
- input.nextLine();
- System.out.println("Enter your password: ");
- String passwordAttempt = input.nextLine();
- while (usernameAttempt != username || passwordAttempt != password) {
- count2++;
- int triesLeft = 3 - count2;
- System.out.println("Wrong username or password. " + triesLeft + " tries left.");
- System.out.println("Enter your username: ");
- usernameAttempt = input.nextLine();
- System.out.print("Enter your password: ");
- passwordAttempt = input.nextLine();
- if (count2 == 3) {
- break;
- }
- }
- }
- // Ends loop at three failed login attempts
- if (count2 >= 3) {
- System.out.println("Too many login attempts. Try again later.");
- break;
- } else if (count2 <= 3) {
- System.out.println("You are now logged in!");
- }
- count1++;
- }
- }
- // Creates method for generating username (initials)
- public static String createUsername(String name) {
- String[] parts = name.split(" ");
- String firstName = parts[0];
- String lastName = parts[1];
- char a = firstName.charAt(0);
- char b = lastName.charAt(0);
- char c = lastName.charAt(1);
- char d = lastName.charAt(2);
- String username = Character.toString(a) + Character.toString(b) + Character.toString(c) + Character.toString(d);
- return username;
- }
- // Crates method for generating password (first three last name chars + last
- // four cpr digits)
- public static String createPassword(String name, String cpr) {
- String[] parts = name.split(" ");
- String firstName = parts[0];
- String lastName = parts[1];
- char a = lastName.charAt(0);
- char b = lastName.charAt(1);
- char c = lastName.charAt(2);
- char d = cpr.charAt(7);
- char e = cpr.charAt(8);
- char f = cpr.charAt(9);
- char g = cpr.charAt(10);
- String password = Character.toString(a) + Character.toString(b) + Character.toString(c) + Character.toString(d)
- + Character.toString(e) + Character.toString(f) + Character.toString(g);
- return password;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement