I have the source code for three separate java programs (TestVendingMachine.java,
VendingMachineException.java, and VendingMachine.java), which work together to recreate the
sample output provided. All three programs compile and work as they should but I need to make
a minor adjustment to the VendingMachine.java program and would appreciate any help. When
the user input returns any of the three possible errors (out of stock, insufficent funds, and invalid
item), the program should automatically move onto the next user input prompt without
displaying the dispensing options and change returned.
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)
{
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("*** Error: Vending Machine - 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("*** Error: Vending Machine - Out of stock on this
item: %s%nCost: $0.%d",
items[selectedRow][col], prices[selectedRow][col]));
}
else
System.out.print("*** Error: Vending Machine - 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();
}
}
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)
{
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);
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.%n");
}
else
throw new VendingMachineException("No Console.");
}
}
VendingMachineException.java
public class VendingMachineException extends Exception
{
public VendingMachineException(String ex)
{
super(ex);
}
}
Sample Output Beginning Vending Machine Simulation Price A (in stock) Price B in stock)
Price C (in stock) $0.65 Snickers (1) $0.75 Pepsi (3) $0.30 Potato Chips (0) 2 $0.80 Diet
Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles (5) $0.40 Fritos(4) $0.70 Dr. Pepper(7) $0.55
M&Ms; (reg) (5) M $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms; (0) $0.45 Doritos (5) $0.60 Sprite
(0) $0.45 Twisters (4) $0.50 Almonds (0) 6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35
Peanuts (3) Enter money as integer (e.g 50) or 0 to exit: 65 Please make a selection (e.g., A1):
BA Error: Vending Machine Out of stock on this item: Peanut M&Ms; ost: $0.60 nter money as
integer (e.g 50) or 0 to exit 5 Please make a selection (e.g., A1): A3 Dispensing Price A (in
stock) Price B (in stock Price C (in stock) $0.75 Pepsi (3) $0.65 Snickers (1) $0.30 Potato Chips
(0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup (6) $0.35 Ruffles (5) 3 $0.70 Dr. Pepper (6) $0.55
M&Ms; (reg) (5) $0.40 Fritos (4) 4 $0.75 Diet Dr. P(5) $0.60 peanut M&Ms; (0) $0.45 Doritos
(5) 6 $0.60 Sprite (0) $0.45 Twisters (4) $0.50 Almonds (0) 5 $0.65 Diet Sprite (6) $0.40
Hershey's(4) $0.35 Peanuts (3) change 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 nter money as integer (e.g 50) or 0 to exit: 90
Please make a selection (e.g., A1): B1 Price A (in stock Price B (in stock) Price C (in stock)
$0.75 Pepsi(3) $0.65 Snickers (0) $0.30 Potato Chips (0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's
Cup (6) $0.35 Ruffles (5) 3 $0.70 Dr. Pepper(6) $0.55 M&Ms; (reg) (5) $0.40 Fritos (4) 4 $0.75
Diet Dr. P(5) $0.60 peanut M&Ms; (0) $0.45 Doritos (5) $0.45 Twisters (4) 5 $0.60 Sprite(0)
$0.50 Almonds (0) 6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts(3) Change returned
$0.50 50) or 0 to exit: 1 Enter money as integer (e.g Please make a selection (e.g A1): A7 Invalid
item selection: A7 Error: Vending Machine Enter money as integer (e.g 50) or 0 to exit 25 Please
make a selection (e.E A1): C3 Error: Vending Machine Insufficient funds for item: Fritos Cost:
$0.40, Deposited: $0.26 Enter money as integer (e.g 50) or 0 to exit: 20 Please make a selection
(e.g A1): B5 Dispensing Price A (in stock) Price B (in stock) Price C (in stock) $0.75 Pepsi(3)
$0.65 Snickers (0) $0.30 Potato Chips (0) $0.60 Resse's Cup(6) $0.35 Ruffles (5) 2 $0.80 Diet
Pepsi $0.70 Dr. Pepper(6) $0.55 M&Ms; (reg)(5) $0.40 Fritos (4) A $0.75 Diet Dr. P(5) $0.60
Peanut M&Ms;(0) $0.45 Doritos (5) 5 $0.60 Sprite (0) $0.45 Twisters (3) $0.50 Almonds (0) 5
$0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts (3) hange returned $0.01 Enter money as
integer (e.g 50) or 0 to exit: 0 End of program ress any key to continue
Solution
Please follow the code and comments for descritpion :
CODE :
a) TestVendingMachine.java :
import javax.swing.*; // required imports
import java.io.Console;
public class TestVendingMachine { // class to run the code
public static void main(String[] args) throws VendingMachineException { // driver method
int deposit = 0; // required local variables
int change = 0;
String choice = null;
Console console = System.console();
if (console != null) { // check for the console
// 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) {
vm1.depositMoney(deposit);
choice = console.readLine("Please make a selection (e.g., A1): ");
change = vm1.buyItem(choice);
if (change < 0) { // check for the change returned
deposit = Integer.parseInt(console.readLine(" Enter money as integer (e.g., 50) or 0 to exit:
"));
} else { // if not then continue the loop
console.format("%nDispensing ...%n%n");
console.format("%s%n", vm1);
console.format("Change returned = $0.%d%n%n", change);
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.%n");
} else {
throw new VendingMachineException("No Console.");
}
}
}
b) 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) {
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("*** Error: Vending Machine - 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("*** Error: Vending Machine - Out of stock on this item:
%s%nCost: $0.%d",
items[selectedRow][col], prices[selectedRow][col]));
}
} else {
System.out.print("*** Error: Vending Machine - 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();
}
}
c) VendingMachineException.java :
public class VendingMachineException extends Exception {
public VendingMachineException(String ex) {
super(ex);
}
}
OUTPUT :
Beginning Vending Machine Simulation.
Price A (in stock) Price B (in stock) Price C (in stock)
1 $0.75 Pepsi(3) $0.65 Snickers(1) $0.30 Potato Chips(0)
2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles(5)
3 $0.70 Dr. Pepper(7) $0.55 M&Ms (reg)(5) $0.40 Fritos(4)
4 $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms(0) $0.45 Doritos(5)
5 $0.60 Sprite(0) $0.45 Twisters(4) $0.50 Almonds(0)
6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts(3)
Enter money as integer (e.g., 50) or 0 to exit: 65
Please make a selection (e.g., A1): B4
*** Error: Vending Machine - Out of stock on this item: Peanut M&Ms
Cost: $0.60
Enter money as integer (e.g., 50) or 0 to exit: 5
Please make a selection (e.g., A1): A3
Dispensing ...
Price A (in stock) Price B (in stock) Price C (in stock)
1 $0.75 Pepsi(3) $0.65 Snickers(1) $0.30 Potato Chips(0)
2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles(5)
3 $0.70 Dr. Pepper(6) $0.55 M&Ms (reg)(5) $0.40 Fritos(4)
4 $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms(0) $0.45 Doritos(5)
5 $0.60 Sprite(0) $0.45 Twisters(4) $0.50 Almonds(0)
6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts(3)
Change returned = $0.0
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
Please make a selection (e.g., A1): B1
Dispensing ...
Price A (in stock) Price B (in stock) Price C (in stock)
1 $0.75 Pepsi(3) $0.65 Snickers(0) $0.30 Potato Chips(0)
2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles(5)
3 $0.70 Dr. Pepper(6) $0.55 M&Ms (reg)(5) $0.40 Fritos(4)
4 $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms(0) $0.45 Doritos(5)
5 $0.60 Sprite(0) $0.45 Twisters(4) $0.50 Almonds(0)
6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts(3)
Change returned = $0.50
Enter money as integer (e.g., 50) or 0 to exit: 1
Please 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
Please make a selection (e.g., A1): C3
*** Error: Vending Machine - Insufficient funds for item: Fritos
Cost: $0.40, Deposited: $0.26
Enter money as integer (e.g., 50) or 0 to exit: 20
Please make a selection (e.g., A1): B5
Dispensing ...
Price A (in stock) Price B (in stock) Price C (in stock)
1 $0.75 Pepsi(3) $0.65 Snickers(0) $0.30 Potato Chips(0)
2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles(5)
3 $0.70 Dr. Pepper(6) $0.55 M&Ms (reg)(5) $0.40 Fritos(4)
4 $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms(0) $0.45 Doritos(5)
5 $0.60 Sprite(0) $0.45 Twisters(3) $0.50 Almonds(0)
Change returned = $0.1
Enter money as integer (e.g., 50) or 0 to exit: 0
End of program.
Hope this is helpful.

I have the source code for three separate java programs (TestVending.pdf

  • 1.
    I have thesource code for three separate java programs (TestVendingMachine.java, VendingMachineException.java, and VendingMachine.java), which work together to recreate the sample output provided. All three programs compile and work as they should but I need to make a minor adjustment to the VendingMachine.java program and would appreciate any help. When the user input returns any of the three possible errors (out of stock, insufficent funds, and invalid item), the program should automatically move onto the next user input prompt without displaying the dispensing options and change returned. 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';
  • 2.
    // default constructor publicVendingMachine() { } 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];
  • 3.
    change = getChange(); } else System.out.print(String.format("***Error: Vending Machine - 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("*** Error: Vending Machine - Out of stock on this item: %s%nCost: $0.%d", items[selectedRow][col], prices[selectedRow][col])); } else System.out.print("*** Error: Vending Machine - 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()
  • 4.
    { // 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(); } } 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;
  • 5.
    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) { 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); 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.%n"); } else throw new VendingMachineException("No Console."); } } VendingMachineException.java public class VendingMachineException extends Exception { public VendingMachineException(String ex) { super(ex); } }
  • 6.
    Sample Output BeginningVending Machine Simulation Price A (in stock) Price B in stock) Price C (in stock) $0.65 Snickers (1) $0.75 Pepsi (3) $0.30 Potato Chips (0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles (5) $0.40 Fritos(4) $0.70 Dr. Pepper(7) $0.55 M&Ms; (reg) (5) M $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms; (0) $0.45 Doritos (5) $0.60 Sprite (0) $0.45 Twisters (4) $0.50 Almonds (0) 6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts (3) Enter money as integer (e.g 50) or 0 to exit: 65 Please make a selection (e.g., A1): BA Error: Vending Machine Out of stock on this item: Peanut M&Ms; ost: $0.60 nter money as integer (e.g 50) or 0 to exit 5 Please make a selection (e.g., A1): A3 Dispensing Price A (in stock) Price B (in stock Price C (in stock) $0.75 Pepsi (3) $0.65 Snickers (1) $0.30 Potato Chips (0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup (6) $0.35 Ruffles (5) 3 $0.70 Dr. Pepper (6) $0.55 M&Ms; (reg) (5) $0.40 Fritos (4) 4 $0.75 Diet Dr. P(5) $0.60 peanut M&Ms; (0) $0.45 Doritos (5) 6 $0.60 Sprite (0) $0.45 Twisters (4) $0.50 Almonds (0) 5 $0.65 Diet Sprite (6) $0.40 Hershey's(4) $0.35 Peanuts (3) change 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 nter money as integer (e.g 50) or 0 to exit: 90 Please make a selection (e.g., A1): B1 Price A (in stock Price B (in stock) Price C (in stock) $0.75 Pepsi(3) $0.65 Snickers (0) $0.30 Potato Chips (0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup (6) $0.35 Ruffles (5) 3 $0.70 Dr. Pepper(6) $0.55 M&Ms; (reg) (5) $0.40 Fritos (4) 4 $0.75 Diet Dr. P(5) $0.60 peanut M&Ms; (0) $0.45 Doritos (5) $0.45 Twisters (4) 5 $0.60 Sprite(0) $0.50 Almonds (0) 6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts(3) Change returned $0.50 50) or 0 to exit: 1 Enter money as integer (e.g Please make a selection (e.g A1): A7 Invalid item selection: A7 Error: Vending Machine Enter money as integer (e.g 50) or 0 to exit 25 Please make a selection (e.E A1): C3 Error: Vending Machine Insufficient funds for item: Fritos Cost: $0.40, Deposited: $0.26 Enter money as integer (e.g 50) or 0 to exit: 20 Please make a selection (e.g A1): B5 Dispensing Price A (in stock) Price B (in stock) Price C (in stock) $0.75 Pepsi(3) $0.65 Snickers (0) $0.30 Potato Chips (0) $0.60 Resse's Cup(6) $0.35 Ruffles (5) 2 $0.80 Diet Pepsi $0.70 Dr. Pepper(6) $0.55 M&Ms; (reg)(5) $0.40 Fritos (4) A $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms;(0) $0.45 Doritos (5) 5 $0.60 Sprite (0) $0.45 Twisters (3) $0.50 Almonds (0) 5 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts (3) hange returned $0.01 Enter money as integer (e.g 50) or 0 to exit: 0 End of program ress any key to continue Solution Please follow the code and comments for descritpion : CODE : a) TestVendingMachine.java :
  • 7.
    import javax.swing.*; //required imports import java.io.Console; public class TestVendingMachine { // class to run the code public static void main(String[] args) throws VendingMachineException { // driver method int deposit = 0; // required local variables int change = 0; String choice = null; Console console = System.console(); if (console != null) { // check for the console // 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) { vm1.depositMoney(deposit); choice = console.readLine("Please make a selection (e.g., A1): "); change = vm1.buyItem(choice); if (change < 0) { // check for the change returned deposit = Integer.parseInt(console.readLine(" Enter money as integer (e.g., 50) or 0 to exit: ")); } else { // if not then continue the loop console.format("%nDispensing ...%n%n"); console.format("%s%n", vm1); console.format("Change returned = $0.%d%n%n", change); 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.%n"); } else { throw new VendingMachineException("No Console."); }
  • 8.
    } } b) VendingMachine.java : importjava.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);
  • 9.
    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("*** Error: Vending Machine - Insufficient funds for item: %s%nCost: $0.%d, Deposited: $%.2f", items[selectedRow][col], prices[selectedRow][col], getDeposit() / 100.0)); }
  • 10.
    } else { System.out.print(String.format("***Error: Vending Machine - Out of stock on this item: %s%nCost: $0.%d", items[selectedRow][col], prices[selectedRow][col])); } } else { System.out.print("*** Error: Vending Machine - 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]
  • 11.
    + ") $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(); } } c) VendingMachineException.java : public class VendingMachineException extends Exception { public VendingMachineException(String ex) { super(ex); } } OUTPUT : Beginning Vending Machine Simulation. Price A (in stock) Price B (in stock) Price C (in stock) 1 $0.75 Pepsi(3) $0.65 Snickers(1) $0.30 Potato Chips(0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles(5) 3 $0.70 Dr. Pepper(7) $0.55 M&Ms (reg)(5) $0.40 Fritos(4) 4 $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms(0) $0.45 Doritos(5) 5 $0.60 Sprite(0) $0.45 Twisters(4) $0.50 Almonds(0) 6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts(3) Enter money as integer (e.g., 50) or 0 to exit: 65 Please make a selection (e.g., A1): B4 *** Error: Vending Machine - Out of stock on this item: Peanut M&Ms
  • 12.
    Cost: $0.60 Enter moneyas integer (e.g., 50) or 0 to exit: 5 Please make a selection (e.g., A1): A3 Dispensing ... Price A (in stock) Price B (in stock) Price C (in stock) 1 $0.75 Pepsi(3) $0.65 Snickers(1) $0.30 Potato Chips(0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles(5) 3 $0.70 Dr. Pepper(6) $0.55 M&Ms (reg)(5) $0.40 Fritos(4) 4 $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms(0) $0.45 Doritos(5) 5 $0.60 Sprite(0) $0.45 Twisters(4) $0.50 Almonds(0) 6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts(3) Change returned = $0.0 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 Please make a selection (e.g., A1): B1 Dispensing ... Price A (in stock) Price B (in stock) Price C (in stock) 1 $0.75 Pepsi(3) $0.65 Snickers(0) $0.30 Potato Chips(0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles(5) 3 $0.70 Dr. Pepper(6) $0.55 M&Ms (reg)(5) $0.40 Fritos(4) 4 $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms(0) $0.45 Doritos(5) 5 $0.60 Sprite(0) $0.45 Twisters(4) $0.50 Almonds(0) 6 $0.65 Diet Sprite(6) $0.40 Hershey's(4) $0.35 Peanuts(3) Change returned = $0.50 Enter money as integer (e.g., 50) or 0 to exit: 1 Please 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 Please make a selection (e.g., A1): C3 *** Error: Vending Machine - Insufficient funds for item: Fritos Cost: $0.40, Deposited: $0.26 Enter money as integer (e.g., 50) or 0 to exit: 20 Please make a selection (e.g., A1): B5
  • 13.
    Dispensing ... Price A(in stock) Price B (in stock) Price C (in stock) 1 $0.75 Pepsi(3) $0.65 Snickers(0) $0.30 Potato Chips(0) 2 $0.80 Diet Pepsi(4) $0.60 Resse's Cup(6) $0.35 Ruffles(5) 3 $0.70 Dr. Pepper(6) $0.55 M&Ms (reg)(5) $0.40 Fritos(4) 4 $0.75 Diet Dr. P(5) $0.60 Peanut M&Ms(0) $0.45 Doritos(5) 5 $0.60 Sprite(0) $0.45 Twisters(3) $0.50 Almonds(0) Change returned = $0.1 Enter money as integer (e.g., 50) or 0 to exit: 0 End of program. Hope this is helpful.