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
) .
Code Redundancy
The subclasses all have near identical class construction. The only difference between the
Weapon
andArmor
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.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.
Item Determination
Right now, when a new
Item
object is created, the item is randomly chosen WITHIN theItem
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;
}
}