Search code examples
javacomposition

Java using composition


I started working on a basic RPG game, and I have a little problem. I have a Character class which is our hero with his data. And I'm using composition to create a character. The problem I have is in the CharacterCreator in the createCharacter method, there's something wrong with hero. Why, and how do I fix this? Here's the code:

public class Game {
    public static void main(String[] args) throws IOException {

        Character h = CharacterCreator.createCharacter();

        try {
            h.displayCharacter();
        } catch (Exception e) {
            System.out.println("Wrong");
        }

    }

}

public class CharacterCreator {

    public static Character createCharacter() {
        System.out.println("Choose a character: ");
        System.out.println("1. Fighter");
        System.out.println("2. Rogue");
        System.out.println("3. Mage");
        System.out.println("4. Cleric");

        Scanner sc = new Scanner(System.in);
        int scan = sc.nextInt();
        String chosenClass = getCharacterClass(scan);

        System.out.println("Choose Name:");
        Scanner nameIn = new Scanner(System.in);
        String name = nameIn.next();

        Character hero = null;

        switch (chosenClass) {
        case "Fighter":
            Fighter hero = new Fighter(name);
            break;
        case "Rogue":
            Rogue hero = new Rogue(name);
            break;
        case "Mage":
            Mage hero = new Mage(name);
            break;
        case "Cleric":
            Cleric hero = new Cleric(name);
            break;
        }

        return hero;
    }

    public static String getCharacterClass(int scan) {

        String classIn;

        switch (scan) {
        case 1:
            classIn = "Fighter";
            break;
        case 2:
            classIn = "Rogue";
            break;
        case 3:
            classIn = "Mage";
            break;
        case 4:
            classIn = "Cleric";
            break;
        default:
            System.out.println("Enter again");
            classIn = "def";
        }

        return classIn;
    }

}

public class Character {

    private String name;
    private String characterClass;
    private int level;
    private int hp;
    private int currentHp;
    private int armorClass;

    private long xp;
    /* private int BAB; /*Base attack bonus */

    private int strength;
    private int constitution;
    private int dexterity;
    private int intelligence;
    private int wisdom;
    private int charisma;

    protected Character(String name) {

        setName(name);
        characterClass = "Class";
        setLevel(1);
        setStrength(10);
        setConstitution(14);
        setDexterity(14);
        setIntelligence(10);
        setWisdom(10);
        setCharisma(10);
        setHp(0 + getModifier(getConstitution()));
        setCurrentHp(getHp());
        setArmorClass(10 + getModifier(getDexterity()));
        setXp(0);

    }

    void displayCharacter() throws IOException {
        System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        System.out.println("Name: " + getName());
        System.out.println("Class: " + getCharacterClass());
        System.out.println("Level: " + getLevel());
        System.out.println("HP: " + getHp());
        System.out.println("Armor Class: " + getArmorClass());

        System.out.println("***************");
        System.out.println("Attributes: ");
        System.out.println("Strength: " + getStrength());
        System.out.println("Constitution: " + getConstitution());
        System.out.println("Dexterity: " + getDexterity());
        System.out.println("Intelligence: " + getIntelligence());
        System.out.println("Wisdom: " + getWisdom());
        System.out.println("Charisma: " + getCharisma());
        System.out.println("***************");
        System.out.println("XP: " + getXp());

    }

    public int getModifier(int number) {
        int mod = (int) ((number - 10) / 2);
        return mod;
    }

    public String getName() {
        return name;
    }

    public String getCharacterClass() {
        return characterClass;
    }

    public int getLevel() {
        return level;
    }

    public int getHp() {
        return hp;
    }

    public int getCurrentHp() {
        return currentHp;
    }

    public int getArmorClass() {
        return armorClass;
    }

    public int getStrength() {
        return strength;
    }

    public int getConstitution() {
        return constitution;
    }

    public int getDexterity() {
        return dexterity;
    }

    public int getIntelligence() {
        return intelligence;
    }

    public int getWisdom() {
        return wisdom;
    }

    public int getCharisma() {
        return charisma;
    }

    public long getXp() {
        return xp;
    }

    protected void setName(String Name) {
        name = Name;
    }

    protected void setCharacterClass(String characterClass) {
        this.characterClass = characterClass;
    }

    protected void setLevel(int lvl) {
        level = lvl;
    }

    protected void setHp(int hitPoints) {
        hp = hitPoints;
    }

    protected void setCurrentHp(int curHp) {
        currentHp = curHp;
    }

    protected void setArmorClass(int ac) {
        armorClass = ac;
    }

    protected void setStrength(int str) {
        strength = str;
    }

    protected void setConstitution(int con) {
        constitution = con;
    }

    protected void setDexterity(int dex) {
        dexterity = dex;
    }

    protected void setIntelligence(int intel) {
        intelligence = intel;
    }

    protected void setWisdom(int wis) {
        wisdom = wis;
    }

    protected void setCharisma(int cha) {
        charisma = cha;
    }

    protected void setXp(int XP) {
        xp = XP;
    }

}

class Fighter extends CharacterClass {

    Fighter(String name) {

        Character hero = new Character(name);

        hero.setName(name);
        hero.setCharacterClass("Fighter");
        hero.setLevel(1);
        hero.setStrength(14);
        hero.setConstitution(16);
        hero.setDexterity(14);
        hero.setIntelligence(10);
        hero.setWisdom(10);
        hero.setCharisma(10);
        hero.setHp(10 + hero.getModifier(getConstitution()));
        hero.setCurrentHp(hero.getHp());
        hero.setArmorClass(10 + hero.getModifier(getDexterity()));
    }

}

Solution

  • You are creating new hero instances inside your switch cases and assigning these to a local variable. The hero variable that you created before entering the switch statement will always remain null. You need to assign your new hero to this variable instead.

    Character hero = null;
    switch (chosenClass){
        case "Fighter":
            hero = new Fighter(name);
            break;
        case "Rogue":
            hero = new Rogue(name);
            break;
        case "Mage":
            hero = new Mage(name);
            break;
        case "Cleric":
            hero = new Cleric(name);
            break;
    }
    return hero;