From 6bdd1788434945a6a24f76050fb466dd7878d9e5 Mon Sep 17 00:00:00 2001 From: Bo Lee Date: Wed, 7 Feb 2018 00:23:40 -0500 Subject: [PATCH 1/2] Sum Or Prduct --- src/main/java/Main.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 1dbc0cb..e2d0f77 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,29 @@ +import java.util.Scanner; + /** * Created by iyasuwatts on 10/17/17. */ public class Main { public static void main(String[] args){ - + Scanner userInput = new Scanner(System.in); + System.out.println("Please enter a number: "); + int userNum = userInput.nextInt(); + System.out.println("Please enter 'sum' or 'product' :"); + String userChoice = userInput.next(); + int sumTotal = 0; + int productTotal = 1; + if (userChoice.equals("sum")){ + for (int i = 1; i <= userNum; i++) { + sumTotal += i; + } + System.out.println("The sum of the numbers 1 to " + userNum +" is " +sumTotal); + } + else if (userChoice.equals("product")) { + for (int k = 1; k <= userNum; k++) { + productTotal *= k; + } + System.out.println("The product of the numbers 1 to " + userNum +" is " +productTotal); + } } } From 05ed10129b23b439382785ecd3199b256210ced4 Mon Sep 17 00:00:00 2001 From: Bo Lee Date: Sun, 1 Apr 2018 21:48:28 -0400 Subject: [PATCH 2/2] Refactored Code --- pom.xml | 7 +++++++ src/Test/MainTest.java | 32 ++++++++++++++++++++++++++++++++ src/main/java/Main.java | 34 ++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 src/Test/MainTest.java diff --git a/pom.xml b/pom.xml index deaeaef..8f7b911 100644 --- a/pom.xml +++ b/pom.xml @@ -7,5 +7,12 @@ io.zipcoder MicroLabs-OOP-SumOrProduct 1.0-SNAPSHOT + + + junit + junit + RELEASE + + \ No newline at end of file diff --git a/src/Test/MainTest.java b/src/Test/MainTest.java new file mode 100644 index 0000000..f081674 --- /dev/null +++ b/src/Test/MainTest.java @@ -0,0 +1,32 @@ +import org.junit.Assert; +import org.junit.Test; + + +public class MainTest { + + @Test + public void sumOfOneToN1() { + //Given + Main main = new Main(); + + //When + int expected = main.sumOfOneToN(4, "sum"); + int actual = 10; + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void sumOfOneToN2() { + //Given + Main main = new Main(); + + //When + int expected = main.sumOfOneToN(4, "product"); + int actual = 24; + + //Then + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index e2d0f77..61f2ac4 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -5,25 +5,35 @@ */ public class Main { - public static void main(String[] args){ - Scanner userInput = new Scanner(System.in); - System.out.println("Please enter a number: "); - int userNum = userInput.nextInt(); - System.out.println("Please enter 'sum' or 'product' :"); - String userChoice = userInput.next(); + public int sumOfOneToN(int num, String choice){ int sumTotal = 0; int productTotal = 1; - if (userChoice.equals("sum")){ - for (int i = 1; i <= userNum; i++) { + int answer = 0; + if (choice.equals("sum")){ + for (int i = 1; i <= num; i++) { sumTotal += i; } - System.out.println("The sum of the numbers 1 to " + userNum +" is " +sumTotal); + answer += sumTotal; + System.out.println("The sum of the numbers 1 to " + num +" is " +sumTotal); } - else if (userChoice.equals("product")) { - for (int k = 1; k <= userNum; k++) { + else if (choice.equals("product")) { + for (int k = 1; k <= num; k++) { productTotal *= k; } - System.out.println("The product of the numbers 1 to " + userNum +" is " +productTotal); + answer += productTotal; + System.out.println("The product of the numbers 1 to " + num +" is " +productTotal); } + return answer; + } + + public static void main(String[] args){ + Scanner userInput = new Scanner(System.in); + System.out.println("Please enter a number: "); + int userNum = userInput.nextInt(); + System.out.println("Please enter 'sum' or 'product' :"); + String userChoice = userInput.next(); + + Main main = new Main(); + main.sumOfOneToN(userNum, userChoice); } }