0

Im working on a problem where I cannot use any of the collections, saving to file, arrays, maps & I need to create ObjectContainer for it to add a person to the one way list & check if the city used in the constructor matches the people added to the list.

It seems that its throwing exception that it didnt find a match of the city. Could you advise how to check if the city that has been added to the costructor of the ObjectContainer, matches the one added to the Person?

ublic static void main(String[] args) {

    ObjectContainer<Person> peopleFromWarsaw = new ObjectContainer<>(p -> p.getCity().equals("Warsaw"));

    peopleFromWarsaw.add(new Person("Jan", "Warsaw", 30));
    peopleFromWarsaw.add(new Person("Weronika","Warsaw", 20));
    peopleFromWarsaw.add(new Person("Waldek", "Monaco", 34));

    for(int i = 0; i < peopleFromWarsaw.getSize(); i++){
        System.out.println(peopleFromWarsaw);
    }

}

Public class Person {

private String name;
private String city;
private int age;

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

public class ObjectContainer<T> {

private int size = 0;
private StringElement<T> head = new StringElement<>(null);
private Function<T, Boolean> personal;
private Person person;


public ObjectContainer(Function<T, Boolean> personal) {
    clear();
    this.personal = personal;
}

public void clear() {
    head.setNext(null);
    size = 0;
}

public void add(Person value) {
    try {
        if (head.getNext() == null) {
            {
                head.setNext(new StringElement(value));
            }
            StringElement last = head.getNext();
            while (last.getNext() != null)
                last = last.getNext();
            ++size;
            last.setNext(new StringElement(value));
        } else {
            throw new InvalidStringContainerPatternException("Niee dzialaaaaa");
        }
   } catch (InvalidStringContainerPatternException e) {
        e.printStackTrace();
    }
}

public boolean delete(String o) {
    if (head.getNext() == null) return false;
    if (head.getNext().getValue().equals(o)) {
        head.setNext(head.getNext().getNext());
        size--;
        return true;
    }
    StringElement delete = head.getNext();
    while (delete != null && delete.getNext() != null) {
        if (delete.getNext().getValue().equals(o)) {
            delete.setNext(delete.getNext().getNext());
            size--;
            return true;
        }
        delete = delete.getNext();
    }
    return false;

}

public boolean delete(int index) {
    if (head.getNext() == null) return false;
    if (head.getNext().getIndex() == index) {
        head.setNext(head.getNext().getNext());
        size--;
        return true;
    }
    StringElement delete = head.getNext();
    while (delete != null && delete.getNext() != null) {
        if (delete.getNext().getValue().equals(index)) {
            delete.setNext(delete.getNext().getNext());
            size--;
            return true;
        }
        delete = delete.getNext();
    }
    return false;

}


public boolean isEmpty() {
    return size == 0;
}

public int getSize() {
    return size;
}

public Object get(int position) {
    if (position < 0 || position > size) {
        throw new IndexOutOfBoundsException();
    }
    StringElement find = head.getNext();
    for (int i = 0; i <= position; i++)
        find = find.getNext();
    return find.getValue();
}

}

wieszak09
  • 59
  • 6

0 Answers0