user3196742
user3196742

Reputation: 27

Combining two if statements

I am only allowed to have 4 options in my main menu and I created 5. I can not figure out how to put the last 2 output statement together right so one can be called from inside of the other. So they can both use the same menu option.

Option 3 and 4 need to be inside each other somehow so I can display the Names of all the contacts entered and then prompt the user to select a contact ID to display the rest of the details of that contact.

Have tried combining both into the one if statement and it does not display the full name correctly for the second contact added in. It displays the first name again and then asks for a contact ID to display details. If you display details then is will give back the correct details and the fist and last name of the last contact entered.

Here is what I have when they are seperate:

package ooo1;

import java.util.ArrayList;
import java.util.Scanner;

public class ContactList {

    public static void main(String[] args) {

        ArrayList<Contact> contacts = new ArrayList<>();

        Scanner input1 = new Scanner(System.in);
        int type = 0;
        while(type != 5){
        System.out.println("Please select an option: ");
        System.out.println("Add a Personal Contact: Enter 1 ");
        System.out.println("Add a Business Contact: Enter 2 ");
        System.out.println("Display Contacts List: Enter 3 ");
        System.out.println("Display Contact Details: Enter 4 ");
        System.out.println("To Quit: Enter 5 ");

        type = input1.nextInt();

        if(type == 5){
            System.out.println("Goodbye ");
            break;
        }
 if (type == 1 || type == 2){

         Contact contact = null;

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter ContactId : ");
        String contactId = input.nextLine();
        System.out.println("Please enter First Name : ");
        String firstName = input.nextLine();
        System.out.println("Please enter Last Name : ");
        String lastName = input.nextLine();
        System.out.println("Please enter Address in the following format : ");
        System.out.println("Street Address, City, State, Zip Code");
        String address = input.nextLine();
        System.out.println("Please enter Phone Number : ");
        String phoneNumber = input.nextLine();
        System.out.println("Please enter Email Address : ");
        String emailAddress = input.nextLine();

        //Create a personal contact.
        if(type == 1){
           System.out.println("Please enter Birthday: ");
           String dateofBirth = input.nextLine();
           Contact pcontact = new PersonalContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, dateofBirth);
           contacts.add(pcontact);
           System.out.println("Contact Added Successfully");
           System.out.println();
        }
        //Create a business contact.
        else if(type == 2){
            System.out.println("Please enter Job Title: ");
            String jobTitle = input.nextLine();
            System.out.println("Please enter Organization: ");
            String organization = input.nextLine();
            Contact bcontact = new BusinessContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, jobTitle, organization);
            contacts.add(bcontact);
            System.out.println("Contact Added Successfully");
            System.out.println();
        }

    }
    if (type == 3 || type == 4){

        //Print full name of each Contact.
        if(type == 3){
            for (Contact namecontact: contacts)
            {
                System.out.println(namecontact.displayFullName());
                System.out.println();
            }
        } 
        //Print contact details for selected contact.
        else if(type == 4){
                System.out.println("Enter a Contact ID to display Contact Details: ");
                Scanner input2 = new Scanner(System.in);
                String soughtID;
                soughtID = input2.nextLine();
            for (Contact showcontact1: contacts)
                {
                   if (showcontact1.displayId().equals(soughtID))
                    System.out.println(showcontact1.displayContact());
                   System.out.println();
                }
            }
         }
      }
  }
}

This is the Parent class they are calling from:

package ooo1;

public abstract class Contact {

    String contactId;
    String firstName;
    String lastName;
    String address;
    String phoneNumber;
    String emailAddress;

    public Contact(String contactId,String firstName,String lastName, String address, String phoneNumber, String emailAddress)
    {
        this.contactId = contactId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.emailAddress = emailAddress;
    }
    public void setContactId(String input){
        this.contactId = input;
    }
    public String getContactId(){
        return contactId;
    }

    public void setFirstName(String input){
        this.firstName = input;
    }
    public String getFirstName(){
        return firstName;
    }

    public void setLastName(String input){
        this.lastName = input;
    }
    public String getLastName(){
        return lastName;
    }

    public void setAddress(String input){
        this.address = input;
    }
    public String getAddress(){
        return address;
    }

    public void setPhoneNumber(String input){
        this.phoneNumber = input;
    }
    public String getPhoneNumber(){
        return phoneNumber;
    }

    public void setEmailAddress(String input){
        this.emailAddress = input;
    }
    public String getEmailAddress(){
        return emailAddress;
    }

    @Override
    public String toString(){
       return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName() + "\nAddress: " + this.getAddress() + "\nPhone Number: " + this.getPhoneNumber() + "\nEmail Address " + this.getEmailAddress());
    }

    public String displayFullName(){
        return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName());
    }

    public String displayContact(){
        return ("ContactID: " + this.getContactId() + "\nFirst Name: " + this.getFirstName() + "\nLast Name: " + this.getLastName() + "\nAddress :" + this.getAddress() + "\nPhone Number :" + this.getPhoneNumber() + "\nEmail Address " + this.getEmailAddress());
    }
    public String displayId(){
        return (this.getContactId());
    }          
}

This is what I tried to change in main to get rid of the other option and results not good:

if(type == 3){ for (Contact namecontact: contacts) { System.out.println(namecontact.displayFullName()); System.out.println(); System.out.println("Enter a Contact ID to display Contact Details: "); Scanner input2 = new Scanner(System.in); String soughtID; soughtID = input2.nextLine(); for (Contact showcontact1: contacts) { if (showcontact1.displayId().equals(soughtID)) System.out.println(showcontact1.displayContact()); System.out.println();

Here is the output the way it was before change that was alright, and the way it is after change.

This is what I had before change which is alright except I have too many options in the main menu.

Please select an option: Add a Personal Contact: Enter 1 Add a Business Contact: Enter 2 Display Contacts List: Enter 3 Display Contact Details: Enter 4 To Quit: Enter 5 3 ContactID: 2 First Name: Tom Last Name: Jones

Please select an option: Add a Personal Contact: Enter 1 Add a Business Contact: Enter 2 Display Contacts List: Enter 3 Display Contact Details: Enter 4 To Quit: Enter 5 4 Enter a Contact ID to display Contact Details: 2 Personal Contact: ContactID: 2 First Name: Tom Last Name: Jones Address :234 West St Phone Number :123-345-2345 Email Address [email protected] Date of Birth: 12-12-1893

This is what happens when I put the 2 together. It takes new entery. Jane Smith is added. When I enter 3 to display contacts. I want it to give me back Jane and Tom, but it gives me back Tom. When I ask for Toms details I get Toms details but I also get Janes first and last name I wanted earlier.

Please enter ContactId : 1 Please enter First Name : Jane Please enter Last Name : Smith Please enter Address in the following format : Street Address, City, State, Zip Code 234 howard st Please enter Phone Number : 123-235-2345 Please enter Email Address : [email protected] Please enter Birthday: 12-12-1978 Contact Added Successfully

Please select an option: Add a Personal Contact: Enter 1 Add a Business Contact: Enter 2 Display Contacts List: Enter 3
To Quit: Enter 5 3 ContactID: 12 First Name: Tom Last Name: Hones

Enter a Contact ID to display Contact Details: 12 Personal Contact: ContactID: 12 First Name: Tom Last Name: Hones Address :234 south st Phone Number :234-232-2356 Email Address [email protected] Date of Birth: 12-12-45

ContactID: 1 First Name: Jane Last Name: Smith

Upvotes: 0

Views: 190

Answers (2)

xlm
xlm

Reputation: 7594

So if I understand correctly you want to nest option 3 and 4 such that option 3 displays all contacts then gives choice to display a specific contact:

// Other menu code here...
System.out.println("Query Contacts: Enter 3");
System.out.println("To Quit: Enter 4");

// Other code here...

// Bring up sub-menu
// Feel free to extend to return to main menu etc.
if (type == 3){
    while (true) {
       System.out.println("List Contacts: Enter 1");
       System.out.println("Display Contact Details: Enter 2");
       System.out.println("To Quit: Enter 3");

       // If you need, store in another int etc.
       type = input1.nextInt();

       //Print full name of each Contact.
       if (type == 1) {
          for (Contact namecontact: contacts) {
             System.out.println(namecontact.displayFullName());
             System.out.println();
          }
       }
       //Print contact details for selected contact.
       else if(type == 2){
          System.out.println("Enter a Contact ID to display Contact Details: ");
          Scanner input2 = new Scanner(System.in);
          String soughtID;
          soughtID = input2.nextLine();

          for (Contact showcontact1: contacts) {
              // Although correct, I'd recommend you add a braces for if statements
              // Saving a line for a closing brace is not generally worth it
              if (showcontact1.displayId().equals(soughtID))
                 System.out.println(showcontact1.displayContact());
              System.out.println();
          }
       } else if (type == 3) {
          break;
       }
    }
}       

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201429

You could do something like this,

System.out.println("Please select an option (1-4), any other number to Quit");
System.out.println("Add a Personal Contact: Enter 1 ");
System.out.println("Add a Business Contact: Enter 2 ");
System.out.println("Display Contacts List: Enter 3 ");
System.out.println("Display Contact Details: Enter 4 ");
type = input1.nextInt();
if (type < 1 || type > 4) {
  System.out.println("Goodbye ");
  break;
}

Upvotes: 1

Related Questions