Item class/system for RPG/DND type game












1












$begingroup$


Thanks for the enormous response on my last question! I've returned with another question, hoping for more insight.



I'm developing an Item class and system, and I know it can be improved. I have a main Item class, with four subclasses (Weapon, Armor, Medical, Misc) .





  1. Code Redundancy



    The subclasses all have near identical class construction. The only difference between the Weapon and Armor class is the name of the classes. I would like to maybe have one class that manages all the different type of items, and can identify the differences between them so they can be used accordingly in my program.




  2. Code Efficiency



    Determining what items are determined are based on blocks of if statements. I would like to cut those down, so the code is more user friendly and more compact, and obviously more efficient.




  3. Item Determination



    Right now, when a new Item object is created, the item is randomly chosen WITHIN the Item class. I would hope for this to be managed by an outside class, which can reduce the complexity of the class.




Item.java



import java.util.Random;

/*
* Item Class
*/
public class Item {

/*
* Private Instance Variables
*/
private Random r;
private final String name;
protected int value; /* allows subclass to use */
private int level; /* Used to determine what level of item to set */

/*
* Arrays used for item level determination (ranges, not each specific level)
*/
Object normal_drops = {new Medical("Medicinal Herb", this.level), new Misc("Antidontial Herb"), new Misc("Moonwort Bulb"), new Misc("Holy Water")};
int normal_drop_values = {3, 2, 5, 4};
Object rare_drops = {new Misc("Slime Drop"), new Armor("Boxer Shorts", this.level), new Armor("Leather Hat", this.level), new Weapon("Oak Staff", this.level)};
int rare_drop_values = {10, 15, 12, 8};

/*
* Item Constructor (no defualt)
*/
public Item(int level) {
this.level = level;
this.r = new Random();
this.determineItem();
}

/*
* determineItem method for deciding if it will drop an item at all, and
* if so, decides if it's rare or normal
*/
private void determineItem() {
int x = this.r.nextInt(100 - 1) + 1;
if(x > 95) { /* Rare Drop Chance (5%) */
this.rareDrop();
} else if(x > 90) { /* Normal Drop Chance (10%) */
this.normalDrop();
}
}

/*
* normalDrop method for normal drop items
*/
private void normalDrop() {
int index = this.r.nextInt(4 - 0) + 0; /* Isn't inclusive, so 4 is needed */
if(this.level >= 1 && this.level <= 10) { /* Level Range 1-10 */
this.name = normal_drops[index];
this.value = normal_drop_values[index];
}
/*
* Now, reset the array each time for new items
*/
if(this.level >= 11 && this.level <= 20) { /* Level Range 11-20 */
normal_drops = {new Misc("Cougulant"), new Armor("Pot Lid", this.level), new Misc("Evencloth"), new Armor("Leather Kilt", this.level)};
updateValues(false, 2);
this.name = normal_drops[index];
this.value = normal_drop_values[index];
}
if(this.level >= 21 && this.level <= 30) { /* Level Range 21-30 */
normal_drops = {new Armor("Scale Shield", this.level), new Medical("Strong Medicine", this.level), new Misc("Wing of Bat"), new Misc("Cowpat")};
updateValues(false, 3);
this.name = normal_drops[index];
this.value = normal_drop_values[index];
}
if(this.level >= 31 && this.level <= 40) { /* Level Range 31-40 */
normal_drops = {new Medical("Superior Medicine"), new Misc("Seashell"), new Misc("Lambswool"), new Misc("Kitty Litter")};
updateValues(false, 4);
this.name = normal_drops[index];
this.value = normal_drop_values[index];
}
if(this.level >= 41 && this.level <= 50) { /* Level Range 41-50 */
normal_drops = {new Misc("Magic Beast Horn"), new Misc("Rockbomb Shell"), new Misc("Lambswool"), new Misc("Manky Mud")};
updateValues(false, 5);
this.name = normal_drops[index];
this.value = normal_drop_values[index];
}

}

/*
* rareDrop method for rare drop items
*/
private void rareDrop() {
int index = this.r.nextInt(4 - 0) + 0; /* Not inclusive, so 4 is needed */
if(this.level >= 1 && this.level <= 10) { /* Level Range 1-10 */
this.name = rare_drops[index];
this.value = rare_drop_values[index];
}
/*
* Reset array each time for new items
*/
if(this.level >= 11 && this.level <= 20) { /* Level Range 11-20 */
rare_drops = {new Misc("Iron Nails"), new Armor("Garish Garb", this.level), new Misc("Angel Bell"), new Misc("Fisticup")};
updateValues(true, 2);
this.name = rare_drops[index];
this.value = rare_drop_values[index];
}
if(this.level >= 21 && this.level <= 30) { /* Level Range 21-30 */
rare_drops = {new Armor("Gold Ring", this.level), new Armor("Agility Ring", this.level), new Armor("Strength Ring", this.level), new Armor("Leather Cape", this.level)};
updateValues(true, 3);
this.name = rare_drops[index];
this.value = rare_drop_values[index];
}
if(this.level >= 31 && this.level <= 40) { /* Level Range 31-40 */
rare_drops = {new Misc("Flintstone"), new Weapon("Iron Claws", this.level), new Misc("Softwort"), new Weapon("Long Spear", this.level)};
updateValues(true, 4);
this.name = rare_drops[index];
this.value = rare_drop_values[index];
}
if(this.level >= 41 && this.level <= 50) { /* Level Range 41-50 */
rare_drops = {new Armor("Fur Poncho", this.level), new Armor("Ice Shield", this.level), new Weapon("Assassins Dagger", this.level), new Weapon("Crow's Claws", this.level)};
updateValues(true, 5);
this.name = rare_drops[index];
this.value = rare_drop_values[index];
}
}

/*
* Method updates the value of each item in the array
*/
private void updateValues(boolean isRare, int amount) {
/*
* Resets values so it doesn't keep stacking with each level
*/
normal_drop_values = {3, 2, 5, 4};
rare_drop_values = {10, 15, 12, 8};

if(!isRare) {
for(int i = 0; i <= this.normal_drop_values.length - 1; i++) {
this.normal_drop_values[i] *= amount;
}
} else {
for(int i = 0; i <= this.rare_drop_values.length - 1; i++) {
this.rare_drop_values[i] *= amount;
}
}
}

/*
* Getters for item value and name
*/
public int getValue() {
return this.value;
}

public String getName() {
return this.name;
}

}


Weapon.java



public class Weapon extends Item {

/*
* Private Instance Variables
*/
private final String name;
private int level;
private final int damage;

/*
* Weapon constructor (no default)
*/
public Weapon(String name, int level) {
this.name = name;
this.level = level;
this.setAttributes();
}

/*
* setAttributes, determines weapon damage
*/
private void setAttributes() {
this.damage = this.level + 2;
}

}


Armor.java



public class Armor extends Item {

/*
* Private Instance Variables
*/
private final String name;
private int level;
private final int defence;

/*
* Armor constructor (no default)
*/
public Armor(String name, int level) {
this.name = name;
this.level = level;
this.setAttributes();
}

/*
* setAttributes, determines defence level
*/
private void setAttributes() {
this.defence = this.level + 2;
}

}


Medical.java



public class Medical extends Item {

/*
* Private Instance Variables
*/
private final String name;
private int level;
private final int heal_value;

/*
* Medical constructor (no default)
*/
public Medical(String name, int level) {
this.name = name;
this.level = level;
this.setAttributes();
}

/*
* setAttributes, determines heal value
*/
private void setAttributes() {
this.heal_value = this.level * 2;
}

/*
* Getters for heal value
*/
public int getHealValue() {
return this.heal_value;
}

}


Misc.java



public class Misc extends Item {

/*
* Private Instance Variables
*/
private final String name;

/*
* Misc constructor (no default)
*/
public Misc(String name) {
this.name = name;
}

}









share|improve this question











$endgroup$

















    1












    $begingroup$


    Thanks for the enormous response on my last question! I've returned with another question, hoping for more insight.



    I'm developing an Item class and system, and I know it can be improved. I have a main Item class, with four subclasses (Weapon, Armor, Medical, Misc) .





    1. Code Redundancy



      The subclasses all have near identical class construction. The only difference between the Weapon and Armor class is the name of the classes. I would like to maybe have one class that manages all the different type of items, and can identify the differences between them so they can be used accordingly in my program.




    2. Code Efficiency



      Determining what items are determined are based on blocks of if statements. I would like to cut those down, so the code is more user friendly and more compact, and obviously more efficient.




    3. Item Determination



      Right now, when a new Item object is created, the item is randomly chosen WITHIN the Item class. I would hope for this to be managed by an outside class, which can reduce the complexity of the class.




    Item.java



    import java.util.Random;

    /*
    * Item Class
    */
    public class Item {

    /*
    * Private Instance Variables
    */
    private Random r;
    private final String name;
    protected int value; /* allows subclass to use */
    private int level; /* Used to determine what level of item to set */

    /*
    * Arrays used for item level determination (ranges, not each specific level)
    */
    Object normal_drops = {new Medical("Medicinal Herb", this.level), new Misc("Antidontial Herb"), new Misc("Moonwort Bulb"), new Misc("Holy Water")};
    int normal_drop_values = {3, 2, 5, 4};
    Object rare_drops = {new Misc("Slime Drop"), new Armor("Boxer Shorts", this.level), new Armor("Leather Hat", this.level), new Weapon("Oak Staff", this.level)};
    int rare_drop_values = {10, 15, 12, 8};

    /*
    * Item Constructor (no defualt)
    */
    public Item(int level) {
    this.level = level;
    this.r = new Random();
    this.determineItem();
    }

    /*
    * determineItem method for deciding if it will drop an item at all, and
    * if so, decides if it's rare or normal
    */
    private void determineItem() {
    int x = this.r.nextInt(100 - 1) + 1;
    if(x > 95) { /* Rare Drop Chance (5%) */
    this.rareDrop();
    } else if(x > 90) { /* Normal Drop Chance (10%) */
    this.normalDrop();
    }
    }

    /*
    * normalDrop method for normal drop items
    */
    private void normalDrop() {
    int index = this.r.nextInt(4 - 0) + 0; /* Isn't inclusive, so 4 is needed */
    if(this.level >= 1 && this.level <= 10) { /* Level Range 1-10 */
    this.name = normal_drops[index];
    this.value = normal_drop_values[index];
    }
    /*
    * Now, reset the array each time for new items
    */
    if(this.level >= 11 && this.level <= 20) { /* Level Range 11-20 */
    normal_drops = {new Misc("Cougulant"), new Armor("Pot Lid", this.level), new Misc("Evencloth"), new Armor("Leather Kilt", this.level)};
    updateValues(false, 2);
    this.name = normal_drops[index];
    this.value = normal_drop_values[index];
    }
    if(this.level >= 21 && this.level <= 30) { /* Level Range 21-30 */
    normal_drops = {new Armor("Scale Shield", this.level), new Medical("Strong Medicine", this.level), new Misc("Wing of Bat"), new Misc("Cowpat")};
    updateValues(false, 3);
    this.name = normal_drops[index];
    this.value = normal_drop_values[index];
    }
    if(this.level >= 31 && this.level <= 40) { /* Level Range 31-40 */
    normal_drops = {new Medical("Superior Medicine"), new Misc("Seashell"), new Misc("Lambswool"), new Misc("Kitty Litter")};
    updateValues(false, 4);
    this.name = normal_drops[index];
    this.value = normal_drop_values[index];
    }
    if(this.level >= 41 && this.level <= 50) { /* Level Range 41-50 */
    normal_drops = {new Misc("Magic Beast Horn"), new Misc("Rockbomb Shell"), new Misc("Lambswool"), new Misc("Manky Mud")};
    updateValues(false, 5);
    this.name = normal_drops[index];
    this.value = normal_drop_values[index];
    }

    }

    /*
    * rareDrop method for rare drop items
    */
    private void rareDrop() {
    int index = this.r.nextInt(4 - 0) + 0; /* Not inclusive, so 4 is needed */
    if(this.level >= 1 && this.level <= 10) { /* Level Range 1-10 */
    this.name = rare_drops[index];
    this.value = rare_drop_values[index];
    }
    /*
    * Reset array each time for new items
    */
    if(this.level >= 11 && this.level <= 20) { /* Level Range 11-20 */
    rare_drops = {new Misc("Iron Nails"), new Armor("Garish Garb", this.level), new Misc("Angel Bell"), new Misc("Fisticup")};
    updateValues(true, 2);
    this.name = rare_drops[index];
    this.value = rare_drop_values[index];
    }
    if(this.level >= 21 && this.level <= 30) { /* Level Range 21-30 */
    rare_drops = {new Armor("Gold Ring", this.level), new Armor("Agility Ring", this.level), new Armor("Strength Ring", this.level), new Armor("Leather Cape", this.level)};
    updateValues(true, 3);
    this.name = rare_drops[index];
    this.value = rare_drop_values[index];
    }
    if(this.level >= 31 && this.level <= 40) { /* Level Range 31-40 */
    rare_drops = {new Misc("Flintstone"), new Weapon("Iron Claws", this.level), new Misc("Softwort"), new Weapon("Long Spear", this.level)};
    updateValues(true, 4);
    this.name = rare_drops[index];
    this.value = rare_drop_values[index];
    }
    if(this.level >= 41 && this.level <= 50) { /* Level Range 41-50 */
    rare_drops = {new Armor("Fur Poncho", this.level), new Armor("Ice Shield", this.level), new Weapon("Assassins Dagger", this.level), new Weapon("Crow's Claws", this.level)};
    updateValues(true, 5);
    this.name = rare_drops[index];
    this.value = rare_drop_values[index];
    }
    }

    /*
    * Method updates the value of each item in the array
    */
    private void updateValues(boolean isRare, int amount) {
    /*
    * Resets values so it doesn't keep stacking with each level
    */
    normal_drop_values = {3, 2, 5, 4};
    rare_drop_values = {10, 15, 12, 8};

    if(!isRare) {
    for(int i = 0; i <= this.normal_drop_values.length - 1; i++) {
    this.normal_drop_values[i] *= amount;
    }
    } else {
    for(int i = 0; i <= this.rare_drop_values.length - 1; i++) {
    this.rare_drop_values[i] *= amount;
    }
    }
    }

    /*
    * Getters for item value and name
    */
    public int getValue() {
    return this.value;
    }

    public String getName() {
    return this.name;
    }

    }


    Weapon.java



    public class Weapon extends Item {

    /*
    * Private Instance Variables
    */
    private final String name;
    private int level;
    private final int damage;

    /*
    * Weapon constructor (no default)
    */
    public Weapon(String name, int level) {
    this.name = name;
    this.level = level;
    this.setAttributes();
    }

    /*
    * setAttributes, determines weapon damage
    */
    private void setAttributes() {
    this.damage = this.level + 2;
    }

    }


    Armor.java



    public class Armor extends Item {

    /*
    * Private Instance Variables
    */
    private final String name;
    private int level;
    private final int defence;

    /*
    * Armor constructor (no default)
    */
    public Armor(String name, int level) {
    this.name = name;
    this.level = level;
    this.setAttributes();
    }

    /*
    * setAttributes, determines defence level
    */
    private void setAttributes() {
    this.defence = this.level + 2;
    }

    }


    Medical.java



    public class Medical extends Item {

    /*
    * Private Instance Variables
    */
    private final String name;
    private int level;
    private final int heal_value;

    /*
    * Medical constructor (no default)
    */
    public Medical(String name, int level) {
    this.name = name;
    this.level = level;
    this.setAttributes();
    }

    /*
    * setAttributes, determines heal value
    */
    private void setAttributes() {
    this.heal_value = this.level * 2;
    }

    /*
    * Getters for heal value
    */
    public int getHealValue() {
    return this.heal_value;
    }

    }


    Misc.java



    public class Misc extends Item {

    /*
    * Private Instance Variables
    */
    private final String name;

    /*
    * Misc constructor (no default)
    */
    public Misc(String name) {
    this.name = name;
    }

    }









    share|improve this question











    $endgroup$















      1












      1








      1





      $begingroup$


      Thanks for the enormous response on my last question! I've returned with another question, hoping for more insight.



      I'm developing an Item class and system, and I know it can be improved. I have a main Item class, with four subclasses (Weapon, Armor, Medical, Misc) .





      1. Code Redundancy



        The subclasses all have near identical class construction. The only difference between the Weapon and Armor class is the name of the classes. I would like to maybe have one class that manages all the different type of items, and can identify the differences between them so they can be used accordingly in my program.




      2. Code Efficiency



        Determining what items are determined are based on blocks of if statements. I would like to cut those down, so the code is more user friendly and more compact, and obviously more efficient.




      3. Item Determination



        Right now, when a new Item object is created, the item is randomly chosen WITHIN the Item class. I would hope for this to be managed by an outside class, which can reduce the complexity of the class.




      Item.java



      import java.util.Random;

      /*
      * Item Class
      */
      public class Item {

      /*
      * Private Instance Variables
      */
      private Random r;
      private final String name;
      protected int value; /* allows subclass to use */
      private int level; /* Used to determine what level of item to set */

      /*
      * Arrays used for item level determination (ranges, not each specific level)
      */
      Object normal_drops = {new Medical("Medicinal Herb", this.level), new Misc("Antidontial Herb"), new Misc("Moonwort Bulb"), new Misc("Holy Water")};
      int normal_drop_values = {3, 2, 5, 4};
      Object rare_drops = {new Misc("Slime Drop"), new Armor("Boxer Shorts", this.level), new Armor("Leather Hat", this.level), new Weapon("Oak Staff", this.level)};
      int rare_drop_values = {10, 15, 12, 8};

      /*
      * Item Constructor (no defualt)
      */
      public Item(int level) {
      this.level = level;
      this.r = new Random();
      this.determineItem();
      }

      /*
      * determineItem method for deciding if it will drop an item at all, and
      * if so, decides if it's rare or normal
      */
      private void determineItem() {
      int x = this.r.nextInt(100 - 1) + 1;
      if(x > 95) { /* Rare Drop Chance (5%) */
      this.rareDrop();
      } else if(x > 90) { /* Normal Drop Chance (10%) */
      this.normalDrop();
      }
      }

      /*
      * normalDrop method for normal drop items
      */
      private void normalDrop() {
      int index = this.r.nextInt(4 - 0) + 0; /* Isn't inclusive, so 4 is needed */
      if(this.level >= 1 && this.level <= 10) { /* Level Range 1-10 */
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }
      /*
      * Now, reset the array each time for new items
      */
      if(this.level >= 11 && this.level <= 20) { /* Level Range 11-20 */
      normal_drops = {new Misc("Cougulant"), new Armor("Pot Lid", this.level), new Misc("Evencloth"), new Armor("Leather Kilt", this.level)};
      updateValues(false, 2);
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }
      if(this.level >= 21 && this.level <= 30) { /* Level Range 21-30 */
      normal_drops = {new Armor("Scale Shield", this.level), new Medical("Strong Medicine", this.level), new Misc("Wing of Bat"), new Misc("Cowpat")};
      updateValues(false, 3);
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }
      if(this.level >= 31 && this.level <= 40) { /* Level Range 31-40 */
      normal_drops = {new Medical("Superior Medicine"), new Misc("Seashell"), new Misc("Lambswool"), new Misc("Kitty Litter")};
      updateValues(false, 4);
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }
      if(this.level >= 41 && this.level <= 50) { /* Level Range 41-50 */
      normal_drops = {new Misc("Magic Beast Horn"), new Misc("Rockbomb Shell"), new Misc("Lambswool"), new Misc("Manky Mud")};
      updateValues(false, 5);
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }

      }

      /*
      * rareDrop method for rare drop items
      */
      private void rareDrop() {
      int index = this.r.nextInt(4 - 0) + 0; /* Not inclusive, so 4 is needed */
      if(this.level >= 1 && this.level <= 10) { /* Level Range 1-10 */
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      /*
      * Reset array each time for new items
      */
      if(this.level >= 11 && this.level <= 20) { /* Level Range 11-20 */
      rare_drops = {new Misc("Iron Nails"), new Armor("Garish Garb", this.level), new Misc("Angel Bell"), new Misc("Fisticup")};
      updateValues(true, 2);
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      if(this.level >= 21 && this.level <= 30) { /* Level Range 21-30 */
      rare_drops = {new Armor("Gold Ring", this.level), new Armor("Agility Ring", this.level), new Armor("Strength Ring", this.level), new Armor("Leather Cape", this.level)};
      updateValues(true, 3);
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      if(this.level >= 31 && this.level <= 40) { /* Level Range 31-40 */
      rare_drops = {new Misc("Flintstone"), new Weapon("Iron Claws", this.level), new Misc("Softwort"), new Weapon("Long Spear", this.level)};
      updateValues(true, 4);
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      if(this.level >= 41 && this.level <= 50) { /* Level Range 41-50 */
      rare_drops = {new Armor("Fur Poncho", this.level), new Armor("Ice Shield", this.level), new Weapon("Assassins Dagger", this.level), new Weapon("Crow's Claws", this.level)};
      updateValues(true, 5);
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      }

      /*
      * Method updates the value of each item in the array
      */
      private void updateValues(boolean isRare, int amount) {
      /*
      * Resets values so it doesn't keep stacking with each level
      */
      normal_drop_values = {3, 2, 5, 4};
      rare_drop_values = {10, 15, 12, 8};

      if(!isRare) {
      for(int i = 0; i <= this.normal_drop_values.length - 1; i++) {
      this.normal_drop_values[i] *= amount;
      }
      } else {
      for(int i = 0; i <= this.rare_drop_values.length - 1; i++) {
      this.rare_drop_values[i] *= amount;
      }
      }
      }

      /*
      * Getters for item value and name
      */
      public int getValue() {
      return this.value;
      }

      public String getName() {
      return this.name;
      }

      }


      Weapon.java



      public class Weapon extends Item {

      /*
      * Private Instance Variables
      */
      private final String name;
      private int level;
      private final int damage;

      /*
      * Weapon constructor (no default)
      */
      public Weapon(String name, int level) {
      this.name = name;
      this.level = level;
      this.setAttributes();
      }

      /*
      * setAttributes, determines weapon damage
      */
      private void setAttributes() {
      this.damage = this.level + 2;
      }

      }


      Armor.java



      public class Armor extends Item {

      /*
      * Private Instance Variables
      */
      private final String name;
      private int level;
      private final int defence;

      /*
      * Armor constructor (no default)
      */
      public Armor(String name, int level) {
      this.name = name;
      this.level = level;
      this.setAttributes();
      }

      /*
      * setAttributes, determines defence level
      */
      private void setAttributes() {
      this.defence = this.level + 2;
      }

      }


      Medical.java



      public class Medical extends Item {

      /*
      * Private Instance Variables
      */
      private final String name;
      private int level;
      private final int heal_value;

      /*
      * Medical constructor (no default)
      */
      public Medical(String name, int level) {
      this.name = name;
      this.level = level;
      this.setAttributes();
      }

      /*
      * setAttributes, determines heal value
      */
      private void setAttributes() {
      this.heal_value = this.level * 2;
      }

      /*
      * Getters for heal value
      */
      public int getHealValue() {
      return this.heal_value;
      }

      }


      Misc.java



      public class Misc extends Item {

      /*
      * Private Instance Variables
      */
      private final String name;

      /*
      * Misc constructor (no default)
      */
      public Misc(String name) {
      this.name = name;
      }

      }









      share|improve this question











      $endgroup$




      Thanks for the enormous response on my last question! I've returned with another question, hoping for more insight.



      I'm developing an Item class and system, and I know it can be improved. I have a main Item class, with four subclasses (Weapon, Armor, Medical, Misc) .





      1. Code Redundancy



        The subclasses all have near identical class construction. The only difference between the Weapon and Armor class is the name of the classes. I would like to maybe have one class that manages all the different type of items, and can identify the differences between them so they can be used accordingly in my program.




      2. Code Efficiency



        Determining what items are determined are based on blocks of if statements. I would like to cut those down, so the code is more user friendly and more compact, and obviously more efficient.




      3. Item Determination



        Right now, when a new Item object is created, the item is randomly chosen WITHIN the Item class. I would hope for this to be managed by an outside class, which can reduce the complexity of the class.




      Item.java



      import java.util.Random;

      /*
      * Item Class
      */
      public class Item {

      /*
      * Private Instance Variables
      */
      private Random r;
      private final String name;
      protected int value; /* allows subclass to use */
      private int level; /* Used to determine what level of item to set */

      /*
      * Arrays used for item level determination (ranges, not each specific level)
      */
      Object normal_drops = {new Medical("Medicinal Herb", this.level), new Misc("Antidontial Herb"), new Misc("Moonwort Bulb"), new Misc("Holy Water")};
      int normal_drop_values = {3, 2, 5, 4};
      Object rare_drops = {new Misc("Slime Drop"), new Armor("Boxer Shorts", this.level), new Armor("Leather Hat", this.level), new Weapon("Oak Staff", this.level)};
      int rare_drop_values = {10, 15, 12, 8};

      /*
      * Item Constructor (no defualt)
      */
      public Item(int level) {
      this.level = level;
      this.r = new Random();
      this.determineItem();
      }

      /*
      * determineItem method for deciding if it will drop an item at all, and
      * if so, decides if it's rare or normal
      */
      private void determineItem() {
      int x = this.r.nextInt(100 - 1) + 1;
      if(x > 95) { /* Rare Drop Chance (5%) */
      this.rareDrop();
      } else if(x > 90) { /* Normal Drop Chance (10%) */
      this.normalDrop();
      }
      }

      /*
      * normalDrop method for normal drop items
      */
      private void normalDrop() {
      int index = this.r.nextInt(4 - 0) + 0; /* Isn't inclusive, so 4 is needed */
      if(this.level >= 1 && this.level <= 10) { /* Level Range 1-10 */
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }
      /*
      * Now, reset the array each time for new items
      */
      if(this.level >= 11 && this.level <= 20) { /* Level Range 11-20 */
      normal_drops = {new Misc("Cougulant"), new Armor("Pot Lid", this.level), new Misc("Evencloth"), new Armor("Leather Kilt", this.level)};
      updateValues(false, 2);
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }
      if(this.level >= 21 && this.level <= 30) { /* Level Range 21-30 */
      normal_drops = {new Armor("Scale Shield", this.level), new Medical("Strong Medicine", this.level), new Misc("Wing of Bat"), new Misc("Cowpat")};
      updateValues(false, 3);
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }
      if(this.level >= 31 && this.level <= 40) { /* Level Range 31-40 */
      normal_drops = {new Medical("Superior Medicine"), new Misc("Seashell"), new Misc("Lambswool"), new Misc("Kitty Litter")};
      updateValues(false, 4);
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }
      if(this.level >= 41 && this.level <= 50) { /* Level Range 41-50 */
      normal_drops = {new Misc("Magic Beast Horn"), new Misc("Rockbomb Shell"), new Misc("Lambswool"), new Misc("Manky Mud")};
      updateValues(false, 5);
      this.name = normal_drops[index];
      this.value = normal_drop_values[index];
      }

      }

      /*
      * rareDrop method for rare drop items
      */
      private void rareDrop() {
      int index = this.r.nextInt(4 - 0) + 0; /* Not inclusive, so 4 is needed */
      if(this.level >= 1 && this.level <= 10) { /* Level Range 1-10 */
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      /*
      * Reset array each time for new items
      */
      if(this.level >= 11 && this.level <= 20) { /* Level Range 11-20 */
      rare_drops = {new Misc("Iron Nails"), new Armor("Garish Garb", this.level), new Misc("Angel Bell"), new Misc("Fisticup")};
      updateValues(true, 2);
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      if(this.level >= 21 && this.level <= 30) { /* Level Range 21-30 */
      rare_drops = {new Armor("Gold Ring", this.level), new Armor("Agility Ring", this.level), new Armor("Strength Ring", this.level), new Armor("Leather Cape", this.level)};
      updateValues(true, 3);
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      if(this.level >= 31 && this.level <= 40) { /* Level Range 31-40 */
      rare_drops = {new Misc("Flintstone"), new Weapon("Iron Claws", this.level), new Misc("Softwort"), new Weapon("Long Spear", this.level)};
      updateValues(true, 4);
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      if(this.level >= 41 && this.level <= 50) { /* Level Range 41-50 */
      rare_drops = {new Armor("Fur Poncho", this.level), new Armor("Ice Shield", this.level), new Weapon("Assassins Dagger", this.level), new Weapon("Crow's Claws", this.level)};
      updateValues(true, 5);
      this.name = rare_drops[index];
      this.value = rare_drop_values[index];
      }
      }

      /*
      * Method updates the value of each item in the array
      */
      private void updateValues(boolean isRare, int amount) {
      /*
      * Resets values so it doesn't keep stacking with each level
      */
      normal_drop_values = {3, 2, 5, 4};
      rare_drop_values = {10, 15, 12, 8};

      if(!isRare) {
      for(int i = 0; i <= this.normal_drop_values.length - 1; i++) {
      this.normal_drop_values[i] *= amount;
      }
      } else {
      for(int i = 0; i <= this.rare_drop_values.length - 1; i++) {
      this.rare_drop_values[i] *= amount;
      }
      }
      }

      /*
      * Getters for item value and name
      */
      public int getValue() {
      return this.value;
      }

      public String getName() {
      return this.name;
      }

      }


      Weapon.java



      public class Weapon extends Item {

      /*
      * Private Instance Variables
      */
      private final String name;
      private int level;
      private final int damage;

      /*
      * Weapon constructor (no default)
      */
      public Weapon(String name, int level) {
      this.name = name;
      this.level = level;
      this.setAttributes();
      }

      /*
      * setAttributes, determines weapon damage
      */
      private void setAttributes() {
      this.damage = this.level + 2;
      }

      }


      Armor.java



      public class Armor extends Item {

      /*
      * Private Instance Variables
      */
      private final String name;
      private int level;
      private final int defence;

      /*
      * Armor constructor (no default)
      */
      public Armor(String name, int level) {
      this.name = name;
      this.level = level;
      this.setAttributes();
      }

      /*
      * setAttributes, determines defence level
      */
      private void setAttributes() {
      this.defence = this.level + 2;
      }

      }


      Medical.java



      public class Medical extends Item {

      /*
      * Private Instance Variables
      */
      private final String name;
      private int level;
      private final int heal_value;

      /*
      * Medical constructor (no default)
      */
      public Medical(String name, int level) {
      this.name = name;
      this.level = level;
      this.setAttributes();
      }

      /*
      * setAttributes, determines heal value
      */
      private void setAttributes() {
      this.heal_value = this.level * 2;
      }

      /*
      * Getters for heal value
      */
      public int getHealValue() {
      return this.heal_value;
      }

      }


      Misc.java



      public class Misc extends Item {

      /*
      * Private Instance Variables
      */
      private final String name;

      /*
      * Misc constructor (no default)
      */
      public Misc(String name) {
      this.name = name;
      }

      }






      java object-oriented role-playing-game






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago







      David White

















      asked 1 hour ago









      David WhiteDavid White

      245212




      245212






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212888%2fitem-class-system-for-rpg-dnd-type-game%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212888%2fitem-class-system-for-rpg-dnd-type-game%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?