How can i make my code be able to catch exceptions and display a incorrect input
ID: 3714369 • Letter: H
Question
How can i make my code be able to catch exceptions and display a incorrect input message if user inputs words. sample- incorrect input insert 50.0 cents for candy
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
public class CandyMachine
extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
//set constants for cost of items
//Note : values are not given in question
//These are assumed values .Can be change as per requirement
private static double CANDY_COST=50;
private static double CHIP_COST=40;
private static double GUM_COST=30;
private static double COOKIES_COST=20;
private JLabel headerTop=new JLabel("WELCOME TO SHELLY'S CANDY SHOP"
,SwingConstants.CENTER);
private JLabel headerBottom=new JLabel("To Make a Selection,Check the Product Price"
,SwingConstants.CENTER);
private JButton candyBtn;
private JButton chipsBtn;
private JButton gumBtn;
private JButton cookiesBtn;
private JButton exitBtn;
//constructor to intialize the frame
public CandyMachine()
{
setTitle("Candy Machine");
setSize(350, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(7, 1));
add(headerTop);
add(headerBottom);
//create a button and add action lister for candy button
candyBtn=new JButton("Candy");
candyBtn.addActionListener(this);
add(candyBtn);
//create a button and add action lister for chips button
chipsBtn=new JButton("Chips");
chipsBtn.addActionListener(this);
add(chipsBtn);
//create a button and add action lister for genms button
gumBtn=new JButton("Gum");
gumBtn.addActionListener(this);
add(gumBtn);
//create a button and add action lister for chocolate button
cookiesBtn=new JButton("Cookies");
cookiesBtn.addActionListener(this);
add(cookiesBtn);
//create a button and add action lister for exit button
exitBtn=new JButton("Exit");
exitBtn.addActionListener(this);
add(exitBtn);
//set visibility true
setVisible(true);
}
public static void main(String[] args)
{
//instantiate the class
new CandyMachine();
}
/**Override the actionPerformed method that handles
* the five buttons*/
public void actionPerformed(ActionEvent ae) {
String item=ae.getActionCommand();
Object obj=(JButton)ae.getSource();
double cost=0;
double change=0;
DecimalFormat df=new DecimalFormat("##.00");
//check if button is candy
if(obj==candyBtn)
{
do
{
cost=Double.parseDouble(JOptionPane.showInputDialog(null,
"To buy "+item+" please insert "+CANDY_COST+" cents",
"Input",
JOptionPane.QUESTION_MESSAGE));
if(cost<CANDY_COST)
JOptionPane.showMessageDialog(null, "Not sufficient Amount");
}while(cost<CANDY_COST);
change=cost-CANDY_COST;
if(change!=0)
JOptionPane.showMessageDialog(null,
"Change : "+df.format(change)+" Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
}
//check if button is chips
else if(obj==chipsBtn)
{
do
{
cost=Double.parseDouble(JOptionPane.showInputDialog(null,
"To buy "+item+" please insert "+CHIP_COST+" cents",
"Input",
JOptionPane.QUESTION_MESSAGE));
if(cost<CHIP_COST)
JOptionPane.showMessageDialog(null, "Not sufficient Amount");
}while(cost<CHIP_COST);
change=cost-CHIP_COST;
if(change!=0)
JOptionPane.showMessageDialog(null,
"Change : "+df.format(change)+" Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
}
//check if button is gum
else if(obj==gumBtn)
{
do
{
cost=Double.parseDouble(JOptionPane.showInputDialog(null,
"To buy "+item+" please insert "+GUM_COST+" cents",
"Input",
JOptionPane.QUESTION_MESSAGE));
if(cost<GUM_COST)
JOptionPane.showMessageDialog(null, "Not sufficient Amount");
}while(cost<GUM_COST);
change=cost-GUM_COST;
if(change!=0)
JOptionPane.showMessageDialog(null,
"Change : "+df.format(change)+" Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
}
//check if button is cookies
else if(obj==cookiesBtn)
{
do
{
cost=Double.parseDouble(JOptionPane.showInputDialog(null,
"To buy "+item+" please insert "+COOKIES_COST+" cents",
"Input",
JOptionPane.QUESTION_MESSAGE));
if(cost<COOKIES_COST)
JOptionPane.showMessageDialog(null, "Not sufficient Amount");
}while(cost<COOKIES_COST);
change=cost-COOKIES_COST;
if(change!=0)
JOptionPane.showMessageDialog(null,
"Change : "+df.format(change)
+" Please pick up your "
+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
}
//check if button is exit
else if(obj==exitBtn)
{
JOptionPane.showMessageDialog(null, "Thank you.Come again!");
System.exit(0);
}
}
}
Explanation / Answer
I have modified the code to catch the exceptions when either a blank value is submitted or a non number value is entered as asked in the question.The code will throw an error . However, since the question doesnt talk about prompting the user to enter again after an invalid entry message is thrown , the same has not been implemented. Also, that would also require the question to state as to how many tries is allowed before the user is exited from buying .
Modified Code:
package com.chegg.question6;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
public class CandyMachine
extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
//set constants for cost of items
//Note : values are not given in question
//These are assumed values .Can be change as per requirement
private static double CANDY_COST=50;
private static double CHIP_COST=40;
private static double GUM_COST=30;
private static double COOKIES_COST=20;
private JLabel headerTop=new JLabel("WELCOME TO SHELLY'S CANDY SHOP"
,SwingConstants.CENTER);
private JLabel headerBottom=new JLabel("To Make a Selection,Check the Product Price"
,SwingConstants.CENTER);
private JButton candyBtn;
private JButton chipsBtn;
private JButton gumBtn;
private JButton cookiesBtn;
private JButton exitBtn;
//constructor to intialize the frame
public CandyMachine()
{
setTitle("Candy Machine");
setSize(350, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(7, 1));
add(headerTop);
add(headerBottom);
//create a button and add action lister for candy button
candyBtn=new JButton("Candy");
candyBtn.addActionListener(this);
add(candyBtn);
//create a button and add action lister for chips button
chipsBtn=new JButton("Chips");
chipsBtn.addActionListener(this);
add(chipsBtn);
//create a button and add action lister for genms button
gumBtn=new JButton("Gum");
gumBtn.addActionListener(this);
add(gumBtn);
//create a button and add action lister for chocolate button
cookiesBtn=new JButton("Cookies");
cookiesBtn.addActionListener(this);
add(cookiesBtn);
//create a button and add action lister for exit button
exitBtn=new JButton("Exit");
exitBtn.addActionListener(this);
add(exitBtn);
//set visibility true
setVisible(true);
}
public static void main(String[] args)
{
//instantiate the class
new CandyMachine();
}
/**Override the actionPerformed method that handles
* the five buttons*/
public void actionPerformed(ActionEvent ae) {
String item=ae.getActionCommand();
Object obj=(JButton)ae.getSource();
double cost=0;
double change=0;
DecimalFormat df=new DecimalFormat("##.00");
//check if button is candy
if(obj==candyBtn)
{
try {
do
{
cost=Double.parseDouble(JOptionPane.showInputDialog(null,
"To buy "+item+" please insert "+CANDY_COST+" cents",
"Input",
JOptionPane.QUESTION_MESSAGE));
if(cost<CANDY_COST)
JOptionPane.showMessageDialog(null, "Not sufficient Amount");
}while(cost<CANDY_COST);
}
catch (NumberFormatException ne){
JOptionPane.showMessageDialog(null, "Incorrect input. Insert "+ CANDY_COST+ " cents for candy");
}
if(cost !=0.0){ // This "If" clause is to ensure change message doesnt come up after an error because if an error occurs cost will be 0.0
change=cost-CANDY_COST;
if(change!=0)
JOptionPane.showMessageDialog(null,
"Change : "+df.format(change)+" Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
}
}
//check if button is chips
else if(obj==chipsBtn)
{
try {
do
{
cost=Double.parseDouble(JOptionPane.showInputDialog(null,
"To buy "+item+" please insert "+CHIP_COST+" cents",
"Input",
JOptionPane.QUESTION_MESSAGE));
if(cost<CHIP_COST)
JOptionPane.showMessageDialog(null, "Not sufficient Amount");
}while(cost<CHIP_COST);
}
catch (NumberFormatException ne){
JOptionPane.showMessageDialog(null, "Incorrect input. Insert "+ CHIP_COST+ " cents for CHIP");
}
if(cost !=0.0){ // This "If" clause is to ensure change message doesnt come up after an error because if an error occurs cost will be 0.0
change=cost-CHIP_COST;
if(change!=0)
JOptionPane.showMessageDialog(null,
"Change : "+df.format(change)+" Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
}
}
//check if button is gum
else if(obj==gumBtn)
{
try{
do
{
cost=Double.parseDouble(JOptionPane.showInputDialog(null,
"To buy "+item+" please insert "+GUM_COST+" cents",
"Input",
JOptionPane.QUESTION_MESSAGE));
if(cost<GUM_COST)
JOptionPane.showMessageDialog(null, "Not sufficient Amount");
}while(cost<GUM_COST);
}
catch (NumberFormatException ne)
{
JOptionPane.showMessageDialog(null, "Incorrect input. Insert "+ GUM_COST+ " cents for GUM");
}
if(cost !=0.0){ // This "If" clause is to ensure change message doesnt come up after an error because if an error occurs cost will be 0.0
change=cost-GUM_COST;
if(change!=0)
JOptionPane.showMessageDialog(null,
"Change : "+df.format(change)+" Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
}
}
//check if button is cookies
else if(obj==cookiesBtn)
{
try{
do
{
cost=Double.parseDouble(JOptionPane.showInputDialog(null,
"To buy "+item+" please insert "+COOKIES_COST+" cents",
"Input",
JOptionPane.QUESTION_MESSAGE));
if(cost<COOKIES_COST)
JOptionPane.showMessageDialog(null, "Not sufficient Amount");
}while(cost<COOKIES_COST);
}
catch (NumberFormatException ne)
{
JOptionPane.showMessageDialog(null, "Incorrect input. Insert "+ COOKIES_COST+ " cents for COOKIES");
}
if(cost !=0.0){ // This "If" clause is to ensure change message doesnt come up after an error because if an error occurs cost will be 0.0
change=cost-COOKIES_COST;
if(change!=0)
JOptionPane.showMessageDialog(null,
"Change : "+df.format(change)
+" Please pick up your "
+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null,
"Please pick up your "+item+" and enjoy",
"Thank you.Come again!",JOptionPane.PLAIN_MESSAGE);
}
}
//check if button is exit
else if(obj==exitBtn)
{
JOptionPane.showMessageDialog(null, "Thank you.Come again!");
System.exit(0);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.