Please write in JAVA ONLY! When a user signs in for the first time to a website,
ID: 3924555 • Letter: P
Question
Please write in JAVA ONLY!
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
import java.util.*;
import java.lang.*;
import java.io.*;
class Form
{
private String User_id;
private String Password;
private String Reenter_password;
private String Email_address;
private String Name;
private String Street_Address;
private String City;
private String State;
private String Zip;
private String Telephone;
public Form(String[] array) //constructor
{
User_id =array[0];
Password =array[1];
Reenter_password =array[2];
Email_address =array[3];
Name =array[4];
Street_Address =array[5];
City =array[6];
State =array[7];
Zip =array[8];
Telephone =array[9];
}
public void setUser_ID(String uid) //set and get methods
{
User_id = uid;
}
public String getUser_ID()
{
return User_id;
}
public void setPassword(String pass)
{
Password=pass;
}
public String getPassword()
{
return Password;
}
public void setReenter_Password(String rpass)
{
Reenter_password =rpass;
}
public String getReenter_Password()
{
return Reenter_password;
}
public void setEmail_Address(String email)
{
Email_address=email;
}
public String getEmail_Address()
{
return Email_address;
}
public void setName(String n)
{
Name=n;
}
public String getName()
{
return Name;
}
public void seStreet_Address(String saddress)
{
Street_Address = saddress;
}
public String getStreet_Address()
{
return Street_Address;
}
public void setCity(String c)
{
City=c;
}
public String getCity()
{
return City;
}
public void setState(String s)
{
State=s;
}
public String getState()
{
return State;
}
public void setZip(String z)
{
Zip=z;
}
public String getZip()
{
return Zip;
}
public void setTelephone(String phone)
{
Telephone=phone;
}
public String getTelephone()
{
return Telephone;
}
public String toString() //toString method
{
return User_id+" "+Password+" "+Reenter_password+" "+Email_address+" "+Name+" "+Street_Address+" "+City+ " "+State+" "+Zip+" "+Telephone;
}
public boolean IsEmpty() //check if any field is empty
{
if(User_id.isEmpty()||Password.isEmpty()||Reenter_password.isEmpty()||Email_address.isEmpty()||Name.isEmpty()||Street_Address.isEmpty()||City.isEmpty()||State.isEmpty()||Zip.isEmpty()||Telephone.isEmpty())
return true;
else
return false;
}
public int CharsUserId() //count number of characters in user id
{
return User_id.length();
}
public boolean passwordMatch() //check if passwords match
{
if(Password == Reenter_password)
return true;
else
return false;
}
public boolean ValidEmail() //check if email is valid
{
int counter = 0;
for( int i=0; i<Email_address.length(); i++ )
{
if( Email_address.charAt(i) == '@' )
{
counter++;
}
}
if((counter==1) && (Email_address.indexOf('.') > Email_address.indexOf('@') ) )
return true;
else
return false;
}
}
class TestForm
{
public static void main (String[] args) throws java.lang.Exception
{
String[] array = {"abc123","password","password","abc@yahoo.com","abc","harbour street","LA","CF","7655","098686869"};
Form f = new Form(array);
if(f.IsEmpty())
{
System.out.println("The columns in Form are empty");
}
System.out.println(f.toString());
System.out.println("Number of Characters in user id :"+f.CharsUserId());
System.out.println("The password and reenter password match :"+f.passwordMatch());
System.out.println("email is valid :"+f.ValidEmail());
}
}
Output:
Success time: 0.04 memory: 711168 signal:0