diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..679b7e7 --- /dev/null +++ b/.classpath @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..55ba248 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + POS_for_GUI + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..82bf499 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=13 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=13 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=13 diff --git a/README.md b/README.md index 81881a9..c2cf453 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -POS_for_GUI_tests +POS_for_GUI =============== -POS_for_GUI のテスト用 \ No newline at end of file +POSシステムのGUIアプリケーション \ No newline at end of file diff --git a/lib/abbot.jar b/lib/abbot.jar new file mode 100644 index 0000000..2d366ae --- /dev/null +++ b/lib/abbot.jar Binary files differ diff --git a/lib/ant4eclipse/ant4eclipse.jar b/lib/ant4eclipse/ant4eclipse.jar new file mode 100644 index 0000000..d10e673 --- /dev/null +++ b/lib/ant4eclipse/ant4eclipse.jar Binary files differ diff --git a/lib/costello.jar b/lib/costello.jar new file mode 100644 index 0000000..f5b7286 --- /dev/null +++ b/lib/costello.jar Binary files differ diff --git a/lib/example.jar b/lib/example.jar new file mode 100644 index 0000000..f3e3a71 --- /dev/null +++ b/lib/example.jar Binary files differ diff --git a/lib/gnu-regexp-1.1.4.jar b/lib/gnu-regexp-1.1.4.jar new file mode 100644 index 0000000..c3c8770 --- /dev/null +++ b/lib/gnu-regexp-1.1.4.jar Binary files differ diff --git a/lib/groovy-all-1.8.1.jar b/lib/groovy-all-1.8.1.jar new file mode 100644 index 0000000..a1f432b --- /dev/null +++ b/lib/groovy-all-1.8.1.jar Binary files differ diff --git a/lib/jdom-1.1.1.jar b/lib/jdom-1.1.1.jar new file mode 100644 index 0000000..65a1b3f --- /dev/null +++ b/lib/jdom-1.1.1.jar Binary files differ diff --git a/lib/junit-4.8.2.jar b/lib/junit-4.8.2.jar new file mode 100644 index 0000000..5b4bb84 --- /dev/null +++ b/lib/junit-4.8.2.jar Binary files differ diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..5d49e85 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,15 @@ +import javax.swing.JFrame; + +import frames.MainFrame; + +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 new file mode 100644 index 0000000..a0d20e0 --- /dev/null +++ b/src/frames/IMainFrame.java @@ -0,0 +1,10 @@ +package frames; + +import javax.swing.JPanel; + +// ------------------------------------------------------------------------- +// +public interface IMainFrame { + void showMainPanel(); + void showHistoryPanel(int index); +} diff --git a/src/frames/MainFrame.java b/src/frames/MainFrame.java new file mode 100644 index 0000000..a6b9d8a --- /dev/null +++ b/src/frames/MainFrame.java @@ -0,0 +1,87 @@ +package frames; + +import java.awt.BorderLayout; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +import panels.MainPanel; +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(); + + mainPanel.updateTableByTransition(); + + add(mainPanel); + validate(); + repaint(); + } + + // ------------------------------------------------------------------------- + // + public void showHistoryPanel(int index) { + getContentPane().removeAll(); + + historyPanel.setVisible(true); + historyPanel.updateListModelByIndex(index); + + add(historyPanel); + validate(); + repaint(); + } + + public CustomersModel getModel() { + return model; + } + + public MainPanel getMainPanel() { + return mainPanel; + } + + public ShowHistoryPanel getHistoryPanel() { + return historyPanel; + } +} diff --git a/src/panels/MainPanel.java b/src/panels/MainPanel.java new file mode 100644 index 0000000..5c817c5 --- /dev/null +++ b/src/panels/MainPanel.java @@ -0,0 +1,181 @@ + +package panels; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.table.DefaultTableModel; + +import frames.IMainFrame; +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" }; + + // 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()); + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(addButton, BorderLayout.WEST); + 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); + } + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // private + // ------------------------------------------------------------------------- + // + private void transitionToHistory(int index) { + mainFrame.showHistoryPanel(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 { + + // ------------------------------------------------------------------------- + // + public void actionPerformed(ActionEvent e) { + if (-1 < panelTable.getSelectedRow()) + transitionToHistory(panelTable.getSelectedRow()); + } + } + + public JButton getAddButton() { + return addButton; + } + + public JButton getRemoveButton() { + return removeButton; + } + + public JButton getShowHistoryButton() { + return showHistoryButton; + } + + public JTable getPanelTable() { + return panelTable; + } +} \ No newline at end of file diff --git a/src/panels/ShowHistoryPanel.java b/src/panels/ShowHistoryPanel.java new file mode 100644 index 0000000..e97b0a7 --- /dev/null +++ b/src/panels/ShowHistoryPanel.java @@ -0,0 +1,163 @@ +package panels; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BoxLayout; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextField; + +import frames.IMainFrame; +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"); + backButton = new JButton("Back"); + backButton.setAlignmentX(CENTER_ALIGNMENT); + + JPanel panel = new JPanel(); + panel.add(inputLabel); + panel.add(textField); + panel.add(payButton); + panel.add(backButton); + + 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); + + historiesListModel.removeAllElements(); + historiesListModel.addAll(model.getCustomers().get(customerId).getHistory()); + + nameLabel.setText("name: " + selectedCustomer.getName()); + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // private + // ------------------------------------------------------------------------- + // + private void transitionToMain(IMainFrame mainFrame) { + mainFrame.showMainPanel(); + } + + // ------------------------------------------------------------------------- + // ------------------------------------------------------------------------- + // 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) { + + String text = textField.getText(); + + // filtering + if (!text.matches("^[0-9]+$")) + return; + if (Integer.parseInt(text) <= 0) + return; + + selectedCustomer.purchase(Integer.parseInt(textField.getText())); + historiesListModel.removeAllElements(); + historiesListModel.addAll(selectedCustomer.getHistory()); + } + } + + public JTextField getTextField() { + return textField; + } + + public JButton getBackButton() { + return backButton; + } + + public JButton getPayButton() { + return payButton; + } +} diff --git a/src/resources/Customer.java b/src/resources/Customer.java new file mode 100644 index 0000000..70c0fdd --- /dev/null +++ b/src/resources/Customer.java @@ -0,0 +1,47 @@ +package resources; + +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 + points = new Points(); + total = new Total(); + history = new History(total); + payment = new Payment(points, history); + + this.name = name; // added + } + + 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 diff --git a/src/tests/GUITestPayment.java b/src/tests/GUITestPayment.java new file mode 100644 index 0000000..a88dd3b --- /dev/null +++ b/src/tests/GUITestPayment.java @@ -0,0 +1,55 @@ +package tests; +import javax.swing.JButton; +import javax.swing.JTextField; + +import abbot.tester.JTextComponentTester; +import frames.MainFrame; +import junit.extensions.abbot.ComponentTestFixture; +import panels.ShowHistoryPanel; +import resources.Customer; +import resources.CustomersModel; + +public class GUITestPayment extends ComponentTestFixture { + + public GUITestPayment(String name) { + super(name); + } + + public static void main(String[] args) { + GUITestPayment test = new GUITestPayment("Payment"); + test.doTest(); + } + + private void doTest() { + JTextComponentTester tester = new JTextComponentTester(); + MainFrame mainFrame = new MainFrame(); + mainFrame.setVisible(true); + tester.waitForIdle(); + tester.actionClick(mainFrame.getMainPanel().getAddButton()); + tester.waitForIdle(); + tester.actionClick(mainFrame.getMainPanel().getPanelTable()); + tester.waitForIdle(); + tester.actionClick(mainFrame.getMainPanel().getShowHistoryButton()); + tester.waitForIdle(); + + ShowHistoryPanel historyPanel = mainFrame.getHistoryPanel(); + JTextField textField = historyPanel.getTextField(); + JButton payButton = historyPanel.getPayButton(); + tester.actionEnterText(textField, "100"); + tester.waitForIdle(); + Customer customer = mainFrame.getModel().getCustomers().get(0); + + long time = System.nanoTime(); + for (int i = 0; i < 1000; i++) { +// customer.purchase(100); + tester.actionClick(payButton); + } + System.out.println(System.nanoTime() - time); + time = System.nanoTime(); + for (int i = 0; i < 1000; i++) { + int total = customer.getTotal(); + } + System.out.println(System.nanoTime() - time); + } + +} diff --git a/src/tests/TestPayment.java b/src/tests/TestPayment.java new file mode 100644 index 0000000..5be5a85 --- /dev/null +++ b/src/tests/TestPayment.java @@ -0,0 +1,20 @@ +package tests; +import resources.Customer; + +public class TestPayment { + + public static void main(String[] args) { + Customer customer = new Customer("name0"); + long time = System.nanoTime(); + for (int i = 0; i < 1000; i++) { + customer.purchase(100); + } + System.out.println(System.nanoTime() - time); + time = System.nanoTime(); + for (int i = 0; i < 1000; i++) { + int total = customer.getTotal(); + } + System.out.println(System.nanoTime() - time); + } + +}