diff --git a/src/Main.java b/src/Main.java index 5d49e85..9ce283a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,11 +4,7 @@ public class Main { - // ------------------------------------------------------------------------- - // public static void main(String[] args) { - - // generate window JFrame mainFrame = new MainFrame(); mainFrame.setVisible(true); } diff --git a/src/frames/IMainFrame.java b/src/frames/IMainFrame.java index 6cf26ab..b20d60b 100644 --- a/src/frames/IMainFrame.java +++ b/src/frames/IMainFrame.java @@ -1,7 +1,5 @@ package frames; -// ------------------------------------------------------------------------- -// public interface IMainFrame { void showMainPanel(); void showHistoryPanel(int index); diff --git a/src/frames/MainFrame.java b/src/frames/MainFrame.java index be02c9c..b7b8d99 100644 --- a/src/frames/MainFrame.java +++ b/src/frames/MainFrame.java @@ -8,47 +8,32 @@ import panels.ShowHistoryPanel; import resources.CustomersModel; -//------------------------------------------------------------------------- -// public class MainFrame extends JFrame implements IMainFrame { - // model private CustomersModel model; - // cache private MainPanel mainPanel; private ShowHistoryPanel historyPanel; - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // constructor - // ------------------------------------------------------------------------- - // public MainFrame() { super("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); - // adjust window size this.pack(); } - // ------------------------------------------------------------------------- - // public void showMainPanel() { getContentPane().removeAll(); @@ -59,8 +44,6 @@ repaint(); } - // ------------------------------------------------------------------------- - // public void showHistoryPanel(int index) { getContentPane().removeAll(); diff --git a/src/panels/MainPanel.java b/src/panels/MainPanel.java index 4dcf153..b3c394c 100644 --- a/src/panels/MainPanel.java +++ b/src/panels/MainPanel.java @@ -1,4 +1,3 @@ - package panels; import java.awt.BorderLayout; @@ -16,54 +15,37 @@ import resources.Customer; import resources.CustomersModel; -//------------------------------------------------------------------------- -// overview of customers public class MainPanel extends JPanel { - // frame private IMainFrame mainFrame; - // model private CustomersModel model; - // table private JTable panelTable; private DefaultTableModel panelTableModel; - private String[] columnNames = { "name", "point", "total" }; + private String[] columnNames = { "name", "points", "total" }; - // buttons private JButton addButton; private JButton removeButton; private JButton showHistoryButton; - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // constructor - // ------------------------------------------------------------------------- - // public MainPanel(IMainFrame mainFrame, CustomersModel model) { super(new BorderLayout()); - // initialize frame this.mainFrame = mainFrame; - // initialize model this.model = model; -// customerTable = new HashMap(); - // initialize table panelTableModel = new DefaultTableModel(columnNames, 0); panelTable = new JTable(panelTableModel); - // deny to edit cell + panelTable.setDefaultEditor(Object.class, null); - // add buttons addButton = new JButton("Add"); removeButton = new JButton("Remove"); showHistoryButton = new JButton("Show History"); - // add listener addButton.addActionListener(new AddActionHandler()); removeButton.addActionListener(new RemoveActionHandler()); showHistoryButton.addActionListener(new ShowHistoryActionHandler()); @@ -73,71 +55,47 @@ buttonPanel.add(removeButton, BorderLayout.EAST); add(buttonPanel, BorderLayout.CENTER); - // add show history JPanel showButtonPanel = new JPanel(); FlowLayout flowLayout = new FlowLayout(); showButtonPanel.add(showHistoryButton, BorderLayout.SOUTH); showButtonPanel.setLayout(flowLayout); add(showButtonPanel, BorderLayout.SOUTH); - // add scroll menu JScrollPane listScrollPane = new JScrollPane(panelTable); add(listScrollPane, BorderLayout.NORTH); } - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // 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); + panelTableModel.setValueAt(model.getCustomerById(i).getName(), i, 0); + panelTableModel.setValueAt(model.getCustomerById(i).getPoints(), i, 1); + panelTableModel.setValueAt(model.getCustomerById(i).getTotal(), i, 2); } } - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // private - // ------------------------------------------------------------------------- - // private void transitionToHistory(int index) { mainFrame.showHistoryPanel(index); } - // ------------------------------------------------------------------------- - // private Customer createNewCustomerByName(String name) { - Customer customer = new Customer(name + model.getCustomers().size()); + Customer customer = new Customer(name + model.getCustomersCount()); return customer; } - // ------------------------------------------------------------------------- - // private Object[] generateCustomerRow(Customer customer) { Object[] data = { customer.getName(), customer.getPoints(), customer.getTotal() }; return data; } - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // interface - // ------------------------------------------------------------------------- - // add button's event private class AddActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { Customer customer = createNewCustomerByName("name"); - model.getCustomers().add(customer); + model.addCustomer(customer); panelTableModel.addRow(generateCustomerRow(customer)); } } - // ------------------------------------------------------------------------- - // remove button's event private class RemoveActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { @@ -145,18 +103,13 @@ return; if (1 < panelTable.getRowCount()) { - model.getCustomers().remove(panelTable.getSelectedRow()); + model.removeCustomer(panelTable.getSelectedRow()); panelTableModel.removeRow(panelTable.getSelectedRow()); } } } - // ------------------------------------------------------------------------- - // transition to history private class ShowHistoryActionHandler implements ActionListener { - - // ------------------------------------------------------------------------- - // public void actionPerformed(ActionEvent e) { if (-1 < panelTable.getSelectedRow()) transitionToHistory(panelTable.getSelectedRow()); diff --git a/src/panels/ShowHistoryPanel.java b/src/panels/ShowHistoryPanel.java index 2dee09e..c18b369 100644 --- a/src/panels/ShowHistoryPanel.java +++ b/src/panels/ShowHistoryPanel.java @@ -17,58 +17,41 @@ import resources.Customer; import resources.CustomersModel; -//------------------------------------------------------------------------- -// public class ShowHistoryPanel extends JPanel { - // frame private IMainFrame mainFrame; - // model private Customer selectedCustomer; private CustomersModel model; - // table private JList historiesList; private DefaultListModel historiesListModel; - // buttons private JLabel nameLabel; private JTextField textField; private JButton backButton; private JButton payButton; - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // constructor - // ------------------------------------------------------------------------- - // public ShowHistoryPanel(IMainFrame mainFrame, CustomersModel model) { super(new BorderLayout()); - // initialize frame this.mainFrame = mainFrame; - // initialize model this.model = model; - // initialize table historiesListModel = new DefaultListModel(); historiesList = new JList(historiesListModel); BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS); this.setLayout(layout); - // nameLabel = new JLabel(); nameLabel.setAlignmentX(CENTER_ALIGNMENT); add(nameLabel); - // add scroll menu JScrollPane scrollPanel = new JScrollPane(historiesList); add(scrollPanel); - // add payment fields JLabel inputLabel = new JLabel("payment?"); textField = new JTextField(25); payButton = new JButton("Payment"); @@ -84,60 +67,35 @@ this.add(panel); this.add(backButton); - // add listener backButton.addActionListener(new BackActionHandler()); payButton.addActionListener(new PaymentActionHandler()); } - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // public - // ------------------------------------------------------------------------- - // public void updateListModelByIndex(int customerId) { - selectedCustomer = model.getCustomers().get(customerId); + selectedCustomer = model.getCustomerById(customerId); historiesListModel.removeAllElements(); - historiesListModel.addAll(model.getCustomers().get(customerId).getHistory()); + historiesListModel.addAll(model.getCustomerById(customerId).getHistory()); nameLabel.setText("name: " + selectedCustomer.getName()); } - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // private - // ------------------------------------------------------------------------- - // private void transitionToMain() { mainFrame.showMainPanel(); } - // ------------------------------------------------------------------------- - // ------------------------------------------------------------------------- - // interface - // ------------------------------------------------------------------------- - // back button's event private class BackActionHandler implements ActionListener { - - // ------------------------------------------------------------------------- - // public void actionPerformed(ActionEvent e) { transitionToMain(); } } - // ------------------------------------------------------------------------- - // payment button's event private class PaymentActionHandler implements ActionListener { - - // ------------------------------------------------------------------------- - // public void actionPerformed(ActionEvent e) { String text = textField.getText(); - // filtering if (!text.matches("^[0-9]+$")) return; if (Integer.parseInt(text) <= 0) diff --git a/src/resources/Customer.java b/src/resources/Customer.java index 70c0fdd..23d816a 100644 --- a/src/resources/Customer.java +++ b/src/resources/Customer.java @@ -2,24 +2,23 @@ import java.util.List; -//------------------------------------------------------------------------- -// 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) { // added + private String name; + + public Customer(String name) { points = new Points(); total = new Total(); history = new History(total); payment = new Payment(points, history); - this.name = name; // added + this.name = name; } - + public void purchase(int x) { this.payment.purchase(x); } @@ -35,12 +34,11 @@ public List getHistory() { return history.getValue(); } - + public int getPoints() { return points.getValue(); } - // added public String getName() { return this.name; } diff --git a/src/resources/CustomersModel.java b/src/resources/CustomersModel.java index 233b625..5df0802 100644 --- a/src/resources/CustomersModel.java +++ b/src/resources/CustomersModel.java @@ -1,30 +1,27 @@ 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; + public Customer getCustomerById(int customerId) { + return customers.get(customerId); + } + + public void addCustomer(Customer customer) { + customers.add(customer); + } + + public void removeCustomer(int customerId) { + customers.remove(customerId); + } + + public int getCustomersCount() { + return customers.size(); } } diff --git a/src/resources/History.java b/src/resources/History.java index 93dc4dd..6952710 100644 --- a/src/resources/History.java +++ b/src/resources/History.java @@ -1,4 +1,5 @@ package resources; + import java.util.*; public class History { diff --git a/src/resources/Payment.java b/src/resources/Payment.java index ed04d04..db0291f 100644 --- a/src/resources/Payment.java +++ b/src/resources/Payment.java @@ -1,4 +1,5 @@ package resources; + import java.util.*; public class Payment { diff --git a/src/resources/Points.java b/src/resources/Points.java index 6455824..d3bea2d 100644 --- a/src/resources/Points.java +++ b/src/resources/Points.java @@ -1,5 +1,4 @@ package resources; -import java.util.*; public class Points { private int value; diff --git a/src/resources/Total.java b/src/resources/Total.java index 43e5eb1..12baf97 100644 --- a/src/resources/Total.java +++ b/src/resources/Total.java @@ -1,5 +1,6 @@ package resources; -import java.util.*; + +import java.util.List; public class Total { private int value;