Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.BufferedWriter;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Guerra {
- private static final String FILENAME = "db.txt";
- public static void main(String args[]){
- //Variables supplied by the user
- String firstName;
- String lastName;
- String address;
- String dob;
- String phoneNumber;
- String cprNumber;
- //Variables the program will generate based on user input.
- //They will then be stored in the database along with all user data.
- String username;
- //first 3 letters of last name
- //last 4 digits of the cpr number
- String password;
- //this boolean will allow us to stop normal program execution
- //in order to make certain format checks on user-supplied input.
- boolean validityCheck = true;
- //Number of tries the user has to log in should he or she choose this option.
- int validityCounter = 2;
- //A string which will be used to guide the switch statement.
- //The user can either log in, create an account, or exit.
- //The input string is formatted into caps, following conventions.
- String decision = "";
- Scanner input = new Scanner(System.in);
- //***************************************
- //Program Start Here
- System.out.println("Welcome to Java Bikes!");
- System.out.println("-----------*-----------");
- System.out.println("----------***----------");
- System.out.println("---------*****---------");
- System.out.println("--------*******--------");
- System.out.println("-------*********-------");
- System.out.println("-----------------------");
- System.out.println("Please enter one of the following options using your keyboard(login, create account, exit)");
- decision = input.nextLine().toUpperCase();
- decision = decision.replaceAll("\\s", "");
- switch (decision) {
- case "LOGIN":
- System.out.println("LOGIN");
- System.out.println("-----------------------");
- System.out.println("-----------------------");
- System.out.println("-----------------------");
- //The username the user inputs into the keyboard
- String enteredUserName = "";
- //This variable will hold the line from our text file if that line contains the username.
- String userDetails = "";
- //This variable will contain the "password" part of the userDetails line. We will use it to compare against what the user entered.
- String correctPassword = "";
- //The password the user inputs into the keyboard. We will compare this against the enteredPassword variable.
- String enteredPassword = "";
- //we set up our file reading variables
- //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.
- validityCheck = true;
- FileReader fr = null;
- BufferedReader br = null;
- while(validityCheck){
- System.out.println("Please enter your username");
- //We do this in upper case to compare against what is in the file, which is in upper case
- //Design consideration: I decided to NOT make the username case sensitive.
- enteredUserName = input.nextLine().toUpperCase();
- //This is where we open our text file database to extract the required information.
- //Step 1: Open the file and begin reading line by line
- //Step 2: If the entered username is found in a particular file line, store that entire line in the userDetails variable as a string
- //Step 3: Break out of the while loop.
- try {
- String currentLine = "";
- fr = new FileReader(FILENAME);
- br = new BufferedReader(fr);
- while((currentLine = br.readLine()) != null){
- if(currentLine.indexOf(enteredUserName) != -1){
- userDetails = currentLine;
- //found the user. we exit the loop.
- break;
- }
- }
- } catch(IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(br != null){
- br.close();
- }
- if(fr != null){
- fr.close();
- }
- } catch (IOException ex){
- ex.printStackTrace();
- }
- }
- //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.
- //Thus, we maintain the correctPassword variable empty.
- if(userDetails.length() > 0){
- correctPassword = userDetails.split("\\s")[1];
- } else {
- correctPassword = "";
- }
- System.out.println("Please enter your password");
- enteredPassword = input.nextLine();
- // Step 1: Compare the correctPassword and the enteredPassword for equality
- // Step 2: Ensure the correctPassword variable is of length more than 6.
- // This is because we know that our auto-generated passwords are of 7 characters
- // Step 3: We only allow a succesfull login if the conditions in Step 1 and Step 2 check out.
- // Step 4: Otherwise, we subtract 1 from the validityCounter and allow the user to try inputting his/her credentials again.
- // Step 5: If the user fails to correctly input his/her details, and the validityCounter is -1, the application exits for expired tries.
- if(correctPassword.equals(enteredPassword) && correctPassword.length() > 6){
- System.out.println("You are now logged in...");
- validityCheck = false;
- } else if(validityCounter > -1){
- System.out.println("You pressed wrong password and/or username");
- validityCounter--;
- } else {
- System.out.println("Sorry. You have exceeded the number of tries. Please try again after a few hours");
- validityCheck = false;
- }
- }
- break;
- case "CREATEACCOUNT":
- System.out.println("CREATE ACCOUNT");
- System.out.println("-----------------------");
- System.out.println("-----------------------");
- System.out.println("-----------------------");
- System.out.println("Please enter your first name");
- firstName = input.nextLine().toUpperCase();
- System.out.println("Please enter your last name");
- lastName = input.nextLine().toUpperCase();
- // ZIP CODE
- //Step 1: Validate if the zip code supplied is 4 digits and below 2500.
- //Step 2. Validate the zip code only contains numbers and not letters or special characters
- System.out.println("Please enter your zipcode");
- address = input.nextLine();
- while(validityCheck){
- if(Integer.parseInt(address) > 2500 || Integer.parseInt(address) < 1000){
- System.out.println("Your address must be located in Copenhagen. Try again");
- address = input.nextLine();
- } else if(address.matches(".*[a-zA-Z].*")) {
- System.out.println("Your address must not contain letters. Try again");
- address = input.nextLine();
- } else if (address.matches(".*[!@#$%^&*()_~].*")) {
- System.out.println("Your address must not contain special characters. Try again");
- address = input.nextLine();
- } else {
- validityCheck = false;
- }
- }
- // DATE OF BIRTH
- //Step 1: Validate if the date of birth is of length 8.
- //Step 2. Validate the input string contains only numbers.
- //Step 3. Validate the last 4 digits correspond to a year after 1900.
- System.out.println("Please enter your date of birth in the format ddmmyyyy");
- dob = input.nextLine();
- validityCheck = true;
- while(validityCheck){
- if(dob.length() > 8 || dob.length() < 8){
- System.out.println("Your DOB must be exactly 9 characters. Try again");
- dob = input.nextLine();
- } else if (dob.matches(".*[a-zA-Z].*")){
- System.out.println("Your DOB must not contain letters. Try again.");
- dob = input.nextLine();
- } else if(dob.matches(".*[!@#$%^&*()_~].*")){
- System.out.println("Your DOB must not contain illegal characters. Try again.");
- dob = input.nextLine();
- } else if (Integer.parseInt(dob.substring(4)) < 1900 ) {
- System.out.println("Your DOB must have a year after 1900 to be valid. Try again");
- System.out.println(Integer.parseInt(dob.substring(4)));
- dob = input.nextLine();
- } else {
- validityCheck = false;
- }
- }
- //PHONE NUMBER
- //Step 1: Validate the phone number is of length 8.
- //Step 2. Validate the input string contains only numbers.
- //Step 3. Confirm the phone number has no special characters or letters.
- System.out.println("Please enter your phone number in the format XXXXXXXX");
- phoneNumber = input.nextLine();
- validityCheck = true;
- while(validityCheck){
- if(phoneNumber.length() < 8 || phoneNumber.length() > 8){
- System.out.println("Your phone number has to be exactly 8 digits");
- phoneNumber = input.nextLine();
- } else if(phoneNumber.matches(".*[a-zA-Z].*")){
- System.out.println("Your phone number cannot contain letters. Please try again.");
- phoneNumber = input.nextLine();
- } else if(phoneNumber.matches(".*[!@#$%^&*()_~].*")) {
- System.out.println("Your phone number cannot contain illegal characters. Please try again.");
- phoneNumber = input.nextLine();
- } else {
- validityCheck = false;
- }
- }
- //CPR NUMBER
- //Step 1: Validate the user input contains the -.
- //Step 2. Validate the input string contains only numbers.
- //Step 3. Confirm the phone number has no special characters or letters.
- //Step 4. Validate the length of the CPR number is exactly 11 including the -
- //Step 5. We ensure the - is located just before the last 4 numbers.
- System.out.println("Please enter your CPR number in the format XXXXXX-XXXX");
- cprNumber = input.nextLine();
- validityCheck = true;
- while(validityCheck){
- if(!cprNumber.matches(".*[-].*")){
- System.out.println("Your CPR number must contain an -");
- cprNumber = input.nextLine();
- } else if(cprNumber.matches(".*[a-zA-Z].*")){
- System.out.println("Your CPR number cannot contain any letters");
- cprNumber = input.nextLine();
- } else if(cprNumber.length() > 11 || cprNumber.length() < 11){
- System.out.println("Your CPR number must contain exactly 11 characters. This includes the -");
- cprNumber = input.nextLine();
- } else if(cprNumber.indexOf("-") != 6){
- System.out.println("the - in your CPR must precede the last 4 digits of your CPR");
- cprNumber = input.nextLine();
- } else if(cprNumber.matches(".*[!@#$%^&*()_~].*")) {
- System.out.println("Your CPR number cannot contain illegal characters. Please try again.");
- cprNumber = input.nextLine();
- } else {
- validityCheck = false;
- }
- }
- //PROGRAMMERS JUDGMENT CALL
- // I am assuming some people have last names with less than 3 characters.
- // I have thus implemented the following conditionals to cover this case.
- // This way usernames and passwords can be created for everyone.
- if(lastName.length() > 3) {
- username = firstName.charAt(0) + lastName.substring(0, 3);
- password = lastName.substring(0,3) + cprNumber.substring(7);
- } else {
- username = firstName.charAt(0) + lastName;
- password = lastName + cprNumber.substring(7);
- }
- //WRITING TO OUR DATABASE
- //For the purpose of this exercise, I decided to use a text file as my "database"
- //When the user creation process is completed, all information is recorded on a single line in a file, separated by whitespace.
- //The login functionality will read from this file.
- try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME, true))){
- String content = username + " " + password + " " + firstName + " " + lastName + " " + address + " " + dob + " " + phoneNumber + " " + cprNumber + "\n";
- bw.write(content);
- bw.close();
- System.out.println("New user has been created");
- } catch (IOException e){
- e.printStackTrace();
- }
- break;
- case "EXIT":
- System.out.println("Bye Bye!");
- break;
- default:
- System.out.println("That is not a viable option");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement