0

My assignment requires me to use an arrayList to take the user selections of each order and display them in an Order Summary JOptionPane. Do I have to create a new class for the arrayList. We learned them without using GUIs so I am unsure on how to get it to display in the order summary for each order. For instance, if someone picked a sub type how would I get the type and price, store it in the arrayList, and display it in the order summary. If I was explained how to do one then I could figure out the rest.

EDITED

I tried to get the "toppings" the same way as the other options but it wouldn't work. How do you get this to work with a JCheckBox. Also, how do I do it for numbers to get the price? You can see in my code that I am trying a way with a case/switch. What am I doing wrong?

Thanks

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

public class Subway extends JFrame {

    private String[] breadNames = {"9-grain Wheat", "Italian", "Honey Oat",
        "Herbs and Cheese", "Flatbread"};
    private String[] subTypes = {"Oven Roasted Chicken - $3.50",
        "Meatball Marinara - $4.50", "Cold Cut Combo - $4.00",
        "BLT - $3.75", "Blackforest Ham - $4.00", "Veggie Delight - $2.50"};
    private String[] cheesetypes = {
        "Cheddar", "American", "Provolone", "Pepperjack"};
    private String[] size = {"6 inch", "Footlong"};
    private String[] toasted = {"Yes", "No"};
    private JTextField jtfname = new JTextField(10);
    private JComboBox<String> jcbBread = new JComboBox<String>(breadNames);
    private JComboBox<String> jcbtype = new JComboBox<String>(subTypes);
    private JComboBox<String> jcbcheese = new JComboBox<String>(cheesetypes);
    private JButton jbtExit = new JButton("EXIT");
    private JButton jbtAnother = new JButton("Next Order");
    private JButton jbtSubmit = new JButton("SUBMIT");
    private JComboBox<String> jcbSize = new JComboBox<String>(size);
    private JComboBox<String> jcbToasted = new JComboBox<String>(toasted);
    private JCheckBox jcbLettuce = new JCheckBox("Lettuce");
    private JCheckBox jcbSpinach = new JCheckBox("Spinach");
    private JCheckBox jcbOnion = new JCheckBox("Onion");
    private JCheckBox jcbPickles = new JCheckBox("Pickles");
    private JCheckBox jcbTomatoes = new JCheckBox("Tomatoes");
    private JCheckBox jcbPeppers = new JCheckBox("Peppers");
    private JCheckBox jcbMayo = new JCheckBox("Mayo");
    private JCheckBox jcbMustard = new JCheckBox("Mustard");
    private JCheckBox jcbDressing = new JCheckBox("Italian Dressing");

    public Subway() {
        //name
        JPanel p1 = new JPanel(new GridLayout(24, 1));
        p1.add(new JLabel("Enter Name"));
        p1.add(jtfname);
        //size
        p1.add(new JLabel("Select a size"));
        p1.add(jcbSize);
        //bread
        p1.add(new JLabel("Select a Bread"));
        p1.add(jcbBread);
        //type
        p1.add(new JLabel("What type of sub would you like?"));
        p1.add(jcbtype);
        //cheese
        p1.add(new JLabel("Select a cheese"));
        p1.add(jcbcheese);
        //toasted
        p1.add(new JLabel("Would you like it toasted?"));
        p1.add(jcbToasted);
        //toppings
        p1.add(new JLabel("Select your toppings"));
        p1.add(jcbLettuce);
        p1.add(jcbSpinach);
        p1.add(jcbPickles);
        p1.add(jcbOnion);
        p1.add(jcbTomatoes);
        p1.add(jcbPeppers);
        p1.add(jcbMayo);
        p1.add(jcbMustard);
        p1.add(jcbDressing);
        // BUTTON PANEL
        JPanel p5 = new JPanel();
        p5.setLayout(new BoxLayout(p5, BoxLayout.LINE_AXIS));
        p5.add(Box.createHorizontalGlue());// KEEPS THEM HORIZONTAL
        p5.add(jbtExit);
        p5.add(jbtAnother);
        p5.add(jbtSubmit);
        // ADDING PANELS AND WHERE THEY GO
        add(p1, BorderLayout.NORTH);// TOP
        add(p5, BorderLayout.SOUTH);// BOTTOM
        // SETTING INVISIBLE BORDERS AROUND PANELS TO SPACE THEM OUT
        p1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        p5.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        // EXIT BUTTON LISTENER
        jbtExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

                System.exit(0);

            }
        });
        // Another order BUTTON LISTENER
        jbtAnother.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                makeASandwich();


            }
        });
        // SUBMIT BUTTON LISTENER
        jbtSubmit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                makeASandwich();
                for (Sandwich s : Sandwich.order) {
                    String Order = " ";
                    Order = jtfname.getText() + "\n"
                            + "\nSize: " + s.getSize()
                            + "\nType of Sub: " + s.getName()
                            + "\nBread: " + s.getBread()
                            + "\nCheese: " + s.getCheese()
                            + "\nToasted? " + s.getToasted()
                            + "\n\n---Next Order---";
                    JOptionPane.showMessageDialog(null, Order);
                }
            }
        });
    }

    private void makeASandwich() {
        Sandwich sandwich = new Sandwich(jcbtype.getItemAt(jcbtype.getSelectedIndex()));
        sandwich.setBread(jcbBread.getItemAt(jcbBread.getSelectedIndex()));
        sandwich.setCheese(jcbcheese.getItemAt(jcbcheese.getSelectedIndex()));
        sandwich.setToasted(jcbToasted.getItemAt(jcbToasted.getSelectedIndex()));
        sandwich.setSize(jcbSize.getItemAt(jcbSize.getSelectedIndex()));
        // sandwich.setPrice(jcbtype.getItemAt(jcbtype.getSelectedIndex()));
        Sandwich.order.add(sandwich);
    }

    public static void main(String[] args) {
        Subway frame = new Subway();
        frame.pack();
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("SUBWAY");
        frame.setVisible(true);
        frame.setSize(600, 750);
    }// ENDOFMAIN
}// ENDOFCLASS

class HoldOrder {

    public static List<Sandwich> order = new ArrayList<Sandwich>();
}

class Sandwich {

    String bread = "";
    String sandwichName = "";
    String cheese = "";
    String size = "";
    String toasted = "";
    private double total = 0;
    public static List<Sandwich> order = new ArrayList<Sandwich>();

    public Sandwich(String typeOfSandwich) {
        sandwichName = typeOfSandwich;
        //switch(setPrice) { 
        //case "BLT":
        //  total += 3.00D;
        //   break;
        //  case "Turkey Bacon":
        //   total += 3.50D;
        //  break;
        //}
    }

    public String getName() {
        return sandwichName;
    }

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

    public String getBread() {
        return bread;
    }

    public void setCheese(String s) {
        cheese = s;
    }

    public String getCheese() {
        return cheese;
    }

    public void setSize(String s) {
        size = s;
    }

    public String getSize() {
        return size;
    }

    public void setToasted(String s) {
        toasted = s;
    }

    public String getToasted() {
        return toasted;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kyle
  • 33
  • 3
  • 9
  • `ArrayList ` is just a container (like ordinary array). For example, you can create a class `Order` and store objects of this class in your Arraylist. Then when required you will transfer string representations of your objects to JOptionPane. – PM 77-1 Aug 05 '13 at 00:17

2 Answers2

2

You post a lot of codes, so, instead, I create a demo for you:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 *
 * @author Azad
 */
public class ShowListDemo extends JFrame implements ActionListener{
    private java.util.List<String> list;

    public ShowListDemo(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        JButton show = new JButton("Show List Content");
        show.addActionListener(this);
        p.add(show);

        list = new ArrayList<>();
        list.add("infromation one");
        list.add("information two");
        list.add("information three");

        add(p);
        setLocationRelativeTo(null);
        pack();
    }

    public static void main(String...args){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
              new ShowListDemo().setVisible(true);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
       String  s = "" ;
        for(String a : list)
            s += a +"\n";
        JOptionPane.showMessageDialog(this, s);//@Mad programmer's recommended, I can't refuse :)
    }
}

Also create a button for adding elements to the list, however, I prefer JList instead option pane, it's more comfortable.

Azad
  • 5,047
  • 20
  • 38
  • 1
    I hope that means you're suggesting that the user should use a `JList` on the `JOptionPane`, cause that would be one of my suggestions ;) – MadProgrammer Aug 05 '13 at 00:27
  • @MadProgrammer: Actually I didn't mean that, but sounds good idea. – Azad Aug 05 '13 at 00:32
  • It's a nit-pick, but I would highly recommend that you pass `this` as the first parameter to `JOptionPane` as I can't abide dialogs that popup in relation to the component they were instantiated from - especially in a multi-screen environment :P – MadProgrammer Aug 05 '13 at 00:33
  • 1
    I think the use of a `JList` or `JTable` over the use of `JOptionPane` is also a good idea, but may be beyond the scope of what the OP is looking for – MadProgrammer Aug 05 '13 at 00:34
  • @MadProgrammer: I remmember I used this way in one of my assignments, till know I don't know why I get `0.8` on `3` :D – Azad Aug 05 '13 at 00:36
  • @MadProgrammer I'm supposed to use JOptionPane for the assignment but I don't think it is a requirement. – Kyle Aug 05 '13 at 01:25
  • @Kyle That's what I figured ;) – MadProgrammer Aug 05 '13 at 01:26
  • 1
    @Kyle: You're welcome, I noticed you didn't accept any answer of your questions(I don't mean to accept mine off course), do you know how to accept an answer, it's easy, see this link please:[**stackoverflow.com/about**](http://stackoverflow.com/about). – Azad Aug 05 '13 at 01:33
  • @Azad Sorry, about not accepting. I didn't know how. I updated my code and question. Can you please take a look at it? – Kyle Aug 05 '13 at 01:54
  • 1
    @Kyle: I am really want to sleep know, sorry, and be sure I will not answer the EDIT question as you must ask a new question about that maybe you get answer, a lost try to post a brief description with a small area of compiled code :) Sorry for that, but [ask another question about that.](http://msmvps.com/blogs/jon_skeet/archive/2012/11/24/stack-overflow-question-checklist.aspx) – Azad Aug 05 '13 at 01:57
2

You could format the List of Strings as HTML and allow the HTML support in Swing to render it, something like Display array in JOptionPane, JOptionPane Multidimensional Array output and JOptionPane displaying HTML problems in Java for example

Something that many people forget, the second parameter to a JOptionPane is a Object, the JOptionPane allows you to pass a Component which it will use as it's primary display. This means you could display a JTable or JList for example.

Check out JOptionPane displaying HTML problems in Java for an example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366