-
Notifications
You must be signed in to change notification settings - Fork 23
Feat: create java file and test fileLesson_16 #511
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
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fc92fbb
feat:mbLesson_09
e42056b
Merge branch 'code-differently:main' into main
Tezz03 d7ca3b3
Merge branch 'code-differently:main' into main
Tezz03 42cbca7
Merge branch 'code-differently:main' into main
Tezz03 c57408a
Delete lesson_09/types/types_app/src/main/java/com/codedifferently/le…
Tezz03 af8eef3
Delete lesson_09/types/types_app/src/main/resources/data/montezb.json
Tezz03 7b24560
Merge branch 'code-differently:main' into main
Tezz03 d3239c0
Merge branch 'code-differently:main' into main
Tezz03 41cb92f
Merge branch 'code-differently:main' into main
Tezz03 70d28f7
Merge branch 'code-differently:main' into main
Tezz03 1dde77b
Merge branch 'code-differently:main' into main
Tezz03 74fb0ca
Merge branch 'code-differently:main' into main
Tezz03 957c931
Merge branch 'code-differently:main' into main
Tezz03 5991273
feat:create java file and test file
604f2c9
fix:upload more tests
7fe4780
fix: to upload
abb3215
chore: fixes namespaces
anthonydmays 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
99 changes: 99 additions & 0 deletions
99
...objects/objects_app/src/main/java/com/codedifferently/lesson16/playerleveling/Player.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,99 @@ | ||
package com.codedifferently.lesson16.playerleveling; | ||
|
||
public class Player { | ||
private String name; | ||
private Type type; | ||
private int level; | ||
private double currentExperience; | ||
private int expPerFight; | ||
private boolean isAlive; | ||
|
||
public enum Type { | ||
HUMAN, | ||
ELF, | ||
ORC, | ||
DRAGONIOD | ||
} | ||
|
||
public Player( | ||
String name, | ||
Type type, | ||
int level, | ||
double currentExperience, | ||
int expPerFight, | ||
boolean isAlive) { | ||
this.name = name; | ||
this.type = type; | ||
this.level = level; | ||
this.currentExperience = currentExperience; | ||
this.expPerFight = expPerFight; | ||
this.isAlive = isAlive; | ||
} | ||
|
||
// Getters and Setters | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Type getType() { | ||
return type; | ||
} | ||
|
||
public void setType(Type characterType) { | ||
this.type = characterType; | ||
} | ||
|
||
public int getLevel() { | ||
return level; | ||
} | ||
|
||
public void setLevel(int level) { | ||
this.level = level; | ||
} | ||
|
||
public double getCurrentExperience() { | ||
return currentExperience; | ||
} | ||
|
||
public void setCurrentExperiience(double currentExperience) { | ||
this.currentExperience = currentExperience; | ||
} | ||
|
||
public int getExpPerFight() { | ||
if (expPerFight <= 0) { | ||
throw new IllegalStateException("Experience per fight must be greater than 0."); | ||
} | ||
return expPerFight; | ||
} | ||
|
||
public void setExpPerFight(int expPerFight) { | ||
this.expPerFight = expPerFight; | ||
} | ||
|
||
public boolean getIsAlive() { | ||
return isAlive; | ||
} | ||
|
||
public void gainExperience(int fights) { | ||
for (int i = 0; i < fights; i++) { | ||
currentExperience += expPerFight; | ||
|
||
double requiredExp = 100 * Math.pow(2, level - 1); // Level 1: 100 XP, Level 2: 200 XP, etc. | ||
|
||
while (currentExperience >= requiredExp) { | ||
currentExperience -= requiredExp; // carry over any extra XP | ||
level++; | ||
System.out.println(name + " leveled up to level " + level + "!"); | ||
requiredExp = 100 * Math.pow(2, level - 1); // Update the required XP for the new level | ||
} | ||
} | ||
// used ai for lines between 85 and 97 | ||
} | ||
} | ||
|
||
// int level = 1; int experience = 0; for(int i = 1; i <= 100; i++) { |
107 changes: 107 additions & 0 deletions
107
...objects/objects_app/src/test/java/com/codedifferently/lesson16/playertest/PlayerTest.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,107 @@ | ||
package com.codedifferently.lesson16.playertest; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.codedifferently.lesson16.playerleveling.Player; | ||
import com.codedifferently.lesson16.playerleveling.Player.Type; | ||
import java.util.ArrayList; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PlayerTest { | ||
|
||
@Test | ||
public void getName() { | ||
var expectedName = "JinWhoo"; | ||
|
||
Player player1 = new Player("JinWhoo", null, 0, 0, 0, true); | ||
|
||
var actual = player1.getName(); | ||
|
||
assertEquals(expectedName, actual); | ||
} | ||
|
||
@Test | ||
public void setName() { | ||
var expectedName = "Montez"; | ||
|
||
Player player = new Player("JinWhoo", null, 0, 0, 0, true); | ||
|
||
player.setName(expectedName); | ||
|
||
assertEquals(expectedName, player.getName()); | ||
} | ||
|
||
@Test | ||
public void getType() { | ||
Player player1 = new Player(null, Player.Type.DRAGONIOD, 0, 0, 0, true); | ||
Type expected = Type.DRAGONIOD; | ||
assertEquals(expected, player1.getType()); | ||
} | ||
|
||
@Test | ||
public void setType() { | ||
Player player = new Player(null, Player.Type.HUMAN, 0, 0, 0, true); | ||
player.setType(Player.Type.ELF); | ||
assertEquals(Player.Type.ELF, player.getType()); | ||
} | ||
|
||
@Test | ||
public void getLevel() { | ||
Player player = new Player(null, null, 5, 0, 0, true); | ||
assertEquals(5, player.getLevel()); | ||
} | ||
|
||
@Test | ||
public void setLevel() { | ||
Player player = new Player(null, null, 1, 0, 0, true); | ||
player.setLevel(10); | ||
assertEquals(10, player.getLevel()); | ||
} | ||
|
||
@Test | ||
public void getCurrentExperience() { | ||
Player player = new Player(null, null, 0, 150.5, 0, true); | ||
assertEquals(150.5, player.getCurrentExperience()); | ||
} | ||
|
||
@Test | ||
public void setCurrentExperience() { | ||
Player player = new Player(null, null, 0, 0, 0, true); | ||
player.setCurrentExperiience(200.75); | ||
assertEquals(200.75, player.getCurrentExperience()); | ||
} | ||
|
||
@Test | ||
public void getExpPerFight() { | ||
Player player = new Player(null, null, 0, 0, 50, true); | ||
assertEquals(50, player.getExpPerFight()); | ||
} | ||
|
||
@Test | ||
public void setExpPerFight() { | ||
Player player = new Player(null, null, 0, 0, 0, true); | ||
player.setExpPerFight(100); | ||
assertEquals(100, player.getExpPerFight()); | ||
} | ||
|
||
@Test | ||
public void getIsAlive() { | ||
Player player = new Player(null, null, 0, 0, 0, true); | ||
assertTrue(player.getIsAlive()); | ||
} | ||
|
||
@Test | ||
public void testLevelUpOnce() { | ||
ArrayList<String> abilities = new ArrayList<>(); | ||
Player player1 = new Player("Jordan", Player.Type.HUMAN, 1, 0, 100, true); | ||
|
||
player1.gainExperience(1); // 1 fight = 100 XP | ||
|
||
assertEquals(2, player1.getLevel()); // Should now be level 2 | ||
assertEquals(0, player1.getCurrentExperience()); // XP resets after exact level up | ||
} | ||
} | ||
|
||
// String name, Type type, int level, double currentExperience, int expPerFight, ArrayList<String> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cannot commit commented out code, please remove. |
||
// unlockedAbilities, boolean isAlive |
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.
Wrong package and file location, please fix.