我正在盡我所能來創建一個非常簡單的GUI應用程序,這種應用程序的「kind-of」行爲與在餐廳等地點使用的Micros系統相同。我建議閱讀代碼,這是評論,在閱讀問題之前,它會更容易理解:)在將對象添加到對象數組中的對象之前創建對象列表?
所以我的問題是。我首先創建了一個帶按鈕的圖形用戶界面,點擊後可將Meal對象添加到單個Bill中。由於只有一個賬單存在,所以不需要選擇表格,我可以顯示該賬單上的項目,並且沒有任何問題地查看價格。我在Java中的額外弱點是數組,這就是爲什麼我想要嘗試創建類似這樣的東西,但我真的被困在這一個。
我想有一種方法可以爲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());
}
}
感謝您的回覆。我不完全確定你的意思是使用Map>我從未聽說過它。任何你知道的鏈接都會有一個很好的例子嗎?再次感謝。 – alannm37
不用擔心。地圖是關聯的數據結構。你可以在這裏找到更多關於它們的信息http://stackoverflow.com/documentation/java/90/collections/12413/usage-of-hashmap#t=201611292043494826684。 –