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,97 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

package com.codedifferently.lesson16.danielcustomobject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author vscode
*/
public class Home {

private final HomeType homeType;
private List<RoomType> rooms;

private Integer squareFootage;
private String neighborhood;
private Map<RoomType, Integer> roomCount = new HashMap<>();

public Home(HomeType homeType, List<RoomType> rooms, Integer squareFootage, String neighborhood)
throws InvalidHomeException {
this.homeType = homeType;
this.rooms = rooms;
this.squareFootage = squareFootage;
this.neighborhood = neighborhood;
initRoomCount();
if (!isHabitable()) {
throw new InvalidHomeException("Home is not habitable");
}
}

public HomeType getHomeType() {
return homeType;
}

public void setRooms(List<RoomType> rooms) {
this.rooms = rooms;
}

public List<RoomType> getRooms() {
return rooms;
}

public Integer getSquareFootage() {
return squareFootage;
}

public void setSquareFootage(Integer price) {
this.squareFootage = price;
}

public String getNeighborhood() {
return neighborhood;
}

public void setNeighborhood(String neighborhood) {
this.neighborhood = neighborhood;
}

private void initRoomCount() {

for (RoomType room : rooms) {

// get the current value of rooms if the value is not there set it to zero
Integer value = roomCount.getOrDefault(room, 0);
// increase the value and create a frequency map
roomCount.put(room, ++value);
}
}

// checks to see if a home has at least 1 bathroom bedroom and kitchen
private boolean isHabitable() {
return roomCount.containsKey(RoomType.BEDROOM)
&& roomCount.containsKey(RoomType.BATHROOM)
&& roomCount.containsKey(RoomType.KITCHEN);
}

public Integer getNumberOfSpeceficRoom(RoomType room) {

if (!roomCount.containsKey(room)) {
throw new RoomNotFoundException("This house doesn't have a " + room);
}

return roomCount.get(room);
}

public String getHomeDetails() {

return String.format(
"%d Bed %d Bath %d Sq Ft",
roomCount.get(RoomType.BATHROOM), roomCount.get(RoomType.BEDROOM), this.squareFootage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Enum.java to edit this template
*/

package com.codedifferently.lesson16.danielcustomobject;

/**
* @author vscode
*/
public enum HomeType {
SINGLE_FAMILY_HOME,
DUPLEX,
APARTMENT,
LUXURY,
TOWNHOUSE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Exception.java to edit this template
*/

package com.codedifferently.lesson16.danielcustomobject;

/**
* @author vscode
*/
public class InvalidHomeException extends IllegalArgumentException {

/** Creates a new instance of <code>InvalidHomeException</code> without detail message. */
public InvalidHomeException() {}

/**
* Constructs an instance of <code>InvalidHomeException</code> with the specified detail message.
*
* @param msg the detail message.
*/
public InvalidHomeException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Exception.java to edit this template
*/

package com.codedifferently.lesson16.danielcustomobject;

/**
* @author vscode
*/
public class RoomNotFoundException extends RuntimeException {

/** Creates a new instance of <code>RoomNotFoundException</code> without detail message. */
public RoomNotFoundException() {}

/**
* Constructs an instance of <code>RoomNotFoundException</code> with the specified detail message.
*
* @param msg the detail message.
*/
public RoomNotFoundException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.codedifferently.lesson16.danielcustomobject;

public enum RoomType {
LIVING_ROOM,
KITCHEN,
BEDROOM,
BATHROOM,
DINING_ROOM,
OFFICE,
GARAGE
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be in the danielcustomobject sub-folder.

Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

package com.codedifferently.lesson16.danielcustomobject;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* @author vscode
*/
public class HomeTest {

private Home home;

@BeforeEach
public void setUp() {
home =
new Home(
HomeType.DUPLEX,
List.of(RoomType.LIVING_ROOM, RoomType.KITCHEN, RoomType.BEDROOM, RoomType.BATHROOM),
250000,
"Downtown");
}

@Test
public void getterAndSetterTest() {
// Test HomeType getter and setter
assertTrue(home.getHomeType() == HomeType.DUPLEX);

// Test rooms getter and setter
assertTrue(
home.getRooms()
.equals(
List.of(
RoomType.LIVING_ROOM, RoomType.KITCHEN, RoomType.BEDROOM, RoomType.BATHROOM)));
home.setRooms(
List.of(RoomType.LIVING_ROOM, RoomType.KITCHEN, RoomType.BEDROOM, RoomType.BEDROOM));
assertTrue(
home.getRooms()
.equals(
List.of(
RoomType.LIVING_ROOM, RoomType.KITCHEN, RoomType.BEDROOM, RoomType.BEDROOM)));

// Test price getter and setter
assertTrue(home.getSquareFootage() == 250000);
home.setSquareFootage(300000);
assertTrue(home.getSquareFootage() == 300000);

// Test neighborhood getter
assertTrue(home.getNeighborhood().equals("Downtown"));
}

@Test
public void getNumberOfSpeceficRoomTest() {

// Given

// When
Integer numberOfBedRooms = home.getNumberOfSpeceficRoom(RoomType.BEDROOM);
Integer numberOfLivingRooms = home.getNumberOfSpeceficRoom(RoomType.LIVING_ROOM);
Integer numberOfBathrooms = home.getNumberOfSpeceficRoom(RoomType.BATHROOM);
Integer numberOfKitchens = home.getNumberOfSpeceficRoom(RoomType.KITCHEN);

// Then
assertEquals(numberOfBedRooms, 1);
assertEquals(numberOfLivingRooms, 1);
assertEquals(numberOfKitchens, 1);
assertEquals(numberOfBathrooms, 1);
}

@Test
public void getNumberOfSpeceficRoom_RoomNotFound() {

assertThatThrownBy(() -> home.getNumberOfSpeceficRoom(RoomType.OFFICE))
.isInstanceOf(RoomNotFoundException.class)
.hasMessage("This house doesn't have a " + RoomType.OFFICE);
}

@Test
public void getHomeDetailsTest() {
// Given

// When
String expected = "1 Bed 1 Bath 250000 Sq Ft";
String actual = home.getHomeDetails();
assertEquals(expected, actual);
// Then
}

@Test
public void inValidHomeTest() {
// Given

assertThatThrownBy(
() -> new Home(HomeType.DUPLEX, List.of(RoomType.LIVING_ROOM), 250000, "Downtown"))
.isInstanceOf(InvalidHomeException.class)
.hasMessage("Home is not habitable");
}
}