Skip to content

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 17 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.codedifferently.lesson16.player;

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++) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.codedifferently.lesson16.player;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.codedifferently.lesson16.player.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>
// unlockedAbilities, boolean isAlive