JAVA Vending Machine Exception Modifying a class to use Exc
JAVA
Vending Machine Exception - Modifying a class to use Exceptions Purpose: to provide an exercise using exceptions.
Create a VendingMachineException class defined in VendingMachineException.java. This class has a single constructor which takes a String, msg, and passes it to the parent Exception class. It also should
? contain a private String, error, with the value of “***Error: Vending Machine – “
? override the getMessage() method to prepend error to the message from the parent class and return to the caller
I have provided you with a VendingMachine class, defined in VendingMachine.java, which you need to modify to remove all output and to use the VendingMachineException class.
When throwing an exception, pass the appropriate text message to display the reason for the error (see code I supplied and sample output.) When the message indicates data from a variable (e.g., the amount deposited, the selection made, the product selected), it uses the variable(s) – do not simply type the amount, selection, or price as text. Review string concatenation, if necessary. Neither of the above classes should do any input or output. All input and output should be done by the test program.
Throw a VendingMachineException under the following conditions:
? in the buyItem() method: if no money has been deposited, throw an error message containing the user selection.
? in the buyItem() method: if the item selected is not valid, throw an error message containing the user selection.
? in the buyItem() method: if the selected item is out of stock, throw an error message containing the selected item description and price.
? in the buyItem() method: if the money deposited is insufficient for the item cost, throw an error message containing the selected item description, price, and amount deposited so far by the user.
You also will need a program to test your exception handling. I also have provided source code in TestVendingMachine.java, which you will need to modify. You will need to surround appropriate lines of code with try/catch blocks. The locations for these have not been indicated. In your catch block, you should use console I/O to display the error message (see sample output) from the Exception object itself. Try various combinations of data, but use my data for your captured submission. To keep things reasonably simple, entered amounts will be integers. Loop as long as the user enters money into the vending machine. Note the lines to remove from the provided code in the test application, once you implement the proper exceptions and exception-handling.
The code needs to be copyable and match exacly as the picture.
Also NEEDS to be able to compile...
THANK YOU!
****************************************************************************************************************************************************************************************************
TestVendingMachine.java
****************************************************************************************************************************************************************************************************
VendingMachine.java
****************************************************************************************************************************************************************************************************
Sample output: vyours should match exact CNWINDOWSSystem32cmd.exe eginning Vending Machine Simulation. Price A (in stock) (in stock Price B (in stock) $0.65 Snickers(1) 2 $0.80 Diet Pepsi(4) $0.60 Resse\'s Cup (6) $0.35 Ruffles (5) $0.70 Dr. Pepp(7 $0.55 M&Ms; (reg) (5) $%0.40 Fritos (4) $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms; (0) $0.45 Doritos(5) $0.45 Twisters(4) Price $0.30 Potato Chips (0) 5 $0.60 Sprite(0) $0.50 Almonds (0) $0.35 Peanuts (3) 5 $0.65 Diet Sprite(6 $0.40 Hershey\'s (4) Enter money as integer (e.g., 50) or 0 to exit: 65 lease make a selection (e.g., A1) B4 Error: Vending Machine Out of stock on this item: Peanut M&Ms; Enter money as integer (e.g., 50) or 0 to exit: 5 Please make a selection (e.g., A1) A3 ispensing... Price A (in stock) Price B (in stock) 0.65 Snickers (1) 2 $0.80 Diet Pepsi(4) $0.60 Resse\'s Cup (6) $0.35 Ruffles (5) $0.70 Dr. Pepper (6 $0.55 M&Ms; (reg) (5) $0.40 Fritos(4) $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms; (0 $%0.45 Doritos (5) 0.45 Twisters (4) Price C (in stock) $0.30 Potato Chips(0) 5 $0.60 Sprite(0) $0.50 Almonds (0) $0.35 Peanuts (3) $0.65 Diet Sprite (6) $0.40 Hershey\'s(4) hange returned $0.00 Enter money as integer (e.g., 50) or 0 to exit: 25 Please make a selection (e.g., A1): B1 Error: Vending Machine Insufficient funds for item: Snickers Cost: $0.65, Deposited: $0.25 Enter money as integer (e.g., 50) or 0 to exit: 90 lease make a selection (e.g., A1): B1 ispensing . . - Price A (in stock) C (in stock) Price B (in stock) $0.65 Snickers (e) 2 $0.80 Diet Pepsi(4) $0.60 Resse\'s Cup (6 0.35 Ruffles (5) Price $0.30 Potato Chips(0) $0.70 Dr. Pepper(6) $0.55 M&Ms; (reg) (5) $0.40 Fritos(4) $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms; (0)$0.45 Doritos(5) 5 $0.60 Sprite(0) 5 $0.65 Diet Sprite(6) $0.40 Hershey\'s (4) 0.45 Twisters(4) $0.50 Almonds (0) $0.35 Peanuts (3) hange returned$0.50 Enter money as integer (e.g., 50) or 0 to exit: 1 lease make a selection(e.g., A1) A7 **Error: Vending Machine Invalid item selection: A7 Enter money as integer(e.g., 50) or 0 to exit: 25 lease make a selection (e.g., A1): C3 **Error: Vending Machine Insufficient funds for item: Fritos Cost: $0.40, Deposited: $0.26 nter money as integer (e.g., 50) or 0 to exit: 260 lease make a selection (e.g., A1): B5 ispensing Price C (in stock) $0.30 Potato Chips(0) Price A (in stock) Price B (in stock) $0.65 Snickers (0) 2 $0.80 Diet Pepsi(4) $0.60 Resse\'s Cup (6) $0.35 Ruffles (5) $0.70 Dr. Pepper(6) $0.55 MMs (reg)(5) $0.40 Fritos (4) $0.75 Diet Dr P(5) $0.60 Peanut M&Ms; () $0.45 Doritos (5) 0.45 Twisters(3) $0.50 Almonds () $0.35 Peanuts (3) 5 $0.60 Sprite(0) 5 $0.65 Diet Sprite(6) $0.40 Hershey\'s(4) hange returned = $0.01 Enter money as integer(e.g., 50) or 0 to exit: 0 nd of program. Press any key to continue . . .Solution
TestVendingMachine.java
import javax.swing.*;
import java.io.Console;
public class TestVendingMachine
{
public static void main(String[] args)
{
int deposit = 0;
int change = 0;
String choice = null;
Console console = System.console();
//System.out.println(\"Printing console...\"+console);
if (console != null)
{
// create VendingMachine object
console.format(\"Beginning Vending Machine Simulation.%n%n\");
VendingMachine vm1 = new VendingMachine(); // create a new vending machine object
deposit = Integer.parseInt(console.readLine(\"Enter money as integer (e.g., 50) or 0 to exit: \"));
while(deposit > 0)
{
vm1.depositMoney(deposit);
choice = console.readLine(\"Please make a selection (e.g., A1): \");
try {
change = vm1.buyItem(choice);
} catch (VendingMachineException e) {
// TODO Auto-generated catch block
e.getMessage();
}
if(change >= 0) // after exceptions are added to code, do your need this?
{
console.format(\"%nDispensing ...%n%n\");
console.format(\"%s%n\",vm1);
console.format(\"Change returned = $%.2f%n%n\",change/100.0);
}
deposit = Integer.parseInt(console.readLine(\"Enter money as integer (e.g., 50) or 0 to exit: \"));
}
if(vm1.getDeposit() > 0)
console.format(\"%nReturning change: $%.2f%n.\", vm1.getChange()/100.0);
console.format(\"%nEnd of program.%n\");
}
else
System.err.println(\"No console.\");
}
}
VendingMachine.java
import java.util.Random;
import java.util.regex.*;
public class VendingMachine
{
// soft drinks candy bars snacks
private String[][] items = { {\"Pepsi\", \"Snickers\", \"Potato Chips\"},
{\"Diet Pepsi\", \"Resse\'s Cup\", \"Ruffles\"},
{\"Dr. Pepper\", \"M&M chocolate\",\"Fritos\"},
{\"Diet Dr. P\", \"Peanut M&Ms\", \"Doritos\"},
{\"Sprite\", \"Twisters\", \"Almonds\"},
{\"Diet Sprite\",\"Hershey\'s\", \"Peanuts\"} };
private int[][] prices = { {75,65,30},
{80,60,35},
{70,55,40},
{75,60,45},
{60,45,50},
{65,40,35} };
private int[][] quantity = { {3,1,0},
{4,6,5},
{7,5,4},
{5,0,5},
{0,4,0},
{6,4,3} };
private int depositedAmt = 0;
private boolean haveValidSelection = false;
private int selectedRow = 0;
private char selectedCol = \'A\';
// default constructor
public VendingMachine()
{
}
public int buyItem(String selection) throws VendingMachineException
{
Pattern validChars = Pattern.compile(\"[^A-Ca-c1-6]+\");
Matcher m1 = validChars.matcher(selection);
Pattern selectionRange = Pattern.compile(\"[1-6]+\");
Matcher m2;
boolean illegal = false;
int change = -1;
if(depositedAmt > 0)
{
if(selection.length() != 2)
illegal = true;
if(!illegal)
illegal = m1.find();
if(!illegal)
if( Character.getNumericValue(selection.charAt(1)) < 1)
illegal = true;
m2 = selectionRange.matcher(selection.substring(0,1));
if(!illegal)
illegal = m2.find();
haveValidSelection = !illegal;
if(haveValidSelection)
{
selectedRow = Character.getNumericValue(selection.charAt(1) - 1);
selectedCol = Character.toUpperCase(selection.charAt(0)); // allows for upper- or lowercase entry
int col = selectedCol - 65;
if(quantity[selectedRow][col] > 0)
if( depositedAmt >= prices[selectedRow][col])
{
depositedAmt -= prices[selectedRow][col];
--quantity[selectedRow][col];
change = getChange();
}
else
/*System.out.print(String.format(\"Insufficient funds for item: %s%nCost: $0.%d, Deposited: $%.2f\",
items[selectedRow][col], prices[selectedRow][col], getDeposit()/100.0));*/
/*System.out.println(\"Insufficient funds for item: %s%nCost: $0.%d, Deposited: $%.2f, \"+items[selectedRow][col]+\" , \"+prices[selectedRow][col]+\" , \"+getDeposit()/100.0);*/
throw new VendingMachineException(\"Insufficient funds for item: %s%nCost: $0.%d, Deposited: $%.2f, \"+items[selectedRow][col]+\" , \"+prices[selectedRow][col]+\" , \"+getDeposit()/100.0);
else
/*System.out.print(String.format(\"Out of stock on this item: %s%nCost: $0.%d\",
items[selectedRow][col], prices[selectedRow][col]));*/
//System.out.println(\"Out of stock on this item: %s%nCost: $0.%d, \"+items[selectedRow][col]+\" , \"+prices[selectedRow][col]);
throw new VendingMachineException(\"Out of stock on this item: %s%nCost: $0.%d, \"+items[selectedRow][col]+\" , \"+prices[selectedRow][col]);
}
else
//System.out.print(\"Invalid item selection: \"+selection);
throw new VendingMachineException(\"Invalid item selection: \"+selection);
}
else
//System.out.print(\"Please enter proper funds before making selection: \"+selection);
throw new VendingMachineException(\"Please enter proper funds before making selection: \"+selection);
return change;
}
public void depositMoney(int payment)
{
if(payment > 0)
depositedAmt += payment;
}
public int getChange()
{
int returned = depositedAmt;
depositedAmt = 0;
return returned;
}
public int getDeposit()
{
return depositedAmt;
}
// returns String representation of object
public String toString()
{
// display items, quantity on hand, and prices
StringBuffer display = new StringBuffer();
// holds display contents
display.append(\" Price A (in stock) Price B (in stock) Price C (in stock)\ \");
display.append(\"1 $0.\"+prices[0][0]+\" Pepsi(\"+quantity[0][0]+\") $0.\"
+prices[0][1] +\" Snickers(\"+quantity[0][1]
+\") $0.\"+prices[0][2]+\" Potato Chips(\"+quantity[0][2]+\")\ \");
display.append(\"2 $0.\"+prices[1][0]+\" Diet Pepsi(\"+quantity[1][0]
+\") $0.\"+prices[1][1]+\" Resse\'s Cup(\"+quantity[1][1]
+\") $0.\"+prices[1][2]+\" Ruffles(\"+quantity[1][2]+\")\ \");
display.append(\"3 $0.\"+prices[2][0]+\" Dr. Pepper(\"+quantity[2][0]
+\") $0.\"+prices[2][1]+\" M&Ms (reg)(\"+quantity[2][1]
+\") $0.\"+prices[2][2]+\" Fritos(\"+quantity[2][2]+\")\ \");
display.append(\"4 $0.\"+prices[3][0]+\" Diet Dr. P(\"+quantity[3][0]
+\") $0.\"+prices[3][1]+\" Peanut M&Ms(\"+quantity[3][1]
+\") $0.\"+prices[3][2]+\" Doritos(\"+quantity[3][2]+\")\ \");
display.append(\"5 $0.\"+prices[4][0]+\" Sprite(\"+quantity[4][0]
+\") $0.\"+prices[4][1]+\" Twisters(\"+quantity[4][1]
+\") $0.\"+prices[4][2]+\" Almonds(\"+quantity[4][2]+\")\ \");
display.append(\"6 $0.\"+prices[5][0]+\" Diet Sprite(\"+quantity[5][0]
+\") $0.\"+prices[5][1]+\" Hershey\'s(\"+quantity[5][1]
+\") $0.\"+prices[5][2]+\" Peanuts(\"+quantity[5][2]+\")\ \");
return display.toString();
}
}
VendingMachineException.java
public class VendingMachineException extends Exception{
private String error=\"*****Error-Vending Machine - \";
public VendingMachineException(String msg){
// TODO Auto-generated constructor stub
new Exception(msg);
}
public String getMessage() {
String message=new Exception().getMessage();
message=error+message;
return message;
}
}




