我正在努力创建一个非常简单的GUI应用程序,其“类似”的行为与用于餐馆等场所的微系统相同。我建议在阅读问题之前阅读评论的代码,这将更容易理解:)
所以我的问题是。我首先创建了一个带按钮的GUI,点击时将Meal对象添加到单个Bill中。不需要选择表格,因为只有一个账单存在,我能够显示该账单上的项目并查看价格而没有任何问题。我在Java中的另一个弱点是Arrays,这就是为什么我想尝试创建这样的东西,但是我真的被困在这个。
我希望有办法为10张表中的每一张都有一张账单。我已经向JComboBox添加了一个ActionListener,并使用它在JComboBox中使用所选的数字来表示一个表号。同时,它创建一个Bill对象并为其分配一个表号。简单吧?
我遇到问题的方法是如何根据所选的表号添加膳食?我相信我离它不远。就像我希望能够查看与JComboBox所选号码相关的账单,并能够为表创建其他账单,添加项目,稍后再回来并再次查看相关表号码Bill。
我希望我尽可能多地解释。任何花时间帮助的人,我真诚地感谢。以下所有代码。
主类:
/** Created by Alan on 19/11/2016 */
public class MicrosMain {
public static void main(String[] args) {
Micros micros = new Micros();
micros.setVisible(true);
}
}
微观类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
/** Created by Alan on 19/11/2016 */
public class Micros extends JFrame {
// Create Local Variables
private String[] tableNumbers;
private Bill userBill = new Bill();
private ArrayList<Bill> allBills = new ArrayList<>();
private ArrayList<Meal> allMeals = new ArrayList<>();
// Create Global JItem Variables
private JComboBox comboBox;
private JComboBox<String> tableList;
// Micros Menu
private Meal curry = new Meal("Chicken Curry", 10.00);
private Meal spaghetti = new Meal("Spaghetti Bolognese", 15.00);
private Meal steak = new Meal("Steak Sandwich", 20.00);
private Meal tea = new Meal("Lyons Tea", 2.00);
private Meal coffee = new Meal("Bewleys Coffee", 3.00);
private Meal hotChocolate = new Meal("Hot Chocolate", 4.00);
// Constructor Method
Micros()
{
// Set The JFrame Properties ------------------------------------------------------------------
super("MicrosSys");
setSize(230, 515);
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocation(800, 250);
// ---------------------------------- Create The Container ----------------------------------
Container cPane = getContentPane();
cPane.setBackground(Color.WHITE);
cPane.setLayout(null);
// ---------------------------------- Create The Welcome/Instruction Label ----------------------------------
Color red = Color.decode("#ff0000");
JLabel areaLabel = new JLabel("Welcome");
areaLabel.setFont(new Font("SansSerif", Font.BOLD, 20));
areaLabel.setForeground(red);
areaLabel.setLocation(65, 5);
areaLabel.setSize(200, 30);
cPane.add(areaLabel);
areaLabel = new JLabel("Choose A Table Number Then");
areaLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
areaLabel.setLocation(30, 30);
areaLabel.setSize(200, 30);
cPane.add(areaLabel);
areaLabel = new JLabel("Choose What You Want");
areaLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
areaLabel.setLocation(45, 50);
areaLabel.setSize(200, 30);
cPane.add(areaLabel);
areaLabel = new JLabel("- Choose A Table Number -");
areaLabel.setForeground(red);
areaLabel.setLocation(35, 80);
areaLabel.setSize(200, 30);
cPane.add(areaLabel);
// --------------------------------- Create ComboBox for Choosing the Table Number --------------------
tableList = new JComboBox<String>();
tableList.addActionListener(new actionListener());
String[] initialValue = {"- Choose A Table -"};
tableList.addItem(initialValue[0]);
for(int x = 1; x <= userBill.getNumOfTables(); x++)
{
tableList.addItem(String.valueOf(x));
}
DefaultListCellRenderer centerText = new DefaultListCellRenderer();
centerText.setHorizontalAlignment(DefaultListCellRenderer.CENTER);
tableList.setRenderer(centerText);
tableList.setLocation(10,105);
tableList.setSize(200,20);
cPane.add(tableList);
// ---------------------------------- Create The Food Label ----------------------------------
areaLabel = new JLabel("Food Items");
areaLabel.setFont(new Font("SansSerif", Font.BOLD, 20));
areaLabel.setForeground(Color.GRAY);
areaLabel.setLocation(10, 130);
areaLabel.setSize(200, 30);
cPane.add(areaLabel);
// ---------------------------------- Create The Food Buttons ----------------------------------
JButton menuButton = new JButton(curry.toString());
menuButton.setLocation(10, 170);
menuButton.setSize(200, 30);
menuButton.addActionListener(new actionListener());
cPane.add(menuButton);
menuButton = new JButton(spaghetti.toString());
menuButton.setLocation(10, 210);
menuButton.setSize(200, 30);
menuButton.addActionListener(new actionListener());
cPane.add(menuButton);
menuButton = new JButton(steak.toString());
menuButton.setLocation(10, 250);
menuButton.setSize(200, 30);
menuButton.addActionListener(new actionListener());
cPane.add(menuButton);
// ---------------------------------- Create The Drinks Label ----------------------------------
areaLabel = new JLabel("Drink Items");
areaLabel.setFont(new Font("SansSerif", Font.BOLD, 20));
areaLabel.setForeground(Color.GRAY);
areaLabel.setLocation(10, 300);
areaLabel.setSize(200, 30);
cPane.add(areaLabel);
// ---------------------------------- Create The Drink Buttons ----------------------------------
menuButton = new JButton(tea.toString());
menuButton.setLocation(10, 340);
menuButton.setSize(200, 30);
menuButton.addActionListener(new actionListener());
cPane.add(menuButton);
menuButton = new JButton(coffee.toString());
menuButton.setLocation(10, 380);
menuButton.setSize(200, 30);
menuButton.addActionListener(new actionListener());
cPane.add(menuButton);
menuButton = new JButton(hotChocolate.toString());
menuButton.setLocation(10, 420);
menuButton.setSize(200, 30);
menuButton.addActionListener(new actionListener());
cPane.add(menuButton);
// -----------------------------------------------------------------------------------------------
// Create The 'View' Menu To Hold Items
JMenu billMenu = new JMenu("Bill");
// Create Items To Add To 'View' Menu
JMenuItem menuItem = new JMenuItem("View Bill");
menuItem.addActionListener(new actionListener()); // Action Listener For When 'View Bill' Is Clicked
billMenu.add(menuItem); // Add 'View Bill' To The Bill Menu
menuItem = new JMenuItem("Pay Bill");
menuItem.addActionListener(new actionListener());
billMenu.add(menuItem);
// Create The 'File' Menu To Hold Items
JMenu fileMenu = new JMenu("File");
//Create Items To Add To 'File' Menu
menuItem = new JMenuItem("Exit"); // New Menu Item Called Exit
menuItem.addActionListener(e -> System.exit(0)); // Action Listener For When 'Exit' Is Clicked
fileMenu.add(menuItem); // Add 'Exit' To The File Menu
// Create Menu Bar To Add Menus
JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(Color.ORANGE);
menuBar.add(billMenu);
menuBar.add(fileMenu);
// Add The Menu Bar To The Frame
setJMenuBar(menuBar);
}
public class actionListener extends Bill implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
// Action Listener for the JComboBox which will create a new userBill in relation to the chosen table
if(a.getSource() == tableList)
{
String tableChosen = tableList.getSelectedItem().toString();
int tableNumber;
if (!tableChosen.equals("- Choose A Table -"))
{
tableNumber = Integer.parseInt(tableChosen);
userBill = new Bill();
userBill.setTableNum(tableNumber);
System.out.println("Table Number Set and Bill Created");
}
}
// --------------------------- View Bill Option ---------------------------
if (a.getActionCommand().equals("View Bill")) {
JOptionPane.showMessageDialog(null, userBill.getBillList());
}
// --------------------------- Pay Bill Option ---------------------------
if (a.getActionCommand().equals("Pay Bill")) {
JOptionPane.showMessageDialog(null, "Your Bill Total Is: €" + userBill.getBillTotal());
}
/* ************************************************************************************************************************************************ */
/* ************************************************************************************************************************************************ */
// --------------------------- Food Item Number One ---------------------------
if (a.getActionCommand().equals(curry.toString())) {
userBill.setBill(curry);
JOptionPane.showMessageDialog(null, curry.getName() + " Added To Bill");
}
// --------------------------- Food Item Number Two ---------------------------
if (a.getActionCommand().equals(spaghetti.toString())) {
userBill.setBill(spaghetti);
JOptionPane.showMessageDialog(null, spaghetti.getName() + " Added To Bill");
}
// --------------------------- Food Item Number Three ---------------------------
if (a.getActionCommand().equals(steak.toString())) {
userBill.setBill(steak);
JOptionPane.showMessageDialog(null, steak.getName() + " Added To Bill");
}
// --------------------------- Drink Item Number One ---------------------------
if (a.getActionCommand().equals(tea.toString())) {
userBill.setBill(tea);
JOptionPane.showMessageDialog(null, tea.getName() + " Added To Bill");
}
// --------------------------- Drink Item Number Two ---------------------------
if (a.getActionCommand().equals(coffee.toString())) {
userBill.setBill(coffee);
JOptionPane.showMessageDialog(null, coffee.getName() + " Added To Bill");
}
// --------------------------- Drink Item Number Three ---------------------------
if (a.getActionCommand().equals(hotChocolate.toString())) {
userBill.setBill(hotChocolate);
JOptionPane.showMessageDialog(null, hotChocolate.getName() + " Added To Bill");
}
}
}
}
比尔等级:
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/** Created by Alan on 19/11/2016 */
public class Bill {
private int tableNum;
private static int billNum;
private double billTotal;
private ArrayList<Meal> mealList = new ArrayList<>();
public Bill ()
{
billNum++;
tableNum = 0;
mealList = null;
}
public void setTableNum (int tableNum)
{
this.tableNum = tableNum;
}
public void setBill (Meal meal)
{
mealList.add(meal);
billTotal += meal.getPrice();
}
public int getNumOfTables ()
{
return 10;
}
public double getBillTotal ()
{
return this.billTotal;
}
public JTextArea getBillList ()
{
String billFormat = "";
JTextArea billArea = new JTextArea();
billArea.setFont(new Font("monospaced", Font.PLAIN, 14));
billArea.append(String.format("%-20s %-10s\n\n","Name","Price"));
for(Meal i : mealList)
{
billFormat += String.format("%-20s €%-10s\n",i.getName(), i.getPrice());
billArea.append(billFormat);
billFormat = "";
}
billArea.append("\nTotal Price: €" + billTotal);
billArea.setEditable(false);
return billArea;
}
}
膳食等级:
/** Created by Alan on 19/11/2016 */
public class Meal {
private String name;
private double price;
public Meal () {}
public Meal (String name, double price)
{
this.name = name;
this.price = price;
}
public String getName ()
{
return this.name;
}
public double getPrice ()
{
return this.price;
}
public String toString ()
{
return String.format("%s - €%.2f", this.getName(),this.getPrice());
}
}
食品类:
/** Created by Alan on 19/11/2016 */
public class Food {
private String name;
private double price;
public Food () {}
public Food (String name, double price)
{
this.name = name;
this.price = price;
}
public String getName ()
{
return this.name;
}
public double getPrice ()
{
return this.price;
}
public String toString ()
{
return String.format("%s - €%.2f", this.getName(),this.getPrice());
}
}
饮料类:
/** Created by Alan on 19/11/2016 */
public class Drink {
private String name;
private double price;
public Drink () {}
public Drink (String name, double price)
{
this.name = name;
this.price = price;
}
public String getName ()
{
return this.name;
}
public double getPrice ()
{
return this.price;
}
public String toString ()
{
return String.format("%s - €%.2f", this.getName(),this.getPrice());
}
}
答案 0 :(得分:0)
我想我理解你的问题。您需要做的是使用一种数据结构来维护您在模型中利用的关系。
桌子自己的账单,账单自己用餐。假设一张桌子拥有很多账单而一张账单可能拥有多个餐点,那么只需使用add_action( 'save_post_piggy_bank', 'my_function', 10, 2 );
function my_function($post_id, $post){
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if($post->post_status == 'auto-draft' || $post->post_status == 'trash'){
return;
}
$user_id = get_current_user_id();
//get the user's piggy bank
$piggy_bank_amount = get_user_meta($user_id, 'piggy_bank', true);
//increment their bank by 1
update_user_meta($user_id, 'piggy_bank', $piggy_bank_amount + 1);
}
和Map<Table, Collection<Bill>>
。第一张地图将允许您获得给定表所拥有的所有票据,第二张地图将允许您保留任何给定票据所拥有的餐。您可以修改地图以允许使用Map<Bills, Collection<Meal>>
来使用表索引。