Part Two Download VendingChangejava above Run it and become
Part Two: Download VendingChange.java (above). Run it and become familiar with the output. Essentially, you need to create a user friendly GUI app using the code parameters of VendingChange. Turn VendingChange.java into a GUI app.
1) Your program must display the input dialog.
2) You must check that the data entered by the user follows the required input. If not, your program must display an error dialog and exit.
3) If the input is valid, then your program must display a message dialog similar to the one shown in listing 2.12, but the message must pertain to VendingChange and not ChangeMaker. Your message dialog must show the number of pennies, nickels, dimes and quarters.
4) Your program must exit when the user closes the output message dialog.
5) Comment and document your program.
Complete the programming problems such that they produce output that looks similar to that shown in the text or upcoming class presentations (in-class and Announcements). Tips and formulas (if any) will be discussed in class. Additional details will be provided in class and Announcements.
add comments to code please.
instruction number 2 is really important please
Solution
package chegg.vendingchanges;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class VendingChange extends JFrame {
public VendingChange() {
GridLayout gridLayout = new GridLayout(4, 1);
setTitle(\"Vending Changes \");
setSize(200, 200);
setLayout(gridLayout);
int centsInInt = -1; // garbage value.
String cents = JOptionPane
.showInputDialog(\"Enter price of item \ (from 25 cents to a dollar, in 5-cent increments)\");
try {
centsInInt = Integer.parseInt(cents);
if(centsInInt > 100){
JOptionPane
.showMessageDialog(
getContentPane(),
\"Entered price is not valid. \ Kindly enter price of item \ (from 25 cents to a dollar, in 5-cent increments)\");
}else if (centsInInt % 5 == 0) {
int change = 100 - centsInInt;
int quarters = change / 25;
change = change % 25;// remaining change after deducting
// quarters
int dimes = change / 10;
change = change % 10;// remaining change after deducting
// dimes, too
int nickels = change / 5;
change = change % 5;
int pennies = change;
// The grammar will be incorrect if any of the values are 1
// because the required program statements to handle that
// situation have not been covered, yet.
StringBuilder strBuild = new StringBuilder();
strBuild.append(
\"You bought an item for \" + centsInInt
+ \" and gave me a dollar,\").append(\"\ \")
.append(\"so your change is \ \")
.append(quarters + \" quarters,\").append(\"\ \")
.append(dimes + \" dimes, and\").append(\"\ \")
.append(nickels + \" nickel.\");
JOptionPane.showMessageDialog(getContentPane(),
strBuild.toString());
} else {
JOptionPane
.showMessageDialog(
getContentPane(),
\"Entered price is not valid. \ Kindly enter price of item \ (from 25 cents to a dollar, in 5-cent increments)\");
}
} catch (Exception e) {
System.out.println(\"not number\");
JOptionPane
.showMessageDialog(
getContentPane(),
\"Entered price is not valid. \ Kindly enter price of item \ (from 25 cents to a dollar, in 5-cent increments)\");
}
// set up event handler
setLocationRelativeTo(null);
setResizable(true);
setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new VendingChange();
}
}

