Back to project page ZorbsCity.
The source code is released under:
GNU General Public License
If you think the Android project ZorbsCity listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jonathan.geoffroy.zorbscity.model.helpers; /*from w ww . ja va 2 s.c om*/ import java.util.ArrayList; import jonathan.geoffroy.zorbscity.model.cityObjects.resources.Resource; /** * The ResourceList is a special HashMap that contains Resources * A ResourceList can deliver & count amount of Resources. * It also manager the foods system (mainly by differentiate foods: so you can ask if there it at least 1 kind of food for example). * @author Jonathan GEOFFROY * @version 0.1 */ public class ResourcesList extends ArrayList<Resource> { public final static int FOOD1_RESOURCE = 0, FOOD2_RESOURCE = 1, FOOD3_RESOURCE = 2, WATER_RESOURCE = 3,// TODO: add each Resource type POWER_RESOURCE = 4, CLOTHES_RESOURCE = 5, ENTERTAINMENT_RESOURCE = 6, WOOL_RESOURCE = 7; // public final static int FOOD_RESOURCE = 100; public final static int NB_RESOURCES = 8; public static final int MAX_FOOD_NUMBER = 3; /** * Generated serial default (using eclipse IDE) */ private static final long serialVersionUID = 4692124720616702433L; public ResourcesList() { super(); } public ResourcesList(int initialCapacity) { super(initialCapacity); } /** * * @return true if there are at least 1 amount of each Resource in resources list */ public boolean enoughResources(NeededList neededList) { int needed[] = neededList.getNeeded(); for(int i = 0; i < needed.length; i++) { if(!enoughResources(neededList, i)) { return false; } } return true; } /** * check if this Building have enough resources of r * @param resourceType the type of the resource to check * @return true if the resource type is not in the needed list, or return amountNeeded(r) */ public boolean enoughResources(NeededList nl, int resourceType) { int needed[] = nl.getNeeded(); int amountNeeded = needed[resourceType]; if(amountNeeded == 0) { return true; } Resource r = get(resourceType); if(r == null) { return false; } return r.getAmount() >= amountNeeded; } @Override public Resource get(int resourceType) { assert(resourceType >= 0 && resourceType< NB_RESOURCES); for(Resource r : this) { if(r.equals(resourceType)) { return r; } } return null; } /** * Decrement the number of needed amount for each needed resource * enoughResource() must be true */ public void useNeeded(NeededList nl) { assert(enoughResources(nl)); int needed[] = nl.getNeeded(); for(int i = 0; i < needed.length; i++) { if(needed[i] != 0) { get(i).rmAmount(needed[i]); assert(get(i).getAmount() >= 0); } } } /** * @param resourceType * @return the stocked amount of <resourceType> */ public int getAmountOf(int resourceType) { Resource r = get(resourceType); if(r != null) { return r.getAmount(); } return 0; } }