package pizzeria; import java.awt.BorderLayout; import java.awt.Frame; import ja
ID: 3692626 • Letter: P
Question
package pizzeria;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
*
* @author test
*/
public class Pizzeria extends Frame {
NumberFormat formatter = new DecimalFormat("#0.00");
// Constructor to setup the GUI components
public Pizzeria() {
JFrame frame = new JFrame();
frame.setTitle("Jack and Diane's On-line Pizzeria");
frame.setLayout(new BorderLayout(2, 3));
//Create the radio size buttons.
JRadioButton size1 = new JRadioButton("10 ($5.99)");
size1.setActionCommand(Double.toString(5.99));
size1.setSelected(true);
JRadioButton size2 = new JRadioButton("12 ($8.99)");
size2.setActionCommand(Double.toString(8.99));
JRadioButton size3 = new JRadioButton("14 ($11.99)");
size3.setActionCommand(Double.toString(11.99));
JRadioButton size4 = new JRadioButton("16 ($14.99)");
size4.setActionCommand(Double.toString(14.99));
//Group the radio buttons.
ButtonGroup size = new ButtonGroup();
size.add(size1);
size.add(size2);
size.add(size3);
size.add(size4);
//Put the radio buttons in a column in a panel.
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
//radioPanel.setBounds(10, 10, 10, 10);
//radioPanel.setToolTipText("Size");
radioPanel.add(size1);
radioPanel.add(size2);
radioPanel.add(size3);
radioPanel.add(size4);
//Radio Crust Type buttons
JRadioButton type1 = new JRadioButton("Hand-tossed");
type1.setActionCommand(Double.toString(0));
JRadioButton type2 = new JRadioButton("Thin-crust");
type2.setActionCommand(Double.toString(0));
JRadioButton type3 = new JRadioButton("Deep-dish (+$1)");
type3.setActionCommand(Double.toString(1));
type3.setSelected(true);
//Group the radio buttons.
ButtonGroup type = new ButtonGroup();
type.add(type1);
type.add(type2);
type.add(type3);
JPanel radioType = new JPanel(new GridLayout(0, 1));
radioType.add(type1);
radioType.add(type2);
radioType.add(type3);
//Check Box Toppings
JCheckBox topping1 = new JCheckBox("Pepperoni");
JCheckBox topping2 = new JCheckBox("Sausage");
JCheckBox topping3 = new JCheckBox("Onion");
JCheckBox topping4 = new JCheckBox("Mushroom");
JCheckBox topping5 = new JCheckBox("Anchovies");
//Group the radio buttons.
//ButtonGroup topping = new ButtonGroup();
List<JCheckBox> topping = new ArrayList<>();
topping.add(topping1);
topping.add(topping2);
topping.add(topping3);
topping.add(topping4);
topping.add(topping5);
JPanel checkTopping = new JPanel(new GridLayout(0, 1));
checkTopping.add(topping1);
checkTopping.add(topping2);
checkTopping.add(topping3);
checkTopping.add(topping4);
checkTopping.add(topping5);
//place order button
JButton placeOrder = new JButton("Place Order");
placeOrder.setSize(3, 3);
placeOrder.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String sizePrice = new String();
String typePrice = new String();
Double Price;
Double Tax;
Double Total;
int countTopping = 0;
for (Enumeration<AbstractButton> sizebuttons = size.getElements(); sizebuttons.hasMoreElements();) {
AbstractButton sizeSelect = sizebuttons.nextElement();
if (sizeSelect.isSelected()) {
sizePrice = sizeSelect.getActionCommand();
//JOptionPane.showMessageDialog(frame, sizePrice);
}
}
for (Enumeration<AbstractButton> typebuttons = type.getElements(); typebuttons.hasMoreElements();) {
AbstractButton typeSelect = typebuttons.nextElement();
if (typeSelect.isSelected()) {
typePrice = typeSelect.getActionCommand();
}
}
JCheckBox[] boxes = {topping1,topping2,topping3,topping4,topping5};
for(int i = 0; i < boxes.length; ++i){
if(boxes[i].isSelected()){
++countTopping;
}
}
Price = Double.parseDouble(sizePrice) + Double.parseDouble(typePrice) + countTopping * 1.25;
Tax = Price * 0.06;
Total = Price + (Price * 0.06);
JOptionPane.showMessageDialog(frame, "Price: " + "$" + Price + " " + "Tax: " + formatter.format(Tax) + " " + "Total: " + "$"+formatter.format(Total) );
// // display/center the jdialog when the button is pressed
// JDialog d = new JDialog(frame, "Message", true);
// d.setLocationRelativeTo(frame);
// d.setSize(300, 300);
// d.setVisible(true);
}
});
frame.add(radioPanel,BorderLayout.LINE_START );
frame.add(radioType, BorderLayout.CENTER);
frame.add(checkTopping, BorderLayout.LINE_END);
frame.add(placeOrder, BorderLayout.PAGE_END);
frame.setSize(500, 300);
frame.setVisible(true);
}
// Other methods
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Pizzeria();
}
}
What did i do wrong? the error was
Pizzeria.java:130: error: local variable size is accessed from within inner class; needs to be declared final
for (Enumeration<AbstractButton> sizebuttons = size.getElements(); sizebuttons.hasMoreElements();) {
^
Pizzeria.java:139: error: local variable type is accessed from within inner class; needs to be declared final
for (Enumeration<AbstractButton> typebuttons = type.getElements(); typebuttons.hasMoreElements();) {
^
Pizzeria.java:146: error: local variable topping1 is accessed from within inner class; needs to be declared final
JCheckBox[] boxes = {topping1,topping2,topping3,topping4,topping5};
^
Pizzeria.java:146: error: local variable topping2 is accessed from within inner class; needs to be declared final
JCheckBox[] boxes = {topping1,topping2,topping3,topping4,topping5};
^
Pizzeria.java:146: error: local variable topping3 is accessed from within inner class; needs to be declared final
JCheckBox[] boxes = {topping1,topping2,topping3,topping4,topping5};
^
Pizzeria.java:146: error: local variable topping4 is accessed from within inner class; needs to be declared final
JCheckBox[] boxes = {topping1,topping2,topping3,topping4,topping5};
^
Pizzeria.java:146: error: local variable topping5 is accessed from within inner class; needs to be declared final
JCheckBox[] boxes = {topping1,topping2,topping3,topping4,topping5};
^
Pizzeria.java:155: error: local variable frame is accessed from within inner class; needs to be declared final
JOptionPane.showMessageDialog(frame, "Price: " + "$" + Price + " " + "Tax: " + formatter.format(Tax) + " " + "Total: " + "$"+formatter.format(Total) );
^
8 errors
Explanation / Answer
Hello there,
Make them final. Or you can make those variable instance variable or global .
Reason: if two methods see the same local variable, Java wants you to swear you will not change it -final, in Java speak. Together with the absence of by-reference parameters, this rule ensures that locals are only assigned in the method that they belong to. Code is thus more readable.
The reason for the problem is that you're trying to use a local variable in an anonymous inner class(here constructor) , and since these classes make copies of these variables, if the variable is not marked final, there's a chance that the two frame variables, the local one and the copy inside the anonymous class's object, might hold different values.
DO you understand ? Below code is working fine.
======Code======
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
*
* @author test
*/
public class Pizzeria extends Frame {
NumberFormat formatter = new DecimalFormat("#0.00");
// Constructor to setup the GUI components
public Pizzeria() {
final JFrame frame = new JFrame();
frame.setTitle("Jack and Diane's On-line Pizzeria");
frame.setLayout(new BorderLayout(2, 3));
// Create the radio size buttons.
JRadioButton size1 = new JRadioButton("10 ($5.99)");
size1.setActionCommand(Double.toString(5.99));
size1.setSelected(true);
JRadioButton size2 = new JRadioButton("12 ($8.99)");
size2.setActionCommand(Double.toString(8.99));
JRadioButton size3 = new JRadioButton("14 ($11.99)");
size3.setActionCommand(Double.toString(11.99));
JRadioButton size4 = new JRadioButton("16 ($14.99)");
size4.setActionCommand(Double.toString(14.99));
// Group the radio buttons.
final ButtonGroup size = new ButtonGroup();
size.add(size1);
size.add(size2);
size.add(size3);
size.add(size4);
// Put the radio buttons in a column in a panel.
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
// radioPanel.setBounds(10, 10, 10, 10);
// radioPanel.setToolTipText("Size");
radioPanel.add(size1);
radioPanel.add(size2);
radioPanel.add(size3);
radioPanel.add(size4);
// Radio Crust Type buttons
JRadioButton type1 = new JRadioButton("Hand-tossed");
type1.setActionCommand(Double.toString(0));
JRadioButton type2 = new JRadioButton("Thin-crust");
type2.setActionCommand(Double.toString(0));
JRadioButton type3 = new JRadioButton("Deep-dish (+$1)");
type3.setActionCommand(Double.toString(1));
type3.setSelected(true);
// Group the radio buttons.
final ButtonGroup type = new ButtonGroup();
type.add(type1);
type.add(type2);
type.add(type3);
JPanel radioType = new JPanel(new GridLayout(0, 1));
radioType.add(type1);
radioType.add(type2);
radioType.add(type3);
// Check Box Toppings
final JCheckBox topping1 = new JCheckBox("Pepperoni");
final JCheckBox topping2 = new JCheckBox("Sausage");
final JCheckBox topping3 = new JCheckBox("Onion");
final JCheckBox topping4 = new JCheckBox("Mushroom");
final JCheckBox topping5 = new JCheckBox("Anchovies");
// Group the radio buttons.
// ButtonGroup topping = new ButtonGroup();
List<JCheckBox> topping = new ArrayList<>();
topping.add(topping1);
topping.add(topping2);
topping.add(topping3);
topping.add(topping4);
topping.add(topping5);
JPanel checkTopping = new JPanel(new GridLayout(0, 1));
checkTopping.add(topping1);
checkTopping.add(topping2);
checkTopping.add(topping3);
checkTopping.add(topping4);
checkTopping.add(topping5);
// place order button
JButton placeOrder = new JButton("Place Order");
placeOrder.setSize(3, 3);
placeOrder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sizePrice = new String();
String typePrice = new String();
Double Price;
Double Tax;
Double Total;
int countTopping = 0;
for (Enumeration<AbstractButton> sizebuttons = size.getElements(); sizebuttons.hasMoreElements();) {
AbstractButton sizeSelect = sizebuttons.nextElement();
if (sizeSelect.isSelected()) {
sizePrice = sizeSelect.getActionCommand();
// JOptionPane.showMessageDialog(frame, sizePrice);
}
}
for (Enumeration<AbstractButton> typebuttons = type.getElements(); typebuttons.hasMoreElements();) {
AbstractButton typeSelect = typebuttons.nextElement();
if (typeSelect.isSelected()) {
typePrice = typeSelect.getActionCommand();
}
}
JCheckBox[] boxes = { topping1, topping2, topping3, topping4, topping5 };
for (int i = 0; i < boxes.length; ++i) {
if (boxes[i].isSelected()) {
++countTopping;
}
}
Price = Double.parseDouble(sizePrice) + Double.parseDouble(typePrice) + countTopping * 1.25;
Tax = Price * 0.06;
Total = Price + (Price * 0.06);
JOptionPane.showMessageDialog(frame, "Price: " + "$" + Price + " " + "Tax: " + formatter.format(Tax) + " " + "Total: "
+ "$" + formatter.format(Total));
// // display/center the jdialog when the button is pressed
// JDialog d = new JDialog(frame, "Message", true);
// d.setLocationRelativeTo(frame);
// d.setSize(300, 300);
// d.setVisible(true);
}
});
frame.add(radioPanel, BorderLayout.LINE_START);
frame.add(radioType, BorderLayout.CENTER);
frame.add(checkTopping, BorderLayout.LINE_END);
frame.add(placeOrder, BorderLayout.PAGE_END);
frame.setSize(500, 300);
frame.setVisible(true);
}
// Other methods
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Pizzeria();
}
}
This works fine.
Let me know if you have any doubts.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.