Back to home

// Note: comments about which requirements are met will be on top of the line of code that meets the requirement

public class InventoryItem {
// #3 - primitive double
private double attack; // init private double attack
// #3 - primitive boolean
private boolean isGolden; // init private boolean isGolden
// #12 - array in student-designed class
public String[] itemAliases; // init public String[] itemAliases
public InventoryItem() { // zero arg
attack = 0.0;
isGolden = true;
itemAliases = new String[] {"Filler Item", "Temporary Item"};
}
public InventoryItem(double attack, boolean isGolden, String[] itemAliases) { // multi arg
this.attack = attack;
this.isGolden = isGolden;
this.itemAliases = itemAliases;
}
public double getAttack() { // return attack of InventoryItem
return this.attack;
}
public String toString() { // toString()
String toString = "Item attack: " + attack + ", Golden: " + isGolden + ", Item Aliases: ";
// #12 - traverse and access array through for loop
for (int index = 0; index < itemAliases.length; index++) { // traverse array itemAliases and print out all Strings
// #1 - relational operator #1
if (index + 1 == itemAliases.length) {
toString += itemAliases[index];
} else {
toString += itemAliases[index] + ", ";
}
index++;
// #1 - relational operator #2
}
return toString;
}
} // end class