Console.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import java.util.Scanner;
  2. /**
  3. * Created by leon on 2/9/18.
  4. */
  5. public class Console {
  6. public static void main(String[] args) {
  7. // Create all instances of classes to be used for this program
  8. MainMenu mainMenu = new MainMenu();
  9. Basic basicCalc = new Basic();
  10. RealAdvanced advCalc = new RealAdvanced();
  11. SciCalc scientificCalc = new SciCalc();
  12. MemoryFunc memory = new MemoryFunc();
  13. SwitchDisplay baseChange = new SwitchDisplay();
  14. Trig trigMenu = new Trig();
  15. // Stored memory value should be able to be used throughout whole application
  16. Double memoryValue = memory.memory;
  17. // blanket input statement
  18. // create input methods to call when we need them with specific types
  19. // we use 1 input variable statement to call the user to input, then manipulate
  20. // Can user input M if native type is double
  21. // if any input = "m", that input now = memoryValue;
  22. // Menu Input
  23. String menuInput = " ";
  24. // Giant while loop that links back to main menu
  25. while (!menuInput.equals("4")) {
  26. mainMenu.mainMenuDisplay();
  27. menuInput = Console.getStringInput("Enter the calculator you want to use by key. (ie. 1 for basic, etc.)");
  28. switch (menuInput) {
  29. // Basic Calculator
  30. case "1":
  31. basicCalc.Basic();
  32. String basicMode = Console.getStringInput("Enter the mode: ");
  33. double basicInput1 = Console.getDoubleInput("Enter the first input: ");
  34. double basicInput2 = Console.getDoubleInput("Enter the second input: ");
  35. double basicAnswer = 0;
  36. if (basicMode.equals("1")) {
  37. basicAnswer = basicCalc.getSum(basicInput1, basicInput2);
  38. //subtraction
  39. } else if (basicMode.equals("2")) {
  40. basicAnswer = basicCalc.getDiff(basicInput1, basicInput2);
  41. //multiplication
  42. } else if (basicMode.equals("3")) {
  43. basicAnswer = basicCalc.getProduct(basicInput1, basicInput2);
  44. //division
  45. } else if (basicMode.equals("4")) {
  46. //error message when trying to divide by 0.
  47. if (basicInput2 == 0) {
  48. Console.println("undefined");
  49. //Insert return to main menu.
  50. } else {
  51. basicAnswer = basicCalc.getQuotient(basicInput1, basicInput2);
  52. }
  53. } else if (basicMode.equals("5")) {
  54. break;
  55. } else {
  56. //error if wrong input was entered
  57. basicAnswer = Double.NaN;
  58. Console.println("Invalid Input");
  59. }
  60. Console.println(Double.toString(basicAnswer));
  61. break;
  62. case "2":
  63. advCalc.realAdvanced();
  64. String mode = Console.getStringInput("Enter the mode: ");
  65. double advInput1 = Console.getDoubleInput("Enter the first input: ");
  66. double advAnswer = 0;
  67. if (mode.equals("1")){
  68. advAnswer = advCalc.squared(advInput1);
  69. }else if (mode.equals("2")){
  70. double advInput2 = Console.getDoubleInput("Enter the exponent value: ");
  71. advAnswer = advCalc.exponent(advInput1,advInput2);
  72. }else if (mode.equals("3")){
  73. advAnswer = advCalc.inverse(advInput1);
  74. }else if (mode.equals("4")){
  75. advAnswer = advCalc.opposite(advInput1);
  76. }else if (mode.equals("5")){
  77. advAnswer = advCalc.absoluteValue(advInput1);
  78. }else if (mode.equals("6")) {
  79. break;
  80. }else{
  81. Console.println("Invalid input");
  82. }
  83. Console.println(Double.toString(advAnswer));
  84. break;
  85. case "3":
  86. // Sends to Scientific Calculator Functions
  87. // displays the menu for scientific calc
  88. scientificCalc.toSciMenu();
  89. String sciMenuInput = Console.getStringInput("Enter the function you'd like to use by key.");
  90. // if statement to use various calc functions
  91. if (sciMenuInput.equals("1")) {
  92. // Base Conversions
  93. // while loop for display
  94. String baseMode = "";
  95. baseChange.switchDisplay();
  96. baseMode = Console.getStringInput("Enter the desired display mode: ");
  97. int userInput = getIntegerInput("Enter the number you want to convert to selected base: ");
  98. String baseOutput = baseChange.switchDisplayOutput(baseMode, userInput);
  99. Console.println(baseOutput);
  100. break;
  101. // display mode
  102. // gets mode
  103. // asks for input
  104. } else if (sciMenuInput.equals("2")) {
  105. // Memory
  106. memory.memoryMenu();
  107. } else if (sciMenuInput.equals("3")) {
  108. // To Trig Menu
  109. trigMenu.trigFunctions();
  110. String trigMode = Console.getStringInput("Enter trignometric function by key: (ie. 1 for sin)");
  111. double trigInput = Console.getDoubleInput("Enter the input for the trignometric function: ");
  112. double trigAnswer = 0;
  113. if (trigMode.equals("1")) {
  114. trigAnswer = trigMenu.calcSin(trigInput);
  115. //COS
  116. } else if (trigMode.equals("2")) {
  117. trigAnswer = trigMenu.calcCos(trigInput);
  118. //TAN
  119. } else if (trigMode.equals("3")) {
  120. trigAnswer = trigMenu.calcTan(trigInput);
  121. //ARCSIN
  122. } else if (trigMode.equals("4")) {
  123. trigAnswer = trigMenu.calcArcsin(trigInput);
  124. //ARCCOS
  125. } else if (trigMode.equals("5")) {
  126. trigAnswer = trigMenu.calcArccos(trigInput);
  127. //ARCTAN
  128. } else if (trigMode.equals("6")) {
  129. trigAnswer = trigMenu.calcArctan(trigInput);
  130. } else if (trigMode.equals("7")) {
  131. break;
  132. } else {
  133. //Error message if input does not match options, loops back to the top so they can try again.
  134. trigAnswer = Double.NaN;
  135. }
  136. String trigAnswerString = Double.toString(trigAnswer);
  137. Console.println(trigAnswerString);
  138. String trigDefRad = Console.getStringInput("Do you want to convert your answer from radians to degrees?"
  139. + "\nEnter Y or N").toLowerCase();
  140. if (trigDefRad.equals("y") ) {
  141. double convertAnswer = trigMenu.toDegrees(trigAnswer);
  142. Console.println(Double.toString(convertAnswer));
  143. }
  144. break;
  145. } else {
  146. break;
  147. }
  148. break;
  149. case "4":
  150. // Quits the calculator
  151. break;
  152. }
  153. }
  154. }
  155. public static void print(String output, Object... args) {
  156. System.out.printf(output, args);
  157. }
  158. public static void println(String output, Object... args) {
  159. print(output + "\n", args);
  160. }
  161. public static String getStringInput(String prompt) {
  162. Scanner scanner = new Scanner(System.in);
  163. println(prompt);
  164. String userInput = scanner.nextLine();
  165. return userInput;
  166. }
  167. public static Integer getIntegerInput(String prompt) {
  168. Scanner scanner = new Scanner(System.in);
  169. println(prompt);
  170. Integer userInput = scanner.nextInt();
  171. return userInput;
  172. }
  173. public static Double getDoubleInput(String prompt) {
  174. Scanner scanner = new Scanner(System.in);
  175. println(prompt);
  176. Double userInput = scanner.nextDouble();
  177. return userInput;
  178. }
  179. }