In: Computer Science
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.
import java.util.Scanner; public class VendingChange { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter price of item"); System.out.println ("(from 25 cents to a dollar, in 5-cent increments)"); int originalAmount = keyboard.nextInt(); int change = 100 - originalAmount; 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. System.out.println ("You bought an item for " + originalAmount + " and gave me a dollar,"); System.out.println("so your change is"); System.out.println(quarters + " quarters,"); System.out.println(dimes + " dimes, and"); System.out.println(nickels + " nickel."); } }
O U T P U T
Message Box Result window:
J A V A C O D E
//Package declared is vendChange
package vendChange;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class VendingChangeGUI extends JFrame {
private JPanel contentPane;
private JTextField textFieldPrice;
/**
* Launch the application.
*/
public static void main(String[] args) {
//Creating jFrame for GUI
EventQueue.invokeLater(new
Runnable() {
public void
run() {
try {
VendingChangeGUI frame = new
VendingChangeGUI();
//Setting maximize to
false
frame.setResizable(false);
//setting location of app. to
mid position
frame.setLocationRelativeTo(null);
//Setting look and feel(GUI)
to current system look n feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
//declaring the default constructor
public VendingChangeGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 376,
195);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.inactiveCaptionBorder);
contentPane.setBorder(new
EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//Setting welcome label
JLabel lblNewLabel = new
JLabel("Welcome to Vending Change GUI App.");
lblNewLabel.setFont(new
Font("Stencil", Font.PLAIN, 15));
lblNewLabel.setBounds(39, 11, 321,
25);
contentPane.add(lblNewLabel);
//setting Price label
JLabel lblNewLabel_1 = new
JLabel("Enter Price of item:");
lblNewLabel_1.setBackground(SystemColor.controlShadow);
lblNewLabel_1.setFont(new
Font("Tahoma", Font.PLAIN, 13));
lblNewLabel_1.setBounds(64, 47,
128, 25);
contentPane.add(lblNewLabel_1);
//Creating textfield to enter
values
textFieldPrice = new
JTextField();
textFieldPrice.setBounds(212, 47,
64, 23);
contentPane.add(textFieldPrice);
textFieldPrice.setColumns(10);
//textPane to display direction
under price lable
JTextPane txtpnfromCents = new
JTextPane();
txtpnfromCents.setFont(new
Font("Tahoma", Font.ITALIC, 11));
txtpnfromCents.setBackground(SystemColor.control);
txtpnfromCents.setEditable(false);
txtpnfromCents.setText("(from 25
cents to a dollar, in 5-cent increments)");
txtpnfromCents.setBounds(64, 72,
140, 39);
contentPane.add(txtpnfromCents);
//Creating buy button
JButton btnNewButton = new
JButton("Buy");
//Setting action listener to buy
button
btnNewButton.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent arg0) {
int originalAmount=0;
//parsing the value of textbox Price
try
{
originalAmount =
Integer.parseInt(textFieldPrice.getText());
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Please provide a valid
input","Error",JOptionPane.WARNING_MESSAGE);
dispose();
}
//Calculating change
int change = 100 -
originalAmount;
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;
//Displaying result to message
box
JFrame frame = new
JFrame("Message");
JOptionPane.showMessageDialog(frame,
"You bought an item for "+ originalAmount + "
and gave me a dollar, "
+ "so your change is "+quarters + " quarters,
"+dimes + " dimes, and "+nickels + " nickel.");
dispose();
}
});
//adding buy button to content
pane
btnNewButton.setBounds(212, 81, 64,
23);
contentPane.add(btnNewButton);
}
}