Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:added person class and person test #321

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
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.");
}
}
}