【问题标题】:Need advice on how to pass an array of instances to another array需要有关如何将实例数组传递给另一个数组的建议
【发布时间】:2015-08-13 20:31:35
【问题描述】:

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

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"); }
}

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

这里只是两个数组类

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;
   }
}

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

【问题讨论】:

  • Iv 将所有类放在一个文件下,以便于发布我知道这是不好的做法。
  • 我认为我们不需要了解成分、肉类、面包、奶酪等类来回答您的问题。你能简单地发布三明治数组和订单数组吗?
  • 就我个人而言,我将拥有一个 Sandwich 扩展的 Item 类,并拥有一个单独的 Orders 类,该类将具有 ListList ,这是您的订单。那么你所有与总订单成本等相关的逻辑都属于Orders
  • 是的,我可以单独发布该部分我知道有些人喜欢复制和粘贴整个代码,这样更容易使用
  • 回复:您对 JavaDevil 的评论——三明治不是列表,列表也不是三明治。您只需要一个包含三明治(以及您的订单中的其他种类的东西)的列表。当 Something is-a SomethingElse 时使用 extends,例如 Drink is-an 项目.

标签: java arrays jpanel parameter-passing


【解决方案1】:

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

【讨论】:

  • 为了让这一点更清楚——从所有东西中删除“数组”甚至集合的概念。您可能有一个包含订单的对象,您可能有一个包含三明治的对象。您可以将一个对象传递给另一个对象——其他一切(数组/集合操作)都发生在对象内部。
  • 如果您正在处理收到的订单,请将它们添加到java.util.Queue,然后将remove()poll() 加入队列以获取下一个要处理的内容。而且你不需要一个特殊的类,只需要一个具有正确名称的变量。以Queue&lt;Order&gt; orderQueue = new PriorityQueue&lt;&gt;(); 为例。
  • 我还没有广泛的 java 知识,所以我试图坚持基础知识。这个程序将简单地打开一个窗口,一个人可以选择一个三明治压机,一个三明治摘要将被显示,然后他们可以选择制作另一个,当他们完成后,他们将收到一个总成本的摘要,就是这样。
  • @RickySchiebner 这不简单吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 2020-11-15
  • 2017-11-15
  • 2011-11-07
  • 1970-01-01
相关资源
最近更新 更多