I'm writing a program for a project in my software tech class, and it's meant to act as a menu for specials at a cafe. When you click on one special, a confirmation dialog pops up, asking whether you want it or not, and if you do, it'll add it to your total. I can't even compile it, and I can't figure out why. Bear in mind, I'm brand new to Java, so if possible, please explain things clearly.
I get these errors when I try to compile it:
ModernPapyrusCafeOrdering.java:27: error: local variable order is accessed from within inner class; needs to be declared final
order += 10.99;
^
ModernPapyrusCafeOrdering.java:19: error: cannot find symbol
button3.addActionListener(new ActionListener() {
^
symbol: variable button3
location: class ModernPapyrusCafeOrdering
ModernPapyrusCafeOrdering.java:40: error: local variable order is accessed from within inner class; needs to be declared final
order += 8.99;
^
ModernPapyrusCafeOrdering.java:33: error: cannot find symbol
button3.addActionListener(new ActionListener() {
^
symbol: variable button3
location: class ModernPapyrusCafeOrdering
ModernPapyrusCafeOrdering.java:53: error: local variable order is accessed from within inner class; needs to be declared final
order += 9.99;
^
5 errors
Here is my code:
import java.awt.Component; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class ModernPapyrusCafeOrdering extends JFrame { public ModernPapyrusCafeOrdering() throws HeadlessException { setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); double order = 0; JButton button1 = new JButton("$10.99 The Big Easy"); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int result = JOptionPane.showConfirmDialog((Component) e.getSource(), "Sandwich\n" +"Chips\n" +"Drink\n" +"BONUS WITH MEAL: Book of your choice for 50% off!"); if (result == JOptionPane.YES_OPTION) { order += 10.99; } } }); JButton button2 = new JButton("$8.99 The Sophisticated Ensemble"); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int result = JOptionPane.showConfirmDialog((Component) e.getSource(), "Coffee or Tea\n" +"Biscotti or Pastry\n" +"BONUS WITH MEAL: Book of your choice for 20% off!"); if (result == JOptionPane.YES_OPTION) { order += 8.99; } } }); JButton button3 = new JButton("$9.99 The Delectable"); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int result = JOptionPane.showConfirmDialog((Component) e.getSource(), "Milkshake or Smoothie\n" +"Donut or Slice of Cake\n" +"BONUS WITH MEAL: Book of your choice for 20% off!"); if (result == JOptionPane.YES_OPTION) { order += 9.99; } } }); setLayout(new FlowLayout(FlowLayout.CENTER)); getContentPane().add(button1); getContentPane().add(button2); getContentPane().add(button3); } public static void main(String[] args) { new ModernPapyrusCafeOrdering().setVisible(true); } }