Skip to content

Sum Or Product-Bo #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
<groupId>io.zipcoder</groupId>
<artifactId>MicroLabs-OOP-SumOrProduct</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>

</project>
32 changes: 32 additions & 0 deletions src/Test/MainTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
30 changes: 30 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import java.util.Scanner;

/**
* Created by iyasuwatts on 10/17/17.
*/
public class Main {

public int sumOfOneToN(int num, String choice){
int sumTotal = 0;
int productTotal = 1;
int answer = 0;
if (choice.equals("sum")){
for (int i = 1; i <= num; i++) {
sumTotal += i;
}
answer += sumTotal;
System.out.println("The sum of the numbers 1 to " + num +" is " +sumTotal);
}
else if (choice.equals("product")) {
for (int k = 1; k <= num; k++) {
productTotal *= k;
}
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);
}
}