SlideShare a Scribd company logo
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.

More Related Content

More from fcsondhiindia

JAVAneed help with public IteratorItem iterator()import java.u.pdf
JAVAneed help with public IteratorItem iterator()import java.u.pdfJAVAneed help with public IteratorItem iterator()import java.u.pdf
JAVAneed help with public IteratorItem iterator()import java.u.pdf
fcsondhiindia
 
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdf
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdfLet A be an n n matrix. Which of the following are TRUE I. If A i.pdf
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdf
fcsondhiindia
 
How does PERT technique compare with estimating or forecasting techn.pdf
How does PERT technique compare with estimating or forecasting techn.pdfHow does PERT technique compare with estimating or forecasting techn.pdf
How does PERT technique compare with estimating or forecasting techn.pdf
fcsondhiindia
 
in 4 paragraphs explain Profit Sharing in the US between Management .pdf
in 4 paragraphs explain Profit Sharing in the US between Management .pdfin 4 paragraphs explain Profit Sharing in the US between Management .pdf
in 4 paragraphs explain Profit Sharing in the US between Management .pdf
fcsondhiindia
 
How is the urinary system is involved in regulating the pH in bodily.pdf
How is the urinary system is involved in regulating the pH in bodily.pdfHow is the urinary system is involved in regulating the pH in bodily.pdf
How is the urinary system is involved in regulating the pH in bodily.pdf
fcsondhiindia
 
Find a polynomial that describes the shaded area. SolutionArea.pdf
Find a polynomial that describes the shaded area.  SolutionArea.pdfFind a polynomial that describes the shaded area.  SolutionArea.pdf
Find a polynomial that describes the shaded area. SolutionArea.pdf
fcsondhiindia
 
Explain the common interest logic and the economic logic of .pdf
Explain the common interest logic and the economic logic of .pdfExplain the common interest logic and the economic logic of .pdf
Explain the common interest logic and the economic logic of .pdf
fcsondhiindia
 
Does privacy always guarantee integrity. Justify your answerSolu.pdf
Does privacy always guarantee integrity. Justify your answerSolu.pdfDoes privacy always guarantee integrity. Justify your answerSolu.pdf
Does privacy always guarantee integrity. Justify your answerSolu.pdf
fcsondhiindia
 
Distinguish between the GAAP and IFRS standards for internal .pdf
Distinguish between the GAAP and IFRS standards for internal .pdfDistinguish between the GAAP and IFRS standards for internal .pdf
Distinguish between the GAAP and IFRS standards for internal .pdf
fcsondhiindia
 
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdf
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdfDescribe how pH inhibit microbe growth.SolutionMost of the pat.pdf
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdf
fcsondhiindia
 
Write the C code to create a data structure named window that con.pdf
Write the C code to create a data structure named window that con.pdfWrite the C code to create a data structure named window that con.pdf
Write the C code to create a data structure named window that con.pdf
fcsondhiindia
 
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdfWrite 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
fcsondhiindia
 
Who are the people directly involved in the collective bargaining pr.pdf
Who are the people directly involved in the collective bargaining pr.pdfWho are the people directly involved in the collective bargaining pr.pdf
Who are the people directly involved in the collective bargaining pr.pdf
fcsondhiindia
 
Contracts or agreements can sometimes create financial contingencies.pdf
Contracts or agreements can sometimes create financial contingencies.pdfContracts or agreements can sometimes create financial contingencies.pdf
Contracts or agreements can sometimes create financial contingencies.pdf
fcsondhiindia
 
Which of the following is an equation of the line that passes through.pdf
Which of the following is an equation of the line that passes through.pdfWhich of the following is an equation of the line that passes through.pdf
Which of the following is an equation of the line that passes through.pdf
fcsondhiindia
 
When more than one structural arrangement of elements is possible fo.pdf
When more than one structural arrangement of elements is possible fo.pdfWhen more than one structural arrangement of elements is possible fo.pdf
When more than one structural arrangement of elements is possible fo.pdf
fcsondhiindia
 
Click to add title 2. Explain the concept of African Diaspora and dis.pdf
Click to add title 2. Explain the concept of African Diaspora and dis.pdfClick to add title 2. Explain the concept of African Diaspora and dis.pdf
Click to add title 2. Explain the concept of African Diaspora and dis.pdf
fcsondhiindia
 
Based on the following data, what is the quick ratio, rounded to one.pdf
Based on the following data, what is the quick ratio, rounded to one.pdfBased on the following data, what is the quick ratio, rounded to one.pdf
Based on the following data, what is the quick ratio, rounded to one.pdf
fcsondhiindia
 
ATP production during glycolysis;requires an enzymeoccurs in the.pdf
ATP production during glycolysis;requires an enzymeoccurs in the.pdfATP production during glycolysis;requires an enzymeoccurs in the.pdf
ATP production during glycolysis;requires an enzymeoccurs in the.pdf
fcsondhiindia
 
What are symptoms of N, P, K, Mg, S and Fe deficiencies in plants.pdf
What are symptoms of N, P, K, Mg, S and Fe deficiencies in plants.pdfWhat are symptoms of N, P, K, Mg, S and Fe deficiencies in plants.pdf
What are symptoms of N, P, K, Mg, S and Fe deficiencies in plants.pdf
fcsondhiindia
 

More from fcsondhiindia (20)

JAVAneed help with public IteratorItem iterator()import java.u.pdf
JAVAneed help with public IteratorItem iterator()import java.u.pdfJAVAneed help with public IteratorItem iterator()import java.u.pdf
JAVAneed help with public IteratorItem iterator()import java.u.pdf
 
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdf
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdfLet A be an n n matrix. Which of the following are TRUE I. If A i.pdf
Let A be an n n matrix. Which of the following are TRUE I. If A i.pdf
 
How does PERT technique compare with estimating or forecasting techn.pdf
How does PERT technique compare with estimating or forecasting techn.pdfHow does PERT technique compare with estimating or forecasting techn.pdf
How does PERT technique compare with estimating or forecasting techn.pdf
 
in 4 paragraphs explain Profit Sharing in the US between Management .pdf
in 4 paragraphs explain Profit Sharing in the US between Management .pdfin 4 paragraphs explain Profit Sharing in the US between Management .pdf
in 4 paragraphs explain Profit Sharing in the US between Management .pdf
 
How is the urinary system is involved in regulating the pH in bodily.pdf
How is the urinary system is involved in regulating the pH in bodily.pdfHow is the urinary system is involved in regulating the pH in bodily.pdf
How is the urinary system is involved in regulating the pH in bodily.pdf
 
Find a polynomial that describes the shaded area. SolutionArea.pdf
Find a polynomial that describes the shaded area.  SolutionArea.pdfFind a polynomial that describes the shaded area.  SolutionArea.pdf
Find a polynomial that describes the shaded area. SolutionArea.pdf
 
Explain the common interest logic and the economic logic of .pdf
Explain the common interest logic and the economic logic of .pdfExplain the common interest logic and the economic logic of .pdf
Explain the common interest logic and the economic logic of .pdf
 
Does privacy always guarantee integrity. Justify your answerSolu.pdf
Does privacy always guarantee integrity. Justify your answerSolu.pdfDoes privacy always guarantee integrity. Justify your answerSolu.pdf
Does privacy always guarantee integrity. Justify your answerSolu.pdf
 
Distinguish between the GAAP and IFRS standards for internal .pdf
Distinguish between the GAAP and IFRS standards for internal .pdfDistinguish between the GAAP and IFRS standards for internal .pdf
Distinguish between the GAAP and IFRS standards for internal .pdf
 
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdf
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdfDescribe how pH inhibit microbe growth.SolutionMost of the pat.pdf
Describe how pH inhibit microbe growth.SolutionMost of the pat.pdf
 
Write the C code to create a data structure named window that con.pdf
Write the C code to create a data structure named window that con.pdfWrite the C code to create a data structure named window that con.pdf
Write the C code to create a data structure named window that con.pdf
 
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdfWrite 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
Write 4 to 5 paragraphs aboutthe Dyn cyber attack that occurred in.pdf
 
Who are the people directly involved in the collective bargaining pr.pdf
Who are the people directly involved in the collective bargaining pr.pdfWho are the people directly involved in the collective bargaining pr.pdf
Who are the people directly involved in the collective bargaining pr.pdf
 
Contracts or agreements can sometimes create financial contingencies.pdf
Contracts or agreements can sometimes create financial contingencies.pdfContracts or agreements can sometimes create financial contingencies.pdf
Contracts or agreements can sometimes create financial contingencies.pdf
 
Which of the following is an equation of the line that passes through.pdf
Which of the following is an equation of the line that passes through.pdfWhich of the following is an equation of the line that passes through.pdf
Which of the following is an equation of the line that passes through.pdf
 
When more than one structural arrangement of elements is possible fo.pdf
When more than one structural arrangement of elements is possible fo.pdfWhen more than one structural arrangement of elements is possible fo.pdf
When more than one structural arrangement of elements is possible fo.pdf
 
Click to add title 2. Explain the concept of African Diaspora and dis.pdf
Click to add title 2. Explain the concept of African Diaspora and dis.pdfClick to add title 2. Explain the concept of African Diaspora and dis.pdf
Click to add title 2. Explain the concept of African Diaspora and dis.pdf
 
Based on the following data, what is the quick ratio, rounded to one.pdf
Based on the following data, what is the quick ratio, rounded to one.pdfBased on the following data, what is the quick ratio, rounded to one.pdf
Based on the following data, what is the quick ratio, rounded to one.pdf
 
ATP production during glycolysis;requires an enzymeoccurs in the.pdf
ATP production during glycolysis;requires an enzymeoccurs in the.pdfATP production during glycolysis;requires an enzymeoccurs in the.pdf
ATP production during glycolysis;requires an enzymeoccurs in the.pdf
 
What are symptoms of N, P, K, Mg, S and Fe deficiencies in plants.pdf
What are symptoms of N, P, K, Mg, S and Fe deficiencies in plants.pdfWhat are symptoms of N, P, K, Mg, S and Fe deficiencies in plants.pdf
What are symptoms of N, P, K, Mg, S and Fe deficiencies in plants.pdf
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 

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

  • 1. 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';
  • 2. // 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];
  • 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 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 :
  • 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 : 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);
  • 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 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
  • 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.