package cse1030; /** * A class that represents quantities (weight) of mined gold. The class * defines a total reserve of gold that is available to be mined. * Once the reserve is exhausted only gold objects having zero * weight can be created. * * @author CSE1030 * */ public class Gold { /** * The total weight of gold available to be mined. */ public static final int TOTAL_RESERVES = 1000; private static int amountMined = 0; private int weight; /** * Creates an amount of gold mined from the reserves. If * the amount requested exceeds the amount remaining in the * reserves than an amount is created equal to the amount * remaining in the reserves. * * @param weight The weight of gold to mine. * @pre. weight >= 0 */ public Gold(int weight) { if (Gold.amountMined + weight > TOTAL_RESERVES) { weight = TOTAL_RESERVES - Gold.amountMined; } Gold.amountMined += weight; this.weight = weight; } /** * The weight of the gold object. * * @return The weight of the gold object. */ public int getWeight() { return this.weight; } /** * The weight of gold remaining in the reserves. * * @return The weight of gold remaining in the reserves. */ public static int remaining() { return Gold.TOTAL_RESERVES - Gold.amountMined; } /** * Returns a string representation of the gold object. The returned * string is "Weight = " followed by the weight of the gold object. * * @return A string representation of the gold object. */ @Override public String toString() { return "Weight = " + this.weight; } public static void main(String[] args) { Gold g = new Gold(Gold.TOTAL_RESERVES); System.out.println(g); Gold h = new Gold(1); System.out.println(h); } }