Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Issues with my ordering program - if statement issues?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issues with my ordering program - if statement issues?

    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);
      }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Issues with my ordering program - if statement issues?

    Either make button3 a field of the class, or declare and initialize it in the constructor *before* you try to use it.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with my ordering program - if statement issues?

    I'm sorry, I'm really new to Java. What exactly do you mean by a field of the class? As in, put it in curly braces?

    Where is the constructor part?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Issues with my ordering program - if statement issues?

    Quote Originally Posted by Shenaniganizer View Post
    I'm sorry, I'm really new to Java. What exactly do you mean by a field of the class? As in, put it in curly braces?
    A field is a variable that is declared inside of the class, often at the top of the class, and not inside any constructor, method or other block.


    Where is the constructor part?
    Run, don't walk, to a constructor tutorial (click on link) to read up on what a constructor is. Please you must understand this backwards and forwards, and better than any of us can explain if you're going to progress.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with my ordering program - if statement issues?

    I think I can understand some of that. But, I'm just trying to figure out why the double order isn't working. I need to have the order variable there as 0, that way if the user selects one of the menu items, it'll add the price to order. Then, they can proceed to get tax and what-not, which I'll add later. Could you explain that to me?

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Issues with my ordering program - if statement issues?

    Wait, it's looking as if we're discussing a different problem here. The main focus of this question was on why your code was not compiling. Let's clarify this issue first. Is your code now compiling? Have you been able to fix this? Because if not, all other issues are still moot. If you have fixed this issue, then please post so in a reply in this thread, and then perhaps start a new thread on this site for your new and completely different question.

Similar Threads

  1. Issues with IF else Statement
    By YogiB1810 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2012, 11:49 AM
  2. [SOLVED] Using recursion issues for basic program
    By ash12 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 5th, 2012, 10:40 PM
  3. Cookie Jar Program Issues
    By NoranPrease in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 16th, 2012, 02:04 PM
  4. [SOLVED] Having Issues Wish this Program
    By userct in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 9th, 2012, 03:46 PM
  5. Issues with ascending number program
    By newtojava2011 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 30th, 2011, 06:23 PM