Log in method for accounts class

63 Views Asked by At

I have an accounts class that makes an account with username and log in and saves each user in a list account_list. I have a method in this class to log in changing a logged_in value from false to true. At the moment I can't call the function because of a static reference to a non static method or generaly being unsure of how I should call the method. I have two files in this branch Account.java:

import java.util.Scanner;

public class Accounts {
    private String username;
    private String password;
    private Boolean logged_in;
    private String account_id;

    private Accounts(String username, String password, Boolean logged_in, String account_id) {
        this.username = username;
        this.password = password;
        this.logged_in = logged_in;
        this.account_id = account_id;
    }

    static Accounts createAccount(Scanner scanner) {
        System.out.println("Username:");
        var username = scanner.nextLine();
        System.out.println("Password:");
        var password = scanner.nextLine();
        return new Accounts(username, password, false, "1");
    }

    public boolean log_in(Scanner scanner) {
        System.out.println("Username:");
        var username = scanner.nextLine();
        System.out.println("Password:");
        var password = scanner.nextLine();
        if (username == this.username && password == this.password) {
            System.out.println("Logged in sucscefully!");
            return this.logged_in = true;

        } else {
            System.out.println("Log in failed!");
            return this.logged_in = false;
        }

    }

    public String toString() {
        return "Username: %s Password: %s".formatted(username, password);
    }

}

and Project.java

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

public class Project {
    private String choice;

    public static void main(String[] args) {
        List<Accounts> account_list = new ArrayList<>();
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Welcome to my password database program");
            boolean done = false;
            while (!done) {
                System.out.println("1. Create account \n2. Log in \n3. View passwords \n4. Log out \n5. Exit program");
                var choice = scanner.nextLine();
                if (choice.equals("1")) {
                    Accounts account = Accounts.createAccount(scanner);
                    account_list.add(account);
                    System.out.println(account);
                }else if (choice.equals("2")){
                    account_list.log_in(scanner)
                    
                }else if (choice.equals("5")) {
                    done = true;

                }
            }
        }
    }

}

I'm still fairly new to java and I'm stuck to doing everything in console because I'm also doing the same program in Python and for making it easier to be consistent between the two languages I'm staying in console

I have tried making the function static which doesn't work as it's a class when calling the method of the function. I have tried calling on the object in the account_list list fairly unsuccsesfully expecting it to work when a specific object is called on I had problems making it work as I want to have the possibility of having a lot of accounts saved simultaneously.

My spelling is most likely bad I'm dyslexic and not a native English speaker. Thanks in advance!

1

There are 1 best solutions below

0
Hovercraft Full Of Eels On

Your current code won't work because you're trying to call log_in on the ArrayList itself, which doesn't work and doesn't make sense. Instead, and to be honest, I think that you may want to re-think and re-write your code. Suggestions include:

  • Create an Account class that has just the fields needed for one Account, including userName, password, accountId, and perhaps a boolean loggedIn, one that has constructor(s), getters, setters, toString, and perhaps equals and hashCode method overrides.
  • This class should have no UI code within it, for example, no println statements and no use of a Scanner object.
  • Create a separate Accounts class, one that holds a private field, List<Account> accounts = new ArrayList<>();
  • Give this class a logIn method that takes two Strings, one for userName and one for password.
  • Give this an Account field, currentAccount, one that is set if the user is logged in.
  • Create a Project class, or better yet, name it TestAccounts, give it an Accounts object, and give it all the UI code, the println statements and Scanner code. This is where you'd call Accounts logIn method.