In: Computer Science
When a user signs in for the first time to a website, the user has to submit personal information, such as userid, name, email address, telephone number 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 elements:
UserID
Password
Reentered password
Email address
Name
Street Address
City
State
Zip
You will store these values as strings.
Write the following methods in your class:
1. A constructor which will initialize the 9 attributes listed above.
2. Accessor, mutator, toString methods
3. A method returning the number of characters in the userid
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.
class User
{
//Attributes
String userID, password, renteredPassword, email, name,
streetAddress, city,
state, zip;
// Contructor
public User ()
{
this.userID = "";
this.password = "";
this.renteredPassword = "";
this.email = "";
this.name = "";
this.streetAddress = "";
this.city = "";
this.state = "";
this.zip = "";
}
// Accessor and Mutators
public String getUserID ()
{
return this.userID;
}
public void setUserID (String _userID)
{
this.userID = _userID;
}
public String getPassword ()
{
return this.password;
}
public void setPassword (String _password)
{
this.password = _password;
}
public String getRenteredPassword ()
{
return this.renteredPassword;
}
public void setRenteredPassword (String _renteredPassword)
{
this.renteredPassword = _renteredPassword;
}
public String getEmail ()
{
return this.email;
}
public void setEmail (String _email)
{
this.email = _email;
}
public String getName ()
{
return this.name;
}
public void setName (String _name)
{
this.name = _name;
}
public String getStreetAddress ()
{
return this.streetAddress;
}
public void setStreetAddress (String _streetAddress)
{
this.streetAddress = _streetAddress;
}
public String getCity ()
{
return this.city;
}
public void setCity (String _city)
{
this.city = _city;
}
public String getState ()
{
return this.state;
}
public void setState (String _state)
{
this.state = _state;
}
public String getZip ()
{
return this.zip;
}
public void setZip (String _zip)
{
this.zip = _zip;
}
// A Function to find UserID length
public int getUserIDLength ()
{
return this.userID.length ();
}
// Function to comapre passwords
public boolean comparePassword ()
{
return this.password ==
this.renteredPassword;
}
}
public class Main
{
public static void main (String[]args)
{
User newUser = new User ();
// Mutators
newUser.setUserID ("atriax");
newUser.setPassword
("mypass");
newUser.setRenteredPassword
("mypass");
newUser.setEmail
("[email protected]");
newUser.setName ("Atriax
Gethon");
newUser.setCity ("New York");
newUser.setState ("Washington
DC");
newUser.setZip ("124332");
newUser.setStreetAddress ("221
B,Bakers Street");
// Print the Details
System.out.println ("User ID: " +
newUser.getUserID ());
// chehck User ID length
System.out.println ("User ID Length:
" + newUser.getUserIDLength ());
// Compare the passwords
System.out.println ("Passwords
Match: " +
newUser.comparePassword ());
System.out.println ("Email: " +
newUser.getEmail ());
System.out.println ("Street Address:
" + newUser.getStreetAddress ());
System.out.println ("City: " +
newUser.getCity ());
System.out.println ("State: " +
newUser.getState ());
System.out.println ("Zip: " +
newUser.getZip ());
}
}