package UPSTruckSimple; /* * UPSTruck.java * Class supports working with a delivery truck * Some simplifying Assumptions: * 1) All boxes are the same size ==> 1 * 2) All locations exist * 3) A truck always arrives at its destination - no running out of gas ... * Created on April 15, 2003, 10:57 PM * Author: Dr Michael Redmond */ /** * * @author mike */ public class UPSTruckSimple { //////////////////////////////////////////////////////////////////////// // Instance Variables //////////////////////////////////////////////////////////////////////// /** * unique (hopefully) identifier for the truck */ private String truckID = "1"; /** * how many boxes the truck can hold at one time */ private int capacity = 100; /** * how many boxes the truck curreently holds */ private int contains = 0; /** * city that the truck is currently in */ private String currLoc = "Philadelphia"; /** * total number of boxes delivered by the truck since the start of the game/ simulation */ private int boxesDelivered = 0; //////////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////////// /** * Creates a new instance of UPSTruck * Uses all default info */ public UPSTruckSimple() { } /** * Creates a new instance of UPSTruck * Given truck id and location * rest takes defaults */ public UPSTruckSimple(String id, String startLoc) { truckID = id; currLoc = startLoc; } /** * Creates a new instance of UPSTruck * Given truck id and location and capacity of truck * rest takes defaults */ public UPSTruckSimple(String id, String startLoc, int holds) { truckID = id; currLoc = startLoc; capacity = holds; } //////////////////////////////////////////////////////////////////////// // Inspectors //////////////////////////////////////////////////////////////////////// /** * report the unique (we hope) identifier of the truck */ public String getTruckID() { return truckID; } /** * report the capacity of the truck - in number of boxes */ public int getCapacity() { return capacity; } /** * report the contents of the truck - in number of boxes */ public int getContains() { return contains; } /** * Report the current location of the truck */ public String getCurrLoc() { return currLoc; } /** * Report the number of boxes delivered since the start of the game / simulation */ public int getBoxesDelivered() { return boxesDelivered; } //////////////////////////////////////////////////////////////////////// // Pseudo Inspectors (find out about object - but not direct lookup of instance variable) //////////////////////////////////////////////////////////////////////// /** * Report whether the truck is full * should never get over capacity, but this method will report full if somehow it does */ public boolean isFull() { boolean full = false; if (contains >= capacity ) { full = true; } return full; } //////////////////////////////////////////////////////////////////////// // Mutators //////////////////////////////////////////////////////////////////////// /** * Change the truck id to a new value * no validation done - all ids accepted */ public void setTruckID(String id) { truckID = id; } /** * Change the truck capacity - should only be done at the beginning * made private to control things * if negative capacity requested returns false */ private boolean setCapacity(int maxItems) { if (maxItems <= 0) { return false; } else { capacity = maxItems; return true; } } /** * Change the truck contents - should only be done at the beginning * made private to control things * if negative contents specified returns false * if overcapacity, returns false */ private boolean setContains(int number) { if (number < 0) { return false; } else if (number > capacity) { return false; } else { contains = number; return true; } } /** * Change the truck location to a new place * no validation done - all locations accepted */ public void setCurrLoc(String newLoc) { currLoc = newLoc; } /** * Change the truck's number of boxes delivered - should only be done at the beginning * made private to control things - later changes should be done through dropoff * if negative contents specified returns false */ private boolean setBoxesDelivered(int boxes) { if (boxes < 0) { return false; } else { boxesDelivered = boxes; return true; } } //////////////////////////////////////////////////////////////////////// // Methods with significant functionality //////////////////////////////////////////////////////////////////////// /** * pickup a number of boxes in the current location * returns false if hit capacity */ public boolean pickup(int numItems) { // try to update contents and see if it works if (!setContains(numItems + contains)) { // would be overcapacity // Policy - reject request - otherwise caller wouldn't know how many // were picked up return false; } else { // went ok return true; } } /** * pickup a number of boxes in the given location * returns false if hit capacity or if can't make it to the location */ public boolean pickup(int numItems, String where) { boolean madeIt = driveTo(where); if (!madeIt) { // ran out of gas or something return false; } else { // call other pickup to get it done since now in correct place boolean pickedOk = pickup(numItems); return pickedOk; } } /** * drop off a number of boxes in the current location * returns false if hit empty before dropping all off */ public boolean dropoff(int numItems) { // try to update contains and see if it works // could fail if numItems is negative or would go below empty if (!setContains(contains - numItems)) { // would be under zero // Policy - reject request - otherwise caller wouldn't know how many // were dropped off return false; } else { // went ok // update number delivered - set will work since ensure adding setBoxesDelivered(getBoxesDelivered() + numItems); return true; } } /** * drop off a number of boxes in the given location * returns false if try to drop off more than we have or if can't make it to the location */ public boolean dropoff(int numItems, String where) { boolean madeIt = driveTo(where); if (!madeIt) { // ran out of gas or something return false; } else { // call other dropoff to get it done since now in correct place boolean droppedOk = dropoff(numItems); return droppedOk; } } /** * go to given location */ public boolean driveTo(String where) { boolean ok = true; // hackup up in shortened version - always ok if (ok) { // made it there setCurrLoc(where); } return ok; } //////////////////////////////////////////////////////////////////////// // Methods overriding Object's versions //////////////////////////////////////////////////////////////////////// /** * produce a display friendly reprentation of a truck */ public String toString() { String res = "Truck: " + getTruckID(); res += " Loc: " + getCurrLoc(); res += " Contains: " + getContains() + " of " + getCapacity() + " possible"; res += " Boxes Delivered: " + getBoxesDelivered(); return res; } //////////////////////////////////////////////////////////////////////// // Test Driver //////////////////////////////////////////////////////////////////////// /** * @param args the command line arguments * A simple test driver */ public static void main(String[] args) { // default truck UPSTruckSimple truck1 = new UPSTruckSimple(); System.out.println("Truck1: " + truck1); truck1.pickup(5); System.out.println("Truck1: " + truck1); truck1.pickup(5,"Boston"); System.out.println("Truck1: " + truck1); if (!truck1.pickup(5,"Boston")){ System.out.println("pickup failed"); } System.out.println("Truck1: " + truck1); if (!truck1.dropoff(17,"Chicago")){ System.out.println("dropoff failed"); } System.out.println("Truck1: " + truck1); System.exit(0); } }