// The encumbrance variable is eliminated - it was duplicate data since we can derive that from the items themselves. // This class no longer needs to understand the internal implementation of CoinInventory (i.e. which elements of the int array represented which coin). // The addItem(), collectMoney(), and getMoneyValue() methods are now wrappers for the CoinInventory class. That's ok in this circumstance. import java.util.*; public class Player { private String name; private CoinInventory coinInventory; private ArrayList otherInventory; public Player(String name) { this.name = name; coinInventory = new CoinInventory(); otherInventory = new ArrayList(); } public void addItem(Item item) { otherInventory.add(item); } public void collectMoney(int cp, int sp, int ep, int gp, int pp) { coinInventory.collectMoney(cp, sp, ep, gp, pp); } public double getRunningSpeed() { double speed = 5 * (1 - getEncumbrance() / 1000.0); //Speed 5 if unencumbered; speed 0 if 1000 units carried or more. if (speed < 0) { return 0; } else { return speed; } } public double getMoneyValue() { return coinInventory.getMoneyValue(); } public int getEncumbrance() { int encumbrance = 0; for(Item i : otherInventory) encumbrance += i.getWeight(); return coinInventory.getWeight() + encumbrance; } //Test method. public static void main(String[] args) { Player p = new Player("Whistlestick"); Item sword = new Item("Magic sword +2", 20); Item shield = new Item("Normal shield", 40); p.addItem(sword); p.addItem(shield); if (p.getEncumbrance() != 60) throw new RuntimeException("getEncumbrance() should be 60, not " + p.getEncumbrance()); p.collectMoney(10,2,0,40,2); if (p.getEncumbrance() != 60+54) throw new RuntimeException("getEncumbrance() should be 114, not " + p.getEncumbrance()); if (p.getMoneyValue() != 50.3) throw new RuntimeException("getMoneyValue() should be 50.3, not " + p.getMoneyValue()); p.collectMoney(0,0,0,6,0); if (p.getEncumbrance() != 120) throw new RuntimeException("getEncumbrance() should be 120, not " + p.getEncumbrance()); if (p.getRunningSpeed() != 4.4) throw new RuntimeException("getRunningSpeed() should be 4.4, not " + p.getRunningSpeed()); System.out.println("All tests passed."); } }