-
Notifications
You must be signed in to change notification settings - Fork 25
feat: created person object and made tests #506
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
Merged
anthonydmays
merged 13 commits into
code-differently:main
from
XavierCruz5106:feature/lesson_16
Nov 19, 2024
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b1c4074
feat: inital commit
XavierCruz5106 38d9feb
feat: changed object
XavierCruz5106 4ab87a7
feat: Added objects and tests
XavierCruz5106 2956c7b
feat: added test for name change
XavierCruz5106 c650712
chore: spotlessApply
XavierCruz5106 dd68734
feat: refactor
XavierCruz5106 6cb6996
feat: added more tests
XavierCruz5106 269b672
chore: spotlessApply
XavierCruz5106 0005627
feat: spotlessApply, added more test
XavierCruz5106 e041b8a
feat: changed ArrayList to List, added BeforeEach
XavierCruz5106 a59497e
chore: spotlessApply
XavierCruz5106 3a800ff
feat: removed String[] constructor, removed supporting tests
XavierCruz5106 fd83b4b
chore: spotlessApply
XavierCruz5106 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
9 changes: 9 additions & 0 deletions
9
...n_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/xaviercruz/Color.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,9 @@ | ||
| package com.codedifferently.lesson16.xaviercruz; | ||
|
|
||
| public enum Color { | ||
| RED, | ||
| BLUE, | ||
| WHITE, | ||
| GREEN, | ||
| BLACK | ||
| } |
95 changes: 95 additions & 0 deletions
95
..._16/objects/objects_app/src/main/java/com/codedifferently/lesson16/xaviercruz/Person.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,95 @@ | ||
| package com.codedifferently.lesson16.xaviercruz; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class Person { | ||
| private String name; | ||
| private int age; | ||
| private Position position; | ||
| private List<String> traits; | ||
| private Color eyeColor; | ||
| private final int max_traits = 10; | ||
|
|
||
| public Person(String name, int age, Position position, ArrayList<String> traits, Color eyeColor) { | ||
| this.name = name; | ||
| this.age = age; | ||
| this.position = position; | ||
| this.traits = traits; | ||
| this.eyeColor = eyeColor; | ||
| } | ||
|
|
||
| public Person(String name, int age, Position position, String[] traits, Color eyeColor) { | ||
XavierCruz5106 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.name = name; | ||
| this.age = age; | ||
| this.position = position; | ||
| this.traits = Arrays.asList(traits); | ||
| this.eyeColor = eyeColor; | ||
| } | ||
|
|
||
| public void changeName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void increaseAge() { | ||
| age++; | ||
| } | ||
|
|
||
| // Added for mistakes in age. | ||
| public void updateAge(int age) { | ||
| this.age = age; | ||
| } | ||
|
|
||
| public int getAge() { | ||
| return age; | ||
| } | ||
|
|
||
| public void updatePosition(Position position) { | ||
| this.position = position; | ||
| } | ||
|
|
||
| public Position getPosition() { | ||
| return position; | ||
| } | ||
|
|
||
| public void addToTraits(String newTrait) throws TooManyTraitsException { | ||
| if ((traits.size() + 1) > max_traits) { | ||
| throw new TooManyTraitsException("Error too many traits assigned"); | ||
| } | ||
| traits.add(newTrait); | ||
| } | ||
|
|
||
| public void addToTraits(String[] traits) throws TooManyTraitsException { | ||
| if ((this.traits.size() + traits.length) > max_traits) { | ||
| throw new TooManyTraitsException("Error too many traits assigned"); | ||
| } | ||
| this.traits.addAll(Arrays.asList(traits)); | ||
| } | ||
|
|
||
| public void addToTraits(ArrayList<String> traits) throws TooManyTraitsException { | ||
| if ((this.traits.size() + traits.size()) > max_traits) { | ||
| throw new TooManyTraitsException("Error too many traits assigned"); | ||
| } | ||
| for (String trait : traits) { | ||
| this.traits.add(trait); | ||
| } | ||
| } | ||
|
|
||
| public List<String> getTraits() { | ||
| return traits; | ||
| } | ||
|
|
||
| public Color getEyeColor() { | ||
| return eyeColor; | ||
| } | ||
|
|
||
| // Added for mistakes in eye color | ||
| public void setEyeColor(Color eyeColor) { | ||
| this.eyeColor = eyeColor; | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
...6/objects/objects_app/src/main/java/com/codedifferently/lesson16/xaviercruz/Position.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,8 @@ | ||
| package com.codedifferently.lesson16.xaviercruz; | ||
|
|
||
| public enum Position { | ||
| UNEMPLOYED, | ||
| SOFTWARE_DEV, | ||
| TEACHER, | ||
| ARSONIST | ||
| } |
8 changes: 8 additions & 0 deletions
8
...cts_app/src/main/java/com/codedifferently/lesson16/xaviercruz/TooManyTraitsException.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,8 @@ | ||
| package com.codedifferently.lesson16.xaviercruz; | ||
|
|
||
| public class TooManyTraitsException extends Exception { | ||
|
|
||
| public TooManyTraitsException(String message) { | ||
| super(message); | ||
| } | ||
| } |
149 changes: 149 additions & 0 deletions
149
...objects/objects_app/src/test/java/com/codedifferently/lesson16/xaviercruz/PersonTest.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,149 @@ | ||
| package com.codedifferently.lesson16.xaviercruz; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class PersonTest { | ||
| Person person = | ||
| new Person( | ||
XavierCruz5106 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "That one guy right there", | ||
| 25, | ||
| Position.ARSONIST, | ||
| new ArrayList<>(Arrays.asList("Tall", "Insane", "Funny", "something else here idk")), | ||
| Color.RED); | ||
|
|
||
| @Test | ||
| public void testGetName() { | ||
| assertEquals("That one guy right there", person.getName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testChangeName() { | ||
| person.changeName("new name right here bruh"); | ||
| assertEquals("new name right here bruh", person.getName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetAge() { | ||
| assertEquals(25, person.getAge()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetPosition() { | ||
| assertEquals(Position.ARSONIST, person.getPosition()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUpdatePosition() { | ||
| person.updatePosition(Position.SOFTWARE_DEV); | ||
| assertEquals(Position.SOFTWARE_DEV, person.getPosition()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetTraits() { | ||
| assertEquals( | ||
| new ArrayList<>(Arrays.asList("Tall", "Insane", "Funny", "something else here idk")), | ||
| person.getTraits()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetEyeColor() { | ||
| assertEquals(Color.RED, person.getEyeColor()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetEyeColor() { | ||
| Color color = Color.GREEN; | ||
|
|
||
| person.setEyeColor(color); | ||
|
|
||
| assertEquals(Color.GREEN, person.getEyeColor()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIncreaseAge() { | ||
| person.increaseAge(); | ||
| assertEquals(26, person.getAge()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUpdateAge() { | ||
| person.updateAge(99); | ||
| assertEquals(99, person.getAge()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAddToTraitsStringArray_Exception() { | ||
| String[] newTraits = new String[] {"test", "test", "test", "test", "test", "test", "test"}; | ||
| assertThrows( | ||
| TooManyTraitsException.class, | ||
| () -> { | ||
| person.addToTraits(newTraits); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAddToTraitsString_Exception() { | ||
| Person person2 = | ||
| new Person( | ||
| "That one guy right there", | ||
| 25, | ||
| Position.ARSONIST, | ||
| new ArrayList<>( | ||
| Arrays.asList( | ||
| "Tall", | ||
| "Insane", | ||
| "Funny", | ||
| "something else here idk", | ||
| "test", | ||
| "test", | ||
| "test", | ||
| "test", | ||
| "test", | ||
| "test")), | ||
| Color.RED); | ||
| assertThrows( | ||
| TooManyTraitsException.class, | ||
| () -> { | ||
| person2.addToTraits("newTrait"); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAddToTraitsList_Exception() { | ||
| Person person2 = | ||
| new Person( | ||
| "That one guy right there", | ||
| 25, | ||
| Position.ARSONIST, | ||
| new ArrayList<>( | ||
| Arrays.asList( | ||
| "Tall", | ||
| "Insane", | ||
| "Funny", | ||
| "something else here idk", | ||
| "test", | ||
| "test", | ||
| "test", | ||
| "test", | ||
| "test", | ||
| "test")), | ||
| Color.RED); | ||
| var traits = new ArrayList<String>(Arrays.asList("testTrait")); | ||
| assertThrows( | ||
| TooManyTraitsException.class, | ||
| () -> { | ||
| person2.addToTraits(traits); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetTraits_PersonStringArray() { | ||
| Person person2 = new Person(null, 0, null, new String[] {"test"}, null); | ||
| assertEquals(new ArrayList<>(Arrays.asList("test")), person2.getTraits()); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.