Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

10.12 Ch 10 Program: Vending Machine I (Java) Programming Assignment: Vending Machine I Assigned: 11/03/2017 Due: 11/13/2017 Objectives The objectives of this homework assignment: Understand

10.12 Ch 10 Program: Vending Machine I (Java) Programming Assignment: Vending Machine I Assigned: 11/03/2017 Due: 11/13/2017 Objectives The objectives of this homework assignment: Understand the concept of object-oriented programming. Understand the concepts of basic class and application/tester class. a. How one class uses another class elegantly! Understand the concepts of object instantiation. Familiarize with methods a. How to use the results of one method in another method Master the use of exception handling for data format and range checking Master code documentation, compilation, and execution 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 The actual reading and parsing of the input file is already handled in the class supplied to you. You are given the variables as individual values. You are given file drinks.txt and snacks.txt testing your vending machines. You will need to create/complete three classes for this homework assignment: Item, VendingMachine, and VendingMachineDriver. Within your VendingMachine class, include these methods: VendingMachine This constructor method will take in the name of the input file and 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. vend This method will simulate the vending transaction after a valid amount of money 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. printMenu This method prints the menu of items for the chosen vending machine. 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. There is no need to change the code in VendingMachineDriver.java 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. Sample Run A sample run of the program is shown below. (Note: Bold texts are user input and you can use your name in the title for the simulator.) Welcome to Matt's Super Vending Machines!

I sense that you are hungry or thirsty...

Please select a vending machine: A-Drinks, B-Snacks, X-Exit: a Menu: Item# Item Price Qty 1 Milk 2.00 1 2 OJ 2.50 6 3 Water 1.50 10 4 Soda 2.25 6 5 Coffee 1.25 4 6 Monster 3.00 5

Please enter some money into the machine.(Enter -1 to exit.)4.25

You now have $4.25 to spend. Please make a selection (enter 0 to exit)1 You bought Milk for $2.00 your change is $2.25.

Please select a vending machine: A-Drinks, B-Snacks, X-Exit: B Menu: Item# Item Price Qty 1 Gummies 1.50 6 2 Chips 1.00 6 3 Raisins 1.25 5 4 Pretzels 1.50 6 5 Cookies 1.75 5 6 Peanuts 1.25 4 7 Gum 0.75 2

Please enter some money into the machine.(Enter -1 to exit.)1

You now have $1.00 to spend. Please make a selection (enter 0 to exit)1 You do not have enough money. Please add more money or exit. Please enter some money into the machine.(Enter -1 to exit.)1

You now have $2.00 to spend. Please make a selection (enter 0 to exit)1 You bought Gummies for $1.50 your change is $0.50.

Please select a vending machine: A-Drinks, B-Snacks, X-Exit: b Menu: Item# Item Price Qty 1 Gummies 1.50 5 2 Chips 1.00 6 3 Raisins 1.25 5 4 Pretzels 1.50 6 5 Cookies 1.75 5 6 Peanuts 1.25 4 7 Gum 0.75 2

Please enter some money into the machine.(Enter -1 to exit.)1

You now have $1.00 to spend. Please make a selection (enter 0 to exit)6 You do not have enough money. Please add more money or exit. Please enter some money into the machine.(Enter -1 to exit.)-1 You did not buy anything from this machine. Your change is $1.00.

Please select a vending machine: A-Drinks, B-Snacks, X-Exit: A Menu: Item# Item Price Qty 1 Milk 2.00 0 2 OJ 2.50 6 3 Water 1.50 10 4 Soda 2.25 6 5 Coffee 1.25 4 6 Monster 3.00 5

Please enter some money into the machine.(Enter -1 to exit.)3

You now have $3.00 to spend. Please make a selection (enter 0 to exit)1 Sorry the machine is currently out of that item. You now have $3.00 to spend. Please make a selection (enter 0 to exit)6 You bought Monster for $3.00 your change is $0.00.

Please select a vending machine: A-Drinks, B-Snacks, X-Exit: x

The vending machine has made $6.50. Thank you for your business!

the code have three classes (vendingmachine, vendingmachine driver, and item).

vending machine class 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){ } vending machine driver class //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 class package vending_Machine; //class to set and return item information public class Item { protected String itemDesc; protected double price; protected int quantity; /** * @return the itemDesc */ public String getItemDesc() { return itemDesc; } /** * @param itemDesc * the itemDesc to set */ public void setItemDesc(String itemDesc) { this.itemDesc = itemDesc; } /** * @return the price */ public double getPrice() { return price; } /** * @param price * the price to set */ public void setPrice(double price) { this.price = price; } /** * @return the quantity */ public int getQuantity() { return quantity; } /** * @param quantity * the quantity to set */ public void setQuantity(int quantity) { this.quantity = quantity; } public Item(String itemDesc, double price, int quantity) { this.itemDesc = itemDesc; this.price = price; this.quantity = quantity; } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((itemDesc == null) ? 0 : itemDesc.hashCode()); result = (int) (prime * result + price); result = prime * result + quantity; return result; } /* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Item other = (Item) obj; if (itemDesc == null) { if (other.itemDesc != null) return false; } else if (!itemDesc.equals(other.itemDesc)) return false; if (price != other.price) return false; if (quantity != other.quantity) return false; return true; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Item [itemDesc=" + itemDesc + ", price=" + price + ", quantity=" + quantity + "]"; } }

}

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

Step: 3

blur-text-image

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

ISBN: 1285427106, 978-1285427102

More Books

Students also viewed these Databases questions