Console.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. basicAnswer = Double.NaN;
  50. //Insert return to main menu.
  51. } else {
  52. basicAnswer = basicCalc.getQuotient(basicInput1, basicInput2);
  53. }
  54. } else if (basicMode.equals("5")) {
  55. break;
  56. } else {
  57. //error if wrong input was entered
  58. basicAnswer = Double.NaN;
  59. Console.println("Invalid Input");
  60. }
  61. Console.println(Double.toString(basicAnswer));
  62. break;
  63. case "2":
  64. advCalc.realAdvanced();
  65. String mode = Console.getStringInput("Enter the mode: ");
  66. double advInput1 = Console.getDoubleInput("Enter the first input: ");
  67. double advAnswer = 0;
  68. if (mode.equals("1")){
  69. advAnswer = advCalc.squared(advInput1);
  70. }else if (mode.equals("2")){
  71. double advInput2 = Console.getDoubleInput("Enter the exponent value: ");
  72. advAnswer = advCalc.exponent(advInput1,advInput2);
  73. }else if (mode.equals("3")){
  74. if (advInput1 == 0) {
  75. Console.println("undefined");
  76. advAnswer = Double.NaN;
  77. } else {
  78. advAnswer = advCalc.inverse(advInput1);
  79. }
  80. }else if (mode.equals("4")){
  81. advAnswer = advCalc.opposite(advInput1);
  82. }else {
  83. Console.println("Invalid input");
  84. }
  85. Console.println(Double.toString(advAnswer));
  86. break;
  87. case "3":
  88. // Sends to Scientific Calculator Functions
  89. // displays the menu for scientific calc
  90. scientificCalc.toSciMenu();
  91. String sciMenuInput = Console.getStringInput("Enter the function you'd like to use by key.");
  92. // if statement to use various calc functions
  93. if (sciMenuInput.equals("1")) {
  94. // Base Conversions
  95. // while loop for display
  96. String baseMode = "";
  97. baseChange.switchDisplay();
  98. baseMode = Console.getStringInput("Enter the desired display mode: ");
  99. int userInput = getIntegerInput("Enter the number you want to convert to selected base: ");
  100. String baseOutput = baseChange.switchDisplayOutput(baseMode, userInput);
  101. Console.println(baseOutput);
  102. break;
  103. // display mode
  104. // gets mode
  105. // asks for input
  106. } else if (sciMenuInput.equals("2")) {
  107. // Memory
  108. memory.memoryMenu();
  109. } else if (sciMenuInput.equals("3")) {
  110. // To Trig Menu
  111. trigMenu.trigFunctions();
  112. String trigMode = Console.getStringInput("Enter trignometric function by key: (ie. 1 for sin)");
  113. double trigInput = Console.getDoubleInput("Enter the input for the trignometric function: ");
  114. double trigAnswer = 0;
  115. if (trigMode.equals("1")) {
  116. trigAnswer = trigMenu.calcSin(trigInput);
  117. //COS
  118. } else if (trigMode.equals("2")) {
  119. trigAnswer = trigMenu.calcCos(trigInput);
  120. //TAN
  121. } else if (trigMode.equals("3")) {
  122. trigAnswer = trigMenu.calcTan(trigInput);
  123. //ARCSIN
  124. } else if (trigMode.equals("4")) {
  125. trigAnswer = trigMenu.calcArcsin(trigInput);
  126. //ARCCOS
  127. } else if (trigMode.equals("5")) {
  128. trigAnswer = trigMenu.calcArccos(trigInput);
  129. //ARCTAN
  130. } else if (trigMode.equals("6")) {
  131. trigAnswer = trigMenu.calcArctan(trigInput);
  132. } else if (trigMode.equals("7")) {
  133. break;
  134. } else {
  135. //Error message if input does not match options, loops back to the top so they can try again.
  136. trigAnswer = Double.NaN;
  137. }
  138. String trigAnswerString = Double.toString(trigAnswer);
  139. Console.println(trigAnswerString);
  140. String trigDefRad = Console.getStringInput("Do you want to convert your answer from radians to degrees?"
  141. + "\nEnter Y or N").toLowerCase();
  142. if (trigDefRad.equals("y") ) {
  143. double convertAnswer = trigMenu.toDegrees(trigAnswer);
  144. Console.println(Double.toString(convertAnswer));
  145. }
  146. break;
  147. } else {
  148. break;
  149. }
  150. break;
  151. case "4":
  152. // Quits the calculator
  153. break;
  154. }
  155. }
  156. }
  157. public static void print(String output, Object... args) {
  158. System.out.printf(output, args);
  159. }
  160. public static void println(String output, Object... args) {
  161. print(output + "\n", args);
  162. }
  163. public static String getStringInput(String prompt) {
  164. Scanner scanner = new Scanner(System.in);
  165. println(prompt);
  166. String userInput = scanner.nextLine();
  167. return userInput;
  168. }
  169. public static Integer getIntegerInput(String prompt) {
  170. Scanner scanner = new Scanner(System.in);
  171. println(prompt);
  172. Integer userInput = scanner.nextInt();
  173. return userInput;
  174. }
  175. public static Double getDoubleInput(String prompt) {
  176. Scanner scanner = new Scanner(System.in);
  177. println(prompt);
  178. Double userInput = scanner.nextDouble();
  179. return userInput;
  180. }
  181. }