diff --git a/src/Main.java b/src/Main.java index 51b157b..c21df79 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,29 +4,24 @@ import javax.swing.JFrame; import javax.swing.SwingUtilities; +import frames.MainFrame; import panels.*; public class Main { - - private static MainPanel posPane; - + + // ------------------------------------------------------------------------- + // public static void main(String[] args) { - // TODO Auto-generated method stub + + // ------------------------------------------------------------------------- + // SwingUtilities.invokeLater(new Runnable() { public void run() { - JFrame mainFrame = new JFrame("ToDo���X�g"); - mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - - // generate view - posPane = new MainPanel(); - Container contentPane = mainFrame.getContentPane(); - contentPane.add(posPane, BorderLayout.CENTER); - - // - mainFrame.pack(); - mainFrame.setVisible(true); + // generate window + JFrame mainFrame = new MainFrame(); } }); + // ------------------------------------------------------------------------- } } diff --git a/src/frames/MainFrame.java b/src/frames/MainFrame.java index 42c1f0e..af9b221 100644 --- a/src/frames/MainFrame.java +++ b/src/frames/MainFrame.java @@ -1,21 +1,31 @@ package frames; +import java.awt.BorderLayout; +import java.util.HashMap; + import javax.swing.JFrame; import javax.swing.JPanel; import panels.*; +import resources.Customer; +import resources.CustomersModel; //------------------------------------------------------------------------- // public class MainFrame extends JFrame { - public String[] stateNames = { "main", "history" }; + + // field + private State state; + // model + private CustomersModel model; + // panel - JPanel curPanel; + private JPanel curPanel; // cache - MainPanel mainPanel; - ShowHistoryPanel historyPanel; + private MainPanel mainPanel; + private ShowHistoryPanel historyPanel; // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- @@ -23,14 +33,31 @@ // ------------------------------------------------------------------------- // public MainFrame() { - this.add(mainPanel); - mainPanel.setVisible(true); + super("POS"); - this.add(historyPanel); + this.setTitle("POS"); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // initialize model + model = new CustomersModel(); + + // initialize Panels + mainPanel = new MainPanel(this, model); + historyPanel = new ShowHistoryPanel(this, model); + + // set visible historyPanel.setVisible(false); + this.add(historyPanel, BorderLayout.CENTER); + + mainPanel.setVisible(true); + this.add(mainPanel, BorderLayout.CENTER); // set current panel curPanel = mainPanel; + + // adjust window size + this.pack(); + this.setVisible(true); } // ------------------------------------------------------------------------- @@ -40,30 +67,41 @@ // public void reloadPanel(String destinationPanelName) { // main - if (destinationPanelName.equals(stateNames[0])) { + if (destinationPanelName.equals(State.MAIN)) { this.remove(this.mainPanel); - MainPanel mainPanel = new MainPanel(); + MainPanel mainPanel = new MainPanel(this, model); this.add(mainPanel); } // history - else if(destinationPanelName.equals(stateNames[1])) { + else if (destinationPanelName.equals(State.DETAIL)) { this.remove(this.historyPanel); - ShowHistoryPanel historyPanel = new ShowHistoryPanel(); - this.add(historyPanel); + ShowHistoryPanel historyPanel = new ShowHistoryPanel(this, model); + this.add(historyPanel); } } // ------------------------------------------------------------------------- - // + // public void showMainPanel(JPanel nowPanel) { - nowPanel.setVisible(false); - mainPanel.setVisible(true); + getContentPane().removeAll(); + + mainPanel.updateTableByTransition(); + + add(mainPanel); + validate(); + repaint(); } // ------------------------------------------------------------------------- // - public void showHistoryPanel(JPanel nowPanel) { - nowPanel.setVisible(false); + public void showHistoryPanel(JPanel nowPanel, int index) { + getContentPane().removeAll(); + historyPanel.setVisible(true); + historyPanel.updateListModelByIndex(index); + + add(historyPanel); + validate(); + repaint(); } } diff --git a/src/frames/State.java b/src/frames/State.java new file mode 100644 index 0000000..0d4ad4d --- /dev/null +++ b/src/frames/State.java @@ -0,0 +1,8 @@ +package frames; + +// ------------------------------------------------------------------------- +// +public class State { + public static String MAIN = "Main"; + public static String DETAIL = "Detail"; +} diff --git a/src/panels/MainPanel.java b/src/panels/MainPanel.java index 24a4221..5a691bd 100644 --- a/src/panels/MainPanel.java +++ b/src/panels/MainPanel.java @@ -2,9 +2,12 @@ package panels; import java.awt.BorderLayout; +import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.EventListener; import java.util.HashMap; +import java.util.Random; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -18,36 +21,45 @@ import javax.swing.table.TableModel; import frames.MainFrame; +import frames.State; import resources.Customer; +import resources.CustomersModel; //------------------------------------------------------------------------- // overview of customers public class MainPanel extends JPanel { - + + // frame + private MainFrame mainFrame; + // model - private HashMap customerTable; + private CustomersModel model; // table private JTable panelTable; private DefaultTableModel panelTableModel; - private String[] columnNames = { "name", "total", "points" }; + private String[] columnNames = { "name", "point", "total" }; // buttons private JButton addButton; - private JButton modifyButton; private JButton removeButton; + private JButton showDetailButton; // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- // constructor // ------------------------------------------------------------------------- // - public MainPanel() { + public MainPanel(MainFrame mainFrame, CustomersModel model) { super(new BorderLayout()); + // initialize frame + this.mainFrame = mainFrame; + // initialize model - customerTable = new HashMap(); + this.model = model; +// customerTable = new HashMap(); // initialize table panelTableModel = new DefaultTableModel(columnNames, 0); @@ -57,18 +69,25 @@ // add buttons addButton = new JButton("Add"); - modifyButton = new JButton("Modify"); removeButton = new JButton("Remove"); + showDetailButton = new JButton("Show History"); // add listener addButton.addActionListener(new AddActionHandler()); removeButton.addActionListener(new RemoveActionHandler()); + showDetailButton.addActionListener(new ShowHistoryActionHandler()); JPanel buttonPanel = new JPanel(); - buttonPanel.add(addButton); - buttonPanel.add(modifyButton); - buttonPanel.add(removeButton); - add(buttonPanel, BorderLayout.SOUTH); + buttonPanel.add(addButton, BorderLayout.WEST); + buttonPanel.add(removeButton, BorderLayout.EAST); + add(buttonPanel, BorderLayout.CENTER); + + // add show detail + JPanel showButtonPanel = new JPanel(); + FlowLayout flowLayout = new FlowLayout(); + showButtonPanel.add(showDetailButton, BorderLayout.SOUTH); + showButtonPanel.setLayout(flowLayout); + add(showButtonPanel, BorderLayout.SOUTH); // add scroll menu JScrollPane listScrollPane = new JScrollPane(panelTable); @@ -78,19 +97,38 @@ // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- + // public + // ------------------------------------------------------------------------- + // + public void updateTableByTransition() { + for (int i = 0; i < panelTableModel.getRowCount(); i++) { + // name, points, total + panelTableModel.setValueAt(model.getCustomers().get(i).getName(), i, 0); + panelTableModel.setValueAt(model.getCustomers().get(i).getPoints(), i, 1); + panelTableModel.setValueAt(model.getCustomers().get(i).getTotal(), i, 2); + } + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- // private // ------------------------------------------------------------------------- // - private void transitionToHistory(MainFrame mainFrame) { - mainFrame.showHistoryPanel(this); + private void transitionToHistory(MainFrame mainFrame, int index) { + mainFrame.showHistoryPanel(this, index); } - + // ------------------------------------------------------------------------- // - private Object[] generateCustomerRow(String name) { - Customer customer = new Customer(name + panelTableModel.getRowCount()); - Object[] data = { customer.getName(), customer.getPOS().getTotal(), customer.getPOS().getPoints() }; + private Customer createNewCustomerByName(String name) { + Customer customer = new Customer(name + model.getCustomers().size()); + return customer; + } + // ------------------------------------------------------------------------- + // + private Object[] generateCustomerRow(Customer customer) { + Object[] data = { customer.getName(), customer.getPoints(), customer.getTotal() }; return data; } @@ -101,7 +139,9 @@ // add button's event private class AddActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { - panelTableModel.addRow(generateCustomerRow("name")); + Customer customer = createNewCustomerByName("name"); + model.getCustomers().add(customer); + panelTableModel.addRow(generateCustomerRow(customer)); } } @@ -113,8 +153,22 @@ if (panelTable.getSelectedRow() < 0) return; - if (1 < panelTable.getRowCount()) + if (1 < panelTable.getRowCount()) { + model.getCustomers().remove(panelTable.getSelectedRow()); panelTableModel.removeRow(panelTable.getSelectedRow()); + } + } + } + + // ------------------------------------------------------------------------- + // transition to history + private class ShowHistoryActionHandler implements ActionListener, EventListener { + + // ------------------------------------------------------------------------- + // + public void actionPerformed(ActionEvent e) { + if (-1 < panelTable.getSelectedRow()) + transitionToHistory(mainFrame, panelTable.getSelectedRow()); } } } \ No newline at end of file diff --git a/src/panels/ShowHistoryPanel.java b/src/panels/ShowHistoryPanel.java index f07b095..ff77477 100644 --- a/src/panels/ShowHistoryPanel.java +++ b/src/panels/ShowHistoryPanel.java @@ -1,11 +1,15 @@ package panels; import java.awt.BorderLayout; +import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.ArrayList; import java.util.HashMap; +import javax.swing.DefaultListModel; import javax.swing.JButton; +import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; @@ -13,76 +17,105 @@ import frames.MainFrame; import resources.Customer; +import resources.CustomersModel; //------------------------------------------------------------------------- // public class ShowHistoryPanel extends JPanel { + // frame + private MainFrame mainFrame; + // model - private HashMap customerTable; + private Customer selectedCustomer; + private CustomersModel model; // table - private JTable panelTable; - private DefaultTableModel panelTableModel; - private String[] columnNames = { "name", "total", "points" }; + private JList historiesList; + private DefaultListModel historiesListModel; // buttons - private JButton addButton; - private JButton modifyButton; - private JButton removeButton; + private JButton backButton; + private JButton payButton; // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- // constructor // ------------------------------------------------------------------------- // - public ShowHistoryPanel() { + public ShowHistoryPanel(MainFrame mainFrame, CustomersModel model) { super(new BorderLayout()); + // initialize frame + this.mainFrame = mainFrame; + // initialize model - customerTable = new HashMap(); + this.model = model; // initialize table - panelTableModel = new DefaultTableModel(columnNames, 0); - panelTable = new JTable(panelTableModel); - // deny to edit cell - panelTable.setDefaultEditor(Object.class, null); + historiesListModel = new DefaultListModel(); + historiesList = new JList(historiesListModel); // add buttons - addButton = new JButton("Add"); - modifyButton = new JButton("Modify"); - removeButton = new JButton("Remove"); + JPanel buttonPane = new JPanel(); + backButton = new JButton("Back"); + payButton = new JButton("Payment"); + buttonPane.add(backButton); + buttonPane.add(payButton); + add(buttonPane, BorderLayout.SOUTH); // add listener - // todo: add back to main - - JPanel buttonPanel = new JPanel(); - buttonPanel.add(addButton); - buttonPanel.add(modifyButton); - buttonPanel.add(removeButton); - add(buttonPanel, BorderLayout.SOUTH); + backButton.addActionListener(new BackActionHandler()); + payButton.addActionListener(new PaymentActionHandler()); // add scroll menu - JScrollPane listScrollPane = new JScrollPane(panelTable); - add(listScrollPane, BorderLayout.NORTH); + JScrollPane scrollPanel = new JScrollPane(historiesList); + add(scrollPanel, BorderLayout.NORTH); } // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- + // public + // ------------------------------------------------------------------------- + // + public void updateListModelByIndex(int index) { + selectedCustomer = model.getCustomers().get(index); + historiesListModel.removeAllElements(); + historiesListModel.addAll(model.getCustomers().get(index).getHistory()); + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- // private // ------------------------------------------------------------------------- // - private void transitionToHistory(MainFrame mainFrame) { - mainFrame.showHistoryPanel(this); + private void transitionToMain(MainFrame mainFrame) { + mainFrame.showMainPanel(this); } // ------------------------------------------------------------------------- - // - private Object[] generateCustomerRow(String name) { - Customer customer = new Customer(name + panelTableModel.getRowCount()); - Object[] data = { customer.getName(), customer.getPOS().getTotal(), customer.getPOS().getPoints() }; + // ------------------------------------------------------------------------- + // interface + // ------------------------------------------------------------------------- + // back button's event + private class BackActionHandler implements ActionListener { - return data; + // ------------------------------------------------------------------------- + // + public void actionPerformed(ActionEvent e) { + transitionToMain(mainFrame); + } } + + // ------------------------------------------------------------------------- + // payment button's event + private class PaymentActionHandler implements ActionListener { + public void actionPerformed(ActionEvent e) { + selectedCustomer.purchase(1000); + historiesListModel.removeAllElements(); + historiesListModel.addAll(selectedCustomer.getHistory()); + } + } + } diff --git a/src/resources/Customer.java b/src/resources/Customer.java index ff0afb0..225b25d 100644 --- a/src/resources/Customer.java +++ b/src/resources/Customer.java @@ -1,39 +1,41 @@ -package resources; - -//------------------------------------------------------------------------- -// -public class Customer { - private String name; - private POS pos; - - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // constructor - // ------------------------------------------------------------------------- - // - public Customer(String name) { - this.name = name; - this.pos = new POS(); - } - - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // public - // ------------------------------------------------------------------------- - // - public void setName(String name) { - this.name = name; - } - - // ------------------------------------------------------------------------- - // - public String getName() { - return this.name; - } - - // ------------------------------------------------------------------------- - // - public POS getPOS() { - return this.pos; - } -} +package resources; +import java.util.*; + +//------------------------------------------------------------------------- +// todo: add name field +public class Customer { + private Points points; + private Total total; + private History history; + private Payment payment; + private String name; // added + public Customer(String name) { + points = new Points(); + total = new Total(); + history = new History(total); + payment = new Payment(points,history); + + // added + this.name = name; + } + public void purchase(int x) { + this.payment.purchase(x); + } + public int getTotal() { + return total.getValue(); + } + public int getPayment() { + return payment.getValue(); + } + public List getHistory() { + return history.getValue(); + } + public int getPoints() { + return points.getValue(); + } + + // added + public String getName() { + return this.name; + } +} \ No newline at end of file diff --git a/src/resources/CustomersModel.java b/src/resources/CustomersModel.java new file mode 100644 index 0000000..233b625 --- /dev/null +++ b/src/resources/CustomersModel.java @@ -0,0 +1,30 @@ +package resources; + +import java.util.ArrayList; +import java.util.HashMap; + +import javax.swing.table.DefaultTableModel; + +//------------------------------------------------------------------------- +// +public class CustomersModel { + private ArrayList customers; + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // constructor + // ------------------------------------------------------------------------- + // + public CustomersModel() { + customers = new ArrayList<>(); + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // public + // ------------------------------------------------------------------------- + // + public ArrayList getCustomers() { + return this.customers; + } +} diff --git a/src/resources/POS.java b/src/resources/POS.java deleted file mode 100644 index 7a3076b..0000000 --- a/src/resources/POS.java +++ /dev/null @@ -1,32 +0,0 @@ -package resources; -import java.util.*; - -//------------------------------------------------------------------------- -// todo: add name field -public class POS { - private Points points; - private Total total; - private History history; - private Payment payment; - public POS() { - points = new Points(); - total = new Total(); - history = new History(total); - payment = new Payment(points,history); - } - public void purchase(int x) { - this.payment.purchase(x); - } - public int getTotal() { - return total.getValue(); - } - public int getPayment() { - return payment.getValue(); - } - public List getHistory() { - return history.getValue(); - } - public int getPoints() { - return points.getValue(); - } -} \ No newline at end of file