Reputation: 53
I am trying to create a program that lets a user select an item. When the item is selected, there will be a box with the total price cost. When i ran the program, i than clicked on an item, the result came out negative. It seems like my if and else statement is wrong, i just can not find out how to correctly fix it.
package javaapplication118;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class pizza extends JFrame {
JTextField textField, priceField;
JCheckBox pineApple, drink, delivery, meatballs;
JButton button;
pizza() {
super("Pizza order");
setLayout(new FlowLayout());
textField = new JTextField("hello please check anything you would like to order");
textField.setEditable(false);
textField.setBackground(Color.yellow);
add(textField);
EventHandler handler = new EventHandler();
pineApple = new JCheckBox("Pine apples");
pineApple.addItemListener(handler);
add(pineApple);
drink = new JCheckBox("drink");
drink.addItemListener(handler);
add(drink);
delivery = new JCheckBox("delivery");
delivery.addItemListener(handler);
add(delivery);
meatballs = new JCheckBox("meat balls");
meatballs.addItemListener(handler);
add(meatballs);
priceField = new JTextField("$", 15);
add(priceField);
button = new JButton("confirm order");
button.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, "Your order has been placed.");
}
});
add(button);
}
//JTextField textField priceField
// JCheckBox pineApple, drink, delivery, meatballs;
public class EventHandler implements ItemListener {
double price = 0;
public void itemStateChanged(ItemEvent event) {
if (pineApple.isSelected()) { // if pineapple is selected add to the price
price = price + 2.30;
} else if (!pineApple.isSelected()){ // if pineapple is deselected subtract from price
price = price - 2.30;
}
if (delivery.isSelected()) {
price = price + 5;
} else if (!delivery.isSelected()) {
price = price - 5;
}
if (drink.isSelected()) {
price = price + 1;
} else if (!drink.isSelected()) {
price = price - 1;
}
if (meatballs.isSelected()) {
price = price + 1;
} else if (!meatballs.isSelected()) {
price = price - 1;
}
priceField.setText("$" + price);
}
}
}
Upvotes: 0
Views: 975
Reputation: 167
Your code substracts value from the price. That is when you select "meat balls" it adds 1 to the price, now price is $1.00 Then as the other checkboxes are cleared, (you have coded so that it substracts relavent values if checkboxes are cleared)
$2.30 for pineapple, $5,00 for delivary, $1.00 for drink, (total $8.30) is "substracted" from price.
1 - 8.3 = -7.3
That is why you get negative value.
Upvotes: 1
Reputation: 347244
You seem to be "assuming" that one or more items have being selected in the past, but you have no means by which to know if this is true or not...
Instead, reset the price
to 0
and only add to the price
for those items that are actually currently selected...
public void itemStateChanged(ItemEvent event) {
price = 0;
if (pineApple.isSelected()) {
price = price + 2.30;
}
if (delivery.isSelected()) {
price = price + 5;
}
if (drink.isSelected()) {
price = price + 1;
}
if (meatballs.isSelected()) {
price = price + 1;
}
priceField.setText("$" + price);
}
Upvotes: 3