In: Computer Science
When a user signs in for the first time to a website, the user must submit personal information, such as last name, first name, email address, and so on. Typically, there are two fields for passwords, requiring the user to enter the password twice, to ensure that the user did not make a typo in the first password field.
Write a class encapsulating user with the following attributes:
UserID
Password
Reentered password
Email address
Last Name
First Name
Zip
You will store these values as strings.
Write the following methods in your class:
1. A constructor which will initialize the 7 attributes listed above.
2. Accessor, mutator, toString methods
3. A method returning the user id consisting of the first letter of the first name, last two letters of the last name and the first three nambers of the zip code and a random number (between 0 and 999)
4.. A method checking if the two Strings representing the password fields are the same, if they are return true, otherwise false.
Write a test class to create 2 user objects (using your constructor) and test all the methods in your class.
Remember to submit all your .java files, .class files and screenshots of your code and output and psuedocode
Program
import java.util.*;
//User class
class User
{
//private data members
private String userID;
private String password;
private String reenteredPassword;
private String emailAddress;
private String lastName;
private String firstName;
private String zip;
//constructor
public User(String userID, String password, String
reenteredPassword,
String emailAddress, String
lastName, String firstName, String zip)
{
this.userID = userID;
this.password = password;
this.reenteredPassword =
reenteredPassword;
this.emailAddress =
emailAddress;
this.lastName = lastName;
this.firstName = firstName;
this.zip = zip;
}
//method to set userID
public void setUserId(String userID)
{
this.userID = userID;
}
//method to set password
public void setPassword(String password)
{
this.password = password;
}
//method to set reenteredPassword
public void setReenteredPassword(String
reenteredPassword)
{
this.reenteredPassword =
reenteredPassword;
}
//method to set emailAddress
public void setEmailAddress(String emailAddress)
{
this.emailAddress =
emailAddress;
}
//method to set lastName
public void setLastName(String lastName)
{
this.lastName = lastName;
}
//method to set firstName
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
//method to set zip
public void setZip(String zip)
{
this.zip = zip;
}
//method to return userID
public String getUserID()
{
return userID;
}
//method to return password
public String getPassword()
{
return password;
}
//method to return reenteredPassword
public String getReenteredPassword()
{
return reenteredPassword;
}
//method to return emailAddress
public String getEmailAddress()
{
return emailAddress;
}
//method to return lastName
public String getLastName()
{
return lastName;
}
//method to return firstName
public String getFirstName()
{
return firstName;
}
//method to return zip
public String getZip()
{
return zip;
}
//method to convert to String
public String toString()
{
return "User id: " + userID +
" Password: " +
password +
" EmailAddress:
" + emailAddress+
" Name: " +
lastName + " " + firstName +
" Zop: " +
zip;
}
//method to check the password fields are the same or
not
public boolean passwordChecking()
{
return
password.equals(reenteredPassword);
}
//method to generate and return id
public String getID()
{
String id = firstName.charAt(0) +
lastName.substring(lastName.length()-2);
id = id + zip.substring(0,
3);
id = id + (new
Random()).nextInt(1000);
return id;
}
}
//Driver class
class Driver
{
//main method
public static void main (String[] args)
{
//create objects of User
class
User user1 = new User("user1",
"password1", "password1", "[email protected]", "Ali", "Joe",
"12345");
User user2 = new User("user2",
"password2", "password2", "[email protected]", "Ali", "John",
"23456");
//display details of user1
System.out.println("Password fields
are the same: " + user1.passwordChecking());
System.out.println("UserID: " +
user1.getUserID());
System.out.println("Password: " +
user1.getPassword());
System.out.println("Reentered
Password: " + user1.getReenteredPassword());
System.out.println("Email: " +
user1.getEmailAddress());
System.out.println("Name: " +
user1.getFirstName() + " " + user1.getLastName());
System.out.println("Zip: " +
user1.getZip());
System.out.println("ID: " +
user1.getID());
System.out.println(user1);
//display details of user2
System.out.println("Password fields
are the same: " + user2.passwordChecking());
System.out.println("UserID: " +
user2.getUserID());
System.out.println("Password: " +
user2.getPassword());
System.out.println("Reentered
Password: " + user2.getReenteredPassword());
System.out.println("Email: " +
user2.getEmailAddress());
System.out.println("Name: " +
user2.getFirstName() + " " + user2.getLastName());
System.out.println("Zip: " +
user2.getZip());
System.out.println("ID: " +
user2.getID());
System.out.println(user2);
}
}
Output:
Password fields are the same: true
UserID: user1
Password: password1
Reentered Password: password1
Email: [email protected]
Name: Joe Ali
Zip: 12345
ID: Jli12363
User id: user1 Password: password1 EmailAddress: [email protected]
Name: Ali Joe Zop: 12345
Password fields are the same: true
UserID: user2
Password: password2
Reentered Password: password2
Email: [email protected]
Name: John Ali
Zip: 23456
ID: Jli234190
User id: user2 Password: password2 EmailAddress: [email protected]
Name: Ali John Zop: 23456