I finally understood the various possibilities of Comparable from java.util.Collections;
I can order an object, for example "Beer" with attributes float content, String name and String origin in any order that I want, combining them how I want.
Now the question:
I have the following classes (code provided last):
Building can be one of three types, for example Dwelling, House, Bungalow.
Now, task is "to sort all buildings, first by the city (ascending), second by the street (ascending) and then by the street number (ascending).
Normally I would simply sort the list first by city, then by street and then by number. Of course after the first sort its solution would get mixed up by the following sorts.
Thats why I searched the net and found Comparable.
Thing is: It did work with the beer example, but does not with the address. Cause it is implemented as an ArrayList, and as I found out, it just works for Arrays.
But even if it did, the Addresses themselves are not an Array, but part of the Object Building.
So how on god's earth is one supposed to solved this?
I think I spent more than 8 hours with this cr*p, and the students are supposed to solve similiar issues within 2 - 3 hours.
Any suggestions are appreciated.
Thanks,
Neo
import java.util.List;
public abstract class Building {
private Address address;
private List<Owner> owners;
public abstract double getCostRate(double costs);
public void setAddress(Address address) {
this.address = address;
}
public Address getAddress() {
return address;
}
public List<Owner> getOwners() {
return owners;
}
}
Class Address:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import de.uni_mannheim.informatik.swt.pm.extended.ComparatorCityStreetNumber;
public class Address implements Comparable<Address> {
private String street;
private int houseNr;
private int zip;
private String city;
private String country;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public void setHouseNr(int houseNr) {
this.houseNr = houseNr;
}
public int getHouseNr() {
return houseNr;
}
/**
* eclipse is able to generate the equals method automatically ;)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Address other = (Address) obj;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (country == null) {
if (other.country != null)
return false;
} else if (!country.equals(other.country))
return false;
if (houseNr != other.houseNr)
return false;
if (street == null) {
if (other.street != null)
return false;
} else if (!street.equals(other.street))
return false;
if (zip != other.zip)
return false;
return true;
}
Comparator:
public class ComparatorCityStreetNumber implements Comparator<Address> {
public int compare(Address a1, Address a2) {
if (a1.getCity().compareTo(a2.getCity()) == 0) {
if (a1.getStreet().compareTo(a2.getStreet()) == 0) {
if (a1.getHouseNr() > a2.getHouseNr()) {
return -1;
} else {
return a1.getStreet().compareTo(a2.getStreet());
}
} else if (a1.getCity().compareTo(a2.getCity()) > 0) {
return -1;
} else {
return 1;
}
}
return 0;
}
}