diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java new file mode 100644 index 0000000..aead7fc --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Arithmetic.java @@ -0,0 +1,399 @@ +package com.zipcodewilmington.scientificcalculator; + +import org.sonatype.guice.bean.containers.Main; + +import java.lang.invoke.SwitchPoint; +import java.util.Scanner; +public class Arithmetic { + String welcome = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Welcome Menu a) Addition |" + '\n') + + ("| s) Subtraction |" + '\n') + + ("| m) Multiplication |" + '\n') + + ("|Please enter the character of the arithmetic function you would like to use: d) Division |" + '\n') + + ("| q) Square |" + '\n') + + ("| r) Square Root |" + '\n') + + ("| i) Inverse |" + '\n') + + ("| c) Check Memory |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|"+'\n'); + + public Arithmetic() { + } + + ; + + public void DisplayMenu(Scanner response2) { + + + System.out.println(welcome); + String response = response2.nextLine(); + char character = response.toLowerCase().charAt(0); + switch (character) { + case 'a': + add(run()); + break; + case 's': + sub(run()); + break; + case 'm': + mult(run()); + break; + case 'd': + div(run()); + break; + case 'q': + square(run()); + break; + case 'r': + sqrt(run()); + break; + case 'i': + inv(run()); + break; + case 'c': + DisplayMemory(run()); + default: + System.out.println(""); + System.out.println(""); + System.out.println("You have selected an invalid input."); + System.out.println("Waiting three seconds and restarting"); + try { + Thread.sleep(3000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Program Resuming"); + try { + Thread.sleep(2000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + DisplayMenu(run()); + } + results(response2); + } + + public static double add(Scanner response) { + + String addi = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to add first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(addi); + double ret1 = response.nextDouble(); + String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to add to: " + ret1 + " |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(add2); + double ret = response.nextDouble(); + + double a = (ret1 + ret); + MainApplication.setval(a); + + return a; + + + } + + + public static double sub(Scanner response) { + String addi = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to subtract from first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + System.out.println(addi); + double ret1 = response.nextDouble(); + String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to subtract from: " + ret1 + " |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(add2); + + + double ret2 = response.nextDouble(); + double fin = (ret1 - ret2); + MainApplication.setval(fin); + return fin; + + } + + + public static double mult(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to Multiply from first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(addi); + double ret1 = response.nextDouble(); + String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to multiply to: " + ret1 + " |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(add2); + double ret2 = response.nextDouble(); + double f = (ret1 * ret2); + MainApplication.setval(f); + return f; + } + + + public static double div(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to divide from first: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(addi); + double ret1 = response.nextDouble(); + String add2 = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to divide " + ret1 + " by |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(add2); + double ret2 = response.nextDouble(); + double f = (ret1 / ret2); + MainApplication.setval(f); + return f; + + + } + + public static double square(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to square: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + System.out.println(addi); + double ret2 = response.nextDouble(); + double f = (ret2 * ret2); + MainApplication.setval(f); + return f; + + + } + + + public static double sqrt(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to take the square root of |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + System.out.println(addi); + double ret2 = response.nextDouble(); + + double f = Math.sqrt(ret2); + MainApplication.setval(f); + return f; + } + + + public static double inv(Scanner response) { + String addi = + ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Calculation Menu |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|Please enter the number you would like to take the inverse of: |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + + System.out.println(addi); + double ret2 = response.nextDouble(); + + if (ret2 == 0) { + System.out.println("You cannot take the inverse of 0. Restarting Program"); + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + + + } + System.out.println("Restarting now"); + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + inv(run()); + + + } + double inv = 1 / ret2; + MainApplication.setval(inv); + return (1 / ret2); + } + + public void results(Scanner response2) { + + double res = MainApplication.getCurrentVal(); + + String addi = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Results Menu |" + '\n') + + ("| a)Clear display and do a new calculation |" + '\n') + + ("| b)Store result in memory |" + '\n') + + ("|The result of your calculation is: " + res + " c)Quit App |" + '\n') + + ("| |" + '\n') + + ("|Please select what you would like to do next |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(addi); + String response = response2.nextLine(); + char character = response.toLowerCase().charAt(0); + switch (character) { + case 'a': + ClearDisplay(); + break; + case 'b': + StoreMemory(res); + DisplayMenu(run()); + break; + case 'c': + QuitApp(); + break; + default: + System.out.println(""); + System.out.println(""); + System.out.println("You have selected an invalid input."); + System.out.println("Waiting three seconds and restarting"); + try { + Thread.sleep(3000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Program Resuming"); + try { + Thread.sleep(2000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + results(run()); + } + + } + + public void QuitApp() { + System.exit(0); + } + + public void ClearDisplay() { + MainApplication.setval(0); + DisplayMenu(run()); + } + + + public static Scanner run() { + Scanner response = new Scanner(System.in); + return response; + } + + public void StoreMemory(double CurrentValue) { + MainApplication.setMemory(CurrentValue); + } + + public void DisplayMemory(Scanner scan) { + + String menu = ("|----------------------------------------------------------------------------------------------------|" + '\n') + + ("| Memory Menu a)Go back to main menu |" + '\n') + + ("| b)Clear memory |" + '\n') + + ("| c)Quit App |" + '\n') + + ("|The current value of your memory is: " + MainApplication.getMemory() + " |" + '\n') + + ("| |" + '\n') + + ("|Please select what you would like to do next |" + '\n') + + ("| |" + '\n') + + ("|----------------------------------------------------------------------------------------------------|" + '\n'); + System.out.println(menu); + String scanf = scan.nextLine(); + char character = scanf.toLowerCase().charAt(0); + switch (character) { + case 'a': + DisplayMenu(run()); + break; + case 'b': + MainApplication.setMemory(0); + DisplayMenu(run()); + break; + case 'c': + QuitApp(); + break; + default: + System.out.println(""); + System.out.println(""); + System.out.println("You have selected an invalid input."); + System.out.println("Waiting three seconds and restarting"); + try { + Thread.sleep(3000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Program Resuming"); + try { + Thread.sleep(2000); // Pause for 5 seconds (5000 milliseconds) + } catch (InterruptedException e) { + e.printStackTrace(); + } + DisplayMemory(run()); + } + + + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f42132..c6fc32a 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,17 +1,36 @@ package com.zipcodewilmington.scientificcalculator; - +import java.util.Scanner; /** * Created by leon on 2/9/18. */ public class MainApplication { - public static void main(String[] args) { - Console.println("Welcome to my calculator!"); - String s = Console.getStringInput("Enter a string"); - Integer i = Console.getIntegerInput("Enter an integer"); - Double d = Console.getDoubleInput("Enter a double."); +private static double CurrentValue=0; + + + + + private static double Memory = 0; + public MainApplication() + { - Console.println("The user input %s as a string", s); - Console.println("The user input %s as a integer", i); - Console.println("The user input %s as a d", d); } -} + public static void main(String[] args) { + Arithmetic menus = new Arithmetic(); + menus.DisplayMenu(menus.run()); + } + public static double getCurrentVal() + { + return CurrentValue; + } + public static double getMemory() { + return Memory; + } + public static void setMemory(double memory) { + Memory = memory; + } + public static double setval(double f) + { + CurrentValue = f; + return f; + } +} \ No newline at end of file diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java new file mode 100644 index 0000000..6b9658f --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestArithmetic.java @@ -0,0 +1,172 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.*; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import java.util.Scanner; +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; + +public class TestArithmetic { + private Arithmetic arithmetic; + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private Scanner in; + + @Before + public void setup(){ + arithmetic = new Arithmetic(); + System.setOut(new PrintStream(outContent)); + } + + @After + public void restoreStreams() { + System.setOut(originalOut); + } + + /* + * Addition tests + */ + @Test + public void testAdditionInts(){ + in = new Scanner("17 26"); + + double result = arithmetic.add(in); + Assert.assertEquals(17.0+26.0, result); + } + + @Test + public void testAdditionDoubles(){ + in = new Scanner("1.7 2.6"); + + double result = arithmetic.add(in); + Assert.assertEquals(1.7+2.6, result); + } + + /* + * Subtraction tests + */ + @Test + public void testSubtractionInts(){ + in = new Scanner("17 26"); + + double result = arithmetic.sub(in); + Assert.assertEquals(-9.0, result); + } + + @Test + public void testSubtractionDoubles(){ + in = new Scanner("1.7 2.6"); + + double result = arithmetic.sub(in); + Assert.assertEquals(1.7-2.6, result); + } + + /* + * Multiplication tests + */ + @Test + public void testMultiplicationInts(){ + in = new Scanner("17 26"); + + double result = arithmetic.mult(in); + Assert.assertEquals(17.0*26.0, result); + } + + @Test + public void testMultiplicationDoubles(){ + in = new Scanner("1.7 2.6"); + + double result = arithmetic.mult(in); + Assert.assertEquals(1.7*2.6, result); + } + + /* + * Division tests + */ + @Test + public void testDivisionInts(){ + in = new Scanner("17 26"); + + double result = arithmetic.div(in); + Assert.assertEquals(17.0/26.0, result); + } + + @Test + public void testDivisionDoubles(){ + in = new Scanner("1.7 2.6"); + + double result = arithmetic.div(in); + Assert.assertEquals(4.42, result); + } + + @Test + public void testDivisionBy0(){ + in = new Scanner("0 0 17 26"); + String divErrMessage = "You cannot divide by 0. Restarting Program"; + + double result = arithmetic.div(in); + Assert.assertTrue(outContent.toString().contains(divErrMessage)); + Assert.assertEquals(17.0/26.0, result); + } + /* + * Square tests + */ + @Test + public void testSquareInts(){ + in = new Scanner("17"); + + double result = arithmetic.square(in); + Assert.assertEquals(17.0*17.0, result); + } + + @Test + public void testSquareDoubles(){ + in = new Scanner("1.7"); + + double result = arithmetic.square(in); + Assert.assertEquals(1.7*1.7, result); + } + + /* + * Square Root tests + */ + @Test + public void testSquareRootInts(){ + in = new Scanner("169"); + + double result = arithmetic.sqrt(in); + Assert.assertEquals(13.0, result); + } + + @Test + public void testSquareRootDoubles(){ + in = new Scanner("1.69"); + + double result = arithmetic.sqrt(in); + Assert.assertEquals(1.3, result); + } + + /* + * Inverse tests + */ + @Test + public void testInverse(){ + in = new Scanner("17"); + + double result = arithmetic.inv(in); + Assert.assertEquals(1.0/17.0, result); + } + + @Test + public void testInverseOf0(){ + in = new Scanner("0 17"); + String invErrMessage = "You cannot take the inverse of 0. Restarting Program"; + + double result = arithmetic.div(in); + Assert.assertTrue(outContent.toString().contains(invErrMessage)); + Assert.assertEquals(1.0/17.0, result); + } +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 94e8d98..b30bdcc 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -1,7 +1,41 @@ package com.zipcodewilmington.scientific_calculator; +import com.zipcodewilmington.scientificcalculator.MainApplication; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.sonatype.guice.bean.containers.Main; + /** * Created by leon on 2/9/18. */ public class TestMainApplication { + private MainApplication mainApp; + + @Before + public void setup(){ + mainApp = new MainApplication(); + } + + @Test + public void testInitialMemoryValue(){ + Assert.assertEquals(0.0, MainApplication.getMemory()); + } + + @Test + public void testSetMemoryValue(){ + MainApplication.setMemory(9.1); + Assert.assertEquals(9.1, MainApplication.getMemory()); + } + + @Test + public void testInitialCurrentValue(){ + Assert.assertEquals(0.0, MainApplication.getCurrentVal()); + } + + @Test + public void testSetCurrentValue(){ + MainApplication.setval(9.1); + Assert.assertEquals(9.1, MainApplication.getCurrentVal()); + } }