Create a VendingMachineException class defined inVendingMach
Create a VendingMachineException class defined inVendingMachineException.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–\"; and 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. When the message indicates data from a variable, 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.
Throw a VendingMachineException under the following conditions: (A) in the buyItem() method; if no money has been deposited, throw an error message containing the user selection. (B) in the buyItem() method; if the item selected is not valid, throw an error message containing the user selection. (C) in the buyItem() method; if the selected item is out of stock, throw an error message containing the selected item description and price. (D) 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.
(Provided Code)
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)
{
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));
else
System.out.print(String.format(\"Out of stock on this item: %s%nCost: $0.%d\",
items[selectedRow][col], prices[selectedRow][col]));
}
else
System.out.print(\"Invalid item selection: \"+selection);
}
else
System.out.print(\"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();
}
}
Solution
TestVendingMachine.java
import javax.swing.*;
import java.io.Console;
public class TestVendingMachine
{
public static void main(String[] args) throws VendingMachineException
{
int deposit = 0;
int change = 0;
String choice = null;
Console console = System.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
console.format(\"%s%n%n\",vm1);
deposit = Integer.parseInt(console.readLine(\"Enter money as integer (e.g., 50) or 0 to exit: \"));
while(deposit > 0)
{
try
{
vm1.depositMoney(deposit);
choice = console.readLine(\"Please make a selection (e.g., A1): \");
change = vm1.buyItem(choice);
console.format(\"%nDispensing ...%n%n\");
console.format(\"%s%n\",vm1);
console.format(\"Change returned = %d%n%n\",change);
}
catch(VendingMachineException vme)
{
System.out.println(\"Error: \"+vme.getMessage());
}
deposit = Integer.parseInt(console.readLine(\"\ Enter money as integer (e.g., 50) or 0 to exit: \"));
}
if(vm1.getDeposit() > 0)
console.format(\"%nReturning change: $0.%d%n.\", vm1.getChange());
console.format(\"%nEnd of program.\");
}
else
System.err.println(\"No console.\");
}
}
VendingMachineException.java
public class VendingMachineException extends Exception
{
public VendingMachineException(String ex)
{
super(ex);
}
}
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));
int col = selectedCol - 65;
if(quantity[selectedRow][col] > 0)
{
if( depositedAmt >= prices[selectedRow][col])
{
depositedAmt -= prices[selectedRow][col];
--quantity[selectedRow][col];
change = getChange();
}
else
throw new VendingMachineException(\"Insufficient funds for item: \"+items[selectedRow][col]+\"\ Cost: $0.\"+prices[selectedRow][col]+\", Deposited: $0.\"+depositedAmt);
}
else
throw new VendingMachineException(\"Out of stock on this item: \"+items[selectedRow][col]+\"\ Cost: $0.\"+prices[selectedRow][col]);
}
else
throw new VendingMachineException(\"Invalid item selection: \"+selection);
}
else
throw new VendingMachineException(\"Please enter proper funds before making 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();
}
}





