3

I have a PersonalContact class and a BusinessContact class setup and I want to use this driver program to prompt the user for contact info. Once the info is entered I want to use the classes to print their toStrings to display the contact info. I think that I need to create a new PersonalContact or BusinessContact object right? The base class is called Contact and is an abstract class.

import java.util.Scanner;

public class PlannerMain {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        while (true) {

            System.out.println("Create new contact?");
            System.out.println("1.Personal contact ");
            System.out.println("2.Business Contact ");
            System.out.println("3.Exit.");

            int option = scanner.nextInt();

            if (option == 1) { //Create Personal Contact

                System.out.println("Name?");
                String name = scanner.next();

                System.out.println("Age?");
                int age = scanner.nextInt();

                System.out.println("Address?");
                String address = scanner.next();

                System.out.println("City?");
                String city = scanner.next();

                System.out.println("State?");
                String state = scanner.next();

                System.out.println("Zip");
                String zip = scanner.next();

                PersonalContact pc = new PersonalContact;

            } // End option 1

            else if (option == 2) { // Create Business Contact

                System.out.println("Name?");
                String name = scanner.next();

                System.out.println("Age?");
                int age = scanner.nextInt();

                System.out.println("Business Phone?");
                String businessPhone = scanner.next();

                System.out.println("Cellphone?");
                String cellPhone = scanner.next();

                BusinessContact bc = new BusinessContact;

            } // End option 2

            else if (option == 3) { /** Terminates the program */
                System.exit(0);
            } // End option 3
        } // End while

    }// End void main

}// End
Jeff
  • 9,076
  • 1
  • 19
  • 20
Chap
  • 61
  • 9
  • PersonalContact, BusinessContact classes need a constructors. You can pass in the name, age, etc to that constructor to make a contact. – Debosmit Ray Feb 15 '16 at 02:56

1 Answers1

2

if your person class is this:

public abstract class Contact{
    String name;
    int age;

    public Contact(String name, int age){
        this.name = name;
        this.age = age;
    }
}

then your BusinessContact could look like this

public class BusinessContact extends Contact {
    String businessPhone;
    String cellPhone;

    public BusinessContact(String name, int age, String businessPhone, String cellPhone){
        super(name, age);
        this.businessPhone = businessPhone;
        this.cellPhone = cellPhone;
    }

    public String toString(){
        return "Name: " + this.name + " Age: " + Integer.toString(age) + " Business Phone: " + businessPhone + " Cell Phone: " + cellphone;
    }
}

then you could replace this line:

BusinessContact bc = new BusinessContact;

with this:

BusinessContact bc = new BusinessContact(name, age, businessPhone, cellPhone);

System.out.println(bc.toString());
nlloyd
  • 1,966
  • 2
  • 15
  • 18
  • Thanks, I appreciate it. Could you also tell me where I could put try-catch blocks to deal with bad user input? My PersonalContact and BusinessContact classes have validate methods to throw exceptions but I think I need the try-catch blocks in the driver program right? – Chap Feb 15 '16 at 04:03
  • that's a good idea. you could put everything inside the `while` loop into a try-catch block and then just `catch(Exception ex)` with a `System.out.println("An Error Occurred...");`. This is quick and dirty you should also [see this answer for handling scanner errors].(http://stackoverflow.com/questions/3572160/how-to-handle-invalid-input-using-scanner-and-try-catch-currently-have-an-infin#3572233). You can then refine this by adding a more specific `catch(InputMismatchException ime)` and if you need more specific feedback you'll need to wrap each statement that reads user input in a try-catch – nlloyd Feb 15 '16 at 04:19