Home > Blockchain >  How to make a library of string arrays and access them like a login system. (Java 17)
How to make a library of string arrays and access them like a login system. (Java 17)

Time:07-20

I am trying to make a login prompt with usernames and passwords, but I cannot figure out how to make the string array library. My current code can take in 1 username and password by using functions with variables, (like public myFunction(String myVariable);) and login with just that, and change the pass and username. currently, my code is

package main;

import java.util.Scanner;
import logins.Logins;

public class Main {
    public static void main(String[] args) {
        String choice = null;
        Scanner scanner = null;
        start(choice, scanner, "Password", "Username");
    }
    
    public static void command(String choice, Scanner scanner, String pass, String user) {
        if(choice.equals("login")) { 
            login(pass, scanner, choice, user);
        }else if(choice.equals("change password")) {
            passChange(pass, choice, scanner, user);
        }else if(choice.equals("change username")) {
            userChange(pass, choice, scanner, user);
        }else {
            System.err.println("Invalid choice");
            start(choice, scanner, pass, user);
        }
    }
    
    public static void password(String pass, String choice, Scanner scanner, String user) {
        System.out.println("Enter password");
        choice = scanner.nextLine();
        if(choice.equals(pass)) {
            System.out.println("Password Correct. You are logged in");
            start(choice, scanner, pass, user);
        }else {
            System.err.println("Incorrect Password");
            start(choice, scanner, pass, user);
        }
    }
    
    public static void passChange(String pass, String choice, Scanner scanner, String user) {
        System.out.println("Username:");
        choice = scanner.nextLine();
        if (choice.equals(user)) {
            System.out.println("Old Password:");
            choice = scanner.nextLine();
            if (choice.equals(pass)) {
                System.out.println("New Password");
                choice = scanner.nextLine();
                pass = choice;
                start(choice, scanner, pass, user);
            }else {
                System.err.println("Incorrect password");
                start(choice, scanner, pass, user);
            }
        }else {
            System.err.println("No user found named "   choice);
            start(choice, scanner, pass, user);
        }
    }
    
    public static void userChange(String pass, String choice, Scanner scanner, String user) {
        System.out.println("Username:");
        choice = scanner.nextLine();
        if (choice.equals(user)) {
            System.out.println("Password:");
            choice = scanner.nextLine();
            if (choice.equals(pass)) {
                System.out.println("Logged in. New username:");
                choice = scanner.nextLine();
                user = choice;
                start(choice, scanner, pass, user);
            }else {
                System.err.println("Incorrect password");
                start(choice, scanner, pass, user);
            }
        }else {
            System.err.println("Incorrect password");
            start(choice, scanner, pass, user);
        }
    }
    
    public static void start(String choice, Scanner scanner, String pass, String user) {
        scanner = new Scanner(System.in);
        System.out.println("What would you like to do? ");
        choice = scanner.nextLine();
        command(choice, scanner, pass, user);
    }
    public static void login(String pass, Scanner scanner, String choice, String user) {
        System.out.println("Enter username");
        choice = scanner.nextLine();
        if(choice.equals(user)) {
            System.out.println("Valid Username.");
            password(pass, choice, scanner, user);
        }else {
            System.err.println("No user named "   choice);
            start(choice, scanner, pass, user);
        }
    }
}

I have started making an array library, but I don't know how to access any of the usernames or passwords in general,(like once you put in a username, it gets the matching password)

package logins;

public class Logins {
    public static void main(String[] args) {
        String[] User = new String[] {"Username", "Password"};
        String[] User1 = new String[] {"Username1", "Password1"};
    }
}

CodePudding user response:

Are you trying to link Usernames and Passwords together? If so, try using a HashMap. You could try doing something like:

HashMap<String, String> logins= new HashMap<String, String>();

This way a username can retrieve the corresponding password

String password = logins.get("Mario");

Though getting passwords from usernames would not be recommended. You could add to it by:

logins.put("Mario", "password1234");

Not sure if this is what you were asking but hope this helps

CodePudding user response:

As others have pointed out, you can have a Map with username and password. You can retrieve the password as well as shown below.

HashMap<String, String> userLogins = new HashMap<>();
String username = "testUser";
String userPassword = userLogins.get(username);

However, using a HashMap gives you the restriction that usernames should be unique in your application. For example, if you have the username 'Sara' already with a password, and then another user comes with the same name 'Sara', then the initial stored password gets replaced by the new password associated with the new user. If this is the case, a better approach is to use a User object where you can maintain any other related details too for a user.

A sample example is given below. You can choose to use either for loops or streams to iterate through the list of users. Also, you can use an id as well if you think username is not going to be unique.

import java.util.ArrayList;
import java.util.List;

class User {
    private String name;
    private String pswd;
    private int age;

    public User() {
    }

    public User(String name, String pswd, int age) {
        this.name = name;
        this.pswd = pswd;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPswd() {
        return pswd;
    }

    public void setPswd(String pswd) {
        this.pswd = pswd;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class TestClass {
    public static void main(String[] args) {
        User user1 = new User("Sara", "1234", 25);
        User user2 = new User("Ann", "abc", 30);

        List<User> users = new ArrayList<>(List.of(user1, user2));

        //Seeing whether the input username and password are correct
        String userName = "testName";
        String userPswd = "testPassword";
        Boolean isCorrectLogin = checkLogin(userName, userPswd, users);

        //changing password
        String newPswd = "newPassword";
        changePswd(userName, userPswd, newPswd, users);


    }

    private static void changePswd(String userName, String userPswd, String newPswd, List<User> users) {
        for (User u : users) {
            if (userName.equals(u.getName()) && userPswd.equals(u.getPswd())) {
                 u.setPswd(newPswd);
                break;
            }
        }
    }

    private static Boolean checkLogin(String userName, String userPswd, List<User> users) {
        Boolean isCorrectLogin = Boolean.FALSE;
        if (users.isEmpty()) {
            return isCorrectLogin;
        }
        //using for loops
        for (User u : users) {
            if (userName.equals(u.getName()) && userPswd.equals(u.getPswd())) {
                isCorrectLogin = Boolean.TRUE;
                break;
            }
        }

        // using streams
        isCorrectLogin = users.stream().anyMatch(u -> userName.equals(u.getName()) && userPswd.equals(u.getPswd()));

        return isCorrectLogin;
    }

}
  • Related