Skip to content

Commit 4ab87a7

Browse files
feat: Added objects and tests
1 parent 38d9feb commit 4ab87a7

File tree

5 files changed

+171
-1
lines changed

5 files changed

+171
-1
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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,95 @@
11
package com.codedifferently.lesson16.xaviercruz;
22

3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
37
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, ArrayList<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 Person(String name, int age, Position position, String[] traits, Color eyeColor) {
24+
this.name = name;
25+
this.age = age;
26+
this.position = position;
27+
this.traits = Arrays.asList(traits);
28+
this.eyeColor = eyeColor;
29+
}
30+
31+
public void changeName(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void increaseAge() {
40+
age++;
41+
}
42+
43+
// Added for mistakes in age.
44+
public void updateAge(int age) {
45+
this.age = age;
46+
}
47+
48+
public int getAge() {
49+
return age;
50+
}
51+
52+
public void updatePosition(Position position) {
53+
this.position = position;
54+
}
55+
56+
public Position getPosition() {
57+
return position;
58+
}
59+
60+
public void addToTraits(String newTrait) throws TooManyTraitsException {
61+
if ((traits.size() + 1) > max_traits) {
62+
throw new TooManyTraitsException("Error too many traits assigned");
63+
}
64+
traits.add(newTrait);
65+
}
66+
67+
public void addToTraits(String[] traits) throws TooManyTraitsException {
68+
if ((this.traits.size() + traits.length) > max_traits) {
69+
throw new TooManyTraitsException("Error too many traits assigned");
70+
}
71+
this.traits.addAll(Arrays.asList(traits));
72+
}
73+
74+
public void addToTraits(ArrayList<String> traits) throws TooManyTraitsException {
75+
if ((this.traits.size() + traits.size()) > max_traits) {
76+
throw new TooManyTraitsException("Error too many traits assigned");
77+
}
78+
for (String trait : traits) {
79+
this.traits.add(trait);
80+
}
81+
}
82+
83+
public List<String> getTraits() {
84+
return traits;
85+
}
86+
87+
public Color getEyeColor() {
88+
return eyeColor;
89+
}
490

91+
// Added for mistakes in eye color
92+
public void setEyeColor(Color eyeColor) {
93+
this.eyeColor = eyeColor;
94+
}
595
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.codedifferently.lesson16.xaviercruz;
22

33
public enum Position {
4-
4+
UNEMPLOYED,
5+
SOFTWARE_DEV,
6+
TEACHER,
7+
ARSONIST
58
}
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
9+
import org.junit.jupiter.api.Test;
10+
11+
public class PersonTest {
12+
Person person = new Person("That one guy right there", 25, Position.ARSONIST, new ArrayList<>(Arrays.asList("Tall", "Insane", "Funny", "something else here idk")), Color.RED);
13+
14+
@Test
15+
public void testGetName(){
16+
assertEquals("That one guy right there", person.getName());
17+
}
18+
19+
@Test
20+
public void testGetAge(){
21+
assertEquals(25, person.getAge());
22+
}
23+
24+
@Test
25+
public void testGetPosition(){
26+
assertEquals(Position.ARSONIST, person.getPosition());
27+
}
28+
29+
@Test
30+
public void testGetTraits(){
31+
assertEquals(new ArrayList<>(Arrays.asList("Tall", "Insane", "Funny", "something else here idk")), person.getTraits());
32+
}
33+
34+
@Test
35+
public void testGetEyeColor(){
36+
assertEquals(Color.RED, person.getEyeColor());
37+
}
38+
39+
@Test
40+
public void testIncreaseAge(){
41+
person.increaseAge();
42+
assertEquals(26, person.getAge());
43+
}
44+
45+
@Test
46+
public void testUpdateAge(){
47+
person.updateAge(99);
48+
assertEquals(99, person.getAge());
49+
}
50+
51+
@Test
52+
public void testAddToTraits_Exception() throws TooManyTraitsException{
53+
String[] newTraits = new String[]{"test", "test", "test", "test", "test", "test", "test"};
54+
assertThrows(TooManyTraitsException.class, () -> {
55+
person.addToTraits(newTraits);
56+
});
57+
}
58+
59+
60+
}

0 commit comments

Comments
 (0)