-
Notifications
You must be signed in to change notification settings - Fork 25
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
VicenteVigueras
merged 8 commits into
code-differently:main
from
htanweer244:feature/lesson16
Nov 13, 2024
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1631631
"working on object test file "
htanweer244 3f68607
feat: modified tests
htanweer244 52c5d10
chore: fixed package and directory to lowercase
htanweer244 3bf341e
chore: fixed package and directory to lowercase
htanweer244 09b9c31
chore: fixed package and directory to lowercase (now works)
htanweer244 0be3e41
chore: fixed package and directory to lowercase (now works with full …
htanweer244 6de2a34
chore: applied gradle spotlessApply
htanweer244 f773a95
chore: applied gradle spotlessApplygit status
htanweer244 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
.../objects/objects_app/src/main/java/com/codedifferently/lesson16/HummadTanweer/Person.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...ects/objects_app/src/test/java/com/codedifferently/lesson16/HummadTanweer/PersonTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.