diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Person/Person.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Person/Person.java new file mode 100644 index 000000000..e5329172f --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Person/Person.java @@ -0,0 +1,133 @@ +package com.codedifferently.lesson16.person; + +import java.util.ArrayList; + +public class Person { + + private String name; + private String sex; + private String race; + private int age; + private boolean alive; + private double height; + private ArrayList hairColor; + + public Person( + String name, + String sex, + String race, + int age, + double height, + ArrayList hairColor, + boolean alive) { + this.name = name; + this.sex = sex; + this.race = race; + this.age = age; + this.height = height; + this.hairColor = hairColor; + this.alive = alive; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public String getRace() { + return race; + } + + public void setRace(String race) { + this.race = race; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } + + public ArrayList getHairColor() throws PersonIsBaldException { + if (hairColor == null || hairColor.isEmpty()) { + this.hairColor = new ArrayList<>(); + hairColor.add("Bald"); + throw new PersonIsBaldException("No hair color? This person must be bald!"); + } + return hairColor; + } + + public void setHairColor(ArrayList hairColor) { + this.hairColor = hairColor; + } + + public ArrayList getNaturalHairColor() { + ArrayList naturalHairColor = new ArrayList<>(); + ArrayList possibleNaturalColor = new ArrayList<>(); + possibleNaturalColor.add("Black"); + possibleNaturalColor.add("Brown"); + possibleNaturalColor.add("Blonde"); + possibleNaturalColor.add("Ginger"); + possibleNaturalColor.add("Auburn"); + possibleNaturalColor.add("Albino"); + possibleNaturalColor.add("Grey"); + + for (String color : hairColor) { + if (possibleNaturalColor.contains(color)) { + naturalHairColor.add(color); + break; + } + } + return naturalHairColor; + } + + public String getLifeStatus() { + if (alive == true) { + return "Alive"; + } else { + return "Deceased"; + } + } + + public void setLifeStatus(boolean alive) { + this.alive = alive; + } + + public String getPersonInfo() { + return "Name: " + + name + + "| Gender Identity: " + + sex + + "| Race: " + + race + + "| Age: " + + age + + "| Height(In meters): " + + height + + "| Hair Color: " + + hairColor + + "| Status: " + + getLifeStatus(); + } +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Person/PersonIsBaldException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Person/PersonIsBaldException.java new file mode 100644 index 000000000..dbbe7c70f --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Person/PersonIsBaldException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.person; + +class PersonIsBaldException extends Exception { + public PersonIsBaldException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/person/PersonTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/person/PersonTest.java new file mode 100644 index 000000000..92c7260a1 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/person/PersonTest.java @@ -0,0 +1,199 @@ +package com.codedifferently.lesson16.person; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class PersonTest { + + Person person; + + @BeforeEach + void setUp() { + ArrayList hairColor = new ArrayList<>(); + hairColor.add("Black"); + hairColor.add("Purple"); + person = new Person("Chigazo", "Male", "African-American", 20, 1.8288, hairColor, true); + } + + @Test + public void testGetName() { + // Act + String actualName = person.getName(); + // Arrange + String expectedName = "Chigazo"; + // Assert + assertEquals(expectedName, actualName); + } + + @Test + public void testSetName() { + // Arrange + String expectedSet = "Austin"; + // Act + person.setName(expectedSet); + // Assert + assertEquals(expectedSet, person.getName()); + } + + @Test + public void testGetSex() { + // Act + String actualSex = person.getSex(); + // Arrange + String expectedSex = "Male"; + // Assert(Checking the value) + assertEquals(expectedSex, actualSex); + } + + @Test + public void testSetSex() { + // Arrange + String expectedSex = "Female"; + // Act + person.setSex(expectedSex); + // Assert + assertEquals(expectedSex, person.getSex()); + } + + @Test + public void testGetRace() { + // Act + String actualRace = person.getRace(); + // Arrange + String expectedRace = "African-American"; + // Assert(Checking the value) + assertEquals(expectedRace, actualRace); + } + + @Test + public void testSetRace() { + // Arrange + String expectedRace = "Asian"; + // Act + person.setRace(expectedRace); + // Assert + assertEquals(expectedRace, person.getRace()); + } + + @Test + public void testGetAge() { + // Act + int actualAge = person.getAge(); + // Arrange + int expectedAge = 20; + // Assert(Checking the value) + assertEquals(expectedAge, actualAge); + } + + @Test + public void testSetAge() { + // Arrange + int expectedAge = 21; + // Act + person.setAge(expectedAge); + // Assert + assertEquals(expectedAge, person.getAge()); + } + + @Test + public void testGetHeight() { + // Act + double actualHeight = person.getHeight(); + // Arrange + double expectedHeight = 1.8288; + // Assert(Checking the value) + assertEquals(expectedHeight, actualHeight); + } + + @Test + public void testSetHeight() { + // Arrange + double expectedHeight = 1.92024; + // Act + person.setHeight(expectedHeight); + // Assert + assertEquals(expectedHeight, person.getHeight()); + } + + @Test + public void testGetHairColor() throws PersonIsBaldException { + // Act + ArrayList actualHairColor = person.getHairColor(); + // Arrange + ArrayList expectedHairColor = new ArrayList<>(); + expectedHairColor.add("Black"); + expectedHairColor.add("Purple"); + // Assert + assertEquals(expectedHairColor, actualHairColor); + } + + @Test + public void testGetHairColor__hairColorDoesNotExist() throws PersonIsBaldException { + // Arrange + Person baldPerson = new Person("John", "Male", "Caucasian", 25, 1.75, null, true); + // Act + Exception exception = + Assertions.assertThrows( + PersonIsBaldException.class, + () -> { + baldPerson.getHairColor(); + }); + // Assert + assertEquals("No hair color? This person must be bald!", exception.getMessage()); + } + + @Test + public void testGetNaturalHairColor() { + ArrayList expectedNaturalHairColor = new ArrayList<>(); + expectedNaturalHairColor.add("Black"); + ArrayList actualNaturalHairColor = person.getNaturalHairColor(); + assertEquals(expectedNaturalHairColor, actualNaturalHairColor); + } + + @Test + public void testSetHairColor() throws PersonIsBaldException { + // Arrange + ArrayList expectedHairColor = new ArrayList<>(); + expectedHairColor.add("Brown"); + // Act + person.setHairColor(expectedHairColor); + // Assert + assertEquals(expectedHairColor, person.getHairColor()); + } + + @Test + public void testGetLifeStatus() { + // Act + String actualLifeStatus = person.getLifeStatus(); + // Arrange + String expectedHairColor = "Alive"; + // Assert + assertEquals(expectedHairColor, actualLifeStatus); + } + + @Test + public void testSetLifeStatus() { + // Arrange + boolean expectedLifeStatus = false; + // Act + person.setLifeStatus(expectedLifeStatus); + // Assert + assertEquals("Deceased", person.getLifeStatus()); + } + + @Test + public void testGetPersonInfo() { + String expectedPersonInfo = + "Name: Chigazo| Gender Identity: Male| Race: African-American| Age: 20| Height(In meters): 1.8288| Hair Color: [Black, Purple]| Status: Alive"; + + // Act + String actualPersonInfo = person.getPersonInfo(); + + // Assert + assertEquals(expectedPersonInfo, actualPersonInfo); + } +}