diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/src/History.java b/src/History.java deleted file mode 100644 index fcd9b7c..0000000 --- a/src/History.java +++ /dev/null @@ -1,16 +0,0 @@ -import java.util.*; - -public class History { - private Total total; - private List value = new ArrayList<>(); - public void updatePayment(int payment) { - this.value.add(0, payment); - total.updateHistory(value); - } - public History(Total total) { - this.total = total; - } - public List getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..c21df79 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,27 @@ +import java.awt.BorderLayout; +import java.awt.Container; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + +import frames.MainFrame; +import panels.*; + +public class Main { + + // ------------------------------------------------------------------------- + // + public static void main(String[] args) { + + // ------------------------------------------------------------------------- + // + SwingUtilities.invokeLater(new Runnable() { + public void run() { + + // generate window + JFrame mainFrame = new MainFrame(); + } + }); + // ------------------------------------------------------------------------- + } +} diff --git a/src/POS.java b/src/POS.java deleted file mode 100644 index decefac..0000000 --- a/src/POS.java +++ /dev/null @@ -1,29 +0,0 @@ -import java.util.*; - -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 diff --git a/src/Payment.java b/src/Payment.java deleted file mode 100644 index b5b6be6..0000000 --- a/src/Payment.java +++ /dev/null @@ -1,19 +0,0 @@ -import java.util.*; - -public class Payment { - private Points points; - private History history; - private int value; - public Payment(Points points, History history) { - this.points = points; - this.history = history; - } - public void purchase(int x) { - this.value = x; - points.updatePayment(value); - history.updatePayment(value); - } - public int getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/Points.java b/src/Points.java deleted file mode 100644 index c776197..0000000 --- a/src/Points.java +++ /dev/null @@ -1,11 +0,0 @@ -import java.util.*; - -public class Points { - private int value; - public void updatePayment(int payment) { - value = (int)Math.floor((payment*0.05)); - } - public int getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/Total.java b/src/Total.java deleted file mode 100644 index 27996b5..0000000 --- a/src/Total.java +++ /dev/null @@ -1,15 +0,0 @@ -import java.util.*; - -public class Total { - private int value; - public void updateHistory(List history) { - Integer temp_sum1 = 0; - for (Integer x: history) { - temp_sum1 += x; - } - value = temp_sum1; - } - public int getValue() { - return value; - } -} \ No newline at end of file diff --git a/src/frames/MainFrame.java b/src/frames/MainFrame.java new file mode 100644 index 0000000..af9b221 --- /dev/null +++ b/src/frames/MainFrame.java @@ -0,0 +1,107 @@ +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 { + + // field + private State state; + + // model + private CustomersModel model; + + // panel + private JPanel curPanel; + + // cache + private MainPanel mainPanel; + private ShowHistoryPanel historyPanel; + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // constructor + // ------------------------------------------------------------------------- + // + public MainFrame() { + super("POS"); + + 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); + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // public + // ------------------------------------------------------------------------- + // + public void reloadPanel(String destinationPanelName) { + // main + if (destinationPanelName.equals(State.MAIN)) { + this.remove(this.mainPanel); + MainPanel mainPanel = new MainPanel(this, model); + this.add(mainPanel); + } + // history + else if (destinationPanelName.equals(State.DETAIL)) { + this.remove(this.historyPanel); + ShowHistoryPanel historyPanel = new ShowHistoryPanel(this, model); + this.add(historyPanel); + } + } + + // ------------------------------------------------------------------------- + // + public void showMainPanel(JPanel nowPanel) { + getContentPane().removeAll(); + + mainPanel.updateTableByTransition(); + + add(mainPanel); + validate(); + repaint(); + } + + // ------------------------------------------------------------------------- + // + 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 new file mode 100644 index 0000000..5a691bd --- /dev/null +++ b/src/panels/MainPanel.java @@ -0,0 +1,174 @@ + +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; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.event.TableModelEvent; +import javax.swing.table.DefaultTableModel; +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 CustomersModel model; + + // table + private JTable panelTable; + private DefaultTableModel panelTableModel; + private String[] columnNames = { "name", "point", "total" }; + + // buttons + private JButton addButton; + private JButton removeButton; + private JButton showDetailButton; + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // constructor + // ------------------------------------------------------------------------- + // + public MainPanel(MainFrame 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"); + 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, 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); + 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); + } + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // private + // ------------------------------------------------------------------------- + // + private void transitionToHistory(MainFrame mainFrame, int index) { + mainFrame.showHistoryPanel(this, index); + } + + // ------------------------------------------------------------------------- + // + 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; + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // interface + // ------------------------------------------------------------------------- + // add button's event + private class AddActionHandler implements ActionListener { + public void actionPerformed(ActionEvent e) { + Customer customer = createNewCustomerByName("name"); + model.getCustomers().add(customer); + panelTableModel.addRow(generateCustomerRow(customer)); + } + } + + // ------------------------------------------------------------------------- + // remove button's event + private class RemoveActionHandler implements ActionListener { + public void actionPerformed(ActionEvent e) { + + if (panelTable.getSelectedRow() < 0) + return; + + 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 new file mode 100644 index 0000000..ff77477 --- /dev/null +++ b/src/panels/ShowHistoryPanel.java @@ -0,0 +1,121 @@ +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; +import javax.swing.table.DefaultTableModel; + +import frames.MainFrame; +import resources.Customer; +import resources.CustomersModel; + +//------------------------------------------------------------------------- +// +public class ShowHistoryPanel extends JPanel { + + // frame + private MainFrame mainFrame; + + // model + private Customer selectedCustomer; + private CustomersModel model; + + // table + private JList historiesList; + private DefaultListModel historiesListModel; + + // buttons + private JButton backButton; + private JButton payButton; + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // constructor + // ------------------------------------------------------------------------- + // + public ShowHistoryPanel(MainFrame 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); + + // add buttons + JPanel buttonPane = new JPanel(); + backButton = new JButton("Back"); + payButton = new JButton("Payment"); + buttonPane.add(backButton); + buttonPane.add(payButton); + add(buttonPane, BorderLayout.SOUTH); + + // add listener + backButton.addActionListener(new BackActionHandler()); + payButton.addActionListener(new PaymentActionHandler()); + + // add scroll menu + 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 transitionToMain(MainFrame mainFrame) { + mainFrame.showMainPanel(this); + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // interface + // ------------------------------------------------------------------------- + // back button's event + private class BackActionHandler implements ActionListener { + + // ------------------------------------------------------------------------- + // + 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 new file mode 100644 index 0000000..225b25d --- /dev/null +++ b/src/resources/Customer.java @@ -0,0 +1,41 @@ +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/History.java b/src/resources/History.java new file mode 100644 index 0000000..93dc4dd --- /dev/null +++ b/src/resources/History.java @@ -0,0 +1,17 @@ +package resources; +import java.util.*; + +public class History { + private Total total; + private List value = new ArrayList<>(); + public void updatePayment(int payment) { + this.value.add(0, payment); + total.updateHistory(value); + } + public History(Total total) { + this.total = total; + } + public List getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/resources/Payment.java b/src/resources/Payment.java new file mode 100644 index 0000000..ed04d04 --- /dev/null +++ b/src/resources/Payment.java @@ -0,0 +1,20 @@ +package resources; +import java.util.*; + +public class Payment { + private Points points; + private History history; + private int value; + public Payment(Points points, History history) { + this.points = points; + this.history = history; + } + public void purchase(int x) { + this.value = x; + points.updatePayment(value); + history.updatePayment(value); + } + public int getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/resources/Points.java b/src/resources/Points.java new file mode 100644 index 0000000..6455824 --- /dev/null +++ b/src/resources/Points.java @@ -0,0 +1,12 @@ +package resources; +import java.util.*; + +public class Points { + private int value; + public void updatePayment(int payment) { + value = (int)Math.floor((payment*0.05)); + } + public int getValue() { + return value; + } +} \ No newline at end of file diff --git a/src/resources/Total.java b/src/resources/Total.java new file mode 100644 index 0000000..43e5eb1 --- /dev/null +++ b/src/resources/Total.java @@ -0,0 +1,16 @@ +package resources; +import java.util.*; + +public class Total { + private int value; + public void updateHistory(List history) { + Integer temp_sum1 = 0; + for (Integer x: history) { + temp_sum1 += x; + } + value = temp_sum1; + } + public int getValue() { + return value; + } +} \ No newline at end of file