Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

VendingMachine.java import java.io.*; import java.util.Scanner; /************************************************************************* * Simulates a real life vending machine with stock read from a file. * * CSCE 155A Fall 2017

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

VendingMachine.java

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

/************************************************************************* * Simulates a real life vending machine with stock read from a file. * * CSCE 155A Fall 2017 * @file VendingMachine.java * @author Jeremy Suing * @version 1.0 * @date November 3, 2017 *************************************************************************/ 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

//setter and getter method

//method to handle the vending transaction public void vend(Item[] userMachine, Scanner scnr){

}

//returns the proper message if vend has an issue public String outputMessage(Integer message){ } //Creates and prints the menu of the vending machine public void printMenu(Item[] machineChoice, Scanner scnr){ }

VendingMachineDriver.java

//package vending_Machine;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class VendingMachineDriver {

public static void main(String[] args){

boolean exit = false;

Scanner scnr = new Scanner(System.in);

VendingMachine drinks = null;// creates a VendingMachine object for the drink machine

VendingMachine snacks = null;// creates a VendingMachine object for the snacks machine

//reads in the correct file and sends it to VendingMachine constructor to create a stock[]

//of each file

try {

drinks = new VendingMachine("drinks.txt");

snacks = new VendingMachine("snacks.txt");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("Welcome to Matt's Super Vending Machines!");

System.out.println(" I sense that you are hungry or thirsty...");

// allows user to chose their machine or exit the program

while(!exit){

System.out.println(" Please select a vending machine:");

System.out.print("A-Drinks, B-Snacks, X-Exit: ");

String machineChoice = scnr.next();

if(machineChoice.equalsIgnoreCase("a")){

//pass scanner as parameter, since the Zybook can only indentify one scanner

drinks.printMenu(drinks.getStock(), scnr);

}

else if(machineChoice.equalsIgnoreCase("b")){

//pass scanner as parameter, since the Zybook can only indentify one scanner

snacks.printMenu(snacks.getStock(),scnr);

}else if(machineChoice.equalsIgnoreCase("x")){

exit = true;

break;

}

else{

System.out.println("Invalid selection try again.");

}

}

//gets the money earned from snack machine and drink machine and displays it for the total earnings

System.out.printf(" The vending machine has made $%.02f. Thank you for your business!", (snacks.getMoney()+drinks.getMoney()));

scnr.close();

}

}

Item.java

package vending_Machine;

//class to set and return item information public class Item { }

NOTE: please bold what it added for easier understanding. Thanks!

10.12 Ch 10 Program: Vending Machine I (Java) Programming Assignment: Vending Machine l Assigned:11/03/2017 Due: 11/13/2017 Objectives The objectives of this homework assignment: 1. Understand the concept of object-oriented programming 2. Understand the concepts of "basic" class and "application/tester class. a. How one class uses another class elegantly! 3. Understand the concepts of object instantiation. 4. Familiarize with methods a. How to use the results of one method in another method 5. Master the use of exception handling for data format and range checking 6. Master code documentation, compilation, and execution 7. Expose to Java syntax, programming styles, and a Java classes. Problem Description 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: Milk, 2.00,1 OJ,2.50,6 10.12 Ch 10 Program: Vending Machine I (Java) Programming Assignment: Vending Machine l Assigned:11/03/2017 Due: 11/13/2017 Objectives The objectives of this homework assignment: 1. Understand the concept of object-oriented programming 2. Understand the concepts of "basic" class and "application/tester class. a. How one class uses another class elegantly! 3. Understand the concepts of object instantiation. 4. Familiarize with methods a. How to use the results of one method in another method 5. Master the use of exception handling for data format and range checking 6. Master code documentation, compilation, and execution 7. Expose to Java syntax, programming styles, and a Java classes. Problem Description 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: Milk, 2.00,1 OJ,2.50,6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions