Skip to content

Commit 4cb1c61

Browse files
A1-4U2T1NN“A1-4U2T1NN”
andauthored
Feat: Created Chigazo's Lesson 16 Custom {Class}, Custom {Class} Exception, and {Class}Test; (#520)
* feat: created chigazograham.json file * fix: deleted lesson_09 content from main; * feat: added 7 member variables of 5 different types; added Person function; added 'get' function for each member variable; added set function for each variable; added throw arguement to getHairColor to distinguish if person is bald; added getNaturalHairColor function with loop to distinguish original hair color from dyed; added getPersonInfo function that returns the information of the person; * feat: created framework for PersonTest; added try catch statement to getHairColor to change a null hair color to bald; fix: corrected grammar in getLifeStatus return statement strings; * feat: added .person to the package; added <String> to ArrayList innstances; added isEmpty to .getHairColor function to check for null and empty arrays and change the hair color to bald; added '|' to seperate information in .getPersonInfo function; created PersonTest, imported nessesary data, and added PersonTest set up; added tests for all .get and .set functions; added exception testing for .getHairColor; * fix: ran ./gradlew spotlessapply to correct formating mistakes; * fix: ran ./gradlew :objects_app:spotlessApply to correct formating mistakes; * fix: changed IllegalArgumentException to custom PersonIsBaldException; * fix: ran ./gradlew :objects:spotlessApply to correct formating mistakes; --------- Co-authored-by: “A1-4U2T1NN” <“[email protected]”>
1 parent 0f3904a commit 4cb1c61

File tree

3 files changed

+339
-0
lines changed

3 files changed

+339
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.codedifferently.lesson16.person;
2+
3+
import java.util.ArrayList;
4+
5+
public class Person {
6+
7+
private String name;
8+
private String sex;
9+
private String race;
10+
private int age;
11+
private boolean alive;
12+
private double height;
13+
private ArrayList<String> hairColor;
14+
15+
public Person(
16+
String name,
17+
String sex,
18+
String race,
19+
int age,
20+
double height,
21+
ArrayList<String> hairColor,
22+
boolean alive) {
23+
this.name = name;
24+
this.sex = sex;
25+
this.race = race;
26+
this.age = age;
27+
this.height = height;
28+
this.hairColor = hairColor;
29+
this.alive = alive;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public String getSex() {
41+
return sex;
42+
}
43+
44+
public void setSex(String sex) {
45+
this.sex = sex;
46+
}
47+
48+
public String getRace() {
49+
return race;
50+
}
51+
52+
public void setRace(String race) {
53+
this.race = race;
54+
}
55+
56+
public int getAge() {
57+
return age;
58+
}
59+
60+
public void setAge(int age) {
61+
this.age = age;
62+
}
63+
64+
public double getHeight() {
65+
return height;
66+
}
67+
68+
public void setHeight(double height) {
69+
this.height = height;
70+
}
71+
72+
public ArrayList<String> getHairColor() throws PersonIsBaldException {
73+
if (hairColor == null || hairColor.isEmpty()) {
74+
this.hairColor = new ArrayList<>();
75+
hairColor.add("Bald");
76+
throw new PersonIsBaldException("No hair color? This person must be bald!");
77+
}
78+
return hairColor;
79+
}
80+
81+
public void setHairColor(ArrayList<String> hairColor) {
82+
this.hairColor = hairColor;
83+
}
84+
85+
public ArrayList<String> getNaturalHairColor() {
86+
ArrayList<String> naturalHairColor = new ArrayList<>();
87+
ArrayList<String> possibleNaturalColor = new ArrayList<>();
88+
possibleNaturalColor.add("Black");
89+
possibleNaturalColor.add("Brown");
90+
possibleNaturalColor.add("Blonde");
91+
possibleNaturalColor.add("Ginger");
92+
possibleNaturalColor.add("Auburn");
93+
possibleNaturalColor.add("Albino");
94+
possibleNaturalColor.add("Grey");
95+
96+
for (String color : hairColor) {
97+
if (possibleNaturalColor.contains(color)) {
98+
naturalHairColor.add(color);
99+
break;
100+
}
101+
}
102+
return naturalHairColor;
103+
}
104+
105+
public String getLifeStatus() {
106+
if (alive == true) {
107+
return "Alive";
108+
} else {
109+
return "Deceased";
110+
}
111+
}
112+
113+
public void setLifeStatus(boolean alive) {
114+
this.alive = alive;
115+
}
116+
117+
public String getPersonInfo() {
118+
return "Name: "
119+
+ name
120+
+ "| Gender Identity: "
121+
+ sex
122+
+ "| Race: "
123+
+ race
124+
+ "| Age: "
125+
+ age
126+
+ "| Height(In meters): "
127+
+ height
128+
+ "| Hair Color: "
129+
+ hairColor
130+
+ "| Status: "
131+
+ getLifeStatus();
132+
}
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.person;
2+
3+
class PersonIsBaldException extends Exception {
4+
public PersonIsBaldException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package com.codedifferently.lesson16.person;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class PersonTest {
11+
12+
Person person;
13+
14+
@BeforeEach
15+
void setUp() {
16+
ArrayList<String> hairColor = new ArrayList<>();
17+
hairColor.add("Black");
18+
hairColor.add("Purple");
19+
person = new Person("Chigazo", "Male", "African-American", 20, 1.8288, hairColor, true);
20+
}
21+
22+
@Test
23+
public void testGetName() {
24+
// Act
25+
String actualName = person.getName();
26+
// Arrange
27+
String expectedName = "Chigazo";
28+
// Assert
29+
assertEquals(expectedName, actualName);
30+
}
31+
32+
@Test
33+
public void testSetName() {
34+
// Arrange
35+
String expectedSet = "Austin";
36+
// Act
37+
person.setName(expectedSet);
38+
// Assert
39+
assertEquals(expectedSet, person.getName());
40+
}
41+
42+
@Test
43+
public void testGetSex() {
44+
// Act
45+
String actualSex = person.getSex();
46+
// Arrange
47+
String expectedSex = "Male";
48+
// Assert(Checking the value)
49+
assertEquals(expectedSex, actualSex);
50+
}
51+
52+
@Test
53+
public void testSetSex() {
54+
// Arrange
55+
String expectedSex = "Female";
56+
// Act
57+
person.setSex(expectedSex);
58+
// Assert
59+
assertEquals(expectedSex, person.getSex());
60+
}
61+
62+
@Test
63+
public void testGetRace() {
64+
// Act
65+
String actualRace = person.getRace();
66+
// Arrange
67+
String expectedRace = "African-American";
68+
// Assert(Checking the value)
69+
assertEquals(expectedRace, actualRace);
70+
}
71+
72+
@Test
73+
public void testSetRace() {
74+
// Arrange
75+
String expectedRace = "Asian";
76+
// Act
77+
person.setRace(expectedRace);
78+
// Assert
79+
assertEquals(expectedRace, person.getRace());
80+
}
81+
82+
@Test
83+
public void testGetAge() {
84+
// Act
85+
int actualAge = person.getAge();
86+
// Arrange
87+
int expectedAge = 20;
88+
// Assert(Checking the value)
89+
assertEquals(expectedAge, actualAge);
90+
}
91+
92+
@Test
93+
public void testSetAge() {
94+
// Arrange
95+
int expectedAge = 21;
96+
// Act
97+
person.setAge(expectedAge);
98+
// Assert
99+
assertEquals(expectedAge, person.getAge());
100+
}
101+
102+
@Test
103+
public void testGetHeight() {
104+
// Act
105+
double actualHeight = person.getHeight();
106+
// Arrange
107+
double expectedHeight = 1.8288;
108+
// Assert(Checking the value)
109+
assertEquals(expectedHeight, actualHeight);
110+
}
111+
112+
@Test
113+
public void testSetHeight() {
114+
// Arrange
115+
double expectedHeight = 1.92024;
116+
// Act
117+
person.setHeight(expectedHeight);
118+
// Assert
119+
assertEquals(expectedHeight, person.getHeight());
120+
}
121+
122+
@Test
123+
public void testGetHairColor() throws PersonIsBaldException {
124+
// Act
125+
ArrayList<String> actualHairColor = person.getHairColor();
126+
// Arrange
127+
ArrayList<String> expectedHairColor = new ArrayList<>();
128+
expectedHairColor.add("Black");
129+
expectedHairColor.add("Purple");
130+
// Assert
131+
assertEquals(expectedHairColor, actualHairColor);
132+
}
133+
134+
@Test
135+
public void testGetHairColor__hairColorDoesNotExist() throws PersonIsBaldException {
136+
// Arrange
137+
Person baldPerson = new Person("John", "Male", "Caucasian", 25, 1.75, null, true);
138+
// Act
139+
Exception exception =
140+
Assertions.assertThrows(
141+
PersonIsBaldException.class,
142+
() -> {
143+
baldPerson.getHairColor();
144+
});
145+
// Assert
146+
assertEquals("No hair color? This person must be bald!", exception.getMessage());
147+
}
148+
149+
@Test
150+
public void testGetNaturalHairColor() {
151+
ArrayList<String> expectedNaturalHairColor = new ArrayList<>();
152+
expectedNaturalHairColor.add("Black");
153+
ArrayList<String> actualNaturalHairColor = person.getNaturalHairColor();
154+
assertEquals(expectedNaturalHairColor, actualNaturalHairColor);
155+
}
156+
157+
@Test
158+
public void testSetHairColor() throws PersonIsBaldException {
159+
// Arrange
160+
ArrayList<String> expectedHairColor = new ArrayList<>();
161+
expectedHairColor.add("Brown");
162+
// Act
163+
person.setHairColor(expectedHairColor);
164+
// Assert
165+
assertEquals(expectedHairColor, person.getHairColor());
166+
}
167+
168+
@Test
169+
public void testGetLifeStatus() {
170+
// Act
171+
String actualLifeStatus = person.getLifeStatus();
172+
// Arrange
173+
String expectedHairColor = "Alive";
174+
// Assert
175+
assertEquals(expectedHairColor, actualLifeStatus);
176+
}
177+
178+
@Test
179+
public void testSetLifeStatus() {
180+
// Arrange
181+
boolean expectedLifeStatus = false;
182+
// Act
183+
person.setLifeStatus(expectedLifeStatus);
184+
// Assert
185+
assertEquals("Deceased", person.getLifeStatus());
186+
}
187+
188+
@Test
189+
public void testGetPersonInfo() {
190+
String expectedPersonInfo =
191+
"Name: Chigazo| Gender Identity: Male| Race: African-American| Age: 20| Height(In meters): 1.8288| Hair Color: [Black, Purple]| Status: Alive";
192+
193+
// Act
194+
String actualPersonInfo = person.getPersonInfo();
195+
196+
// Assert
197+
assertEquals(expectedPersonInfo, actualPersonInfo);
198+
}
199+
}

0 commit comments

Comments
 (0)