Listing1: AccountClass
public class Account {
String firstName;
String lastName;
final String ACCOUNT_DATA_FILE = "AccountData.txt";
public Account(String fname, String lname) {
firstName = fname;
lastName = lname;
}
public boolean isValid() {
/*
Let's go with simpler validation
here to keep the example simpler.
*/
…
…
}
public boolean save() {
FileUtil futil = new FileUtil();
String dataLine = getLastName() + ”," + getFirstName();
return futil.writeToFile(ACCOUNT_DATA_FILE, dataLine,true, true);
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Listing2: Address Class
public class Address {
String address;
String city;
String state;
final String ADDRESS_DATA_FILE = "Address.txt";
public Address(String add, String cty, String st) {
address = add;
city = cty;
state = st;
}
public boolean isValid() {
/*
The address validation algorithm
could be complex in real-world
applications.
Let's go with simpler validation
here to keep the example simpler.
*/
if (getState().trim().length() < 2)
return false;
return true;
}
public boolean save() {
FileUtil futil = new FileUtil();
String dataLine = getAddress() + ”," + getCity() + ”," + getState();
return futil.writeToFile(ADDRESS_DATA_FILE, dataLine,true, true);
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
}
