Kyle
Kyle

Reputation: 33

How to get info from multiple JOptionPane and put it in a summary dialogbox

I am writing a code for a Subway ordering program. I have the GUI laid out but am unsure of how I should get and set all the buttons in to the order summary. The summary needs to show name, date, time, sub ingredients, subtotal, possible 2nd order ingredients, 2nd sub total, and a grand order total. I understand GUI but when it comes to arrays and lists I'm not so sure and I need to include an arraylist to store the orders.

Will I need to create array classes for veggies, cheese, and condiments like I did for Sandwich and Bread?

Any advice would be great. thanks!

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;


 public class SubwayForm extends JFrame
{
    public static List<Sandwich> orderedSandwiches;
    private List<Sandwich> sandwiches;
    public static List<Bread> breads;
    public static List<Bread> orderedBreads;


    public SubwayForm() {

            setTitle("Subway");
            setSize(250,250);
            setLayout(null);
            setLocationRelativeTo(null);
            CreateSandwiches();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);

            //FRAME

            JFrame frame = new JFrame();

            //NAME

            String name = (String) JOptionPane.showInputDialog(frame,
                            "Enter your name", "SUBWAY", JOptionPane.QUESTION_MESSAGE,
                            null, null, null);

            //SIZE

      JPanel size = new JPanel(new GridLayout(2,1));
      JRadioButton jrbInch = new JRadioButton("6 inch");
      JRadioButton jrbFoot = new JRadioButton("12 inch");
      size.add(jrbInch);
      size.add(jrbFoot);

      JOptionPane.showOptionDialog(frame, size, "Sub Size", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                                            null, null);

            //BREAD

            JPanel bread = new JPanel(new GridLayout(6,1));
            JRadioButton jrbWheat = new JRadioButton("9-Grain Wheat");
            JRadioButton jrbHoney = new JRadioButton("Honey Oat");
            JRadioButton jrbItalian = new JRadioButton("Italian");
            JRadioButton jrbHerbs = new JRadioButton("Italian Herbs & Cheese");
            JRadioButton jrbMontChed = new JRadioButton("Monterey Cheddar");
            JRadioButton jrbFlat = new JRadioButton("Flatbread");
            bread.add(jrbWheat);
            bread.add(jrbHoney);
            bread.add(jrbItalian);
            bread.add(jrbHerbs);
            bread.add(jrbMontChed);
            bread.add(jrbFlat);

            JOptionPane.showOptionDialog(frame, bread, "Choose your bread", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                                            null, null);


            //TYPE OF SUB

            JPanel type1 = new JPanel(new GridLayout(6,1));
            JRadioButton jrbChix = new JRadioButton("Oven Roasted Chicken - $3.00");
            JRadioButton jrbMeatball = new JRadioButton("Meatball Marinara - $3.50");
            JRadioButton jrbHam = new JRadioButton("Blackforest Ham - $2.25");
            JRadioButton jrbBLT = new JRadioButton("BLT - $4.00");
            JRadioButton jrbCold = new JRadioButton("Cold Cut - $3.00");
            JRadioButton jrbVeggie = new JRadioButton("Veggie Delight - $4.00");
            type1.add(jrbChix);
            type1.add(jrbMeatball);
            type1.add(jrbHam);
            type1.add(jrbBLT);
            type1.add(jrbCold);
            type1.add(jrbVeggie);

            JOptionPane.showOptionDialog(frame, type1, "Choose your Sub Type", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                                            null, null);

            //CHEESE

            JPanel cheese = new JPanel(new GridLayout(6,1));
            JRadioButton jrbSwiss = new JRadioButton("Swiss");
            JRadioButton jrbProv = new JRadioButton("Provologne");
            JRadioButton jrbCheddar = new JRadioButton("Cheddar");
            JRadioButton jrbPepper = new JRadioButton("Pepperjack");
            JCheckBox jcbExcheese = new JCheckBox("Extra Cheese - $1.00");
            cheese.add(jrbSwiss);
            cheese.add(jrbProv);
            cheese.add(jrbCheddar);
            cheese.add(jrbPepper);
            cheese.add(jcbExcheese);

            JOptionPane.showOptionDialog(frame, cheese, "Choose your cheese", JOptionPane.OK_CANCEL_OPTION,  JOptionPane.QUESTION_MESSAGE, null, null, null);

            //TOASTED

            JPanel toasted = new JPanel(new GridLayout(2,1));
            JRadioButton jrbToasted = new JRadioButton("Yes");
            JRadioButton jrbNottoasted = new JRadioButton("No");
            toasted.add(jrbToasted);
            toasted.add(jrbNottoasted);

            JOptionPane.showOptionDialog(frame, toasted, "Would you like it toasted?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                                            null, null);

            //VEGGIES

            JPanel veg = new JPanel(new GridLayout(8,1));

            JCheckBox jcbLettuce = new JCheckBox("Lettuce");
            JCheckBox jcbSpinach = new JCheckBox("Spinach");
            JCheckBox jcbOnion = new JCheckBox("Onion");
            JCheckBox jcbPickles = new JCheckBox("Pickles");
            JCheckBox jcbTomatoes = new JCheckBox("Tomatoes");
            JCheckBox jcbPeppers = new JCheckBox("Peppers");
            veg.add(jcbLettuce);
            veg.add(jcbSpinach);
            veg.add(jcbPickles);
            veg.add(jcbOnion);
            veg.add(jcbTomatoes);
            veg.add(jcbPeppers);

            JOptionPane.showOptionDialog(frame, veg, "Choose your veggies", JOptionPane.OK_CANCEL_OPTION,  JOptionPane.QUESTION_MESSAGE, null, null, null);

            //CONDIMENTS

            JPanel condiments = new JPanel(new GridLayout(8,1));

            JCheckBox jcbMayo = new JCheckBox("Mayo");
            JCheckBox jcbMustard = new JCheckBox("Mustard");
            JCheckBox jcbDressing = new JCheckBox("Italian Dressing");
            condiments.add(jcbMayo);
            condiments.add(jcbMustard);
            condiments.add(jcbDressing);

            JOptionPane.showOptionDialog(frame, condiments, "Choose your condiments", JOptionPane.OK_CANCEL_OPTION,  JOptionPane.QUESTION_MESSAGE,null, null, null);

            //DO ANOTHER?

            JPanel another = new JPanel(new GridLayout(2,1));
            JRadioButton jrbAnother = new JRadioButton("Yes");
            JRadioButton jrbNotanother = new JRadioButton("No");
            another.add(jrbAnother);
            another.add(jrbNotanother);

            JOptionPane.showOptionDialog(frame, another, "Do you want another?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                                            null, null);

            //SUMMARY

            JPanel summary = new JPanel();
            JOptionPane.showMessageDialog(frame, " Customer: ",null,     JOptionPane.PLAIN_MESSAGE);
            System.exit(0);


    }//end of subwayform

            private void CreateBread()
            {
                    breads = new ArrayList<Bread>();
                    Bread orc = new Bread("Italian");
                    breads.add(orc);
                    breads.add(new Bread("Wheat"));
                    breads.add(new Bread("Honey Oat"));
                    breads.add(new Bread("Herbs and Cheese"));
                    breads.add(new Bread("Cheddar"));
                    breads.add(new Bread("Flatbread"));
            }//end of bread

      private void CreateSandwiches()
      {
      sandwiches = new ArrayList<Sandwich>();
      Sandwich orc = new Sandwich("Oven Roasted Chicken", 3.00);
      sandwiches.add(orc);
      sandwiches.add(new Sandwich("Meatball Marinara", 3.50));
      sandwiches.add(new Sandwich("Blackforest Ham", 2.25));
      sandwiches.add(new Sandwich("BLT", 4.00));
      sandwiches.add(new Sandwich("Cold Cut Combo", 3.00));
      sandwiches.add(new Sandwich("Veggie Delite", 4.00));
      }//end of sandwiches


       public static void main(String[] args)
      {
                    new SubwayForm();

                    orderedSandwiches = new ArrayList<Sandwich>();
                    orderedBreads = new ArrayList<Bread>();

      }    


}//end of subwayform jframe    

class Bread
{
    private String name;
    private String bread;

    public Bread(String name)
    {
            this.name = name;
    }

    public void setBread(String s)
    {
            this.bread = s;
    }

    public String getBread()
    {
            return bread;
    }

 }// end of bread


class Sandwich {

    private String name;
    private double cost;


    public Sandwich(String name, double price)
    {
            this.name = name;
            this.cost = price;
    }

    public String getName()
    {
            return name;
    }

    public double getCost()
    {
            return cost;
    }

}//end of sandwich

Upvotes: 1

Views: 522

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Some suggestions:

  • Avoid sub-classing JFrame. There is rarely a need to do this, and avoiding doing this can improve the flexibility of your program.
  • The orderedSandwiches variable should not be static. You may need to make other changes to your program to allow this to be an instance variable.
  • Don't set the sizes of your GUI or components, and don't use the null layout. While it will seem to newbies that it is easier to use null layouts and directly place their components, the more GUI's they create the more they'll find that by doing this they're painting themselves in a corner. Using the layout managers properly will make it much easier to maintain and upgrade your GUI.
  • Break up huge methods and constructors into smaller more manageable and testable units.
  • No need to call System.exit(0) and good reasons not to as it appears to be short-circuiting your program's logic.
  • Improve your code formatting to make your program easier to read and debug both for us and for you. All code blocks on the same level should be indented the same amount (I usually use 3 spaces). One blank line between blocks is plenty.
  • Better than throwing a bunch of option panes at the user, consider creating JPanels for each sub-section of your GUI, and then combining the ones that belong together in a larger JPanel that will allow the user to input several selections on one view. Your users will appreciate this.
  • Avoid having many void methods that change your program's state by side effects (by directly changing class fields), and instead prefer methods that leave most class fields alone and instead return objects that the class then uses. This will greatly ease your debugging of errors and your ability to update and refine your program.
  • Some of your smaller classes seem a bit redundant. Perhaps you could use an abstract class, SandwichComponent that has a name and cost field and use subclasses of that for specific sub-types of components.

Upvotes: 2

tbodt
tbodt

Reputation: 17007

First, you should have an Order class that has Sandwich and Bread instance fields, as a way of grouping them together, rather than using lists like that. You should also make the Cheese, Condiment, &c classes and include those as instance variables.

Second, why are you creating frame and extending JFrame at the same time? You only need to do one or the other, never both. You can delete the frame variable and use this instead.

Third, you may want to notice that the last two statements in main never get done, because the end of the constructor is System.exit(0).

There are several thousand other pieces of advice I could give you, but if I went that far I might start getting profane.

Upvotes: 2

Related Questions