-
Notifications
You must be signed in to change notification settings - Fork 29
Daniel Boyce Feat/lesson 16/objects #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Boyce007
wants to merge
14
commits into
code-differently:main
Choose a base branch
from
Boyce007:feat/lesson-16/objects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2f78114
feat:creates test file and base class
Boyce007 2b0345b
feat:adds constructor to home class
Boyce007 79fabf7
added tests for getters and setters using tdd
Boyce007 8ca192e
test:added test to get number of rooms
Boyce007 a8e5d57
chore:fixed small bug with room type data type
Boyce007 cf26882
feat:adds code to populate map of the count of rooms
Boyce007 4e392e0
feat:adds test for get number of a specefied room and creates custom …
Boyce007 9385b12
chore:spotless apply
Boyce007 62c301a
added method to get home details
Boyce007 3f9f102
test:creates test to check for correct assertion
Boyce007 92cde7a
feat:adds function to get home details
Boyce007 8af89b7
test:creates test for get home details
Boyce007 1f39798
feat:adds logic to check if a home is valid
Boyce007 d60ec45
refacotr:adds subfolder for custom tests
Boyce007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
...jects/objects_app/src/main/java/com/codedifferently/lesson16/danielcustomobject/Home.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
...s/objects_app/src/main/java/com/codedifferently/lesson16/danielcustomobject/HomeType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
24 changes: 24 additions & 0 deletions
24
...p/src/main/java/com/codedifferently/lesson16/danielcustomobject/InvalidHomeException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
.../src/main/java/com/codedifferently/lesson16/danielcustomobject/RoomNotFoundException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...s/objects_app/src/main/java/com/codedifferently/lesson16/danielcustomobject/RoomType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
107 changes: 107 additions & 0 deletions
107
...s/objects_app/src/test/java/com/codedifferently/lesson16/danielcustomobject/HomeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must be in the
danielcustomobjectsub-folder.