package vendingMachine; // 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. // The original text can be retrieved from // http://cs.gmu.edu/~offutt/softwaretest/ms-level-syllabus/assigns/vendingMachine.java // // Several modifications have been made to the reference file (Y. Ledru, 2012): // - declared a package vendingMachine; // - changed the name to VendingMachine (Using a leading uppercase). // - the constructor is now public. // - getCredit and getStock have been added to increase testability // - the main() method has been commented out. import java.util.*; import java.io.*; 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) { 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); } */ } // End class vendingMachine