anyone do question 2? please Create a class named UserAccounts that maintains a
ID: 3685489 • Letter: A
Question
anyone do question 2? please
Create a class named UserAccounts that maintains a
list of user accounts, with an appropriate constructor,and the following methods:
isEmpty()returns true if list is empty, and false otherwise
findPassword - returns the password for a given user name
changePassword- updates the password for a given username and password. If they do not match password does not update
addUser- passed a user object to add
deleteUser- deletes a user for a given user name and password. If they do not match user is not deleted
printUsers- prints a list of all usernames, username and password
Explanation / Answer
Hello there ,
Please find below code and the output for the application .
=======
/**
* This is a User class containing username and password.
* @author dipal.prajapati
*
*/
public class User {
private String userName;
private String password;
public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + "]";
}
@Override
public boolean equals(Object obj){
if( obj != null && obj.getClass().equals(this.getClass())){
return ((User)obj).toString().equals(this.toString());
} else {
return false;
}
}
}
=============
import java.util.ArrayList;
import java.util.List;
/**
* This is a class maintains list of Users and provide basic functionalities.
* @author dipal.prajapati
*
*/
public class UserAccounts {
List<User> listOfUsers = new ArrayList<User>();
/**
* It checks if the list is empty.
* @return
*/
public boolean isEmpty() {
return listOfUsers.isEmpty();
}
/**
* It returns the password for a given user name
* @param userName
* @return
*/
public String findPassword(String userName) {
String password = null;
for (User user : listOfUsers) {
if (user.getUserName() != null
&& user.getUserName().equals(userName)) {
password = user.getPassword();
}
}
return password;
}
/**
* It updates the password for a given username and password. If they do not match password does not update.
* @param user
* @param newPassword
*/
public void changePassword(User user, String newPassword) {
if (listOfUsers.contains(user)) {
for (User currentUser : listOfUsers) {
if (currentUser.equals(user)) {
currentUser.setPassword(newPassword);
System.out.println("Password updated:"+newPassword);
}
}
} else {
System.out.println("Usename or Password does not match.");
}
}
/**
* It adds the user to the list.
* @param user
*/
public void addUser(User user) {
listOfUsers.add(user);
System.out.println(user.toString()+" added.");
}
/**
* It deletes a user for a given user name and password. If they do not match user is not deleted.
* @param user
*/
public void deleteUser(User user) {
listOfUsers.remove(user);
System.out.println(user.toString()+" deleted.");
}
/**
* It prints a list of all user name and password;
*/
public void printUsers() {
System.out.println(listOfUsers.toString());
}
public static void main(String args[]) {
UserAccounts userAccounts = new UserAccounts();
System.out.println(" Is UserList empty? " + userAccounts.isEmpty());
System.out.println(" Add User/s:");
userAccounts.addUser(new User("John", "abc"));
userAccounts.addUser(new User("Tommy", "xyz"));
userAccounts.addUser(new User("Lee", "pqr"));
System.out.println(" Is UserList empty? " + userAccounts.isEmpty());
System.out.println(" Find passwrod for John:"+userAccounts.findPassword("John"));
User user= new User("John", "abc");
System.out.println(" Change Password for John:");
userAccounts.changePassword(user, "ABC");
System.out.println(" Delete user Tommy:");
user= new User("Tommy", "xyz");
userAccounts.deleteUser(user);
System.out.println(" Print all Users:");
userAccounts.printUsers();
}
}
========
Output on execution:
Is UserList empty? true
Add User/s:
User [userName=John, password=abc] added.
User [userName=Tommy, password=xyz] added.
User [userName=Lee, password=pqr] added.
Is UserList empty? false
Find passwrod for John:abc
Change Password for John:
Password updated:ABC
Delete user Tommy:
User [userName=Tommy, password=xyz] deleted.
Print all Users:
[User [userName=John, password=ABC], User [userName=Lee, password=pqr]]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.