Overview We will be adding some validation to our Contact cl

Overview

We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new contact objects.

Contact Classes

Copy your three contact classes from your last homework to a new project.

Change your Contact class to an abstract class. This will prevent instantiation of Contact objects.

Add an abstract method, public void validate(), to your Contact class.

Validation

Implement the validate() method in your BusinessContact class.

If the name field is the empty string or null, then throw a NullPointerException.

If the age field is not between 1-100, then throw an IllegalStateException

If either of the phone number fields is not exactly 12 characters, then throw an IllegalStateException

Implement the validate() method in your PersonalContact class.

Validate name and age as described above

If the address or city field is the empty string or null, then throw a NullPointerException.

If the state field is not exactly 2 characters, then throw an IllegalStateException

If the zip code field is not exactly 5 numeric characters, then throw an IllegalStateException

Note: you should not have any try-catch blocks in your contact classes.

Driver Program

Create a driver program that allows a user to create a personal or business contact. Your program should prompt the user for a contact type:

Create a new contact?
1. Personal
2. Business
2

It should then prompt the user for all details for that contact type:

Name? Susie Grace
Age? 39
Business Phone? 222-333-4444
Cell Phone? 555-666-7777

Call your validate() method to check for bad input and then print out the toString() of your new contact object.

Name? Susie Grace
Age? 39
Business Phone? 222-333-4444
Cell Phone? 555-666-7777

Business Contact: Susie Grace (39), business - 222-333-4444, cell - 555-666-7777

Validating User Input

Your driver program should have appropriate try-catch blocks to respond to bad user input. This includes NullPointerExceptions and IllegalStateExceptions. Each exception type should print an appropriate message to the console.

For example:

Name? Susie Grace
Age? -10
Business Phone? 222-333-4444
Cell Phone? 555-666-7777

Please enter a valid age from 1-100

Another Example:

Name? Susie Grace
Age? 39
Business Phone? 2223334444
Cell Phone? 555-666-7777

Please enter a phone number using the following format: ###-###-####.

Solution

import java.lang.Exception;
import java.util.*;
//Abstract class Contact
abstract class Contact
{
   //Instance variable
   String name;
   int age;
   String phoneNo;
   //Default constructor to initialize instance variables
   Contact()
   {
       name = null;
       age = 0;
       phoneNo = null;
   }//End of constructor
  
   //Abstract method to validate
   abstract int validate();
   //Abstract method to accept
   abstract void accept();
}//End of abstract class Contact


//Class BusinessContact derived from abstract class Contact
class BusinessContact extends Contact
{
   //Instance variable
   String businessPhone;
   //Default constructor
   BusinessContact()
   {
       //Calls base class constructor
       super();
       businessPhone = null;
   }//End of constructor
  
   //Overrides toString() method to display
   public String toString()
   {
       String msg;
       msg = \"Business Contact: \" + name + \"(\" + age + \"),\" + \" Business - \" + businessPhone + \" Cell - \" + phoneNo;
       return msg;
   }//End of toString() method
  
   //Overrides accept() method
   void accept()
   {
       //Creates a scanner class object
       Scanner sc = new Scanner(System.in);
       //Accepts data
       System.out.println(\"\ Name? \");
       name = sc.nextLine();
       System.out.println(\"\ Age? \");
       age = sc.nextInt();
       sc.nextLine();
       System.out.println(\"\ Business Phone? \");
       businessPhone = sc.nextLine();
       System.out.println(\"\ Cell Phone? \");
       phoneNo = sc.nextLine();
   }//End of accept method
  
   //Overrides validate method
   int validate()
   {
       int flag = 0;
       try
       {
           //Checks if name is zero length
           if(name.length() == 0)
               throw new NullPointerException();
       }
       catch(NullPointerException n)
       {
           System.out.println(\"Name cannot be left blank\");
           //Sets the flag to 1
           flag = 1;
       }
       try
       {
           //Checks age for between 1 - 100
           if(age <= 0)
               throw new IllegalStateException();
           if(age > 100)
               throw new IllegalStateException();
       }      
       catch(IllegalStateException ie)
       {
           System.out.println(\"Please enter a valid age from 1-100. \ Age must be between 1 and 100\");
           flag = 1;
       }
       try
       {
           //Checks phone number length for exactly 12
           if(phoneNo.length() != 12)
               throw new IllegalStateException();              
       }
       catch(IllegalStateException ie)
       {
           System.out.println(\"Please enter a phone number using the following format: ###-###-####. \ Phone number must be 12 character long\");
           flag = 1;
       }
       //returns the flag status
       return flag;
   }//End of method validate
  
}//End of class Business contact

//class PersonalContact derived from abstract class Contact
class PersonalContact extends Contact
{
   //Instance variable
   String city, state, zip;
   //Default constructor
   PersonalContact()
   {
       //Calls the base class constructor
       super();
       city = null;
       state = null;
       zip = null;
   }//End of constructor
  
   //Overrides toString() method to display
   public String toString()
   {
       String msg;
       msg = \"Personal Contact: \" + name + \"(\" + age + \"),\" + \" Cell - \" + phoneNo + \" City - \" + city + \" State - \" + state + \" Zip - \" + zip;
       return msg;
   }//End of toString() method
  
   //Overrides accept method
   void accept()
   {
       //Creates scanner class object
       Scanner sc = new Scanner(System.in);  
      
       //Accepts data
       System.out.println(\"\ Name? \");
       name = sc.nextLine();
       System.out.println(\"\ Age? \");
       age = sc.nextInt();
       sc.nextLine();
       System.out.println(\"\ City? \");
       city = sc.nextLine();
       System.out.println(\"\ State? \");
       state = sc.nextLine();
       System.out.println(\"\ Zip? \");
       zip = sc.nextLine();
       System.out.println(\"\ Phone Number? \");
       phoneNo = sc.nextLine();
   }//End of accept method
  
   //Overrides validate method
   int validate()
   {
       int flag = 0;
       try
       {
           //Checks name is null or not
           if(name.length() == 0)
               throw new NullPointerException();
       }
       catch(NullPointerException n)
       {
           System.out.println(\"Name cannot be left blank\");
           flag = 1;
       }
       try
       {
           //Checks age between 1 - 100
           if(age <= 0)
               throw new IllegalStateException();
           if(age > 100)
               throw new IllegalStateException();
       }      
       catch(IllegalStateException ie)
       {
           System.out.println(\"Please enter a valid age from 1-100. \ Age must be between 1 and 100\");
           flag = 1;
       }
       try
       {
           //Checks phone number must be exactly 12
           if(phoneNo.length() != 12)
               throw new IllegalStateException();              
       }
       catch(IllegalStateException ie)
       {
           System.out.println(\"Please enter a phone number using the following format: ###-###-####. \ Phone number must be 12 character long\");
           flag = 1;
       }
       try
       {
           //Checks city must not be null
           if(city.length() == 0)
               throw new NullPointerException();
       }
       catch(NullPointerException n)
       {
           System.out.println(\"City cannot be left blank\");
           flag = 1;
       }
       try
       {
           //Checks state length must be exactly 2
           if(state.length() != 2)
               throw new IllegalStateException();
       }
       catch(IllegalStateException ie)
       {
           System.out.println(\"State must be exactly 2 characters\");
           flag = 1;
       }
       try
       {
           //Checks zip must be 5 character long
           if(zip.length() != 5)
               throw new IllegalStateException();
       }
       catch(IllegalStateException n)
       {
           System.out.println(\"Zip must be exactly 5 numeric characters\");
           flag = 1;
       }
       //returns the status flag
       return flag;
   }//End of method validate
  
}//End of class Personal Contact

//Driver class
public class ContactDemo
{
   //Main method
   public static void main(String ss[])
   {
       //Scanner class object created
       Scanner sc = new Scanner(System.in);
       //PersonalContact class object created
       PersonalContact pc = new PersonalContact();
       //BusinessContact class object created
       BusinessContact bc = new BusinessContact();
       //Loops till user enters 3
       do
       {
           //Displays menu
           System.out.println(\"1) Personal Contact\");
           System.out.println(\"2) Business Contact\");
           System.out.println(\"3) Exit \");
           //Accepts user choice
           int choice = sc.nextInt();
           switch(choice)
           {
               case 1:
                   //Accepts personal contact
                   pc.accept();
                   //Calls the validate and checks the flag return value
                   //If it is zero then print personal contact
                   if(pc.validate() == 0)
                       System.out.println(pc);
                   break;
               case 2:
                   //Accepts business contact
                   bc.accept();
                   //Calls the validate and checks the flag return value
                   //If it is zero then print business contact
                   if(bc.validate() == 0)
                       System.out.println(bc);
                   break;
               case 3:
                   System.exit(0);
               default:
                   System.out.println(\"Invalid Choice\");
           }//End of switch
       }while(true);
   }//End of main method
}//End of driver class

Output:

1) Personal Contact
2) Business Contact
3) Exit
1

Name?


Age?
101

City?
bam

State?
ori

Zip?
123

Phone Number?
12-23-45
Name cannot be left blank
Please enter a valid age from 1-100.
Age must be between 1 and 100
Please enter a phone number using the following format: ###-###-####.
Phone number must be 12 character long
State must be exactly 2 characters
Zip must be exactly 5 numeric characters
1) Personal Contact
2) Business Contact
3) Exit
2

Name?


Age?
0

Business Phone?
12-23-45

Cell Phone?
12-23-45
Name cannot be left blank
Please enter a valid age from 1-100.
Age must be between 1 and 100
Please enter a phone number using the following format: ###-###-####.
Phone number must be 12 character long
1) Personal Contact
2) Business Contact
3) Exit
1

Name?
Pyari

Age?
32

City?
Bam

State?
OR

Zip?
76000

Phone Number?
123-345-1234
Personal Contact: Pyari(32), Cell - 123-345-1234 City - Bam State - OR Zip - 76000
1) Personal Contact
2) Business Contact
3) Exit
2

Name?
Mohan

Age?
23

Business Phone?
123-456-6541

Cell Phone?
654-123-4567
Business Contact: Mohan(23), Business - 123-456-6541 Cell - 654-123-4567
1) Personal Contact
2) Business Contact
3) Exit
3

Overview We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new c
Overview We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new c
Overview We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new c
Overview We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new c
Overview We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new c
Overview We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new c
Overview We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new c
Overview We will be adding some validation to our Contact classes to prevent bad values from being stored in the classes. We will also let the user create new c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site