Java 需要关于如何将实例数组传递给另一个数组的建议吗
首先,我还在学习中,非常感谢在这里得到的帮助。我正在创建一个程序,一个客户可以创建多个三明治订单。现在这个程序非常基本,但只适用于下一个订单。一旦一切正常运行,我会用更多的项目来填写。我想将我的子数组传递给订单数组以容纳多个三明治,然后一旦整个订单完成,我将打印出一个总计。我也不知道我将如何循环程序来下多个订单,但我觉得这应该在order类中完成,除非有一个更简单的方法来完成。任何建议或指点都将不胜感激。我将在继续工作时更新它Java 需要关于如何将实例数组传递给另一个数组的建议吗,java,arrays,jpanel,parameter-passing,Java,Arrays,Jpanel,Parameter Passing,首先,我还在学习中,非常感谢在这里得到的帮助。我正在创建一个程序,一个客户可以创建多个三明治订单。现在这个程序非常基本,但只适用于下一个订单。一旦一切正常运行,我会用更多的项目来填写。我想将我的子数组传递给订单数组以容纳多个三明治,然后一旦整个订单完成,我将打印出一个总计。我也不知道我将如何循环程序来下多个订单,但我觉得这应该在order类中完成,除非有一个更简单的方法来完成。任何建议或指点都将不胜感激。我将在继续工作时更新它 import com.sun.corba.se.impl.ior.O
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"); }
}
//===============================================================\\
导入com.sun.corba.se.impl.ior.OldJIDLObjectKeyTemplate;
导入java.awt.*;
导入javax.swing.*;
导入javax.swing.BorderFactory;
导入java.awt.event.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JOptionPane;
导入javax.swing.border.border;
导入java.util.ArrayList;
公共级地铁移动订单
{
公共静态void main(字符串[]args)
{
JFrame=newtabletscreen();
frame.setTitle(“地铁订单应用程序”);
框架设置尺寸(800500);
frame.setLocationRelativeTo(空);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
类TabletScreen扩展JFrame实现ActionListener
{
//客户名称
private JLabel jlbCustomerName=new JLabel(“为订单输入名称:”);
私有JTextField jtfCustomerName=新JTextField(15);
//面包选择
私有JLabel jlbread=新JLabel(“选择一种面包”);
私有JComboBox jcbreadtype=newjcombobox(新对象[]{“选择面包类型”、“意大利香草和奶酪”、“小麦”});
//小号
private JLabel jlbSize=新JLabel(“您想要多大尺寸的接头”);
私有JCOMBOX jcbSize=新JCOMBOX(新对象[]{“选择子大小”、“12英寸”、“6英寸”});
//亚肉质
private JLabel jlbMeat=new JLabel(“选择您想要的肉”);
私有JCheckBox jcxChicken=新JCheckBox(“鸡”);
私有JCheckBox jcxBacon=新JCheckBox(“培根”);
//亚干酪
private JLabel jlbCheese=新JLabel(“选择您想要的奶酪”);
私有JCheckBox jcxAmerican=新JCheckBox(“美国”);
私有JCheckBox jcxSwiss=新JCheckBox(“瑞士”);
//蔬菜什么的
私人JLabel jlbVeggies=新JLabel(“选择您想要的蔬菜”);
私有JCheckBox jcxOnion=新JCheckBox(“洋葱”);
私有JCheckBox jcxLettuce=新JCheckBox(“莴苣”);
//插件
私人JLabel jlbDoubleCheese=新JLabel(“双奶酪”);
私有JComboBox jcbDoubleCheese=newjcombobox(新对象[]{“是”、“否”});
私人JLabel jlbDoubleMeat=新JLabel(“双肉”);
私有JComboBox jcbDoubleMeat=newjcombobox(新对象[]{“是”、“否”});
//Ok按钮
JButton-okButton=新JButton(“子完成”);
公共表屏幕(){
//主面板:名称、面包类型、尺寸
JPanel mainOrderInfo=新的JPanel();
setLayout(新的GridLayout(6,1,10,10));
mainOrderInfo.add(jlbCustomerName);
mainOrderInfo.add(JTFCCustomerName);
mainOrderInfo.add(jlbread);
mainOrderInfo.add(jcbreadType);
mainOrderInfo.add(jlbSize);
mainOrderInfo.add(jcbSize);
//肉盘
JPanel meatPanel=新的JPanel();
meatPanel.setLayout(新的GridLayout(3,1,10,10));
肉面板。添加(jlbMeat);
meatPanel.add(jcxBacon);
meatPanel.add(jcxChicken);
//奶酪面板
JPanel cheesePanel=新的JPanel();
cheesePanel.setLayout(新网格布局(3,1,10,10));
干酪香肠。添加(jlbCheese);
奶酪香肠。添加(JCX美国);
奶酪香肠。添加(jcxSwiss);
//蔬菜小组
JPanel vegiespanel=新的JPanel();
设置布局(新的网格布局(3,1,10,10));
添加(jlbVeggies);
添加(jcxOnion);
添加(jcxLettuce);
//插件面板
JPanel addonsPanel=newjpanel();
setLayout(新的GridLayout(4,1,10,10));
addonsPanel.add(jlbDoubleCheese);
addonsPanel.add(jcbDoubleCheese);
addonsPanel.add(jlbDoubleMeat);
addonsPanel.add(jcbDoubleMeat);
//设置整个主面板
JPanel主面板=新JPanel(新网格布局(1,5,15,15));
mainPanel.setBorder(BorderFactory.createEmptyByOrder(10,10,10,10));
mainPanel.setOpaque(true);
mainPanel.add(mainOrderInfo);
主面板。添加(meatPanel);
主面板。添加(奶酪面板);
主面板。添加(veggiesPanel);
主面板。添加(addonsPanel);
JPanel buttonPanel=新的JPanel(新的FlowLayout(FlowLayout.RIGHT,10,10));
按钮面板。添加(确定按钮);
setLayout(新的BorderLayout());
添加(主面板,BorderLayout.CENTER);
添加(OK按钮,BorderLayout.SOUTH);
okButton.addActionListener(这个);
}
//------------------------------------------------------------------------\\
已执行的公共无效操作(操作事件e)
{
三明治接头=新三明治();
//买面包
if(jcbreadType.getSelectedIndex()==1)
{sub.addingElements(new itlainHerbies());}
if(jcbreadType.getSelectedIndex()==2)
{sub.addingelements(新小麦());}
//吃肉
if(jcxBacon.isSelected())
{sub.addingElements(new Bacon());}
if(jcxChicken.isSelected())
{sub.addingelements(new Chicken());}
//吃奶酪
if(jcxAmerican.isSelected())
{sub.addingelements(new American());}
if(jcxSwiss.isSelected())
{sub.addingelements(new Swiss());}
//买蔬菜
if(jcxOnion.isSelected())
{sub.addingElements(new洋葱());}
if(jcxLettuce.isSelected())
{sub.addingElements(new莴苣());}
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;
}
}