Home > Back-end >  I wanted to make a register and login system in console in java
I wanted to make a register and login system in console in java

Time:10-22

I just made a program that makes you choose if you want to register or login to a user that is already in the collections or user array list that I made. What my problem is that i can't find a way where i put a username and password that is already registered and to verify it using the list that i made.

This is my Main Class:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import java.time.LocalDate;
import java.util.stream.Collectors;


public class Main {

    public static void main(String[] args) {


        Collection<User> c = new ArrayList<User>();
        Scanner sc1 = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        int ch;

        do{
            System.out.println("1. Register");
            System.out.println("2. Login");
            System.out.print("Enter your Choice: ");
            ch = sc1.nextInt();

            switch(ch) {
                case 1:
                    System.out.println("Enter New Username:");
                    String username = sc2.nextLine();
                    System.out.println("Enter New Password:");
                    String pass = sc2.nextLine();
                    c.add(new User(username, pass));
                    System.out.println("Your account has been registered to the system!");
                    break;
                case 2:
                    System.out.println("Enter your Username:");
                    String loginusername = sc2.nextLine();
                    System.out.println("Enter your Password:");
                    String loginpassword = sc2.nextLine();

            }

        }while(ch!=0);


    }

}

and this is my User class:

public class User {

    private String username;
    private String pass;

    User(String username, String pass){
        this.username = username;
        this.pass = pass;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

CodePudding user response:

Collection might not be the best suited interface for the task given. I suggest to use Map with username serving as a key.

Nevertheless, if you are adamant on using Collection, you'll have to iterate through your collection of Users, call getUsername() on each item retrieved and compare the username stored to the username provided.

CodePudding user response:

You can intercept after catching the username and trying to find that in the list.

without comments

Collection<User> c = new ArrayList<User>();

System.out.println("Enter New Username:");
String username = sc2.nextLine();
User temp = c.stream().filter(username.equals(c.getUsername()))
   .findAny()
   .orElse(null);

if(temp != null){
    System.out.println("This user already exists");
}

// with comments

Collection<User> c = new ArrayList<User>();

System.out.println("Enter New Username:");
String username = sc2.nextLine();

// these next lines are using streamApi and lambda
User temp = c.stream() //streams on all the list
   .filter(
      username.equals(c.getUsername()) // this filter is getting a user and comparing it to the input username
    )
   .findAny() // finds if any
   .orElse(null);  // returns null if there is no user with this username

if(temp != null){  // check if the top result returned something different than null
    System.out.println("This user already exists");
}

CodePudding user response:

You can find the user on your collection. For example :

case 2:
                System.out.println("Enter your Username:");
                String loginusername = sc2.nextLine();
                System.out.println("Enter your Password:");
                String loginpassword = sc2.nextLine();
                boolean isUserFound = c.stream().anyMatch(user -> user.getUsername().equals(loginusername) && user.getPass().equals(loginpassword));
                if (isUserFound) {
                    System.out.println("Login success");
                } else {
                    System.out.println("Wrong credentials!! Login Failed.");
                }

Output of above code

enter image description here

  •  Tags:  
  • java
  • Related