I cant find a java version of this anywhere. In js this is simple. I have a list of objects with attributes and I want to create arrays for each attribute. The user class simply initializes its own attributes. The main class initializes the list with user objects and then creates lists of usernames and passwords.
User:
public class User{
String username, password;
public User(){}
public User(String myName, String myPwd){
username = myName;
password = myPwd;
}
}
Main:
import java.util.stream.Collectors;
public class Main{
public static void main(String[] args){
List<User> users = new ArrayList<>();
int i = 0;
while(i<5){
users.add(new User("Brandon" + i,"LetsGo" + i));
i++
}
System.out.println("Enter your username.");
String username = scanner.nextLine();
System.out.println("Enter your password.");
String password = scanner.nextLine();
//how do I make these lists?
List<String> names = users.stream().map(User::username).collect(Collectors.toList());
List<String> pwds = users.stream().map(User::password).collect(Collectors.toList());
if(names.contains(username)){
if(pwds.contains(password)){
System.out.println("Welcome back " + username);
}else{
System.out.println("The credentials do not match our records.");
}
}else{
users.add(new User(username, password));
System.out.println("Welcome " + username);
}
}
}