1

So, I have three classes where the Class1 is used for different objects. In main class, the Class1 objects are made and names are set. Main class also makes new Household objects, the names are given to households and finally, persons are added to households as follows:

HouseholdOne.addPeopleToHousehold(person1);

The main class does not concern in present problem.

public class Class1 {
    private String firstName;
    private String middleName;
    private String lastName;

    public String setFirstName(String firstName) {
        return this.firstName = firstName;
    }
    public String setLastName(String lastName) {
        return this.lastName = lastName;
    }
    public String setMiddleName(String middleName) {
        return this.middleName = middleName;
    }
    public String toString() {
        if(firstName == "" && middleName == "" && lastName == "") {
            return "";
        } else if (firstName == null && middleName == null && lastName == null) {
            return null;
        } else
        return firstName + " \"" + middleName + "\" " + lastName;
    }
}

In the second class Household "addPeopleToHousehold" method the middle name of person should be parsed out from parameter and added to people list.

I had in mind to use .split function, but it does not work with object parameter. How can I get given person middle name and

peopleList.add(personMiddleName)

Also in Household class, toString should print out household members like:

member1, member2, ... , memberx

public class Household {

    List<Class1> peopleList = new ArrayList<>();
    public String householdName;

    public String setHouseholdName(String householdName) {
        return this.householdName = householdName;
    }

    public void addPeopleToHousehold(Class1 people) {
        // implementation needed //
    }

    public int getNumberOfPeople() {
        return people.size();
    }

    public String toString() {
        if (householdName == null || householdName == "") {
            return "" + people;
        }
        return householdName + ": " + people;
    }
}
3
  • I am aware of the fact that I should have used constructor for Class1, but the assignment was given this way. Commented Mar 9, 2016 at 19:17
  • You can create a public get method (similar to the set methods you have) for middleName in Class1. That way you can do something like this: peopleList.add(people.getMiddleName()) Commented Mar 9, 2016 at 19:19
  • I have tried that. Then it is necessary to make new method in Class1: public Class1 getMiddleName() { return middleName; } But then in the field middleName have to be changed to Class1 middle name and that just messes up the structure of program. Commented Mar 9, 2016 at 19:35

2 Answers 2

1

Something like this perhaps? It was difficult to decipher your post, but the signature from the various methods says a lot. My changes simply adds people do the ArrayList peopleList. When you print the Household it will first check if a valid household name exists, after that it will loop each individual in the Household and add their full name to the list. The ((i + 1) != peopleList.size()) is only used to separate the names with a , except the last name.

Updated Class1. Fixed the setters and added a public getter for middle name.

public class Class1 {

    private String firstName;
    private String middleName;
    private String lastName;

    /*
     * Setters
     */

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    /*
     * Getters
     */

    public String getMiddleName() {
        return middleName;
    }

    /*
     * toString
     */

    public String toString() {
        if(firstName == "" && middleName == "" && lastName == "") {
            return "";
        } else if (firstName == null && middleName == null && lastName == null) {
            return null;
        } else
        return firstName + " \"" + middleName + "\" " + lastName;
    }
}

Updated Household class. Various fixes:

public class Household {

    List<Class1> peopleList = new ArrayList<>();
    public String householdName;

    public String setHouseholdName(String householdName) {
        this.householdName = householdName;
    }

    public void addPeopleToHousehold(Class1 people) {
        peopleList.add(people);
    }

    public int getNumberOfPeople() {
        return peopleList.size();
    }

    public String toString() {
        String returnString = "";
        if (householdName != null) {
            returnString = householdName + ": ";
        }

        // Loop the members
        for (int i = 0; i < peopleList.size(); i++) {
            returnString += peopleList.get(i).getMiddleName();
            if ((i + 1) != peopleList.size()) {
                returnString += ", ";
            }
        }

        return returnString;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the String method solution. But I cannot just add people aka person to peopleList because it will come as (John Detective McClane) but I only need to add list Detective.
You mean you will do addPeopleToHousehold("Detective"); when you add people to a household?
The public void addPeopleToHousehold(Class1 people) takes in the whole name, but the middleName is parsed out. It should be pointed out, that the middle name is between quotation marks.
@user3260577 Ah. You need to understand that you must add the entire object to the addPeopleToHousehold method because it only accepts Class1 objects. What you do is when you output the members of the household you only grab the middle name. Let me show you.
0

In addition to the answer of OptimusCrime, you might want to use StringBuilder instead of just using returnString += to append strings.

The drawback of using += to append strings is it will create new String objects whenever you append, so you will have greater memory consumption.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.