1

I'm trying to make a program in java that you can add people's birthdays, names, birthmonths, and birthyears. I'm having a hard time trying to come up with the code to remove an object from the arraylist here is the following code. How would I go about writing the removePerson method?

import java.util.ArrayList;

public class Analyzer {
    // instance variables - replace the example below with your own
    private final static int DAYS_PER_MONTH = 31;
    private final static int MONTHS_PER_YEAR = 12;
    private int[] birthDayStats;
    private int[] birthMonthStats;
    private ArrayList<Person> people;

    /**
     * Constructor for objects of class Analyzer
     */
    public Analyzer() {
        this.people = new ArrayList<Person>();
        this.birthDayStats = new int[Analyzer.DAYS_PER_MONTH];
        this.birthMonthStats = new int[Analyzer.MONTHS_PER_YEAR];
    }

    public void addPerson(String name, int birthDay, int birthMonth, int
            birthYear) {
        Person person = new Person(name, birthDay, birthMonth, birthYear);
        if (person.getBirthDay() != -1 || person.getBirthMonth() != -1) {
            people.add(person);
            birthMonthStats[birthMonth - 1]++;
            birthDayStats[birthDay - 1]++;
        } else {
            System.out.println("Your current Birthday is " + birthDay + " or "
                    + birthMonth + " which is not a correct number 1-31 or 1-12 please " +
                    "put in a correct number ");
        }
    }

    public void printPeople() { //prints all people in form: “  Name: Tom   Month: 5   Day: 2  Year: 1965”
        int index = 0;
        while (index < people.size()) {
            Person person = (Person) people.get(index);
            System.out.println(person);
            index++;
        }
    }

    public void printMonthList() { //prints the number of people born in each month
        // Sample output to the right with days being similar
        int index = 0;
        while (index < birthMonthStats.length) {
            System.out.println("Month number " + (index + 1) + " has " +
                    birthMonthStats[index] + " people");
            index++;
        }
    }

    public Person removePerson(String name) {// removes the person from the arrayList
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Stevo4586
  • 7
  • 1
  • 5

2 Answers2

1
/**
 * Removes the {@code Person} with the given {@code name} from the list
 * @param name the {@code Person}'s name
 * @return the {@code Person} removed from the list or {@code null} if not found
 */
public Person removePerson(String name) {
    if (name != null) {
        for (Iterator<Person> iter = people.iterator(); iter.hasNext(); ) {
            Person person = iter.next();
            if (name.equalsIgnoreCase(person.getName())) {
                iter.remove();
                return person;
            }
        }
    }
    return null;
}

See the java.util.Iterator#remove() method.


Tuesday learning bonus:

If you want to look for a name faster in your list, you should consider to use a java.util.Map implementation:

HashMap<String,Person> people;

You can add Person objects in a smart way in order to make your search case insensitive:

people.put(name.toLowerCase(), new Person(name, ...));

... and your removePerson method become:

public Person removePerson(String name) {
    if (name != null)
        name = name.toLowerCase();
    return people.remove(name);
}

See the java.util.Map#remove() method.

fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
  • same as @OH GOD SPIDERS's comment – fantaghirocco Sep 26 '17 at 13:48
  • someone that understands that i'm just learning java and needed help literally that's all. – Stevo4586 Sep 26 '17 at 13:49
  • exactly what im saying – Stevo4586 Sep 26 '17 at 14:01
  • is it possible to remove the stats from the array too? Because the method you gave just gets rid of the name and not the birth year, birth day, and birth month. – Stevo4586 Sep 27 '17 at 13:17
  • it should be easy to modify the code provided to get what is required, shouldn't it? The given method simply meets the question requirements. You can think of a new method which removes all the `Person`s found for a given `stat` or a method that accepts a `Person` as argument which looks for its non-null properties in order to identify the candidates for the deletion – fantaghirocco Sep 27 '17 at 16:18
1

If you are using Java 1.8. It's pretty simple way to do. This will remove Person having 'name' from your list.

 people.removeIf(x -> name.equalsIgnoreCase(x.getName()));
nagendra547
  • 5,672
  • 3
  • 29
  • 43