Compare Generic Items?

advertisements

I am currently stuck on a particular part of my code. For my class, we are to create a train containing boxcars containing people or cargo. We use generics to define whether a boxcar can hold people or cargo. Then we load individual people/cargo onto the boxcar and if it has the same String "ID" as someone already on the boxcar then we log an error and do not load that person/cargo. This is where I'm having trouble. I cannot for the life of me figure out how to compare their "ID's" to see if they are equal. The following is the code that I have so far,

package proj5;

public class Person implements Comparable<Person> {

private String id;
private String name;
private int age;

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

public Person(String id){
    this.id = id;
}

public String getId(){
    return id;
}

public int getAge(){
    return age;
}

public String getName(){
    return name;
}

public String toString(){
    String str = "        " + "ID: " + id + "  " + " Name: " + name + "  " + " Age: " + age;
    return str;
}

public int compareTo(Person p) {
    int result = this.id.compareTo(p.getId());
    return result;
}

}

package proj5;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class Boxcar<T extends Comparable<T>> {

private ArrayList<T> boxcar;
private int maxItems;
private int boxcarID;

public Boxcar(){
    boxcar = new ArrayList<T>();
}

public void load(T thing){
    for(int i = 0; i < boxcar.size(); i++){
    if(boxcar.size() < maxItems && !boxcar.get(i).equals(thing)){
        boxcar.add(thing);
        System.out.println(boxcar.get(i));
    }
    else{
        boxcar.remove(thing);
    }
    }
    Collections.sort(boxcar);
}

public int getBoxcarId(){
    return boxcarID;
}

public int getMaxItems(){
    return maxItems;
}

public void setMaxItems(int i){
    maxItems = i;
}

public void unload(T thing){
    for(T item : boxcar){
        if(item.equals(thing)){
            boxcar.remove(item);
        }
    }
}

public List<T> getBoxcar(){
    return boxcar;
}

public String toString(){
    String str = "";
    for(T item : boxcar){
        str += item + "\n";
    }
    return str;
}

}

The problem is with my load function. I do not know how to compare their ID's. For clarification, the objects ID's are Strings. I have other classes but I have only included the ones I thought were necessary. If you need more files I will be happy to provide them. I have been stuck on this for hours and would appreciate any help! Thank you very much in advance!

EDIT: I have tried using the contains() method from the Collections API but why does that not work? It sounds like it would work perfectly.


You have to implement the equals and hashCode for your class Person.

The problem is that boxcar.get(i).equals(thing) is calling the generic equals and its only comparing the references.

so a generic equals will look like.

public boolean equals(Object obj){
  if (obj == null) return false;
  if (obj == this) return true;
  if (obj instanceof Person){
    Person p = (Person) obj;
    return p.getId().equals(this.getId());
  }
  return false;
}

and hashCode can be like this

public int hashCode(){
  return 37*this.getId().hashCode();
}