diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/kenricksutherland/Person.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/kenricksutherland/Person.java new file mode 100644 index 00000000..b1194c99 --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/kenricksutherland/Person.java @@ -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 getPersonInfo() { + ArrayList 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 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; + } +} diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/PersonsTest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/PersonsTest.java new file mode 100644 index 00000000..22f2d816 --- /dev/null +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/PersonsTest.java @@ -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."); + } + } +}