Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that mimics the operations of several vending machines. More spe

ID: 3804189 • Letter: W

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 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.

Explanation / Answer

please see below Code

VendingMachine class

package vendingMachne;

import java.io.*;
import java.util.Scanner;


public class VendingMachine
{
  
//data members
private Item[] stock; //Array of Item objects in machine
private double money; //Amount of revenue earned by machine
String outputMess;
/*********************************************************************
* 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
  
public void vend(int i, double money)
{
   if(stock[i].getPrice()>money)
   {
       outputMess="Enter more "+(stock[i].getPrice()-money)+" Amount Or Cancel by pressing X";
   }
   else if(stock[i].getPrice()<money)
   {
       outputMess="Collect Change"+(money-stock[i].getPrice())+" Amount";
   }
   else
   {
       outputMess="Successfull";
   }
}
  
public void outputMessage()
{
   System.out.println(outputMess);
}
  
public void printMenu()
{
   for(int i=0;i<stock.length;i++)
   {
       System.out.println(i+". "+stock[i].getDescription());
   }
  
}
} //End VendingMachine class definition

Item class

package vendingMachne;

public class Item
{
   private String description;
   private double price;
   private int quantity;
  
   public String getDescription() {
       return description;
   }

   public void setDescription(String description) {
       this.description = description;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }

   public int getQuantity() {
       return quantity;
   }

   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }

   public Item(String itemDesc, double itemPrice, int itemQuantity)
   {
       this.description=itemDesc;
       this.price=itemPrice;
       this.quantity=itemQuantity;
   }
}

VendingMachineDriver

package vendingMachne;

import java.io.FileNotFoundException;
import java.util.Scanner;

public class VendingMachineDriver
{

   public static void main(String args[])
   {
       VendingMachine vendingMachine=null;
       try
       {
      
       for(;;)
       {
           Scanner sc=new Scanner(System.in);
          
           vendingMachine=selectVendingMachine();
           System.out.println("Enter the Item from Below Menu");
           vendingMachine.printMenu();
           int i=sc.nextInt();
           System.out.println("Enter Amount");
           int amout=sc.nextInt();
           vendingMachine.vend(i, amout);
       vendingMachine.outputMessage();
           String line=sc.nextLine();
           System.out.println(line);
           if(line.equals('X'))
           {
               break;
           }
       }
       }
       catch(Exception e)
       {
           System.out.println(e);
       }
       System.out.println("Existed...");
   }

   private static VendingMachine selectVendingMachine() throws FileNotFoundException
   {
       VendingMachine vendingMachine;
      
       System.out.println("Select from Vending Machine");
       System.out.println("1. Drinks");
       System.out.println("2. Snacks");
       Scanner sc = new Scanner(System.in);
       int machine=sc.nextInt();
       if(machine==1)
       {
           vendingMachine=new VendingMachine("drinks.txt");
       }
       else
       {
           vendingMachine=new VendingMachine("snacks.txt");
       }
      
       return vendingMachine;
   }
}

dont forget to give feedback.. comment if any more queries

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote