When a user signs in for the first time to a website, the user has to submit per
ID: 3806344 • Letter: W
Question
When a user signs in for the first time to a website, the user has to submit personal information, such as user_id, 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 the concept of processing a form with the following elements:
User_id
Password
Reenter password
Email address
Name
Street Address
City
State
Zip
Telephone
You will store these values as strings in an array.
Write the following methods in your class:
1. A constructor with one parameter, an array of 10 strings (representing one element for each of the fields above), the only instance variable.
2. Accessor, mutator, toString methods
3. A method checking that no Strings are empty. If at least one is empty, return false; otherwise return true.
4. A method returning the number of characters in the user_id
5. A method checking if the two Strings representing the password fields are the same, if they are return true, otherwise false.
6. A method checking if the email address contains one and only one @ character and contains at least one "." after the @ character. If these conditions are met, return true; otherwise return false.
Write a test client to test all your methods in your class.
Explanation / Answer
// FormProcessing.java
public class FormProcessing {
public FormProcessing(String[] data) {
this.userId = data[0];
this.password = data[1];
this.renteredPassword = data[2];
this.email = data[3];
this.name = data[4];
this.streetAdress = data[5];
this.city = data[6];
this.state = data[7];
this.zip = data[8];
this.telephone = data[9];
}
private String userId;
private String password;
private String renteredPassword;
private String email;
private String name;
private String streetAdress;
private String city;
private String state;
private String zip;
private String telephone;
public boolean areAllNonEmpty()
{
return !(userId.isEmpty() || password.isEmpty() || renteredPassword.isEmpty() ||
email.isEmpty() || name.isEmpty() || streetAdress.isEmpty() || city.isEmpty() ||
state.isEmpty() || zip.isEmpty() || telephone.isEmpty());
}
public int numCharUserId()
{
return userId.length();
}
public boolean isPasswordSame()
{
return password.equals(renteredPassword);
}
public boolean isEmailValid()
{
int index1 = email.indexOf('@');
int index2 = email.lastIndexOf('@');
int index3 = email.lastIndexOf('.');
return (index1 != -1) && (index1 == index2) && (index3 != -1) && (index1 < index3);
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRenteredPassword() {
return renteredPassword;
}
public void setRenteredPassword(String renteredPassword) {
this.renteredPassword = renteredPassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreetAdress() {
return streetAdress;
}
public void setStreetAdress(String streetAdress) {
this.streetAdress = streetAdress;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "FormProcessing [userId=" + userId + ", password=" + password + ", renteredPassword=" + renteredPassword
+ ", email=" + email + ", name=" + name + ", streetAdress=" + streetAdress + ", city=" + city
+ ", state=" + state + ", zip=" + zip + ", telephone=" + telephone + "]";
}
}
// TestFormProcessing.java
public class TestFormProcessing {
public static void main(String[] args)
{
String[] data = {"user1", "pass1", "pass1", "a@b.c", "name", "adress", "city1", "state1", "zip1", "tele1"};
FormProcessing f = new FormProcessing(data);
System.out.println("Checking all positive case");
if (f.areAllNonEmpty())
{
System.out.println("All non empty pass");
}
if (f.isPasswordSame())
{
System.out.println("Password same pass");
}
if (f.isEmailValid())
{
System.out.println("Valid email pass");
}
System.out.println("All negative case");
f.setUserId("");
if (!f.areAllNonEmpty())
{
System.out.println("All non empty with empty field pass");
}
f.setPassword("pass2");
if (!f.isPasswordSame())
{
System.out.println("Password not same pass");
}
f.setEmail("ab.c");
if (!f.isEmailValid())
{
System.out.println("Email without '@' pass");
}
f.setEmail("ab.@c");
if (!f.isEmailValid())
{
System.out.println("Email with '.' before '@' pass");
}
f.setEmail("ab@.@c");
if (!f.isEmailValid())
{
System.out.println("Email with multiple '@' pass");
}
}
}
Sample run
Checking all positive case
All non empty pass
Password same pass
Valid email pass
All negative case
All non empty with empty field pass
Password not same pass
Email without '@' pass
Email with '.' before '@' pass
Email with multiple '@' pass
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.