Skip to content

Commit 1631631

Browse files
committed
"working on object test file "
1 parent 2e32759 commit 1631631

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.HummadTanweer;
2+
3+
public class HobbyLimitExceededException extends Exception {
4+
public HobbyLimitExceededException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.codedifferently.lesson16.HummadTanweer;
2+
import java.util.ArrayList;
3+
4+
public class Person {
5+
private final String name;
6+
private final int age;
7+
private final Gender gender;
8+
private final ArrayList<String> hobbies;
9+
private final String email;
10+
private final int MAX_HOBBIES = 2;
11+
12+
enum Gender {
13+
MALE,
14+
FEMALE,
15+
OTHER
16+
}
17+
18+
public Person(String name, int age, Gender gender, String email) {
19+
this.name = name;
20+
this.age = age;
21+
this.gender = gender;
22+
this.email = email;
23+
this.hobbies = new ArrayList<>();
24+
}
25+
26+
public void addHobby(String hobby) throws HobbyLimitExceededException {
27+
if (hobbies.size() >= MAX_HOBBIES) {
28+
throw new HobbyLimitExceededException("Cannot add more than " + MAX_HOBBIES + " hobbies.");
29+
}
30+
hobbies.add(hobby);
31+
}
32+
33+
public ArrayList<String> getHobbies() {
34+
return hobbies;
35+
}
36+
37+
public void displayHobbies() {
38+
if (hobbies.isEmpty()) {
39+
System.out.println(name + " has no hobbies.");
40+
} else {
41+
System.out.println(name + "'s hobbies:");
42+
for (String hobby : hobbies) {
43+
System.out.println("- " + hobby);
44+
}
45+
}
46+
}
47+
48+
public boolean isAdult() {
49+
return age >= 18;
50+
}
51+
52+
public String getName() {
53+
return name;
54+
}
55+
56+
public Gender getGender() {
57+
return gender;
58+
}
59+
60+
public String getEmail() {
61+
return email;
62+
}
63+
64+
public static void main(String[] args) {
65+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
66+
67+
try {
68+
person.addHobby("Football");
69+
person.addHobby("Pickleball");
70+
person.addHobby("Basketball");
71+
} catch (HobbyLimitExceededException e) {
72+
System.out.println(e.getMessage());
73+
}
74+
75+
person.displayHobbies();
76+
77+
if (person.isAdult()) {
78+
System.out.println(person.getName() + " Adult");
79+
} else {
80+
System.out.println(person.getName() + " Not an adult");
81+
}
82+
}
83+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
import org.junit.jupiter.api.Test;
8+
9+
import com.codedifferently.lesson16.HummadTanweer.Person.Gender;
10+
11+
public class PersonTest {
12+
@Test
13+
void testIsAdult() {
14+
Person adult = new Person("xyz", 29, Gender.MALE, "[email protected]");
15+
assertTrue(adult.isAdult(), "True for 29");
16+
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+
void testAddHobbyExceedsLimit() throws HobbyLimitExceededException {
35+
Person person = new Person("xyz", 29, Gender.MALE, "[email protected]");
36+
37+
try {
38+
person.addHobby("Football");
39+
person.addHobby("Pickleball");
40+
person.addHobby("Basketball");
41+
fail("Expected HobbyLimitExceededException to be thrown");
42+
} catch (HobbyLimitExceededException e) {
43+
assertEquals("Cannot add more than 2 hobbies.", e.getMessage());
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)