2016-11-29 4 views
-1

Ich versuche mein Bestes, um eine sehr einfache GUI-Anwendung zu erstellen, die "ähnlich" wie ein Micros-System funktioniert, das an Orten wie Restaurants verwendet wird. Ich empfehle, den Code zu lesen, der kommentiert wird, bevor Sie die Frage lesen, wird es einfacher zu verstehen :)Erstellen Sie eine Liste von Objekten, bevor Sie sie einem Objekt in einem Objekt-Array hinzufügen?

Also meine Frage ist. Ich habe zuerst eine GUI mit Schaltflächen erstellt, die, wenn sie angeklickt wurden, Meal-Objekte zu einer einzelnen Rechnung hinzugefügt haben. Da nur eine Rechnung vorhanden war, war keine Tischauswahl erforderlich, und ich konnte die Artikel auf dieser Rechnung anzeigen und den Preis ohne Probleme anzeigen. Mein zusätzlicher Schwachpunkt in Java ist Arrays, weshalb ich versuchen wollte, so etwas zu erstellen, aber ich bin wirklich auf diesem hier festgefahren.

Ich möchte eine Möglichkeit haben, eine Rechnung für jede der 10 Tabellen zu haben. Ich habe der JComboBox einen ActionListener hinzugefügt und verwende ihn, um eine Tabellennummer unter Verwendung der ausgewählten Nummer in der JComboBox zuzuweisen. Gleichzeitig erstellt es ein Bill-Objekt und weist ihm eine Tabellennummer zu. Einfach richtig?

Wo habe ich Probleme, wie kann ich Mahlzeiten zu einer Rechnung in Bezug auf die ausgewählte Tischnummer hinzufügen? Ich bin mir sicher, dass ich nicht weit davon entfernt sein kann. Ich möchte die Rechnung in Bezug auf die ausgewählte JComboBox-Nummer anzeigen und in der Lage sein, andere Rechnungen für Tabellen zu erstellen, Artikel hinzuzufügen, später wiederzukommen und die Relavent-Tabellennummer Bill erneut anzuzeigen.

Ich hoffe, ich erklärte so viel wie möglich. Jeder, der seine Zeit zur Verfügung stellt, danke ich aufrichtig. Der gesamte Code unten.

Hauptklasse:

/** Created by Alan on 19/11/2016 */ 

public class MicrosMain { 
    public static void main(String[] args) { 
     Micros micros = new Micros(); 
     micros.setVisible(true); 
    } 
} 

Micros Klasse:

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

Bill Klasse:

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

Meal Klasse:

/** 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()); 
    } 
} 

Lebensmittel Klasse:

/** 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()); 
    } 
} 

Getränk Klasse:

/** 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()); 
    } 
} 

Antwort

0

Ich glaube, ich Ihr Problem verstanden. Sie müssen eine Datenstruktur verwenden, die die Beziehungen aufrechterhält, die Sie im Modell ausnutzen.

Tabellen eigene Rechnungen, Rechnungen eigene Mahlzeiten. Angenommen, eine Tabelle besitzt viele Rechnungen und eine Rechnung könnte mehrere Mahlzeiten besitzen, verwenden Sie einfach eine Map<Table, Collection<Bill>> und eine Map<Bills, Collection<Meal>>. Die erste Karte ermöglicht es Ihnen, alle Rechnungen zu erhalten, die zu einer bestimmten Tabelle gehören, und die zweite Karte ermöglicht es Ihnen, die Mahlzeiten zu behalten, die einer bestimmten Rechnung gehören. Sie können die Maps ändern, um die Verwendung des Tabellenindex mithilfe einer Map<Integer, Collection<Bill>> zu ermöglichen.

+0

Vielen Dank für Ihre Antwort. Ich bin mir nicht ganz sicher, was Sie mit Map meinen,> ich habe noch nie davon gehört. Jeder Link, wo Sie es wissen, wäre ein gutes Beispiel? Danke noch einmal. – alannm37

+0

Keine Sorge. Maps sind assoziative Datenstrukturen. Sie können mehr Informationen über sie hier finden http://stackoverflow.com/documentation/java/90/collections/12413/usage-of-hashmap#t=201611292043494826684. –

Verwandte Themen