Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code to let user create business or personal contact

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
like image 694
Chap Avatar asked Dec 28 '25 16:12

Chap


1 Answers

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());
like image 144
nlloyd Avatar answered Dec 30 '25 04:12

nlloyd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!