Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds Kenrick's person class and person test (#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 <Ksutherland007@gmail.com> Co-authored-by: Anthony D. Mays <anthonydmays@users.noreply.github.com>
- Loading branch information
1 parent
8d73669
commit 1a7a3ae
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
...jects/objects_app/src/main/java/com/codedifferently/lesson7/kenricksutherland/Person.java
This file contains 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,88 @@ | ||
package com.codedifferently.lesson7.kenricksutherland; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Person { | ||
private String name; | ||
private int age; | ||
private Gender gender; | ||
private String occupation; | ||
private String nationality; | ||
|
||
public enum Gender { | ||
MALE, | ||
FEMALE, | ||
OTHER | ||
} | ||
|
||
// Custom exception for invalid age | ||
public static class InvalidAgeException extends Exception { | ||
public InvalidAgeException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
public Person(String name, int age, Gender gender, String occupation, String nationality) | ||
throws InvalidAgeException { | ||
if (age <= 0) { | ||
throw new InvalidAgeException("Age must be a positive integer."); | ||
} | ||
this.name = name; | ||
this.age = age; | ||
this.gender = gender; | ||
this.occupation = occupation; | ||
this.nationality = nationality; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public Gender getGender() { | ||
return gender; | ||
} | ||
|
||
public String getOccupation() { | ||
return occupation; | ||
} | ||
|
||
public String getNationality() { | ||
return nationality; | ||
} | ||
|
||
public String getGenderPronoun() { | ||
return switch (gender) { | ||
case MALE -> "He"; | ||
case FEMALE -> "She"; | ||
default -> "They"; | ||
}; | ||
} | ||
|
||
// Function using a collection (ArrayList) | ||
public ArrayList<String> getPersonInfo() { | ||
ArrayList<String> info = new ArrayList<>(); | ||
info.add("Name: " + name); | ||
info.add("Age: " + age); | ||
info.add("Gender: " + gender); | ||
info.add("Occupation: " + occupation); | ||
info.add("Nationality: " + nationality); | ||
return info; | ||
} | ||
|
||
// Function using a loop | ||
public void displayPersonInfo() { | ||
ArrayList<String> info = getPersonInfo(); | ||
for (String line : info) { | ||
System.out.println(line); | ||
} | ||
} | ||
|
||
// Function using a conditional expression | ||
public String getOccupationStatus() { | ||
return occupation.isEmpty() ? "Unemployed" : "Employed as a " + occupation; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/PersonsTest.java
This file contains 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,69 @@ | ||
package com.codedifferently.lesson7.kenricksutherland; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.codedifferently.lesson7.kenricksutherland.Person.Gender; | ||
import com.codedifferently.lesson7.kenricksutherland.Person.InvalidAgeException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PersonsTest { | ||
|
||
@Test | ||
void testPersonInfo() { | ||
try { | ||
Person person = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA"); | ||
assertEquals("John Doe", person.getName()); | ||
assertEquals(30, person.getAge()); | ||
assertEquals(Gender.MALE, person.getGender()); | ||
assertEquals("Software Engineer", person.getOccupation()); | ||
assertEquals("USA", person.getNationality()); | ||
} catch (Person.InvalidAgeException e) { | ||
fail("InvalidAgeException should not be thrown in this test."); | ||
} | ||
} | ||
|
||
@Test | ||
void testGetGenderPronoun() throws InvalidAgeException { | ||
Person person1 = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA"); | ||
assertEquals("He", person1.getGenderPronoun()); | ||
|
||
Person person2 = new Person("Jane Doe", 25, Gender.FEMALE, "Doctor", "Canada"); | ||
assertEquals("She", person2.getGenderPronoun()); | ||
|
||
Person person3 = new Person("Alex Smith", 40, Gender.OTHER, "Artist", "France"); | ||
assertEquals("They", person3.getGenderPronoun()); | ||
} | ||
|
||
@Test | ||
void testGetPersonInfo() { | ||
try { | ||
Person person = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA"); | ||
assertEquals(5, person.getPersonInfo().size()); | ||
} catch (Person.InvalidAgeException e) { | ||
fail("InvalidAgeException should not be thrown in this test."); | ||
} | ||
} | ||
|
||
@Test | ||
void testDisplayPersonInfo() { | ||
try { | ||
Person person = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA"); | ||
assertDoesNotThrow(() -> person.displayPersonInfo()); | ||
} catch (Person.InvalidAgeException e) { | ||
fail("InvalidAgeException should not be thrown in this test."); | ||
} | ||
} | ||
|
||
@Test | ||
void testGetOccupationStatus() { | ||
try { | ||
Person employedPerson = new Person("John Doe", 30, Gender.MALE, "Software Engineer", "USA"); | ||
assertEquals("Employed as a Software Engineer", employedPerson.getOccupationStatus()); | ||
|
||
Person unemployedPerson = new Person("Jane Doe", 25, Gender.FEMALE, "", "Canada"); | ||
assertEquals("Unemployed", unemployedPerson.getOccupationStatus()); | ||
} catch (Person.InvalidAgeException e) { | ||
fail("InvalidAgeException should not be thrown in this test."); | ||
} | ||
} | ||
} |