-2

I am newer to Java, so I am still trying to understand how to update variables properly. I am creating a program that acts as a restaurant menu. The listed menu items will appear on the left side, and once the user checks an item and clicks the right button, the item will appear on the right side, and the price of the item is supposed to be added to the total variable; however, this is not working. There is some unnecessary code, but please ignore these and some of the comments if you can. Thank you.

package finalproject;

import java.awt.event.*;  
import java.awt.*;
import javax.swing.*;

public class MenuFrame extends JFrame implements ActionListener {

final double BURGER_PRICE = 6.24;
final double FISH_PRICE = 13.78;
final double SOUP_PRICE = 4.49;
final double CHICKEN_PRICE = 11.36;
final double SALAD_PRICE = 5.60;
final double FRIES_PRICE = 3.24;
JPanel menuPanel = new JPanel();                       //Panel to place the menu in, which will be on left side
JPanel buttonPanel = new JPanel();                     //Panel to place buttons in, which will be in center
JPanel orderPanel = new JPanel();                      //Panel to place order menu in, which will be on right side
JLabel menuLabel = new JLabel("Menu");                 //Label for the menu
JLabel orderLabel = new JLabel("Your Order");          //Label for the user's order
JCheckBox burger = new JCheckBox("Burger: $6.24", false);     //Burger check box
JCheckBox fish = new JCheckBox("Fish: $13.78", false);         //Fish check box
JCheckBox soup = new JCheckBox("Soup: $4.49", false);         //Soup check box
JCheckBox chicken = new JCheckBox("Chicken: $11.36", false);   //Chicken check box
JCheckBox salad = new JCheckBox("Salad: $5.60", false);       //Salad check box
JCheckBox fries = new JCheckBox("Fries: $3.24", false);       //Fries check box
JButton rightButton = new JButton("->");               //Button to move items from left to right
JButton leftButton = new JButton("<-");                //Button to move items from right to left
JLabel skipPosition = new JLabel("");                  //Label used to skip position in GridLayout
static double totalPrice = 0;                                        //Total price of order
Font titleFont = new Font("Times New Roman",Font.BOLD,20);
JLabel totalLabel = new JLabel("");  //total label

public MenuFrame() {

    setLayout(new GridLayout(0,3));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1055, 275);                                //Set size of Menu Frame

    menuPanel.setLayout(new GridLayout(0,3));         //Setting Menu's layout to GridLayout
    //Box buttonBox = Box.createVerticalBox();       //Creating box object for button panel
    buttonPanel.setLayout(new GridLayout(8,3));
    orderPanel.setLayout(new GridLayout(0,3));            //Setting Order's layout to FlowLayout

    menuLabel.setFont(titleFont);
    orderLabel.setFont(titleFont);

    //Adding menu items to Menu
    menuPanel.add(new JLabel(""));
    menuPanel.add(menuLabel);
    menuPanel.add(new JLabel(""));
    menuPanel.add(burger);
    menuPanel.add(fish);
    menuPanel.add(soup);
    menuPanel.add(chicken);
    menuPanel.add(salad);
    menuPanel.add(fries);

    //Adding buttons to Box
    //buttonBox.add(rightButton);
    //buttonBox.add(Box.createVerticalStrut(100));
    //buttonBox.add(leftButton);

    //Adding box to button panel
    //buttonPanel.add(buttonBox);

    //Adding "Your Order" label to Order. Items will be subsequently added or removed
    orderPanel.add(new JLabel(""));
    orderPanel.add(orderLabel);
    orderPanel.add(new JLabel(""));

    totalLabel.setText("$" + totalPrice);

    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(rightButton);
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(leftButton);
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(new JLabel(""));
    buttonPanel.add(totalLabel);

    //Adding action listener to rightButton and leftButton
    rightButton.addActionListener(this);
    leftButton.addActionListener(this);

    add(menuPanel);
    add(buttonPanel);
    add(orderPanel);

}


@Override
public void actionPerformed(ActionEvent e) {

    Object source = e.getSource();

    //If right button was clicked
    if(source == rightButton) {

        //if burger was selected and is in the menu panel
        if(burger.isSelected() && burger.getParent() == menuPanel) {

            //Move burger from menu to order, deselect burger, and add 6.24 to total price
            menuPanel.remove(burger);
            orderPanel.add(burger);
            burger.setSelected(false);
            totalPrice += BURGER_PRICE;

        }

        //if fish was selected and is in the menu panel
        if(fish.isSelected() && fish.getParent() == menuPanel) {

            //Move fish from menu to order, deselect fish, and add 13.78 to total price
            menuPanel.remove(fish);
            orderPanel.add(fish);
            fish.setSelected(false);
            totalPrice += FISH_PRICE;

        }

        //if soup was selected and is in the menu panel
        if(soup.isSelected() && soup.getParent() == menuPanel) {

            //Move soup from menu to order, deselect soup, and add 4.49 to total price
            menuPanel.remove(soup);
            orderPanel.add(soup);
            soup.setSelected(false);
            totalPrice += SOUP_PRICE;

        }

        //if chicken was selected and is in the menu panel
        if(chicken.isSelected() && chicken.getParent() == menuPanel) {

            //Move chicken from menu to order, deselect chicken, and add 11.36 to total price
            menuPanel.remove(chicken);
            orderPanel.add(chicken);
            chicken.setSelected(false);
            totalPrice += CHICKEN_PRICE;

        }

        //if salad was selected and is in the menu panel
        if(salad.isSelected() && salad.getParent() == menuPanel) {

            //Move salad from menu to order, deselect salad, and add 5.60 to total price
            menuPanel.remove(salad);
            orderPanel.add(salad);
            salad.setSelected(false);
            totalPrice += SALAD_PRICE;

        }

        //if fries was selected and is in the menu panel
        if(fries.isSelected() && fries.getParent() == menuPanel) {

            //Move fries from menu to order, deselect fries, and add 3.24 to total price
            menuPanel.remove(fries);
            orderPanel.add(fries);
            fries.setSelected(false);
            totalPrice += FRIES_PRICE;

        }

    }

    //if left button was clicked
    if(source == leftButton) {

        //if burger was selected and is in the order panel
        if(burger.isSelected() && burger.getParent() == orderPanel) {

            //Move burger from order to menu, deselect burger, and remove 6.24 from total price
            orderPanel.remove(burger);
            menuPanel.add(burger);
            burger.setSelected(false);
            totalPrice -= BURGER_PRICE;

        }

        //if fish was selected and is in the order panel
        if(fish.isSelected() && fish.getParent() == orderPanel) {

            //Move fish from order to menu, deselect fish, and remove 13.78 from total price
            orderPanel.remove(fish);
            menuPanel.add(fish);
            fish.setSelected(false);
            totalPrice -= FISH_PRICE;

        }

        //if soup was selected and is in the order panel
        if(soup.isSelected() && soup.getParent() == orderPanel) {

            //Move soup from order to menu, deselect soup, and remove 4.49 from total price
            orderPanel.remove(soup);
            menuPanel.add(soup);
            soup.setSelected(false);
            totalPrice -= SOUP_PRICE;

        }

        //if chicken was selected and is in the order panel
        if(chicken.isSelected() && chicken.getParent() == orderPanel) {

            //Move chicken from order to menu, deselect chicken, and remove 11.35 from total price
            orderPanel.remove(chicken);
            menuPanel.add(chicken);
            chicken.setSelected(false);
            totalPrice -= CHICKEN_PRICE;

        }

        //if salad was selected and is in the order panel
        if(salad.isSelected() && salad.getParent() == orderPanel) {

            //Move salad from order to menu, deselect salad, and remove 5.60 from total price
            orderPanel.remove(salad);
            menuPanel.add(salad);
            salad.setSelected(false);
            totalPrice -= SALAD_PRICE;

        }

        //if fries was selected and is in the order panel
        if(fries.isSelected() && fries.getParent() == orderPanel) {

            //Move fries from order to menu, deselect fries, and remove 3.24 from total price
            orderPanel.remove(fries);
            menuPanel.add(fries);
            fries.setSelected(false);
            totalPrice -= FRIES_PRICE;

        }

    }



    //Make changes appear
    repaint();            
    revalidate();        

}


public static void main(String[] args) {

    //Instantiating MenuFrame object
    MenuFrame menuFrame = new MenuFrame();


    //Making frame visible
    menuFrame.setVisible(true);

}
CMoney
  • 3
  • 3
  • 1
    Tell us what is actually not working? Not printing? variable cannot be set? more detail please – Luminous_Dev Nov 24 '16 at 19:55
  • 2
    1. If you're new to Java, you probably shouldn't be trying to write a GUI. 2. You really need to limit the amount of code here so only relevant code is included. – Carcigenicate Nov 24 '16 at 19:58
  • 1
    There is way too much code here, and it involves asynchronous stuff and listeners and things that really add complexity. Please start by putting `System.out.println` in some key places, to make sure you're getting to the code that's supposed to add to the total. So if you think a burger should be selected, put a `println` inside the `if` that's supposed to add. If you don't get there, then try other `System.out.printlns` to see if `source==rightbutton` is true, if `actionPerformed` is getting called at all, etc. – ajb Nov 24 '16 at 19:59
  • Basically, there are just too many ways for things to go wrong. You'll need to do some debugging on your own. – ajb Nov 24 '16 at 20:00
  • One obvious strange thing: all your fields are non-static; besides your total value. That works, but simply doesnt make any sense. I complete agree to the other comments: while your skills are on such a basic level, please please please do not go for such complex challenges. Start small, understand and **master** all the basics; and then go for UI stuff. Writing down hundreds of lines of complex code ... that gets too much for **you** to understand ... is not something that you should throw at other people ... – GhostCat Nov 24 '16 at 20:03
  • Thank you very much for your answers. I appreciate it and they were extremely helpful – CMoney Nov 24 '16 at 23:34

2 Answers2

0

totalPrice is not going to be set into the textField if you don't assign it. Put this before refreshing:

totalLabel.setText("$" + totalPrice);
Fernando
  • 396
  • 1
  • 5
0

Variable is updated correct, its just that you have not re-set your totalLabel text.

    if(burger.isSelected() && burger.getParent() == menuPanel) {

        //Move burger from menu to order, deselect burger, and add 6.24 to total price
        menuPanel.remove(burger);
        orderPanel.add(burger);
        burger.setSelected(false);
        totalPrice += BURGER_PRICE;
        totalLabel.setText("$" + totalPrice);  <<<<<< Add this to all of items
    }
Luminous_Dev
  • 614
  • 6
  • 14