I am writing a program to allow a customer can place a sandwich order via a JFrame GUI (with Swing). I want the customer to be able to place multiple orders.
Currentyly, you would open the program and select what you want. A window pops up showing your selection. I want the user to be able to place another order if they choose (this is the part I haven't figured out yet). All the while, I want to be able to track their total so I can display a grand total after they finish ordering. My code works for simple placing a single order:
import java.awt.*;
import javax.swing.*;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import static javax.swing.JOptionPane.YES_OPTION;
public class SubwayMobileOrder
{
public static void main(String[] args) {
Order placeOrder = new Order();
placeOrder.placeOrder();
placeOrder.printOrder();
}
}
class TabletScreen extends JFrame implements ActionListener
{
// Customer name
private JLabel jlbCustomerName = new JLabel("Enter a Name for the order:");
private JTextField jtfCustomerName = new JTextField(15);
// Bread choices
private JLabel jlbBread = new JLabel("Choose a type of bread");
private JComboBox jcbBreadType = new JComboBox(new Object[]{"Select Bread Type", "Italin Herbs and Cheese", "Wheat"});
// Sub Size
private JLabel jlbSize = new JLabel("What size sub would you like");
private JComboBox jcbSize = new JComboBox(new Object[]{"Select Sub Size", "12 inch", "6 inch"});
// Sub meat
private JLabel jlbMeat = new JLabel("Select what Meat(s) you would like");
private JCheckBox jcxChicken = new JCheckBox("Chicken");
private JCheckBox jcxBacon = new JCheckBox("Bacon");
// Sub Cheese
private JLabel jlbCheese = new JLabel("Select what Cheese(s) you would like");
private JCheckBox jcxAmerican = new JCheckBox("American");
private JCheckBox jcxSwiss = new JCheckBox("Swiss");
// Ingrediants ie veggies and what not
private JLabel jlbVeggies = new JLabel("Select what Veggies you would like");
private JCheckBox jcxOnion = new JCheckBox("Onion");
private JCheckBox jcxLettuce = new JCheckBox("Lettuce");
// Addons
private JLabel jlbDoubleCheese = new JLabel("Double Cheese");
private JComboBox jcbDoubleCheese = new JComboBox(new Object[] {"yes", "no"});
private JLabel jlbDoubleMeat = new JLabel("Double Meat");
private JComboBox jcbDoubleMeat = new JComboBox(new Object[] {"yes", "no"});
// Ok button
JButton okButton = new JButton("Sub Complete");
public TabletScreen() {
// Main panel with: name, bread type, size
JPanel mainOrderInfo = new JPanel();
mainOrderInfo.setLayout(new GridLayout(6, 1, 10, 10));
mainOrderInfo.add(jlbCustomerName);
mainOrderInfo.add(jtfCustomerName);
mainOrderInfo.add(jlbBread);
mainOrderInfo.add(jcbBreadType);
mainOrderInfo.add(jlbSize);
mainOrderInfo.add(jcbSize);
// Meat Panel
JPanel meatPanel = new JPanel();
meatPanel.setLayout(new GridLayout(3, 1, 10, 10));
meatPanel.add(jlbMeat);
meatPanel.add(jcxBacon);
meatPanel.add(jcxChicken);
// Cheese Panel
JPanel cheesePanel = new JPanel();
cheesePanel.setLayout(new GridLayout(3, 1, 10, 10));
cheesePanel.add(jlbCheese);
cheesePanel.add(jcxAmerican);
cheesePanel.add(jcxSwiss);
// Veggies Panel
JPanel veggiesPanel = new JPanel();
veggiesPanel.setLayout(new GridLayout(3, 1, 10, 10));
veggiesPanel.add(jlbVeggies);
veggiesPanel.add(jcxOnion);
veggiesPanel.add(jcxLettuce);
// Addons Panel
JPanel addonsPanel = new JPanel();
addonsPanel.setLayout(new GridLayout(4, 1, 10, 10));
addonsPanel.add(jlbDoubleCheese);
addonsPanel.add(jcbDoubleCheese);
addonsPanel.add(jlbDoubleMeat);
addonsPanel.add(jcbDoubleMeat);
// Setup overall main panel
JPanel mainPanel = new JPanel(new GridLayout(1, 5, 15, 15));
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
mainPanel.setOpaque(true);
mainPanel.add(mainOrderInfo);
mainPanel.add(meatPanel);
mainPanel.add(cheesePanel);
mainPanel.add(veggiesPanel);
mainPanel.add(addonsPanel);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
buttonPanel.add(okButton);
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
add(okButton, BorderLayout.SOUTH);
okButton.addActionListener(this);
}
//------------------------------------------------------------------------\\
public void actionPerformed (ActionEvent e)
{
Sandwich sub = new Sandwich();
Order order = new Order();
// getting bread
if (jcbBreadType.getSelectedIndex() == 1)
{ sub.addSub(new ItlainHerbs()); }
if (jcbBreadType.getSelectedIndex() == 2)
{ sub.addSub(new Wheat()); }
// getting meat
if (jcxBacon.isSelected())
{ sub.addSub(new Bacon()); }
if (jcxChicken.isSelected())
{ sub.addSub(new Chicken()); }
// getting cheese
if (jcxAmerican.isSelected())
{ sub.addSub(new American()); }
if (jcxSwiss.isSelected())
{ sub.addSub(new Swiss()); }
// getting veggies
if (jcxOnion.isSelected())
{ sub.addSub(new Onion()); }
if (jcxLettuce.isSelected())
{ sub.addSub(new Lettuce()); }
// getting
order.addOrder(sub);
JOptionPane.showMessageDialog(null, sub.toString());
}
}
//---------------------------------------------------------------------------------------\\
class Order
{
private static ArrayList<Sandwich> order = new ArrayList<Sandwich>(15);
public Order() {}
public void addOrder(Sandwich a)
{
order.add(a);
}
public void placeOrder()
{
JFrame frame = new TabletScreen();
frame.setTitle("Subway Order App");
frame.setSize(800, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void printOrder()
{
String totalOrder = "\nThis is your total order \n";
for (int i = 0; i < order.size(); i++)
{
if (order.get(i) instanceof Sandwich)
{
totalOrder += "\nSub: " + order.get(i);
}
JOptionPane.showMessageDialog(null, totalOrder);
}
}
}
class Sandwich
{
private ArrayList<Sub> sub = new ArrayList<Sub>(20);
public Sandwich() {}
public void addSub(Sub x) { sub.add(x); }
public String toString()
{
String yourOrder = "\n Your Sub is as follows: \n ";
for (int i = 0; i < sub.size(); i++)
{
if (sub.get(i) instanceof Bread)
{ yourOrder += "\nBread type: " + sub.get(i); }
if (sub.get(i) instanceof Meat)
{ yourOrder += "\nMeat type: " + sub.get(i); }
if (sub.get(i) instanceof Cheese)
{ yourOrder += "\nCheese type: " + sub.get(i); }
if (sub.get(i) instanceof Veggies)
{ yourOrder += "\nVeggie type: " + sub.get(i); }
}
return yourOrder;
}
}
//===============================================================\\
class Sub // Sub super class
{
Sub() {}
}
//---------------------------------------------------------------\\
class Bread extends Sub
{
String bread;
Bread(String bread) { this.bread = bread; }
public String toString() { return bread; }
}
class ItlainHerbs extends Bread
{
ItlainHerbs() { super("Italin Herbs and Cheese"); }
}
class Wheat extends Bread
{
Wheat() { super("Wheat"); }
}
//---------------------------------------------------------------\\
class Meat extends Sub
{
String meat;
Meat(String meat) { this.meat = meat; }
public String toString() { return meat; }
}
class Bacon extends Meat
{
Bacon() { super("Bacon"); }
}
class Chicken extends Meat
{
Chicken() { super("Chiken"); }
}
//---------------------------------------------------------------\\
class Cheese extends Sub
{
String cheese;
Cheese(String cheese) { this.cheese = cheese; }
public String toString() { return cheese; }
}
class American extends Cheese
{
American() { super("American"); }
}
class Swiss extends Cheese
{
Swiss() { super("Swiss"); }
}
//---------------------------------------------------------------\\
class Veggies extends Sub
{
String veggies;
Veggies(String veggies) { this.veggies = veggies; }
public String toString() { return veggies; }
}
class Onion extends Veggies
{
Onion() { super("Onion"); }
}
class Lettuce extends Veggies
{
Lettuce() { super("Lettuce"); }
}
//===============================================================\\