Housing Project Part 2

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.5 kB
10
Indexable
Never
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Subdivision {
    private ArrayList<House> houses;

    public Subdivision() {
        houses = new ArrayList<>();
    }

    public boolean add(House h) {
        return houses.add(h);
    }

    public House get(int i) {
        if (i >= 0 && i < houses.size()) {
            return houses.get(i);
        }
        return null;
    }

    public int size() {
        return houses.size();
    }

    public ArrayList<House> list() {
        return new ArrayList<>(houses);
    }

    public ArrayList<House> listByBedrooms(int floor, int ceiling) {
        ArrayList<House> result = new ArrayList<>();
        for (House house : houses) {
            int bedrooms = house.getBedrooms();
            if (bedrooms >= floor && bedrooms <= ceiling) {
                result.add(house);
            }
        }
        return result;
    }

    public ArrayList<House> listByArea(double floor, double ceiling) {
        ArrayList<House> result = new ArrayList<>();
        for (House house : houses) {
            double totalArea = house.getTotalArea();
            if (totalArea >= floor && totalArea <= ceiling) {
                result.add(house);
            }
        }
        return result;
    }

    public ArrayList<House> listByPlot(double floor, double ceiling) {
        ArrayList<House> result = new ArrayList<>();
        for (House house : houses) {
            double plotSize = house.getPlot();
            if (plotSize >= floor && plotSize <= ceiling) {
                result.add(house);
            }
        }
        return result;
    }

    public ArrayList<House> sortByPlot() {
        ArrayList<House> sortedHouses = new ArrayList<>(houses);
        Collections.sort(sortedHouses, new Comparator<House>() {
            public int compare(House h1, House h2) {
                return Double.compare(h1.getPlot(), h2.getPlot());
            }
        });
        return sortedHouses;
    }

    public ArrayList<House> sortByArea() {
        ArrayList<House> sortedHouses = new ArrayList<>(houses);
        Collections.sort(sortedHouses, new Comparator<House>() {
            public int compare(House h1, House h2) {
                return Double.compare(h1.getTotalArea(), h2.getTotalArea());
            }
        });
        return sortedHouses;
    }
}
Leave a Comment