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..51b157b --- /dev/null +++ b/src/Main.java @@ -0,0 +1,32 @@ +import java.awt.BorderLayout; +import java.awt.Container; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + +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); + } + }); + } +} 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..42c1f0e --- /dev/null +++ b/src/frames/MainFrame.java @@ -0,0 +1,69 @@ +package frames; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +import panels.*; + +//------------------------------------------------------------------------- +// +public class MainFrame extends JFrame { + public String[] stateNames = { "main", "history" }; + + // panel + JPanel curPanel; + + // cache + MainPanel mainPanel; + ShowHistoryPanel historyPanel; + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // constructor + // ------------------------------------------------------------------------- + // + public MainFrame() { + this.add(mainPanel); + mainPanel.setVisible(true); + + this.add(historyPanel); + historyPanel.setVisible(false); + + // set current panel + curPanel = mainPanel; + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // public + // ------------------------------------------------------------------------- + // + public void reloadPanel(String destinationPanelName) { + // main + if (destinationPanelName.equals(stateNames[0])) { + this.remove(this.mainPanel); + MainPanel mainPanel = new MainPanel(); + this.add(mainPanel); + } + // history + else if(destinationPanelName.equals(stateNames[1])) { + this.remove(this.historyPanel); + ShowHistoryPanel historyPanel = new ShowHistoryPanel(); + this.add(historyPanel); + } + } + + // ------------------------------------------------------------------------- + // + public void showMainPanel(JPanel nowPanel) { + nowPanel.setVisible(false); + mainPanel.setVisible(true); + } + + // ------------------------------------------------------------------------- + // + public void showHistoryPanel(JPanel nowPanel) { + nowPanel.setVisible(false); + historyPanel.setVisible(true); + } +} diff --git a/src/panels/MainPanel.java b/src/panels/MainPanel.java new file mode 100644 index 0000000..24a4221 --- /dev/null +++ b/src/panels/MainPanel.java @@ -0,0 +1,120 @@ + +package panels; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +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.JTextField; +import javax.swing.event.TableModelEvent; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableModel; + +import frames.MainFrame; +import resources.Customer; + +//------------------------------------------------------------------------- +// overview of customers +public class MainPanel extends JPanel { + + // model + private HashMap customerTable; + + // table + private JTable panelTable; + private DefaultTableModel panelTableModel; + private String[] columnNames = { "name", "total", "points" }; + + // buttons + private JButton addButton; + private JButton modifyButton; + private JButton removeButton; + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // constructor + // ------------------------------------------------------------------------- + // + public MainPanel() { + + super(new BorderLayout()); + + // initialize 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"); + modifyButton = new JButton("Modify"); + removeButton = new JButton("Remove"); + + // add listener + addButton.addActionListener(new AddActionHandler()); + removeButton.addActionListener(new RemoveActionHandler()); + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(addButton); + buttonPanel.add(modifyButton); + buttonPanel.add(removeButton); + add(buttonPanel, BorderLayout.SOUTH); + + // add scroll menu + JScrollPane listScrollPane = new JScrollPane(panelTable); + add(listScrollPane, BorderLayout.NORTH); + + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // private + // ------------------------------------------------------------------------- + // + private void transitionToHistory(MainFrame mainFrame) { + mainFrame.showHistoryPanel(this); + } + + // ------------------------------------------------------------------------- + // + private Object[] generateCustomerRow(String name) { + Customer customer = new Customer(name + panelTableModel.getRowCount()); + Object[] data = { customer.getName(), customer.getPOS().getTotal(), customer.getPOS().getPoints() }; + + return data; + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // interface + // ------------------------------------------------------------------------- + // add button's event + private class AddActionHandler implements ActionListener { + public void actionPerformed(ActionEvent e) { + panelTableModel.addRow(generateCustomerRow("name")); + } + } + + // ------------------------------------------------------------------------- + // remove button's event + private class RemoveActionHandler implements ActionListener { + public void actionPerformed(ActionEvent e) { + + if (panelTable.getSelectedRow() < 0) + return; + + if (1 < panelTable.getRowCount()) + panelTableModel.removeRow(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..f07b095 --- /dev/null +++ b/src/panels/ShowHistoryPanel.java @@ -0,0 +1,88 @@ +package panels; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.HashMap; + +import javax.swing.JButton; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.table.DefaultTableModel; + +import frames.MainFrame; +import resources.Customer; + +//------------------------------------------------------------------------- +// +public class ShowHistoryPanel extends JPanel { + + // model + private HashMap customerTable; + + // table + private JTable panelTable; + private DefaultTableModel panelTableModel; + private String[] columnNames = { "name", "total", "points" }; + + // buttons + private JButton addButton; + private JButton modifyButton; + private JButton removeButton; + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // constructor + // ------------------------------------------------------------------------- + // + public ShowHistoryPanel() { + super(new BorderLayout()); + + // initialize 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"); + modifyButton = new JButton("Modify"); + removeButton = new JButton("Remove"); + + // add listener + // todo: add back to main + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(addButton); + buttonPanel.add(modifyButton); + buttonPanel.add(removeButton); + add(buttonPanel, BorderLayout.SOUTH); + + // add scroll menu + JScrollPane listScrollPane = new JScrollPane(panelTable); + add(listScrollPane, BorderLayout.NORTH); + + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // private + // ------------------------------------------------------------------------- + // + private void transitionToHistory(MainFrame mainFrame) { + mainFrame.showHistoryPanel(this); + } + + // ------------------------------------------------------------------------- + // + private Object[] generateCustomerRow(String name) { + Customer customer = new Customer(name + panelTableModel.getRowCount()); + Object[] data = { customer.getName(), customer.getPOS().getTotal(), customer.getPOS().getPoints() }; + + return data; + } +} diff --git a/src/resources/Customer.java b/src/resources/Customer.java new file mode 100644 index 0000000..ff0afb0 --- /dev/null +++ b/src/resources/Customer.java @@ -0,0 +1,39 @@ +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; + } +} 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/POS.java b/src/resources/POS.java new file mode 100644 index 0000000..7a3076b --- /dev/null +++ b/src/resources/POS.java @@ -0,0 +1,32 @@ +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 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