diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/hummadtanweer/Person.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/hummadtanweer/Person.java new file mode 100644 index 000000000..38d62521f --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/hummadtanweer/Person.java @@ -0,0 +1,55 @@ +package com.codedifferently.lesson16.hummadtanweer; + +import java.util.ArrayList; + +public class Person { + private final String name; + private final int age; + private final Gender gender; + private final ArrayList hobbies; + private final String email; + private final int MAX_HOBBIES = 2; + + public class HobbyLimitExceededException extends Exception { + public HobbyLimitExceededException(String message) { + super(message); + } + } + + enum Gender { + MALE, + FEMALE, + OTHER + } + + public Person(String name, int age, Gender gender, String email) { + this.name = name; + this.age = age; + this.gender = gender; + this.email = email; + this.hobbies = new ArrayList<>(); + } + + public void addHobby(String hobby) throws HobbyLimitExceededException { + if (hobbies.size() >= MAX_HOBBIES) { + throw new HobbyLimitExceededException("Cannot add more than " + MAX_HOBBIES + " hobbies."); + } + hobbies.add(hobby); + } + + public ArrayList getHobbies() { + return hobbies; + } + + public boolean isAdult() { + return age >= 18; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/hummadtanweer/PersonTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/hummadtanweer/PersonTest.java new file mode 100644 index 000000000..a4f74fd67 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/hummadtanweer/PersonTest.java @@ -0,0 +1,58 @@ +package com.codedifferently.lesson16.hummadtanweer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import com.codedifferently.lesson16.hummadtanweer.Person.Gender; +import com.codedifferently.lesson16.hummadtanweer.Person.HobbyLimitExceededException; +import org.junit.jupiter.api.Test; + +public class PersonTest { + @Test + void testIsAdult() { + Person adult = new Person("xyz", 29, Gender.MALE, "xyz@gmail.com"); + assertTrue(adult.isAdult(), "True for 29"); + Person minor = new Person("abc", 15, Gender.FEMALE, "abc@gmail.com"); + assertFalse(minor.isAdult(), "False for 15"); + } + + @Test + void testAddHobby() throws HobbyLimitExceededException { + Person person = new Person("xyz", 29, Gender.MALE, "xyz@gmail.com"); + + person.addHobby("Football"); + person.addHobby("Pickleball"); + + assertEquals(2, person.getHobbies().size()); + assertTrue(person.getHobbies().contains("Football")); + assertTrue(person.getHobbies().contains("Pickleball")); + } + + @Test + public void testGetName() { + Person person = new Person("xyz", 29, Gender.MALE, "xyz@gmail.com"); + assertEquals("xyz", person.getName()); + } + + @Test + public void getEmail() { + Person person = new Person("xyz", 29, Gender.MALE, "xyz@gmail.com"); + assertEquals("xyz@gmail.com", person.getEmail()); + } + + @Test + void testAddHobbyExceedsLimit() throws HobbyLimitExceededException { + Person person = new Person("xyz", 29, Gender.MALE, "xyz@gmail.com"); + + try { + person.addHobby("Football"); + person.addHobby("Pickleball"); + person.addHobby("Basketball"); + fail("Expected HobbyLimitExceededException to be thrown"); + } catch (HobbyLimitExceededException e) { + assertEquals("Cannot add more than 2 hobbies.", e.getMessage()); + } + } +}