/** * VendingMachine simulates a campus vending machine. * * @author Chuck Bates * @version 1.1 final */ public class VendingMachine { private int drinks = 10; // sets machine to have 10 drinks at creation private int price = 125; // sets default price to 125 private int credit = 0; // sets credit to 0 as no money has yet been inserted int due = 0; /** * Constructor for objects of * class VendingMachine */ public VendingMachine() { } public VendingMachine(int cost) { if(cost > 0){ // if the number set is positive then the price is the users input price = cost; }else{ // if user sets a negative amount the cost is set at 125 price = 125; } } /** * displayGreeting displays a brief welcome and warns customer * if the vending machine is empty */ public void displayGreeting() { System.out.println(" Thank you for using this machine "); System.out.println(" The price is " + price ); if(drinks == 0){ System.out.println(" I am sorry, the machine is currently empty "); credit = 0; }else{ return; } //end of if statement } //end of displayGreeting /** * getPrice returns the price of one drink */ public int getPrice() { return price; } //end getPrice method /** * get credit returns how much money if any the user has inserted * * public int getCredit() * { * return credit; * } */ /** * insertMoney adds the number of cents to the credit balance. * and tells the customer to make a selection when they have * inserted enough money */ public void insertMoney(int amount){ if(drinks < 1){// if machine is empty tells user System.out.println(" Sorry the machine is currently empty "); amount = 0; } if(amount < 0 ){// if user puts in a negative amount, tells customer to enter money System.out.println("you put in " + amount + " You need to put money in!"); amount = 0; } // if user puts has a credit higher than the price the user // is asked to make a selection if user puts in a positive // amount under the price this is displayed credit = amount + credit; if(credit >= price){ System.out.println(" please make selection "); }else{ // System.out.println(" you have a " + credit + " credit, the price is " + price ); } } // ends insert money /** * makeSelection dispenses a drink */ public void makeSelection() { if(credit >= price){ System.out.println(" enjoy your ice cold drink! "); credit = 0; drinks = drinks -1; }else{ System.out.println("credit " + credit + " price " + price); due = price - credit; System.out.println(" you need to insert " + due); } } //end of makeSelection method /** * this method restocks the machine */ public void reStock() { drinks = 10; } //ends restock method /** * getStock tells the user how many drinks are left in the machine */ public int getStock() { return drinks; } /** * output per project document * * Thank you for using this machine * The price is 125 * you have a 100 credit, the price is 125 * please make selection * cangrats dude, you can now do the dew! * you put in -25 You need to put money in! * you have a 0 credit, the price is 125 */ } // end of class