// A Tank holds fuel of a particular type. class Tank { // Set how much this tank can hold. This is fixed for life. public Tank(long max,FuelType type){ capacity = max; fuelType = type; // The tank is full to start with. refill(); } public long getCapacity(){ return capacity; } public FuelType getFuelType(){ return fuelType; } // Can this tank deliver some of the amount required? public boolean canDeliver(){ return getLevel() > 0; } // Deliver the required amount, if possible. // Return the actual amount delivered. public long deliver(long amount){ long delivered = 0L; if(amount > 0L){ long currentLevel = getLevel(); if(amount <= currentLevel){ setLevel(currentLevel-amount); delivered = amount; } else{ // Deliver what we have. setLevel(0L); delivered = currentLevel; } } return delivered; } // Refill the tank to its full capacity. public void refill(){ setLevel(getCapacity()); } public long getLevel(){ return level; } protected void setLevel(long newLevel){ level = newLevel; } // How much the tank can hold. private final long capacity; // What sort of fuel it holds. private final FuelType fuelType; // How much is in the tank. private long level; }