Question: Examine the provided User class and run the code. Your goal for this problem is to add validation by testing the values of arguments being

Examine the provided User class and run the code.
Your goal for this problem is to add validation by testing the values of arguments
being passed to the setter methods and throwing IllegalArgumentExceptions if the
values do not meet certain criteria. You have been provided argument validation for
the password setter that tests if the password contains both an upper and lowercase
letter. Add the following addtional validations. You may find the java String_
documentation helpful.
Make sure you include try and catch blocks in the main method to catch the
Exceptions. Every attempt value should be able to be retried if an invalid value is
entered.
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
User newUser = new User();
System.out.println("Please enter the user's first name:");
String fname = scnr.nextLine();
newUser.setFirstName(fname);
System.out.println("Please enter the user's last name:");
String lname = scnr.nextLine();
newUser.setLastName(lname);
System.out.println("Please enter the user's birthday in the format mm/dd/yyyy:");
String bday = scnr.nextLine();
newUser.setBirthday(bday);
System.out.println("Please enter the user's password:");
String psw = scnr.nextLine();
newUser.setPassword(psw);
System.out.println("Please enter the user's zipcode:");
int zip = scnr.nextInt();
newUser.setZipcode(zip);
}
}
public class User {
private String firstName;
private String lastName;
private String password;
private String birthday;
private int zipcode;
public User(){
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setPassword(String password){
if (!password.matches(".*[A-Z].*")||!password.matches(".*[a-z].*")){
throw new IllegalArgumentException();
}
this.password = password;
}
public void setBirthday(String birthday){
this.birthday = birthday;
}
public void setZipcode(int zipcode){
this.zipcode = zipcode;
}
}
Examine the provided User class and run the code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!