Skip to content

Commit af515aa

Browse files
authored
Merge branch 'code-differently:main' into main
2 parents 52373df + 30c9c25 commit af515aa

File tree

2 files changed

+113
-0
lines changed
  • lesson_16/objects/objects_app/src
    • main/java/com/codedifferently/lesson16/hummadtanweer
    • test/java/com/codedifferently/lesson16/hummadtanweer

2 files changed

+113
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.codedifferently.lesson16.hummadtanweer;
2+
3+
import java.util.ArrayList;
4+
5+
public class Person {
6+
private final String name;
7+
private final int age;
8+
private final Gender gender;
9+
private final ArrayList<String> hobbies;
10+
private final String email;
11+
private final int MAX_HOBBIES = 2;
12+
13+
public class HobbyLimitExceededException extends Exception {
14+
public HobbyLimitExceededException(String message) {
15+
super(message);
16+
}
17+
}
18+
19+
enum Gender {
20+
MALE,
21+
FEMALE,
22+
OTHER
23+
}
24+
25+
public Person(String name, int age, Gender gender, String email) {
26+
this.name = name;
27+
this.age = age;
28+
this.gender = gender;
29+
this.email = email;
30+
this.hobbies = new ArrayList<>();
31+
}
32+
33+
public void addHobby(String hobby) throws HobbyLimitExceededException {
34+
if (hobbies.size() >= MAX_HOBBIES) {
35+
throw new HobbyLimitExceededException("Cannot add more than " + MAX_HOBBIES + " hobbies.");
36+
}
37+
hobbies.add(hobby);
38+
}
39+
40+
public ArrayList<String> getHobbies() {
41+
return hobbies;
42+
}
43+
44+
public boolean isAdult() {
45+
return age >= 18;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public String getEmail() {
53+
return email;
54+
}
55+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.codedifferently.lesson16.hummadtanweer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
import com.codedifferently.lesson16.hummadtanweer.Person.Gender;
9+
import com.codedifferently.lesson16.hummadtanweer.Person.HobbyLimitExceededException;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class PersonTest {
13+
@Test
14+
void testIsAdult() {
15+
Person adult = new Person("xyz", 29, Gender.MALE, "[email protected]");
16+
assertTrue(adult.isAdult(), "True for 29");
17+
Person minor = new Person("abc", 15, Gender.FEMALE, "[email protected]");
18+
assertFalse(minor.isAdult(), "False for 15");
19+
}
20+
21+
@Test
22+
void testAddHobby() throws HobbyLimitExceededException {
23+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
24+
25+
person.addHobby("Football");
26+
person.addHobby("Pickleball");
27+
28+
assertEquals(2, person.getHobbies().size());
29+
assertTrue(person.getHobbies().contains("Football"));
30+
assertTrue(person.getHobbies().contains("Pickleball"));
31+
}
32+
33+
@Test
34+
public void testGetName() {
35+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
36+
assertEquals("xyz", person.getName());
37+
}
38+
39+
@Test
40+
public void getEmail() {
41+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
42+
assertEquals("[email protected]", person.getEmail());
43+
}
44+
45+
@Test
46+
void testAddHobbyExceedsLimit() throws HobbyLimitExceededException {
47+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
48+
49+
try {
50+
person.addHobby("Football");
51+
person.addHobby("Pickleball");
52+
person.addHobby("Basketball");
53+
fail("Expected HobbyLimitExceededException to be thrown");
54+
} catch (HobbyLimitExceededException e) {
55+
assertEquals("Cannot add more than 2 hobbies.", e.getMessage());
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)