// A Java implementation of VendingMachine. // Original in C++ by Liu Ling, // Taken from a paper by Mary Jean Harrold. // Translated to Java by Jeff Offutt, January 2004 // Updated for Java 1.5, 2006 (probably imperfectly), Jeff Offutt. // Main added later for testing. import java.util.*; import java.io.*; import java.lang.reflect.*; public class vendingMachine { private int credit; private LinkedList stock; // Maximum size of vendingMachine private static final int MAX = 10; // In the C++ version, Ling used a C++ list // with operations like push(), pop(), etc to look // a lot like a queue. // list stock; ?? // In the Java version I'm emulating a queue on top of the // LinkedList. Ugly, but I don't want to redesign. //************************************************ // Constructor // vendingmachine starts empty. //************************************************ public vendingMachine() { credit = 0; stock = new LinkedList(); // Empty stock. } //************************************************ // A coin is given to the vendingMachine. // Must be a dime, quarter or dollar. // Ignores invalid input //************************************************ public void coin (int coin) { if (coin != 10 && coin != 25 && coin != 100) return; if (credit >= 90) return; credit = credit + coin; return; } //************************************************ // User asks for a chocolate. // Returns the change and the sets the // parameter StringBuffer variable Choc. // If not enough money or no chocolates, // returns 0 and a blank string. //************************************************ // Necessary because strings are immutable // C++ version returned both choc and change as parameters public int getChoc (StringBuffer choc) { int change; if (credit < 90 || stock.size() <= 0) { change = 0; choc.replace (0, choc.length(), ""); return (change); } change = credit - 90; credit = 0; choc.replace (0, choc.length(), (String) stock.removeFirst()); return (change); } //************************************************ // Adds one new piece of chocolate to the machine // If machine is full, nothing happens //************************************************ public void addChoc (String choc) { if (stock.size() >= MAX) return; stock.add (choc); return; } //Needed for testing -- increases observability public int getCredit () { return (credit); } //Needed for testing -- increases observability public LinkedList getStock () { return (stock); } /* //************************************************ // mainV1() for initial testing. //************************************************ public static void main (String[] argv) throws Exception { /* StringBuffer choc = new StringBuffer ("xx"); VendingMachine v = new VendingMachine (); v.addChoc ("c1"); v.addChoc ("c2"); v.addChoc ("c3"); v.coin (10); v.coin (25); v.coin (100); int ch = v.getChoc (choc); System.out.println ("First get, c: " + choc + ", ch: " + ch); ch = v.getChoc (choc); v.coin (100); ch = v.getChoc (choc); System.out.println ("Second get, c: " + choc + ", ch: " + ch); */ /* VMTEST1 vmTest = new VMTEST1(); vmTest.setUp(); vmTest.test1(); /* Class vmClass = vmTest.getClass(); Method methods[] = vmClass.getDeclaredMethods(); for (int i = 0;i < methods.length; i++){ System.out.println("method name: " + methods[i].getName()); System.out.println("Return type: " + methods[i].getReturnType()); } }*/ } // End class vendingMachine