import javax.swing.JOptionPane;
public class PC2 {
public static void main(String[] args){
// Let the DataAcquisition2 class collect
// and validate the user input.
DataAcquisition2 dataAcqn = new DataAcquisition2();
dataAcqn.collectData();
// Retrieve the collected/validated data.
String firstName = dataAcqn.getFirstName();
String lastName = dataAcqn.getLastName();
String phone = dataAcqn.getPhone();
int rolls = dataAcqn.getRolls();
int prints = dataAcqn.getPrints();
// Use this data to create a new instance of the
// ProjectDefinition2 class and save a reference
// to the instance in a local variable for later use.
ProjectDefinition2 person =
new ProjectDefinition2(firstName, lastName, phone, rolls, prints);
System.out.println("person = " + person);
}
}
class DataAcquisition2 {
String firstName;
String lastName;
String phoneNumber;
int rollsOfFilm; // validation requirements
int numberOfPrints; // indicate we need ints here
public void collectData() {
firstName = getItem("First Name");
lastName = getItem("Last Name");
phoneNumber = getItem("Phone Number");
boolean okay = true;
rollsOfFilm = getNumber("Rolls of Film");
numberOfPrints = getNumber("Number of Prints");
validateData();
}
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public String getPhone() { return phoneNumber; }
public int getRolls() { return rollsOfFilm; }
public int getPrints() { return numberOfPrints; }
private String getItem(String s) {
return JOptionPane.showInputDialog(null, "Enter " + s);
}
private int getNumber(String subject) {
int n = 0;
boolean success = false;
do {
// Make sure you can get a parsable value for the int
// data variables. Otherwise you will have to parse them in
// the validateData method and you would be unable to get
// corrected input from the user if you came up short.
try {
String input = getItem(subject);
n = Integer.parseInt(input);
success = true;
} catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Number format error: " +
e.getMessage());
}
} while(!success);
return n;
}
/**
* a. The name of the customer is not blank
* b. The phone number of the customer is not blank and
* doesn’t exceed 10 characters and is an integer
* with positive number only
* c. The number of rolls is between 1 and 100 and is an
* positive integer
* d. The number of prints can only be (1,2,8,10, 25,30,50),
* positive integer
*/
private void validateData() {
try {
if(firstName.length() == 0 || lastName.length() == 0)
throw new IllegalArgumentException("names must not be blank");
// validate the rest of the four things listed above...
} catch(IllegalArgumentException e) {
System.out.println("Illegal Argument: " + e.getMessage());
System.exit(1);
}
}
}
class ProjectDefinition2 {
String userNameFirst;
String userNameLast;
String userPhone;
int userFilm;
int userPrints;
final int USER_EXPOSURES = 1;
public ProjectDefinition2(String first, String last, String phone,
int rolls, int prints) {
userNameFirst = first;
userNameLast = last;
userPhone = phone;
userFilm = rolls;
userPrints = prints;
}
public String toString() {
return "First Name:" + " " + userNameFirst + ", " +
"Last Name:" + " " + userNameLast + ", " +
"Phone Number:" + " " + userPhone + ", " +
"Rolls of Film:" + " " + userFilm + ", " +
"Number of Prints:" + " " + userPrints;
}
}