housing project step 3

mail@pastecode.io avatar
unknown
java
2 months ago
3.0 kB
12
Indexable
Never
import java.util.ArrayList;

public class Subdivision {
    private ArrayList<House> houses;

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

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

    public House get(int index) {
        return houses.get(index);
    }

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

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

    public House find(String style) {
        for (House house : houses) {
            if (house.getStyle().equalsIgnoreCase(style)) {
                return house;
            }
        }
        return null;
    }

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

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

    public ArrayList<House> sortByArea() {
        ArrayList<House> sortedHouses = new ArrayList<>(houses);
        for (int i = 0; i < sortedHouses.size() - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < sortedHouses.size(); j++) {
                if (sortedHouses.get(j).compareArea(sortedHouses.get(minIndex)) < 0) {
                    minIndex = j;
                }
            }
            House temp = sortedHouses.get(minIndex);
            sortedHouses.set(minIndex, sortedHouses.get(i));
            sortedHouses.set(i, temp);
        }
        return sortedHouses;
    }

    public ArrayList<House> sortByPlot() {
        ArrayList<House> sortedHouses = new ArrayList<>(houses);
        for (int i = 0; i < sortedHouses.size() - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < sortedHouses.size(); j++) {
                if (sortedHouses.get(j).getPlot() < sortedHouses.get(minIndex).getPlot()) {
                    minIndex = j;
                }
            }
            House temp = sortedHouses.get(minIndex);
            sortedHouses.set(minIndex, sortedHouses.get(i));
            sortedHouses.set(i, temp);
        }
        return sortedHouses;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (House house : houses) {
            sb.append(house).append("\n");
        }
        return sb.toString();
    }
}
Leave a Comment