Data Class Definition
public class Account
{
String name, zipCode;
public boolean setName(String iName)
{
if (validateName(iName))
{
name = iName;
return true;
}
return false;
}
public boolean setZipCode(String iZipCode)
{
if (validateZipCode(iZipCode))
{
zipCode = iZipCode;
return true;
}
return false;
}
public boolean validateName(String iName)
{
if (iName.indexOf(" ") < 0)
return false;
if (iName.indexOf(",") < 0)
return false;
String temp1;
String temp2;
temp1 = iName.replace(",", " ");
temp2 = temp1.trim();
for (int i = 0; i < iName.length(); i++)
if (!Character.isLetter(iName.charAt(i)))
return false;
return true;
}
public boolean validateZipCode(String iZipCode)
{
if (iZipCode.length() != 5)
return false;
for (int i = 0; i < iZipCode.length(); i++)
if (!Character.isDigit(iZipCode.charAt(i)))
return false;
return true;
}
}
Implementation
import javax.swing.JOptionPane;
public class AccountSetup
{
public static void main(String[] args)
{
do{
Account account = input();
JOptionPane.showMessageDialog(null, account.toString());
}while(JOptionPane.showConfirmDialog(null, "Would you like to set up another account?") == JOptionPane.YES_OPTION);
}
public static Account input()
{
Account acct = new Account();
while (!acct.setName(JOptionPane.showInputDialog(null, "Enter your name (last, first):")))
JOptionPane.showMessageDialog(null, "Invalid name - must have 1 comma, and at least one space between the " +
"first and last name, and can ONLY be letters.");
while (!acct.setZipCode(JOptionPane.showInputDialog(null, "Enter your zipcode (five digits):")))
JOptionPane.showMessageDialog(null, "Invalid zipcode - must be only 5 digits, and can ONLY contain digits.");
return acct;
}
}

New Topic/Question
Reply



MultiQuote




|