Skip to content

Commit 1a7a3ae

Browse files
Kenrixkk22Kenrixkk22anthonydmays
authored
feat: adds Kenrick's person class and person test (code-differently#321)
* feature:added person class and person test * Feat:fixed person test * chore: deletes lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java --------- Co-authored-by: Kenrixkk22 <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent 8d73669 commit 1a7a3ae

File tree

2 files changed

+157
-0
lines changed
  • lesson_07/objects/objects_app/src
    • main/java/com/codedifferently/lesson7/kenricksutherland
    • test/java/com/codedifferently/lesson7/kenricksutherland

2 files changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.codedifferently.lesson7.kenricksutherland;
2+
3+
import java.util.ArrayList;
4+
5+
public class Person {
6+
private String name;
7+
private int age;
8+
private Gender gender;
9+
private String occupation;
10+
private String nationality;
11+
12+
public enum Gender {
13+
MALE,
14+
FEMALE,
15+
OTHER
16+
}
17+
18+
// Custom exception for invalid age
19+
public static class InvalidAgeException extends Exception {
20+
public InvalidAgeException(String message) {
21+
super(message);
22+
}
23+
}
24+
25+
public Person(String name, int age, Gender gender, String occupation, String nationality)
26+
throws InvalidAgeException {
27+
if (age <= 0) {
28+
throw new InvalidAgeException("Age must be a positive integer.");
29+
}
30+
this.name = name;
31+
this.age = age;
32+
this.gender = gender;
33+
this.occupation = occupation;
34+
this.nationality = nationality;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public int getAge() {
42+
return age;
43+
}
44+
45+
public Gender getGender() {
46+
return gender;
47+
}
48+
49+
public String getOccupation() {
50+
return occupation;
51+
}
52+
53+
public String getNationality() {
54+
return nationality;
55+
}
56+
57+
public String getGenderPronoun() {
58+
return switch (gender) {
59+
case MALE -> "He";
60+
case FEMALE -> "She";
61+
default -> "They";
62+
};
63+
}
64+
65+
// Function using a collection (ArrayList)
66+
public ArrayList<String> getPersonInfo() {
67+
ArrayList<String> info = new ArrayList<>();
68+
info.add("Name: " + name);
69+
info.add("Age: " + age);
70+
info.add("Gender: " + gender);
71+
info.add("Occupation: " + occupation);
72+
info.add("Nationality: " + nationality);
73+
return info;
74+
}
75+
76+
// Function using a loop
77+
public void displayPersonInfo() {
78+
ArrayList<String> info = getPersonInfo();
79+
for (String line : info) {
80+
System.out.println(line);
81+
}
82+
}
83+
84+
// Function using a conditional expression
85+
public String getOccupationStatus() {
86+
return occupation.isEmpty() ? "Unemployed" : "Employed as a " + occupation;
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.codedifferently.lesson7.kenricksutherland;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.codedifferently.lesson7.kenricksutherland.Person.Gender;
6+
import com.codedifferently.lesson7.kenricksutherland.Person.InvalidAgeException;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class PersonsTest {
10+
11+
@Test
12+
void testPersonInfo() {
13+
try {
14+
Person person = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA");
15+
assertEquals("John Doe", person.getName());
16+
assertEquals(30, person.getAge());
17+
assertEquals(Gender.MALE, person.getGender());
18+
assertEquals("Software Engineer", person.getOccupation());
19+
assertEquals("USA", person.getNationality());
20+
} catch (Person.InvalidAgeException e) {
21+
fail("InvalidAgeException should not be thrown in this test.");
22+
}
23+
}
24+
25+
@Test
26+
void testGetGenderPronoun() throws InvalidAgeException {
27+
Person person1 = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA");
28+
assertEquals("He", person1.getGenderPronoun());
29+
30+
Person person2 = new Person("Jane Doe", 25, Gender.FEMALE, "Doctor", "Canada");
31+
assertEquals("She", person2.getGenderPronoun());
32+
33+
Person person3 = new Person("Alex Smith", 40, Gender.OTHER, "Artist", "France");
34+
assertEquals("They", person3.getGenderPronoun());
35+
}
36+
37+
@Test
38+
void testGetPersonInfo() {
39+
try {
40+
Person person = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA");
41+
assertEquals(5, person.getPersonInfo().size());
42+
} catch (Person.InvalidAgeException e) {
43+
fail("InvalidAgeException should not be thrown in this test.");
44+
}
45+
}
46+
47+
@Test
48+
void testDisplayPersonInfo() {
49+
try {
50+
Person person = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA");
51+
assertDoesNotThrow(() -> person.displayPersonInfo());
52+
} catch (Person.InvalidAgeException e) {
53+
fail("InvalidAgeException should not be thrown in this test.");
54+
}
55+
}
56+
57+
@Test
58+
void testGetOccupationStatus() {
59+
try {
60+
Person employedPerson = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA");
61+
assertEquals("Employed as a Software Engineer", employedPerson.getOccupationStatus());
62+
63+
Person unemployedPerson = new Person("Jane Doe", 25, Gender.FEMALE, "", "Canada");
64+
assertEquals("Unemployed", unemployedPerson.getOccupationStatus());
65+
} catch (Person.InvalidAgeException e) {
66+
fail("InvalidAgeException should not be thrown in this test.");
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)