// VendingMachine source code for Project 3 // Allow users to insert coins and purchase // candy bars. public class VendingMachine { // Price of candy bar. final private int CANDY_BAR_PRICE = 75; // Amount deposited for purchase in cents. private int amtForPurchase; // Number of candy bars currently in vending machine. private int numCandyBars; // Message displayed after action taken. private String message; // Constructor for VendingMachine class. public VendingMachine( ) { this.amtForPurchase = 0; this.numCandyBars = 0; this.message = "Vending machine initialized."; } // Getter for amtForPurchase instance variable. public int getAmtForPurchase( ) { return this.amtForPurchase; } // Getter for numCandyBars instance variable. public int getNumCandyBars( ) { return this.numCandyBars; } // Getter for message instance variable. public String getMessage( ) { return this.message; } // Add 5 cents to amount deposited for purchase. public void depositNickel( ) { this.amtForPurchase += 5; this.message = "Nickel deposited."; } // Add 10 cents to amount deposited for purchase. public void depositDime( ) { this.amtForPurchase += 10; this.message = "Dime deposited."; } // Add 25 cents to amount deposited for purchase. public void depositQuarter( ) { this.amtForPurchase += 25; this.message = "Quarter deposited."; } // Purchase candy bar if amount for purchase is enough. public void purchaseCandyBar( ) { if (this.amtForPurchase >= CANDY_BAR_PRICE && this.numCandyBars > 0) { this.amtForPurchase -= CANDY_BAR_PRICE; this.numCandyBars--; this.message = "Candy bar dispensed"; } else if (this.amtForPurchase < CANDY_BAR_PRICE) { this.message = "Insufficient amount to purchase candy bar"; } else { this.message = "No candy bars in vending machine"; } } // Load more candy bars into vending machine. public void loadCandyBars(int candyBarsLoaded) { this.numCandyBars += candyBarsLoaded; } @Override public String toString( ) { return String.format("%d %d %s", this.numCandyBars, this.amtForPurchase, this.message); } }