Skip to content

feat: Added object person and test lesson 16 Hw by Hummad Tanweer #530

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

Merged
merged 8 commits into from
Nov 13, 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,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<String> 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<String> getHobbies() {
return hobbies;
}

public boolean isAdult() {
return age >= 18;
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}
}
Original file line number Diff line number Diff line change
@@ -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, "[email protected]");
assertTrue(adult.isAdult(), "True for 29");
Person minor = new Person("abc", 15, Gender.FEMALE, "[email protected]");
assertFalse(minor.isAdult(), "False for 15");
}

@Test
void testAddHobby() throws HobbyLimitExceededException {
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");

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, "[email protected]");
assertEquals("xyz", person.getName());
}

@Test
public void getEmail() {
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
assertEquals("[email protected]", person.getEmail());
}

@Test
void testAddHobbyExceedsLimit() throws HobbyLimitExceededException {
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");

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());
}
}
}