Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package company;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import validation.ConsoleValidator;
- import validation.Validator;
- import java.util.Scanner;
- public class Login {
- public static Map<String, String> users = new HashMap<>();
- public static ArrayList<Customer> customerList = new ArrayList<>();
- public static ArrayList<HashMap<String, String>> loginDetailsList = new ArrayList<>();
- Scanner sc = new Scanner(System.in);
- int numCustomers = 0;
- Validator v = new ConsoleValidator();
- /**
- * The function below asks for the username and password of the user it will
- * return the appropriate login menu depending on the login of the user
- * additionally if the "quit" command is inputed the while loop will exit
- * and the user will be returned to the main menu
- */
- public ArrayList<Customer> getCustomerList(){
- return customerList;
- }
- public String login() {
- while (true) {
- lineBreak();
- System.out.println("Type quit to return to main menu");
- System.out.println("Please enter your username: ");
- String username = sc.nextLine();
- if (username.equals("quit"))
- return "quit";
- lineBreak();
- System.out.println("Please enter your password: ");
- String password = sc.nextLine();
- if (password.equals("quit"))
- return "quit";
- if (isOwner(username, password))
- return "owner";
- if (isUser(username, password)) {
- return "customer";
- } else
- System.out.println("Invalid login details");
- lineBreak();
- }
- }
- /**
- * The register function is used when the user selects option 2 register it
- * analyzes the user name and password checking if the login details are
- * already registered as well as ensuring that the entered username or
- * password contain no spaces e.g they are only one word if the user
- * successfully registers then they are created using the HashMap as a
- * customer and their login details are stored
- */
- public void register() {
- String name = registerName();
- if(name == null)
- return;
- String username = registerUsername();
- if(username == null)
- return;
- String password = registerPassword();
- if(password == null)
- return;
- writeUsernameAndPasswordToFile(username, password, name);
- HashMap<String, String> hm = new HashMap<String, String>();
- hm.put(username, password);
- loginDetailsList.add(hm);
- customerList.add(new Customer(loginDetailsList.get(numCustomers), null, false));
- numCustomers++;
- return;
- }
- /**
- * As suggested by the name of this function it is used to write the
- * username and password to the customerinfo.txt file as a string,
- * additionally it is specified to write the username and password on one
- * line with a space in between and then move to the following line for the
- * next input
- */
- public void writeUsernameAndPasswordToFile(String username, String password, String name) {
- String fileName = "customerinfo.txt";
- try {
- FileWriter fw = new FileWriter(fileName, true);
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(username + " " + password + " " + name);
- bw.newLine();
- bw.close();
- lineBreak();
- System.out.println("Successfully registered!");
- } catch (IOException ex) {
- System.out.println("Error writing to file '" + fileName + "'");
- }
- createUsersFromFile();
- }
- /**
- * This function is used to authenticate the user input with the stored key
- * value if the user does not exist it will return false and an error
- * message will be printed in the function that userExists is called
- */
- public boolean userExists(String username) {
- for (Entry<String, String> entry : users.entrySet()) {
- String key = entry.getKey();
- if (key.equals(username)) {
- return true;
- }
- }
- return false;
- }
- // Store all details from file
- public void createUsersFromFile() {
- String fileName = "customerinfo.txt";
- String line = null;
- try {
- BufferedReader br = new BufferedReader(new FileReader(fileName));
- while ((line = br.readLine()) != null) {
- String delims[] = line.split(" ");
- users.put(delims[0], delims[1]);
- }
- br.close();
- } catch (FileNotFoundException ex) {
- System.out.println("Unable to open file '" + fileName + "'");
- } catch (IOException ex) {
- System.out.println("Error reading file '" + fileName + "'");
- }
- for (Entry<String, String> entry : users.entrySet()) {
- String username = entry.getKey();
- String password = entry.getValue();
- // create a customer and store their login details
- HashMap<String, String> hm = new HashMap<String, String>();
- hm.put(username, password);
- loginDetailsList.add(hm);
- customerList.add(new Customer(loginDetailsList.get(numCustomers), null, false));
- numCustomers++;
- }
- }
- /**
- * This function authenticates the login of the business owner by opening up
- * the business.txt where the details for the business owner are stored and
- * then comparing them to the login details the user has entered if the file
- * is not found or there is an issue reading the file an appropriate error
- * message is displayed.
- */
- public boolean isOwner(String username, String password) {
- String fileName = "business.txt";
- String line = null;
- try {
- BufferedReader br = new BufferedReader(new FileReader(fileName));
- while ((line = br.readLine()) != null) {
- String delims[] = line.split(" - ");
- for (int i = 0; i < delims.length; ++i) {
- if (delims[i].equals("Username") && delims[i + 1].equals(username)) {
- }
- }
- for (int i = 0; i < delims.length; ++i) {
- if (delims[i].equals("Password") && delims[i + 1].equals(password)) {
- return true;
- }
- }
- }
- br.close();
- } catch (FileNotFoundException ex) {
- System.out.println("Unable to open file '" + fileName + "'");
- } catch (IOException ex) {
- System.out.println("Error reading file '" + fileName + "'");
- }
- return false;
- }
- /**
- * This function simply compares the username and the password that the
- * customer has entered with the stored key in the customer list, if they
- * match the user is greeted with a welcome message
- */
- public boolean isUser(String username, String password) {
- if (users.containsKey(username)) {
- if (users.get(username).equals(password)) {
- System.out.println("Welcome " + username);
- System.out.println(" ");
- for (int i = 0; i < customerList.size(); ++i) {
- if (customerList.get(i).getLoginDetails().containsKey(username)) {
- customerList.get(i).setLoggedOn(true);
- }
- }
- return true;
- } else
- return false;
- } else
- return false;
- }
- public String registerName() {
- lineBreak();
- System.out.println("Type quit to return to main menu");
- System.out.println("Please enter your first name:");
- String firstName = sc.nextLine();
- if (!v.validInput(firstName, "letters"))
- return null;
- lineBreak();
- System.out.println("Please enter your last name:");
- String lastname = sc.nextLine();
- if (!v.validInput(lastname, "letters"))
- return null;
- String name = firstName + " " + lastname;
- return name;
- }
- public String registerUsername() {
- lineBreak();
- System.out.println("Please enter your new username: ");
- System.out.println("(Minimum of 4 Characters)");
- String username = sc.nextLine();
- if (!v.validInput(username, "letters and numbers"))
- return null;
- if (userExists(username)) {
- System.out.println("User already exists");
- return null;
- }
- return username;
- }
- public String registerPassword() {
- lineBreak();
- System.out.println("Please enter your password: ");
- System.out.println("Password must contain the following:");
- System.out.println(" Have a minumum of 8 characters");
- System.out.println(" Have a maximum of 20 characters");
- System.out.println(" At least one uppercase letter");
- System.out.println(" At least one lowercase letter");
- System.out.println(" At lease one special character");
- System.out.println(" At least one number");
- String password = sc.nextLine();
- lineBreak();
- if(!v.validInput(password, "password"))
- return null;
- System.out.println("Please re-enter your password: ");
- String password2 = sc.nextLine();
- if (!password.equals(password2)) {
- lineBreak();
- System.out.println("ERROR - password mismatch. Registration was not successful");
- lineBreak();
- return null;
- }
- return password;
- }
- public void lineBreak() {
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement