diff --git a/src/main/java/src/TasksFrame.java b/src/main/java/src/TasksFrame.java index 603f328..2fc7fb0 100644 --- a/src/main/java/src/TasksFrame.java +++ b/src/main/java/src/TasksFrame.java @@ -66,7 +66,7 @@ public class TasksFrame extends JFrame { setupDiff(); - setupSumCalc(0, 10); + setupSumFromTo(); setup5ModList(1, 20); @@ -207,22 +207,51 @@ public class TasksFrame extends JFrame { return a-b; } - private void setupSumCalc(int from, int to) { + + public void setupSumFromTo(){ JPanel jPanel = setupPanel("Sum from to"); - JLabel jLabel = new JLabel(); - jLabel.setVisible(true); - jLabel.setBounds(30, 30, 50, 20); + JTextField a = new JTextField(); + a.setBounds(10, 50, 30, 20); + a.setVisible(true); + + JTextField b = new JTextField(); + b.setBounds(50, 50, 30, 20); + b.setVisible(true); + + JTextField result = new JTextField(); + result.setBounds(100, 50, 40, 20); + result.setVisible(true); + + JButton sumFromTo = new JButton("calculate"); + sumFromTo.setBounds(10,90,100,30); + sumFromTo.setVisible(true); + + sumFromTo.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String texta = a.getText(); + String textb = b.getText(); + Integer val_a = Integer.valueOf(texta); + Integer val_b = Integer.valueOf(textb); + Integer res = sumCalc(val_a, val_b); + result.setText(String.valueOf(res)); + } + }); + jPanel.add(a); + jPanel.add(b); + jPanel.add(result); + jPanel.add(sumFromTo); + } + + public int sumCalc(int from, int to) { int sum = 0; for (int i = from; i < to; i++) { sum += i; } - - jLabel.setText(String.valueOf(sum)); - - jPanel.add(jLabel); + return sum+to; } diff --git a/src/test/java/src/TasksFrameTest.java b/src/test/java/src/TasksFrameTest.java index 146396e..ebe61ca 100644 --- a/src/test/java/src/TasksFrameTest.java +++ b/src/test/java/src/TasksFrameTest.java @@ -35,6 +35,17 @@ class TasksFrameTest { assertEquals(-10,sum3); } + @Test + void setupSumFromTo(){ + TasksFrame frame = new TasksFrame(); + int p1= frame.sumCalc(1,5); + assertEquals(15,p1); + int p2= frame.sumCalc(1,10); + assertEquals(55,p2); + int p3= frame.sumCalc(0,2); + assertEquals(3,p3); + } + }