I have two Classes. one DAO that has an Arrraylist of Users (user is instace of User class) and one method in another class that checks if there is a user with our input name or not How can I use Hashmap (usernames, User) instead of Arraylist of Users?
public class UserDAO {
private static UserDAO instance;
private static String fileName = "sources/users.aaa";
//--------------------------------------------------------
private UserDAO(){
}
//--------------------------------------------------------
public boolean storeUsers(ArrayList<User> Users){
return IOFile.writeObject(Users, fileName);
}
//--------------------------------------------------------
public ArrayList<User> getUsers(){
ArrayList<User> Users = (ArrayList<User>) IOFile.readObject(fileName);
return Users;
}
//--------------------------------------------------------
public static UserDAO getInstance(){
if(instance == null)
instance = new UserDAO();
return instance;
}
}
and one method in another class that checks if there is a user with our input name or not:
User user = new User(firstName, lastName, userName, password);
ArrayList<User> users = UserDAO.getInstance().getUsers();
for (User user2 : users) {
if (user.equals(user2)){
system.out.println ("Error!!");
return;
}
}
users.add(user);
UserDAO.getInstance().storeUsers(users);