Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<Property> properties = List.of(
new Apartment(90.0, 3, 2),
new House(80.0, 3, 3, true),
new Duplex(250.0, 3, 6, false)
);

List<String> types = properties.stream()
.map(Property::getPropertyType)
.toList();

assertEquals(List.of("Apartment", "Single-Family House", "Duplex (2 Units)"), types);
}
}