diff --git a/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Apartment.java b/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Apartment.java new file mode 100644 index 0000000..4c26f68 --- /dev/null +++ b/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Apartment.java @@ -0,0 +1,25 @@ +package com.example.apartment_predictor.lab01; + +public class Apartment extends Property { + + protected int numberOfRooms; + + public Apartment(double area, int locationRating, int numberOfRooms) { + super(area, locationRating); + if (numberOfRooms <= 0) { + throw new IllegalArgumentException("numberOfRooms must be > 0. Provided: " + numberOfRooms); + } + this.numberOfRooms = numberOfRooms; + } + + @Override + public String getPropertyType() { + return "Apartment"; + } + + @Override + public double calculatePrice() { + double basePrice = area * 120 + (numberOfRooms * 8000); + return basePrice * (1 + (locationRating * 0.04)); + } +} diff --git a/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Duplex.java b/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Duplex.java new file mode 100644 index 0000000..c119bf1 --- /dev/null +++ b/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Duplex.java @@ -0,0 +1,25 @@ +package com.example.apartment_predictor.lab01; + +public class Duplex extends Apartment { + + private boolean hasSeparateUtilities; + + public Duplex(double area, int locationRating, int numberOfRooms, boolean hasSeparateUtilities) { + super(area, locationRating, numberOfRooms); + this.hasSeparateUtilities = hasSeparateUtilities; + } + + @Override + public String getPropertyType() { + return "Duplex (2 Units)"; + } + + @Override + public double calculatePrice() { + double basePrice = area * 220 + (numberOfRooms * 12000); + if (hasSeparateUtilities) { + basePrice *= 1.15; + } + return basePrice * (1 + (locationRating * 0.09)); + } +} diff --git a/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/House.java b/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/House.java new file mode 100644 index 0000000..2539a84 --- /dev/null +++ b/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/House.java @@ -0,0 +1,25 @@ +package com.example.apartment_predictor.lab01; + +public class House extends Apartment { + + private boolean hasGarage; + + public House(double area, int locationRating, int numberOfRooms, boolean hasGarage) { + super(area, locationRating, numberOfRooms); + this.hasGarage = hasGarage; + } + + @Override + public String getPropertyType() { + return "Single-Family House"; + } + + @Override + public double calculatePrice() { + double basePrice = area * 180 + (numberOfRooms * 15000); + if (hasGarage) { + basePrice += 25000; + } + return basePrice * (1 + (locationRating * 0.06)); + } +} diff --git a/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Property.java b/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Property.java new file mode 100644 index 0000000..59e4be9 --- /dev/null +++ b/ApartmentPredictor/src/main/java/com/example/apartment_predictor/lab01/Property.java @@ -0,0 +1,24 @@ +package com.example.apartment_predictor.lab01; + +public abstract class Property { + + protected double area; + protected int locationRating; + + protected Property(double area, int locationRating) { + if (area <= 0) { + throw new IllegalArgumentException("area must be > 0. Provided: " + area); + } + if (locationRating < 0 || locationRating > 5) { + throw new IllegalArgumentException("locationRating must be between 0 and 5. Provided: " + locationRating); + } + this.area = area; + this.locationRating = locationRating; + } + + // Lab Variant 1 + public abstract String getPropertyType(); + + // Lo dejamos porque el enunciado base lo tenĂ­a (aunque no lo uses ahora) + public abstract double calculatePrice(); +} diff --git a/ApartmentPredictor/src/test/java/com/example/apartment_predictor/lab01/PropertyTypePolymorphismTest.java b/ApartmentPredictor/src/test/java/com/example/apartment_predictor/lab01/PropertyTypePolymorphismTest.java new file mode 100644 index 0000000..4bf2086 --- /dev/null +++ b/ApartmentPredictor/src/test/java/com/example/apartment_predictor/lab01/PropertyTypePolymorphismTest.java @@ -0,0 +1,36 @@ +package com.example.apartment_predictor.lab01; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class PropertyTypePolymorphismTest { + + @Test + void getPropertyType_returnsCorrectType_forEachSubclass() { + Property apartment = new Apartment(90.0, 3, 2); + Property house = new House(80.0, 3, 3, true); + Property duplex = new Duplex(250.0, 3, 6, false); + + assertEquals("Apartment", apartment.getPropertyType()); + assertEquals("Single-Family House", house.getPropertyType()); + assertEquals("Duplex (2 Units)", duplex.getPropertyType()); + } + + @Test + void getPropertyType_polymorphismWorks_insideAListOfProperty() { + List properties = List.of( + new Apartment(90.0, 3, 2), + new House(80.0, 3, 3, true), + new Duplex(250.0, 3, 6, false) + ); + + List types = properties.stream() + .map(Property::getPropertyType) + .toList(); + + assertEquals(List.of("Apartment", "Single-Family House", "Duplex (2 Units)"), types); + } +}