需要有关如何将一组实例传递给另一个数组的建议

时间:2015-08-13 20:31:36

标签: java arrays jpanel parameter-passing

首先,我仍然在学习和欣赏我在这里收到的所有帮助。我正在创建一个程序,客户可以排队多个三明治订单。现在该程序非常基本,但适用于下订单。一切准备就绪后,我会用更多项目填写。我想将我的子数组传递给一个订单数组来保存多个三明治,然后一旦整个订单完成,我将打印出一个总数。我也不确定如何循环程序来放置多个订单,但我觉得这应该在订单类中完成,除非有一个更简单的方法在主要完成所有操作。任何建议或指示表示赞赏。我会继续更新,我会更新。

In [4]: game = ChessGame()

In [5]: game.board
Out[5]: 
[['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.']]

In [6]: game.display_board()
........
........
........
........
........
........
........
........

这里只是两个数组类

import com.sun.corba.se.impl.ior.OldJIDLObjectKeyTemplate;

import java.awt.*;
import javax.swing.*;
import javax.swing.BorderFactory;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.border.Border;
import java.util.ArrayList;


public class SubwayMobileOrder
{

public static void main(String[] args)
{
    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);


}
}

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

        // getting bread
        if (jcbBreadType.getSelectedIndex() == 1)
         { sub.addIngredients(new ItlainHerbs()); }
        if (jcbBreadType.getSelectedIndex() == 2)
            { sub.addIngredients(new Wheat()); }

        // getting meat
        if (jcxBacon.isSelected())
            { sub.addIngredients(new Bacon()); }
        if (jcxChicken.isSelected())
            { sub.addIngredients(new Chicken()); }

        // getting cheese
        if (jcxAmerican.isSelected())
            { sub.addIngredients(new American()); }
        if (jcxSwiss.isSelected())
            { sub.addIngredients(new Swiss()); }

        // getting veggies
        if (jcxOnion.isSelected())
            { sub.addIngredients(new Onion()); }
        if (jcxLettuce.isSelected())
            { sub.addIngredients(new Lettuce()); }

        JOptionPane.showMessageDialog(null, sub.toString());

    }



}

//---------------------------------------------------------------------------------------\\

class Order
{
    private static List<Items> order = new List<Order>(15);

    public Order() {}
}

class Sandwich
{
    private static ArrayList<Ingredients> ingredients = new ArrayList<Ingredients>(20);

    public Sandwich() {}

    public static void addIngredients(Ingredients x) { ingredients.add(x); }


public String toString()
{
    String yourOrder = "\n Your order is as follows: \n ";
    for (int i = 0; i < ingredients.size(); i++)
    {
        if (ingredients.get(i) instanceof Bread)
            { yourOrder += "\nBread type: " + ingredients.get(i); }
        if (ingredients.get(i) instanceof Meat)
            { yourOrder += "\nMeat type: " + ingredients.get(i); }
        if (ingredients.get(i) instanceof Cheese)
            { yourOrder += "\nCheese type: " + ingredients.get(i); }
        if (ingredients.get(i) instanceof Veggies)
            { yourOrder += "\nVeggie type: " + ingredients.get(i); }

    }

    return yourOrder;
}
}

//===============================================================\\

class Ingredients // Ingredients super class
{
    Ingredients() {}
}

//---------------------------------------------------------------\\
class Bread extends Ingredients
{
    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 Ingredients
{
    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 Ingredients
{
    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 Ingredients
{
    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"); }
}

//===============================================================\\

我甚至不确定这是否是为订单类声明数组的最佳方式。但我想将三明治阵列存储在订单类中,以便以后打印出来并加起来价格,而不是。

1 个答案:

答案 0 :(得分:1)

您应该创建一个名为Orders或OrdersList的新类。 OrdersList类将包含Order类的多个。