Advertisement
Guest User

Untitled

a guest
Oct 8th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.68 KB | None | 0 0
  1. package company;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Map.Entry;
  12.  
  13. import validation.ConsoleValidator;
  14. import validation.Validator;
  15.  
  16. import java.util.Scanner;
  17.  
  18.  
  19. public class Login {
  20.     public static Map<String, String> users = new HashMap<>();
  21.     public static ArrayList<Customer> customerList = new ArrayList<>();
  22.     public static ArrayList<HashMap<String, String>> loginDetailsList = new ArrayList<>();
  23.     Scanner sc = new Scanner(System.in);
  24.     int numCustomers = 0;
  25.     Validator v = new ConsoleValidator();
  26.  
  27.     /**
  28.      * The function below asks for the username and password of the user it will
  29.      * return the appropriate login menu depending on the login of the user
  30.      * additionally if the "quit" command is inputed the while loop will exit
  31.      * and the user will be returned to the main menu
  32.      */
  33.     public ArrayList<Customer> getCustomerList(){
  34.         return customerList;
  35.     }
  36.     public String login() {
  37.         while (true) {
  38.             lineBreak();
  39.             System.out.println("Type quit to return to main menu");
  40.             System.out.println("Please enter your username: ");
  41.             String username = sc.nextLine();
  42.  
  43.             if (username.equals("quit"))
  44.                 return "quit";
  45.             lineBreak();
  46.             System.out.println("Please enter your password: ");
  47.             String password = sc.nextLine();
  48.  
  49.             if (password.equals("quit"))
  50.                 return "quit";
  51.             if (isOwner(username, password))
  52.                 return "owner";
  53.             if (isUser(username, password)) {
  54.                 return "customer";
  55.             } else
  56.                 System.out.println("Invalid login details");
  57.             lineBreak();
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * The register function is used when the user selects option 2 register it
  63.      * analyzes the user name and password checking if the login details are
  64.      * already registered as well as ensuring that the entered username or
  65.      * password contain no spaces e.g they are only one word if the user
  66.      * successfully registers then they are created using the HashMap as a
  67.      * customer and their login details are stored
  68.      */
  69.     public void register() {
  70.  
  71.         String name = registerName();
  72.         if(name == null)
  73.             return;
  74.         String username = registerUsername();
  75.         if(username == null)   
  76.             return;
  77.         String password = registerPassword();
  78.         if(password == null)
  79.             return;
  80.  
  81.        
  82.         writeUsernameAndPasswordToFile(username, password, name);
  83.  
  84.         HashMap<String, String> hm = new HashMap<String, String>();
  85.         hm.put(username, password);
  86.         loginDetailsList.add(hm);
  87.         customerList.add(new Customer(loginDetailsList.get(numCustomers), null, false));
  88.         numCustomers++;
  89.         return;
  90.  
  91.     }
  92.  
  93.     /**
  94.      * As suggested by the name of this function it is used to write the
  95.      * username and password to the customerinfo.txt file as a string,
  96.      * additionally it is specified to write the username and password on one
  97.      * line with a space in between and then move to the following line for the
  98.      * next input
  99.      */
  100.     public void writeUsernameAndPasswordToFile(String username, String password, String name) {
  101.         String fileName = "customerinfo.txt";
  102.         try {
  103.             FileWriter fw = new FileWriter(fileName, true);
  104.             BufferedWriter bw = new BufferedWriter(fw);
  105.             bw.write(username + " " + password + " " + name);
  106.             bw.newLine();
  107.             bw.close();
  108.             lineBreak();
  109.             System.out.println("Successfully registered!");
  110.         } catch (IOException ex) {
  111.             System.out.println("Error writing to file '" + fileName + "'");
  112.         }
  113.         createUsersFromFile();
  114.     }
  115.  
  116.     /**
  117.      * This function is used to authenticate the user input with the stored key
  118.      * value if the user does not exist it will return false and an error
  119.      * message will be printed in the function that userExists is called
  120.      */
  121.     public boolean userExists(String username) {
  122.         for (Entry<String, String> entry : users.entrySet()) {
  123.             String key = entry.getKey();
  124.             if (key.equals(username)) {
  125.                 return true;
  126.             }
  127.         }
  128.         return false;
  129.     }
  130.  
  131.     // Store all details from file
  132.     public void createUsersFromFile() {
  133.         String fileName = "customerinfo.txt";
  134.         String line = null;
  135.  
  136.         try {
  137.             BufferedReader br = new BufferedReader(new FileReader(fileName));
  138.             while ((line = br.readLine()) != null) {
  139.                 String delims[] = line.split(" ");
  140.                 users.put(delims[0], delims[1]);
  141.             }
  142.             br.close();
  143.         } catch (FileNotFoundException ex) {
  144.             System.out.println("Unable to open file '" + fileName + "'");
  145.         } catch (IOException ex) {
  146.             System.out.println("Error reading file '" + fileName + "'");
  147.         }
  148.         for (Entry<String, String> entry : users.entrySet()) {
  149.             String username = entry.getKey();
  150.             String password = entry.getValue();
  151.  
  152.             // create a customer and store their login details
  153.             HashMap<String, String> hm = new HashMap<String, String>();
  154.             hm.put(username, password);
  155.             loginDetailsList.add(hm);
  156.             customerList.add(new Customer(loginDetailsList.get(numCustomers), null, false));
  157.             numCustomers++;
  158.         }
  159.     }
  160.  
  161.     /**
  162.      * This function authenticates the login of the business owner by opening up
  163.      * the business.txt where the details for the business owner are stored and
  164.      * then comparing them to the login details the user has entered if the file
  165.      * is not found or there is an issue reading the file an appropriate error
  166.      * message is displayed.
  167.      */
  168.     public boolean isOwner(String username, String password) {
  169.         String fileName = "business.txt";
  170.         String line = null;
  171.         try {
  172.             BufferedReader br = new BufferedReader(new FileReader(fileName));
  173.             while ((line = br.readLine()) != null) {
  174.                 String delims[] = line.split(" - ");
  175.                 for (int i = 0; i < delims.length; ++i) {
  176.                     if (delims[i].equals("Username") && delims[i + 1].equals(username)) {
  177.                     }
  178.                 }
  179.                 for (int i = 0; i < delims.length; ++i) {
  180.                     if (delims[i].equals("Password") && delims[i + 1].equals(password)) {
  181.                         return true;
  182.                     }
  183.                 }
  184.             }
  185.             br.close();
  186.         } catch (FileNotFoundException ex) {
  187.             System.out.println("Unable to open file '" + fileName + "'");
  188.         } catch (IOException ex) {
  189.             System.out.println("Error reading file '" + fileName + "'");
  190.         }
  191.         return false;
  192.     }
  193.  
  194.     /**
  195.      * This function simply compares the username and the password that the
  196.      * customer has entered with the stored key in the customer list, if they
  197.      * match the user is greeted with a welcome message
  198.      */
  199.     public boolean isUser(String username, String password) {
  200.         if (users.containsKey(username)) {
  201.             if (users.get(username).equals(password)) {
  202.                 System.out.println("Welcome " + username);
  203.                 System.out.println(" ");
  204.                 for (int i = 0; i < customerList.size(); ++i) {
  205.                     if (customerList.get(i).getLoginDetails().containsKey(username)) {
  206.                         customerList.get(i).setLoggedOn(true);
  207.                     }
  208.                 }
  209.                 return true;
  210.             } else
  211.                 return false;
  212.         } else
  213.             return false;
  214.     }
  215.  
  216.  
  217.     public String registerName() {
  218.         lineBreak();
  219.         System.out.println("Type quit to return to main menu");
  220.         System.out.println("Please enter your first name:");
  221.         String firstName = sc.nextLine();
  222.         if (!v.validInput(firstName, "letters"))
  223.             return null;
  224.  
  225.         lineBreak();
  226.         System.out.println("Please enter your last name:");
  227.         String lastname = sc.nextLine();
  228.         if (!v.validInput(lastname, "letters"))
  229.             return null;
  230.  
  231.         String name = firstName + " " + lastname;
  232.         return name;
  233.  
  234.     }
  235.  
  236.     public String registerUsername() {
  237.         lineBreak();
  238.         System.out.println("Please enter your new username: ");
  239.         System.out.println("(Minimum of 4 Characters)");
  240.         String username = sc.nextLine();
  241.        
  242.         if (!v.validInput(username, "letters and numbers"))
  243.             return null;
  244.         if (userExists(username)) {
  245.             System.out.println("User already exists");
  246.             return null;
  247.         }
  248.         return username;
  249.     }
  250.    
  251.     public String registerPassword() {
  252.         lineBreak();
  253.         System.out.println("Please enter your password: ");
  254.         System.out.println("Password must contain the following:");
  255.         System.out.println("    Have a minumum of 8 characters");
  256.         System.out.println("    Have a maximum of 20 characters");
  257.         System.out.println("    At least one uppercase letter");
  258.         System.out.println("    At least one lowercase letter");
  259.         System.out.println("    At lease one special character");
  260.         System.out.println("    At least one number");
  261.         String password = sc.nextLine();
  262.         lineBreak();
  263.         if(!v.validInput(password, "password"))
  264.             return null;
  265.         System.out.println("Please re-enter your password: ");
  266.         String password2 = sc.nextLine();
  267.         if (!password.equals(password2)) {
  268.             lineBreak();
  269.             System.out.println("ERROR - password mismatch. Registration was not successful");
  270.             lineBreak();
  271.             return null;
  272.         }
  273.         return password;
  274.        
  275.     }
  276.  
  277.     public void lineBreak() {
  278.         System.out.println();
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement