Skip to content
Snippets Groups Projects
Commit 5292e145 authored by joeappleton18's avatar joeappleton18
Browse files

exercise complete

parent 7bcd970c
No related branches found
No related tags found
No related merge requests found
...@@ -7,31 +7,67 @@ public class House { ...@@ -7,31 +7,67 @@ public class House {
private List<Room> rooms = new ArrayList<Room>(); private List<Room> rooms = new ArrayList<Room>();
private boolean hasLounge = false; private boolean hasLounge = false;
int numberOfSmokeAlarms = 1;
public House() { 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 * simple method to add a room to a house
* *
* @param room * @param room
*/ */
public void addRoom(Room room) throws IllegalArgumentException{ public void addRoom(Room room) throws IllegalArgumentException {
if (room instanceof Lounge) { if (room instanceof Lounge) {
if (hasLounge) { if (hasLounge) {
throw new IllegalArgumentException("Error: Only one lounge allowed"); throw new IllegalArgumentException("Error: Only one lounge allowed");
} else { } else {
this.hasLounge = true; this.hasLounge = true;
} }
} }
this.rooms.add(room); this.rooms.add(room);
} }
......
package l_1; package l_1;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach; 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.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 * This is a simple test class which provides until tests for the House class
* from exercise 1 * from exercise 1
...@@ -16,64 +16,90 @@ import static org.junit.jupiter.api.Assertions.assertEquals; ...@@ -16,64 +16,90 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* *
*/ */
class HouseTest { class HouseTest {
private House house; private House house;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
house = new House(); house = new House();
/** Set up a particular kind of house */ /** Set up a particular kind of house */
Lounge lounge1 = new Lounge(10.0); Lounge lounge1 = new Lounge(10.0);
DiningRoom dining1 = new DiningRoom(16.0); DiningRoom dining1 = new DiningRoom(16.0);
Bedroom bedroom = new Bedroom(10.0); Bedroom bedroom = new Bedroom(10.0);
house.addRoom(lounge1); house.addRoom(lounge1);
house.addRoom(dining1); house.addRoom(dining1);
house.addRoom(bedroom); 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 @Test
public void testRequirement2() { public void testRequirement2() {
try { try {
Lounge l2 = new Lounge(12.0); // Attempting to add a second lounge Lounge l2 = new Lounge(12.0); // Attempting to add a second lounge
house.addRoom(l2); house.addRoom(l2);
// If the code reaches here without throwing an exception, you can fail the test // If the code reaches here without throwing an exception, you can fail the test
fail("Expected IllegalArgumentException, but it was not thrown."); fail("Expected IllegalArgumentException, but it was not thrown.");
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
// Asserting that the exception's message contains the expected error message // Asserting that the exception's message contains the expected error message
assertTrue(exception.getMessage().contains("Error: Only one lounge allowed")); 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 @Test
void testRequirement3() { void testRequirement3() {
assertEquals(3, house.howManyRooms()); 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 @Test
void testRequirement4() { void testRequirement4() {
assertEquals(36.0, house.getTotalArea()); 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 @AfterEach
public void tearDown() throws Exception { public void tearDown() throws Exception {
house = null; house = null;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment