diff --git a/src/main/java/l_1/House.java b/src/main/java/l_1/House.java index fdb41128c1f2a0127da948c5c963a3ced2f3ab9a..bd3d8ab4ac7b6111a10f4dfbbd2784b205622260 100644 --- a/src/main/java/l_1/House.java +++ b/src/main/java/l_1/House.java @@ -7,31 +7,67 @@ public class House { private List<Room> rooms = new ArrayList<Room>(); private boolean hasLounge = false; + int numberOfSmokeAlarms = 1; public House() { } + /** + * We can have more than one constructor in a class. This is called overloading. + * It means we can create a house with a different number of smoke alarms. + * + * @param numberOfSmokeAlarms + */ + public House(int numberOfSmokeAlarms) { + this.numberOfSmokeAlarms = numberOfSmokeAlarms; + } + + /** + * Get the number of smoke alarms in the house + * + * @return the number of smoke alarms in the house + */ + + public int getNumberOfSmokeAlarms() { + return numberOfSmokeAlarms; + } + + /** + * Set the number of smoke alarms in the house + * + * @param numberOfSmokeAlarms + */ + + public void setNumberOfSmokeAlarms(int numberOfSmokeAlarms) { + + if (numberOfSmokeAlarms < 1) { + throw new IllegalArgumentException("Error: A house must have one or more smoke detectors"); + } + + this.numberOfSmokeAlarms = numberOfSmokeAlarms; + } + /** * simple method to add a room to a house * * @param room */ - public void addRoom(Room room) throws IllegalArgumentException{ - + public void addRoom(Room room) throws IllegalArgumentException { + if (room instanceof Lounge) { - + if (hasLounge) { - + throw new IllegalArgumentException("Error: Only one lounge allowed"); - + } else { this.hasLounge = true; } - - } - + + } + this.rooms.add(room); } diff --git a/src/test/java/l_1/HouseTest.java b/src/test/java/l_1/HouseTest.java index 996bbbca65ebeada8986c55f840ec370c0a7832e..bc5353b685ed26788606408d06aa50f20f2950d0 100644 --- a/src/test/java/l_1/HouseTest.java +++ b/src/test/java/l_1/HouseTest.java @@ -1,13 +1,13 @@ package l_1; -import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.BeforeEach; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - /** * This is a simple test class which provides until tests for the House class * from exercise 1 @@ -16,64 +16,90 @@ import static org.junit.jupiter.api.Assertions.assertEquals; * */ class HouseTest { - + private House house; - - + @BeforeEach public void setUp() { - house = new House(); - - /** Set up a particular kind of house */ + house = new House(); + + /** Set up a particular kind of house */ Lounge lounge1 = new Lounge(10.0); DiningRoom dining1 = new DiningRoom(16.0); Bedroom bedroom = new Bedroom(10.0); - + house.addRoom(lounge1); house.addRoom(dining1); house.addRoom(bedroom); } - + /*** - * 2. A house may be associated with many rooms but there can only be one lounge + * 2. A house may be associated with many rooms but there can only be one lounge */ - + @Test public void testRequirement2() { - try { + try { - Lounge l2 = new Lounge(12.0); // Attempting to add a second lounge - house.addRoom(l2); + Lounge l2 = new Lounge(12.0); // Attempting to add a second lounge + house.addRoom(l2); - // If the code reaches here without throwing an exception, you can fail the test - fail("Expected IllegalArgumentException, but it was not thrown."); + // If the code reaches here without throwing an exception, you can fail the test + fail("Expected IllegalArgumentException, but it was not thrown."); - } catch (IllegalArgumentException exception) { - // Asserting that the exception's message contains the expected error message - assertTrue(exception.getMessage().contains("Error: Only one lounge allowed")); - } + } catch (IllegalArgumentException exception) { + // Asserting that the exception's message contains the expected error message + assertTrue(exception.getMessage().contains("Error: Only one lounge allowed")); + } } - + /** - * 3. The house shall be able to display how many rooms it has. + * 3. The house shall be able to display how many rooms it has. */ @Test void testRequirement3() { assertEquals(3, house.howManyRooms()); } - + /** - * 4. The house shall be able to display the total area of all its rooms. - */; + * 4. The house shall be able to display the total area of all its rooms. + */ + ; + @Test void testRequirement4() { assertEquals(36.0, house.getTotalArea()); } - + + /** + * 5. If we create a house without specifying the number of smoke detectors, + * then the house must have one smoke detector. + */ + @Test + void testRequirement5() { + assertEquals(1, house.getNumberOfSmokeAlarms()); + } + + /** + * 6. A house must have one or more smoke detectors. If we try to set the number + * of + * smoke detectors to zero, then an exception must be thrown. + */ + + @Test + void testRequirement6() { + try { + house.setNumberOfSmokeAlarms(0); + fail("Expected IllegalArgumentException, but it was not thrown."); + } catch (IllegalArgumentException exception) { + assertTrue(exception.getMessage() + .contains("Error: A house must have one or more smoke detectors")); + } + } + @AfterEach public void tearDown() throws Exception { house = null; } - }