-
Notifications
You must be signed in to change notification settings - Fork 25
Feat: added Boxer.java and BoxerTest.java to create and test a class object. lesson_16 - JosephCaballero #532
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
anthonydmays
merged 8 commits into
code-differently:main
from
Josephcabs:feature/lesson_16
Nov 23, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fd647ea
feat: added Boxer.java to create a real world object and BoxerTest.j…
Josephcabs 310ef07
feat: modified tests more in BoxerTest.java and modified code in Boxe…
Josephcabs a05482a
Merge branch 'main' of https://github.com/Josephcabs/code-differently…
Josephcabs 88edd33
fix: adds spotlessApply. lesson_16 -JosephCaballero
Josephcabs 76f0c6c
chore:fixing basic spacing to ensure usability. lesson_16 - JosephCab…
Josephcabs 2d71735
chore: applied spotlessApply and adadjusted tests. lesson_16 -JosephC…
Josephcabs b109574
feat: merging commit
Josephcabs d44f561
fix: adds an enum to check aginst another enum. lesson_16 -JosephCaba…
Josephcabs 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
144 changes: 144 additions & 0 deletions
144
lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/boxer/Boxer.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,144 @@ | ||
package com.codedifferently.lesson16.boxer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Random; | ||
|
||
public class Boxer { | ||
public enum BoxerStyle { | ||
SWITCHHITTER, | ||
BOXERPUNCHER, | ||
SOUTHPAW, | ||
INFIGHTER, | ||
SLUGGER, | ||
OUTFIGHTER, | ||
ORTHODOX; | ||
} | ||
|
||
public class BoxerIsRetiredException extends Exception { | ||
public BoxerIsRetiredException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
public class BoxerHasNoFightsException extends Exception { | ||
public BoxerHasNoFightsException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
private String name; | ||
private HashMap<String, Character> fights; | ||
private int power; | ||
private int health; | ||
private BoxerStyle skillSet; | ||
private boolean ableToFight; | ||
private BoxerStyle[] style = BoxerStyle.values(); | ||
private static Random rand = new Random(); | ||
|
||
public Boxer(String name, int power, int health) { | ||
this.name = name; | ||
this.power = power; | ||
this.health = health; | ||
this.ableToFight = true; | ||
this.skillSet = BoxerStyle.ORTHODOX; | ||
fights = new HashMap<>(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public boolean getAbleToFight() { | ||
return ableToFight; | ||
} | ||
|
||
public void workout(int additional) { | ||
power += additional; | ||
} | ||
|
||
public int getPower() { | ||
return power; | ||
} | ||
|
||
public int getHealth() { | ||
return health; | ||
} | ||
|
||
public void rest() { | ||
health += 1; | ||
} | ||
|
||
public void rollSkillSet() { | ||
int randomIndex = rand.nextInt(style.length); | ||
BoxerStyle newStyle = style[randomIndex]; | ||
skillSet = newStyle; | ||
System.out.println("Random style: " + skillSet); | ||
} | ||
|
||
public void addFights(String name, char d) throws BoxerIsRetiredException { | ||
if (ableToFight == false) { | ||
throw new BoxerIsRetiredException("Boxer is retired"); | ||
} | ||
fights.put(name, d); | ||
} | ||
|
||
public void retire() throws BoxerIsRetiredException { | ||
checkRetirementStatus(); | ||
ableToFight = false; | ||
} | ||
|
||
private void checkRetirementStatus() throws BoxerIsRetiredException { | ||
if (ableToFight == false) { | ||
throw new BoxerIsRetiredException("Boxer is already retired"); | ||
} | ||
} | ||
|
||
private void checkBoxerHasntFoughtAnyone() throws BoxerHasNoFightsException { | ||
if (fights.isEmpty()) { | ||
throw new BoxerHasNoFightsException("Boxer hasnt fought anyone"); | ||
} | ||
} | ||
|
||
public String getFights() throws BoxerHasNoFightsException { | ||
checkBoxerHasntFoughtAnyone(); | ||
String fightsHad = ""; | ||
for (Map.Entry<String, Character> entry : fights.entrySet()) { | ||
String key = entry.getKey(); | ||
char value = entry.getValue(); | ||
fightsHad += "fought: " + key + " " + value + "."; | ||
} | ||
return fightsHad; | ||
} | ||
|
||
public void bout(Boxer boxer) throws BoxerIsRetiredException { | ||
if (ableToFight == false) { | ||
throw new BoxerIsRetiredException("Boxer is retired"); | ||
} | ||
String boxerName = boxer.getName(); | ||
if (power < boxer.getPower()) { | ||
ableToFight = false; | ||
fights.put(boxerName, 'L'); | ||
} else if (power == boxer.getPower()) { | ||
fights.put(boxerName, 'D'); | ||
} else { | ||
fights.put(boxerName, 'W'); | ||
} | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setHealth(int health) { | ||
this.health = health; | ||
} | ||
|
||
public BoxerStyle getSkillSet() { | ||
return skillSet; | ||
} | ||
|
||
public void setAbleToFight(boolean ableToFight) { | ||
this.ableToFight = ableToFight; | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
...on_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/boxer/BoxerTest.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,159 @@ | ||
package com.codedifferently.lesson16.boxer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
import com.codedifferently.lesson16.boxer.Boxer.BoxerHasNoFightsException; | ||
import com.codedifferently.lesson16.boxer.Boxer.BoxerIsRetiredException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BoxerTest { | ||
public enum testStyle { | ||
BASIC | ||
} | ||
|
||
Boxer boxer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
boxer = new Boxer("Joseph", 99, 99); | ||
} | ||
|
||
@Test | ||
void testWorkout_addingTenPointsOfPower() { | ||
boxer.workout(10); | ||
assertEquals(109, boxer.getPower()); | ||
} | ||
|
||
@Test | ||
void testRest_restingToIncreaseHealth() { | ||
int previousHealth = boxer.getHealth(); | ||
boxer.rest(); | ||
assertNotEquals(boxer.getHealth(), previousHealth); | ||
} | ||
|
||
@Test | ||
void testGetFightHistory_makingAFighterToHaveABout() | ||
throws BoxerHasNoFightsException, BoxerIsRetiredException { | ||
Boxer mike = new Boxer("mike", 1000, 1000); | ||
boxer.bout(mike); | ||
assertEquals("fought: " + mike.getName() + " L.", boxer.getFights()); | ||
} | ||
|
||
@Test | ||
void testIsAbleToFight_boxerIsNotRetiredOrInjured() { | ||
assertEquals(true, boxer.getAbleToFight()); | ||
} | ||
|
||
@Test | ||
void testIsAbleToFight_boxerIsNotAbleToFight() throws BoxerIsRetiredException { | ||
boxer.retire(); | ||
assertEquals(false, boxer.getAbleToFight()); | ||
} | ||
|
||
@Test | ||
void testBout_makingNewBoxerToFightAnotherAndSeeTheResults() throws BoxerIsRetiredException { | ||
Boxer mike = new Boxer("Mike", 1000, 1000); | ||
boxer.bout(mike); | ||
assertEquals(false, boxer.getAbleToFight()); | ||
} | ||
|
||
@Test | ||
void testBout_makingNewBoxerToFightAndDraw() | ||
throws BoxerIsRetiredException, BoxerHasNoFightsException { | ||
Boxer ryan = new Boxer("Ryan Garcia", 99, 99); | ||
boxer.bout(ryan); | ||
assertEquals(boxer.getFights(), "fought: Ryan Garcia D."); | ||
} | ||
|
||
@Test | ||
void testBout_makingNewBoxerToWinAgainstWithBoxer() | ||
throws BoxerIsRetiredException, BoxerHasNoFightsException { | ||
Boxer hitman = new Boxer("Thomas Hearns", 98, 98); | ||
boxer.bout(hitman); | ||
assertEquals(boxer.getFights(), "fought: " + hitman.getName() + " W."); | ||
} | ||
|
||
@Test | ||
void testGetHealth() { | ||
assertEquals(boxer.getHealth(), 99); | ||
} | ||
|
||
@Test | ||
void testRollSkillSet_testingBasicIsNotWhatIsRolled() { | ||
boxer.rollSkillSet(); | ||
assertNotEquals(boxer.getSkillSet(), testStyle.BASIC); | ||
} | ||
|
||
@Test | ||
void testAddFights() throws BoxerIsRetiredException, BoxerHasNoFightsException { | ||
boxer.addFights("Marvin Hagler", 'L'); | ||
assertEquals(boxer.getFights(), "fought: Marvin Hagler L."); | ||
} | ||
|
||
@Test | ||
void testAddFights_whenRetired() throws BoxerIsRetiredException { | ||
boxer.retire(); | ||
assertThatThrownBy( | ||
() -> { | ||
boxer.addFights("lomachenko", 'W'); | ||
}) | ||
.isInstanceOf(BoxerIsRetiredException.class) | ||
.hasMessage("Boxer is retired"); | ||
} | ||
|
||
@Test | ||
void testRetire_whenRetired() throws BoxerIsRetiredException { | ||
boxer.retire(); | ||
assertThatThrownBy( | ||
() -> { | ||
boxer.retire(); | ||
}) | ||
.isInstanceOf(BoxerIsRetiredException.class) | ||
.hasMessage("Boxer is already retired"); | ||
} | ||
|
||
@Test | ||
void testSetName() { | ||
boxer.setName("Pablo"); | ||
assertEquals(boxer.getName(), "Pablo"); | ||
} | ||
|
||
@Test | ||
void testSetAbleToFight() { | ||
boxer.setAbleToFight(false); | ||
assertFalse(boxer.getAbleToFight()); | ||
} | ||
|
||
@Test | ||
void testSetHealth() { | ||
boxer.setHealth(2); | ||
assertEquals(boxer.getHealth(), 2); | ||
} | ||
|
||
@Test | ||
void testBoxerIsRetiredException_retiringBoxerThenThrowingException() | ||
throws BoxerIsRetiredException { | ||
Boxer leaonard = new Boxer("Sugar ray", 200, 200); | ||
boxer.retire(); | ||
assertThatThrownBy( | ||
() -> { | ||
boxer.bout(leaonard); | ||
}) | ||
.isInstanceOf(BoxerIsRetiredException.class) | ||
.hasMessageContaining("Boxer is retired"); | ||
} | ||
|
||
@Test | ||
void testBoxerHasNoFightsException_gettingFightsWhenNoFightsHaveBeenHad() { | ||
assertThatThrownBy( | ||
() -> { | ||
boxer.getFights(); | ||
}) | ||
.isInstanceOf(BoxerHasNoFightsException.class) | ||
.hasMessageContaining("Boxer hasnt fought anyone"); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertion in this test compares the
SkillSet
to the string"BASIC"
, but there are two issues:BoxerStyle
enum does not have aBASIC
value.BASIC
were an enum value, it should be compared asBoxerStyle.BASIC
.