Skip to content

Commit

Permalink
feat: adds Kenrick's person class and person test (#321)
Browse files Browse the repository at this point in the history
* 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
3 people committed Mar 28, 2024
1 parent 8d73669 commit 1a7a3ae
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
@@ -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;
}
}
@@ -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.");
}
}
}

0 comments on commit 1a7a3ae

Please sign in to comment.