Download code
From LiteratePrograms
Back to Vending_Machine_(Java)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
VendingMachine.java
1 /* Copyright (c) 2009 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Vending_Machine_(Java)?action=history&offset=20070517164020 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Vending_Machine_(Java)?oldid=10232 25 */ 26 27 public class VendingMachine 28 { 29 private int itemPrice; 30 31 private int currentBalance; 32 33 private int totalCollected; 34 35 public VendingMachine(int itemCost) 36 { 37 itemPrice = itemCost; 38 39 } 40 41 public int getItemPrice() 42 43 { 44 return itemPrice; 45 46 } 47 48 49 public int getCurrentBalance() 50 { 51 return currentBalance; 52 } 53 54 public void insertCoin(int faceValue) 55 { 56 if(faceValue > 0) 57 { 58 currentBalance = currentBalance + faceValue; 59 60 } 61 else 62 { 63 System.out.println("Invalid coin with face value " + faceValue + 64 " inserted. Use a positive value!"); 65 } 66 67 } 68 69 public void dispenseItem() 70 { 71 if (currentBalance >= itemPrice) 72 { 73 System.out.println("#######################################"); 74 System.out.println("# Thank you for your custom."); 75 System.out.println("# Your item has been released. "); 76 System.out.println("# Your item cost " + itemPrice + " pence."); 77 System.out.println("#######################################"); 78 System.out.println(); 79 80 totalCollected = totalCollected + itemPrice; 81 82 currentBalance = currentBalance - itemPrice; 83 84 } 85 else 86 { 87 System.out.println("Please insert more coins to at least a value of " + 88 (itemPrice - currentBalance) + "."); 89 } 90 91 } 92 93 public int refundCurrentBalance() 94 { 95 int refundableAmount; 96 refundableAmount = currentBalance; 97 currentBalance = 0; 98 99 return refundableAmount; 100 } 101 102 103 /* Test the class */ 104 public static void main(String[] args) { 105 VendingMachine sodaVendor = new VendingMachine(75); 106 VendingMachine candyBarVendor = new VendingMachine(60); 107 108 System.out.println("A can of soda costs " + sodaVendor.getItemPrice() + " pence."); 109 System.out.println("A candy bar costs " + candyBarVendor.getItemPrice() + " pence."); 110 111 sodaVendor.insertCoin(50); 112 System.out.println("The current balance in the soda vendor is: " + sodaVendor.getCurrentBalance() + 113 " pence."); 114 sodaVendor.insertCoin(20); 115 System.out.println("The current balance in the soda vendor is now: " + sodaVendor.getCurrentBalance() + 116 " pence."); 117 118 sodaVendor.insertCoin(5); // now have 75p 119 sodaVendor.dispenseItem(); 120 121 System.out.println("The current balance is the soda vendor is now: " + sodaVendor.getCurrentBalance() + " pence."); 122 123 candyBarVendor.insertCoin(50); 124 candyBarVendor.dispenseItem(); // should fail - 10 short! 125 candyBarVendor.insertCoin(50); 126 candyBarVendor.dispenseItem(); 127 128 // balance should now be 40! 129 System.out.println("Candy bar vendor balance is now: " + 130 candyBarVendor.getCurrentBalance() + " pence."); 131 132 int refund = candyBarVendor.refundCurrentBalance(); 133 System.out.println("Is refund equal to 40? [true/false]: " + (refund == 40) + "."); 134 System.out.println("Current balance in the candy bar vending machine is now: " + candyBarVendor.getCurrentBalance() + "."); 135 136 sodaVendor.insertCoin(1); // OK 137 sodaVendor.insertCoin(0); // Invalid 138 sodaVendor.insertCoin(-1); // Invalid 139 140 System.out.println("Current balance in soda vendor is: " + sodaVendor.getCurrentBalance() + 141 " (should be 1)."); 142 143 } 144 145 146 } 147
README.TXT
1 This is a [http://en.wikipedia.org/wiki/Java_programming_language Java] program which simulates the behaviour 2 of a vending machine that issues drinks and food to customers. 3 4 * The price of all of the items that are in a given machine is the same. 5 * Users pass values to the program which represents users inserting coins to purchase an item. 6 * The vending machine keeps note of the amount of money it has received since it was initiated and the 7 balance of a customer's coins. 8 * Customers can have a refund of their money. 9 10 This is a good program for learning the basics of Java, as it introduces the ideas of ''fields'', ''local variables'', 11 ''constructors'', ''accessor'' and ''mutator methods'' and ''conditional statements''. 12
results.txt
1 A can of soda costs 75 pence. 2 A candy bar costs 60 pence. 3 4 The current balance in the soda vendor is: 50 pence. 5 The current balance in the soda vendor is now: 70 pence. 6 7 ####################################### 8 # Thank you for your custom. 9 # Your item has been released. 10 # Your item cost 75 pence. 11 ####################################### 12 13 14 The current balance is the soda vendor is now: 0 pence. 15 16 Please insert more coins to at least a value of 10. 17 ####################################### 18 # Thank you for your custom. 19 # Your item has been released. 20 # Your item cost 60 pence. 21 ####################################### 22 23 24 Candy bar vendor balance is now: 40 pence. 25 26 Is refund equal to 40? [true/false]: true. 27 Current balance in the candy bar vending machine is now: 0. 28 29 Invalid coin with face value 0 inserted. Use a positive value! 30 Invalid coin with face value -1 inserted. Use a positive value! 31 32 Current balance in soda vendor is: 1 (should be 1). 33 34
bluej.pkg
1 #BlueJ package file 2
