From 7bcd8143bebcf6bbc293f2aa07c8edb78fc575c8 Mon Sep 17 00:00:00 2001 From: Kenrixkk22 Date: Thu, 28 Mar 2024 18:17:21 +0000 Subject: [PATCH 1/3] feature:added person class and person test --- .../lesson7/kenricksutherland/Person.java | 89 +++++++++++++++++++ .../lesson7/kenricksutherland/Persontest.java | 85 ++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/kenricksutherland/Person.java create mode 100644 lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java 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..88cb788e --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/kenricksutherland/Person.java @@ -0,0 +1,89 @@ +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/Persontest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java new file mode 100644 index 00000000..3108499e --- /dev/null +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java @@ -0,0 +1,85 @@ +package com.codedifferently.lesson7.kenricksutherland; + + +import com.codedifferently.lesson7.kenricksutherland.Person.Gender; +import com.codedifferently.lesson7.kenricksutherland.Person.InvalidAgeException; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + + +public class Persontest { + + + @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."); + } + } +} + + + + + + From c88690fd104262177e77d8a310fe4678c074cd8e Mon Sep 17 00:00:00 2001 From: Kenrixkk22 Date: Thu, 28 Mar 2024 18:41:39 +0000 Subject: [PATCH 2/3] Feat:fixed person test --- .../lesson7/kenricksutherland/Person.java | 165 +++++++++--------- .../kenricksutherland/PersonsTest.java | 69 ++++++++ 2 files changed, 151 insertions(+), 83 deletions(-) create mode 100644 lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/PersonsTest.java 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 index 88cb788e..b1194c99 100644 --- 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 @@ -1,89 +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; - } + 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."); + } + } +} From f56275bce2c2463da66c39f86e9e328a6e1c74f7 Mon Sep 17 00:00:00 2001 From: "Anthony D. Mays" Date: Thu, 28 Mar 2024 12:44:33 -0700 Subject: [PATCH 3/3] chore: deletes lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java --- .../lesson7/kenricksutherland/Persontest.java | 85 ------------------- 1 file changed, 85 deletions(-) delete mode 100644 lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java deleted file mode 100644 index 3108499e..00000000 --- a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/kenricksutherland/Persontest.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.codedifferently.lesson7.kenricksutherland; - - -import com.codedifferently.lesson7.kenricksutherland.Person.Gender; -import com.codedifferently.lesson7.kenricksutherland.Person.InvalidAgeException; - -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - - -public class Persontest { - - - @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."); - } - } -} - - - - - -