Question: Write a program that mimics the operations of several vending machines. More specifically, the program simulates what happens when the user chooses one of the
Write a program that mimics the operations of several vending machines. More specifically, the program simulates what happens when the user chooses one of the vending machines, inputs money and picks an item from the vending machine. Assume there are two vending machines: one for drinks and one for snacks. Each vending machine contains several items. The name, price, and quantity of each item is given in two text files; one named drinks.txt for the drinks vending machine and the other named snacks.txt for the snacks vending machine.
The format of the input values is comma-separated. The items listed should be organized in the file with the following order: name, price, quantity. Here are some example items:
The actual reading and parsing of the input file is already handled in the class supplied to you (see code on BlackBoard). You are given the variables as individual values. You will need to create the .txt files for creating and testing your vending machines.
You will need to create/complete three classes for this homework assignment: Item, VendingMachine, and VendingMachineDriver.
Problem Description
Milk,2.00,1
Within your VendingMachine class, include these methods:
VendingMachineThisconstructormethodwilltakeinthenameoftheinputfileand create a vending machine object. A vending machine object will contain an array of Item objects called stock and an amount of revenue earned. This constructor method has been completed for you and should work appropriately once you have completed the rest of this class and the other class definitions.
vendThismethodwillsimulatethevendingtransactionafteravalidamountofmoney and an item selection are entered. This method will decide if the transaction is successful (enough money or item) and update the vending machine appropriately.
outputMessage This method will print an appropriate message depending on the success of the transaction. If the user does not have enough money to buy the chosen item, the vending machine should prompt the user to enter more money or exit the machine. If the vending machine is out of the chosen item, the program will print an apology message and prompt the user to choose another item or exit the machine. If there is enough money for the item selected, then the vending machine will give the user the item and return the change.
printMenuThismethodprintsthemenuofitemsforthechosenvendingmachine. The Item class needs to include the following data variables:
description as a String
price as a double quantity as an int
Within your VendingMachineDriver class, include a main method as the starting point for your solution that creates the vending machine objects appropriately and then use a loop that continues interacting with the vending machines until the user enters X to exit. See the sample session for details.
As you implement your solution, you might find that some methods contain some repeated coding logic. To avoid unnecessary redundancies in your code, have these methods call an appropriate helper method.
import java.io.*; import java.util.Scanner; /************************************************************************* * Simulates a real life vending machine with stock read from a file. * * CSCE 155A Fall 2016 * Assignment 4 * @file VendingMachine.java * @author Jeremy Suing * @version 1.0 * @date March 7, 2016 *************************************************************************/ public class VendingMachine { //data members private Item[] stock; //Array of Item objects in machine private double money; //Amount of revenue earned by machine /********************************************************************* * This is the constructor of the VendingMachine class that take a * file name for the items to be loaded into the vending machine. * * It creates objects of the Item class from the information in the * file to populate into the stock of the vending machine. It does * this by looping the file to determine the number of items and then * reading the items and populating the array of stock. * * @param filename Name of the file containing the items to stock into * this instance of the vending machine. * @throws FileNotFoundException If issues reading the file. *********************************************************************/ public VendingMachine(String filename) throws FileNotFoundException{ //Open the file to read with the scanner File file = new File(filename); Scanner scan = new Scanner(file); //Determine the total number of items listed in the file int totalItem = 0; while (scan.hasNextLine()){ scan.nextLine(); totalItem++; } //End while another item in file //Create the array of stock with the appropriate number of items stock = new Item[totalItem]; scan.close(); //Open the file again with a new scanner to read the items scan = new Scanner(file); int itemQuantity = -1; double itemPrice = -1; String itemDesc = ""; int count = 0; String line = ""; //Read through the items in the file to get their information //Create the item objects and put them into the array of stock while(scan.hasNextLine()){ line = scan.nextLine(); String[] tokens = line.split(","); try { itemDesc = tokens[0]; itemPrice = Double.parseDouble(tokens[1]); itemQuantity = Integer.parseInt(tokens[2]); stock[count] = new Item(itemDesc, itemPrice, itemQuantity); count++; } catch (NumberFormatException nfe) { System.out.println("Bad item in file " + filename + " on row " + (count+1) + "."); } } //End while another item in file scan.close(); //Initialize the money data variable. money = 0.0; } //End VendingMachine constructor } //End VendingMachine class definition
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
