Make a Linked List in Java Declare a class named User that contains the followin
ID: 3684934 • Letter: M
Question
Make a Linked List in Java Declare a class named User that contains the following information: String Name String username String password Include in the class a single constructor that is passed a particular first and last name,username,and password; setters and getters, and a toString method that returns the information ia given User object as follows:"Ashley Smith" username: asmith password:"aWjPP406!" Declare a class named UserNode that contains a single User object and a link to another UserNode. Include an appropriate constructor and an appropriate set of methods for this class. 2. 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 user name and password. If they do not match, the password is not updated. addUser – passed a User object to add deleteUser – deletes a user for a given user name and pass word. If they do not match, the user is not deleted. printUsers–prints a list of all user names, username and password
Explanation / Answer
1)
Java code creates two classes, User and UserNode.
User.java:
//Declare a class named User
class User
{
//Declare variables
String Name;
String username;
String password;
User next;
//single constructor that is passed a particular
//name,username,and password; setters and getters,
//and a toString method
public User(String firstLastName, String uName, String pwd)
{
this.Name = firstLastName;
this.username = uName;
this.password = pwd;
next = null;
}
public String getNameValue()
{
return Name;
}
public void setName(String name)
{
this.Name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String name) {
this.username = name;
}
public String getPassword() {
return password;
}
public void setPassword(String pwd) {
this.password = pwd;
}
public String toString()
{
return Name;
}
}
UserNode.java:
//Declare a class named UserNode that
//contains a single User object and
//a link to another UserNode
public class UserNode
{
private User head = null;
public void append(String name, String username, String pwd )
{
User nodeUser = getLastNodeUser();
if (nodeUser == null)
{
head = new User(name, username, pwd);
} else
{
nodeUser.next = new User(name, username, pwd);
}
}
public void delete(String name, String username, String pwd)
{
if(head == null)
{
return;
}
User firstUser = null;
User presentUser = head;
while (presentUser != null && presentUser.Name != name && presentUser.username != username && presentUser.password != pwd) {
firstUser = presentUser;
presentUser = presentUser.next;
}
if(firstUser == null){
head = head.next;
return;
}
if (presentUser == null) {
System.out.println("User not have here.");
return;
}
firstUser.next = presentUser.next;
}
public void print() {
System.out.println("");
if(head == null){
System.out.print("empty list");
return;
}
User alternateUser = head;
while (alternateUser != null)
{
System.out.print(alternateUser.Name + " -> ");
System.out.print(alternateUser.Name + " -> ");
System.out.print(alternateUser.username + " -> ");
alternateUser = alternateUser.next;
}
}
private User getLastNodeUser() {
if (head == null) {
return null;
}
User alternateUser = head;
while (alternateUser.next != null) {
alternateUser = alternateUser.next;
}
return alternateUser;
}
public static void main(String[] args)
{
UserNode listNodeUser = new UserNode();
// User object as follows:"Ashley Smith"
//username: asmith password:"aWjPP406!"
listNodeUser.append("Ashley Smith", "asmith", "aWjPP406!");
listNodeUser.print();
listNodeUser.delete("Ashley Smith", "asmith", "aWjPP406!");
}
}
Output:
Ashley Smith -> Ashley Smith -> asmith ->
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.