Question: Java VendingMachine class Add a Vending class and insert the following skeletal code. Then, follow the bullet points provided below the skeletal code. import java.util.Scanner;
Java
VendingMachine class
Add a Vending class and insert the following skeletal code. Then, follow the bullet points provided below the skeletal code.
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
import java.text.DecimalFormat;
/**
* @author Your Name Here
* class VendingMachine simulates a simple Vending Machine
* where items may be purchased, and item information is updated
* and stored.
*/
public class VendingMachine
{
private ArrayList items;
public static void main(String[] args)
{
VendingMachine machine = new VendingMachine();
try
{
machine.start("items.txt");
machine.run();
machine.stop("items.txt");
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public VendingMachine()
{
// Assign a new empty array list to the items field
}
/**
* Method start loads the machine with vending machine data.
* @param filename The name of the file storing the vending
* machine data.
* @throws IOException
*/
public void start(String filename) throws IOException
{
// Check if the filename exists. If not, inform the user that the
// file does not exist and exit with an error code of 1.
// Open the file for reading
// While there are more lines in the file, read each vending machine
// item from file into the items list
while (/* Insert condition */)
{
// Read the next line from the file and split into an
// array of strings
// IF the line of data read represents a Soda, build a new
// Soda object with the name/price/quantity/caffeine information
// and then add it to items
// ELSE build a new Candy object with the name/price/quantity/nuts
// information and add it to items
}
// Close the file
}
/**
* Method lookup performs a case-insensitive linear search to determine
* if a vending machine item with the given name exists.
* @param itemName The name of the vending machine item to find.
* @return A reference to the item found or null if not found.
*/
public Item lookup(String itemName)
{
}
/**
* Method displayItems displays each item in the machine.
*/
public void displayItems()
{
}
/**
* Method run allows a user to purchase items from the machine.
*/
public void run()
{
// Process transactions until the user quits
do
{
displayItems();
//Prompt for and retrieve the item into a String variable named choice
Item item = lookup(choice);
// If item is found, proceed with the purchase
if (item != null)
{
// If there is at least one item, then process the purchase
if (/* Insert condition */)
{
// Prompt for and retrieve the money
// Loop until enough money is entered
// Tell the user to take the product and any remaining change.
// Reduce the quantity of the item in stock by 1
}
else
{
System.out.println("Item sold out.");
}
}
else
{
System.out.println("Invalid item.");
}
// Prompt for and retrieve choice whether the user wants to quit.
} while (/* choice does not equal an upper or lower case n */);
} // end run
/**
* Method stop writes the items to file.
* @param filename The name of the file to which the items are written.
* @throws IOException
*/
public void stop(String filename) throws IOException
{
} // end stop
} // end class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
