Skip to content

Commit dae0b70

Browse files
feat: created person object and made tests (#506)
* feat: inital commit * feat: changed object * feat: Added objects and tests * feat: added test for name change * chore: spotlessApply * feat: refactor * feat: added more tests * chore: spotlessApply * feat: spotlessApply, added more test * feat: changed ArrayList to List, added BeforeEach * chore: spotlessApply * feat: removed String[] constructor, removed supporting tests * chore: spotlessApply
1 parent 2e9b8b5 commit dae0b70

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codedifferently.lesson16.xaviercruz;
2+
3+
public enum Color {
4+
RED,
5+
BLUE,
6+
WHITE,
7+
GREEN,
8+
BLACK
9+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.codedifferently.lesson16.xaviercruz;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Person {
8+
private String name;
9+
private int age;
10+
private Position position;
11+
private List<String> traits;
12+
private Color eyeColor;
13+
private final int max_traits = 10;
14+
15+
public Person(String name, int age, Position position, List<String> traits, Color eyeColor) {
16+
this.name = name;
17+
this.age = age;
18+
this.position = position;
19+
this.traits = traits;
20+
this.eyeColor = eyeColor;
21+
}
22+
23+
public void changeName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void increaseAge() {
32+
age++;
33+
}
34+
35+
// Added for mistakes in age.
36+
public void updateAge(int age) {
37+
this.age = age;
38+
}
39+
40+
public int getAge() {
41+
return age;
42+
}
43+
44+
public void updatePosition(Position position) {
45+
this.position = position;
46+
}
47+
48+
public Position getPosition() {
49+
return position;
50+
}
51+
52+
public void addToTraits(String newTrait) throws TooManyTraitsException {
53+
if ((traits.size() + 1) > max_traits) {
54+
throw new TooManyTraitsException("Error too many traits assigned");
55+
}
56+
traits.add(newTrait);
57+
}
58+
59+
public void addToTraits(String[] traits) throws TooManyTraitsException {
60+
if ((this.traits.size() + traits.length) > max_traits) {
61+
throw new TooManyTraitsException("Error too many traits assigned");
62+
}
63+
this.traits.addAll(Arrays.asList(traits));
64+
}
65+
66+
public void addToTraits(ArrayList<String> traits) throws TooManyTraitsException {
67+
if ((this.traits.size() + traits.size()) > max_traits) {
68+
throw new TooManyTraitsException("Error too many traits assigned");
69+
}
70+
for (String trait : traits) {
71+
this.traits.add(trait);
72+
}
73+
}
74+
75+
public List<String> getTraits() {
76+
return traits;
77+
}
78+
79+
public Color getEyeColor() {
80+
return eyeColor;
81+
}
82+
83+
// Added for mistakes in eye color
84+
public void setEyeColor(Color eyeColor) {
85+
this.eyeColor = eyeColor;
86+
}
87+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.xaviercruz;
2+
3+
public enum Position {
4+
UNEMPLOYED,
5+
SOFTWARE_DEV,
6+
TEACHER,
7+
ARSONIST
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.xaviercruz;
2+
3+
public class TooManyTraitsException extends Exception {
4+
5+
public TooManyTraitsException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.codedifferently.lesson16.xaviercruz;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class PersonTest {
12+
Person person;
13+
14+
@BeforeEach
15+
public void setUp() {
16+
person =
17+
new Person(
18+
"That one guy right there",
19+
25,
20+
Position.ARSONIST,
21+
new ArrayList<>(Arrays.asList("Tall", "Insane", "Funny", "something else here idk")),
22+
Color.RED);
23+
}
24+
25+
@Test
26+
public void testGetName() {
27+
assertEquals("That one guy right there", person.getName());
28+
}
29+
30+
@Test
31+
public void testChangeName() {
32+
person.changeName("new name right here bruh");
33+
assertEquals("new name right here bruh", person.getName());
34+
}
35+
36+
@Test
37+
public void testGetAge() {
38+
assertEquals(25, person.getAge());
39+
}
40+
41+
@Test
42+
public void testGetPosition() {
43+
assertEquals(Position.ARSONIST, person.getPosition());
44+
}
45+
46+
@Test
47+
public void testUpdatePosition() {
48+
person.updatePosition(Position.SOFTWARE_DEV);
49+
assertEquals(Position.SOFTWARE_DEV, person.getPosition());
50+
}
51+
52+
@Test
53+
public void testGetTraits() {
54+
assertEquals(
55+
new ArrayList<>(Arrays.asList("Tall", "Insane", "Funny", "something else here idk")),
56+
person.getTraits());
57+
}
58+
59+
@Test
60+
public void testGetEyeColor() {
61+
assertEquals(Color.RED, person.getEyeColor());
62+
}
63+
64+
@Test
65+
public void testSetEyeColor() {
66+
Color color = Color.GREEN;
67+
68+
person.setEyeColor(color);
69+
70+
assertEquals(Color.GREEN, person.getEyeColor());
71+
}
72+
73+
@Test
74+
public void testIncreaseAge() {
75+
person.increaseAge();
76+
assertEquals(26, person.getAge());
77+
}
78+
79+
@Test
80+
public void testUpdateAge() {
81+
person.updateAge(99);
82+
assertEquals(99, person.getAge());
83+
}
84+
85+
@Test
86+
public void testAddToTraitsStringArray_Exception() {
87+
String[] newTraits = new String[] {"test", "test", "test", "test", "test", "test", "test"};
88+
assertThrows(
89+
TooManyTraitsException.class,
90+
() -> {
91+
person.addToTraits(newTraits);
92+
});
93+
}
94+
95+
@Test
96+
public void testAddToTraitsString_Exception() {
97+
Person person2 =
98+
new Person(
99+
"That one guy right there",
100+
25,
101+
Position.ARSONIST,
102+
new ArrayList<>(
103+
Arrays.asList(
104+
"Tall",
105+
"Insane",
106+
"Funny",
107+
"something else here idk",
108+
"test",
109+
"test",
110+
"test",
111+
"test",
112+
"test",
113+
"test")),
114+
Color.RED);
115+
assertThrows(
116+
TooManyTraitsException.class,
117+
() -> {
118+
person2.addToTraits("newTrait");
119+
});
120+
}
121+
122+
@Test
123+
public void testAddToTraitsList_Exception() {
124+
Person person2 =
125+
new Person(
126+
"That one guy right there",
127+
25,
128+
Position.ARSONIST,
129+
new ArrayList<>(
130+
Arrays.asList(
131+
"Tall",
132+
"Insane",
133+
"Funny",
134+
"something else here idk",
135+
"test",
136+
"test",
137+
"test",
138+
"test",
139+
"test",
140+
"test")),
141+
Color.RED);
142+
var traits = new ArrayList<String>(Arrays.asList("testTrait"));
143+
assertThrows(
144+
TooManyTraitsException.class,
145+
() -> {
146+
person2.addToTraits(traits);
147+
});
148+
}
149+
}

0 commit comments

Comments
 (0)