Console.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import java.util.Scanner;
  2. import java.util.*;
  3. /**
  4. * Created by leon on 2/9/18.
  5. */
  6. public class Console {
  7. public static void main(String[] args) {
  8. // Create all instances of classes to be used for this program
  9. MainMenu mainMenu = new MainMenu();
  10. Basic basicCalc = new Basic();
  11. RealAdvanced advCalc = new RealAdvanced();
  12. SciCalc scientificCalc = new SciCalc();
  13. MemoryFunc memory = new MemoryFunc();
  14. SwitchDisplay baseChange = new SwitchDisplay();
  15. Trig trigMenu = new Trig();
  16. // Menu Input
  17. String menuInput = " ";
  18. // Giant while loop that links back to main menu
  19. while (!menuInput.equals("4")) {
  20. mainMenu.mainMenuDisplay();
  21. menuInput = Console.getStringInput("Enter the calculator you want to use by key. (ie. 1 for basic, etc.)");
  22. switch (menuInput) {
  23. // Basic Calculator
  24. case "1":
  25. basicCalc.Basic();
  26. double basicInput1 = 0,
  27. basicInput2 = 0;
  28. String basicMode = Console.getStringInput("Enter the mode: ");
  29. if (memory.memoryChanged != true) {
  30. basicInput1 = Console.getDoubleInput("Enter the first input: ");
  31. basicInput2 = Console.getDoubleInput("Enter the second input: ");
  32. } else if (memory.memoryChanged == true) {
  33. String useMemory = Console.getStringInput("Do you want to use the stored memory value? Y or N: ").toLowerCase();
  34. if (useMemory.equals("y")) {
  35. String whichBasInput = Console.getStringInput("Which input? 1 or 2 or both").toLowerCase();
  36. if (whichBasInput.equals("1")) {
  37. basicInput1 = memory.memory;
  38. basicInput2 = Console.getDoubleInput("Enter the second input: ");
  39. } else if (whichBasInput.equals("2")) {
  40. basicInput2 = memory.memory;
  41. basicInput1 = Console.getDoubleInput("Enter the first input: ");
  42. } else if (whichBasInput.equals("both")) {
  43. basicInput1 = memory.memory;
  44. basicInput2 = memory.memory;
  45. }
  46. } else {
  47. basicInput1 = Console.getDoubleInput("Enter the first input: ");
  48. basicInput2 = Console.getDoubleInput("Enter the second input: ");
  49. }
  50. }
  51. double basicAnswer = 0;
  52. if (basicMode.equals("1")) {
  53. basicAnswer = basicCalc.getSum(basicInput1, basicInput2);
  54. //subtraction
  55. } else if (basicMode.equals("2")) {
  56. basicAnswer = basicCalc.getDiff(basicInput1, basicInput2);
  57. //multiplication
  58. } else if (basicMode.equals("3")) {
  59. basicAnswer = basicCalc.getProduct(basicInput1, basicInput2);
  60. //division
  61. } else if (basicMode.equals("4")) {
  62. //error message when trying to divide by 0.
  63. if (basicInput2 == 0) {
  64. Console.println("undefined");
  65. basicAnswer = Double.NaN;
  66. //Insert return to main menu.
  67. } else {
  68. basicAnswer = basicCalc.getQuotient(basicInput1, basicInput2);
  69. }
  70. } else if (basicMode.equals("5")) {
  71. break;
  72. } else {
  73. //error if wrong input was entered
  74. basicAnswer = Double.NaN;
  75. Console.println("Invalid Input");
  76. }
  77. Console.println(Double.toString(basicAnswer));
  78. break;
  79. case "2":
  80. advCalc.realAdvanced();
  81. String mode = Console.getStringInput("Enter the mode: ");
  82. double advInput1 = 0,
  83. advInput2 = 0;
  84. // checking if user wants to use memory
  85. if (memory.memoryChanged != true) {
  86. advInput1 = Console.getDoubleInput("Enter the first input: ");
  87. } else if (memory.memoryChanged == true) {
  88. String useMemory = Console.getStringInput("Do you want to use the stored memory value? Y or N: ").toLowerCase();
  89. if (useMemory.equals("y")) {
  90. advInput1 = memory.memory;
  91. } else if (useMemory.equals("n")) {
  92. advInput1 = Console.getDoubleInput("Enter the first input: ");
  93. }
  94. }
  95. double advAnswer = 0;
  96. if (mode.equals("1")){
  97. advAnswer = advCalc.squared(advInput1);
  98. }else if (mode.equals("2")){
  99. // checking if user wants to use memory
  100. if (memory.memoryChanged != true) {
  101. advInput2 = Console.getDoubleInput("Enter the exponent value: ");
  102. } else if (memory.memoryChanged == true) {
  103. String useMemory = Console.getStringInput("Do you want to use the stored memory value? Y or N: ").toLowerCase();
  104. if (useMemory.equals("y")) {
  105. advInput2 = memory.memory;
  106. } else if (useMemory.equals("n")) {
  107. advInput2 = Console.getDoubleInput("Enter the first input: ");
  108. }
  109. }
  110. advAnswer = advCalc.exponent(advInput1,advInput2);
  111. }else if (mode.equals("3")){
  112. if (advInput1 == 0) {
  113. Console.println("undefined");
  114. advAnswer = Double.NaN;
  115. } else {
  116. advAnswer = advCalc.inverse(advInput1);
  117. }
  118. }else if (mode.equals("4")){
  119. advAnswer = advCalc.opposite(advInput1);
  120. }else if (mode.equals("5")){
  121. advAnswer = advCalc.absoluteValue(advInput1);
  122. }else if (mode.equals("6")){
  123. advAnswer = advCalc.sqrt(advInput1);
  124. }else if (mode.equals("7")){
  125. advAnswer = advCalc.factorial(advInput1);
  126. }else if (mode.equals("8")) {
  127. break;
  128. }else{
  129. Console.println("Invalid input");
  130. }
  131. Console.println(Double.toString(advAnswer));
  132. break;
  133. case "3":
  134. // Sends to Scientific Calculator Functions
  135. // displays the menu for scientific calc
  136. scientificCalc.toSciMenu();
  137. String sciMenuInput = Console.getStringInput("Enter the function you'd like to use by key.");
  138. // if statement to use various calc functions
  139. if (sciMenuInput.equals("1")) {
  140. // Base Conversions
  141. // while loop for display
  142. String baseMode = "";
  143. baseChange.switchDisplay();
  144. String baseAnswer = "";
  145. baseMode = Console.getStringInput("Enter the desired display mode: ");
  146. double userInput = 0;
  147. if (memory.memoryChanged != true) {
  148. userInput = (Console.getDoubleInput("Enter the exponent value: "));
  149. } else if (memory.memoryChanged == true) {
  150. String useMemory = Console.getStringInput("Do you want to use the stored memory value? Y or N: ").toLowerCase();
  151. if (useMemory.equals("y")) {
  152. userInput = memory.memory;
  153. } else if (useMemory.equals("n")) {
  154. userInput = Console.getDoubleInput("Enter the number you want to convert to selected base: ");
  155. }
  156. }
  157. switch (baseMode) {
  158. case "1":
  159. baseAnswer = baseChange.toBinary((int)userInput);
  160. case "2":
  161. baseAnswer = baseChange.toOctal((int)userInput);
  162. case "3":
  163. baseAnswer = baseChange.toDecimal((int)userInput);
  164. case "4":
  165. baseAnswer = baseChange.toHexa((int)userInput);
  166. case "5":
  167. break;
  168. default:
  169. Console.println("Invalid Input");
  170. break;
  171. }
  172. Console.println(baseAnswer);
  173. break;
  174. } else if (sciMenuInput.equals("2")) {
  175. // Memory
  176. // access memory menu
  177. memory.memoryMenu();
  178. } else if (sciMenuInput.equals("3")) {
  179. // To Trig Menu
  180. trigMenu.trigFunctions();
  181. String trigMode = Console.getStringInput("Enter trignometric function by key: (ie. 1 for sin)");
  182. double trigInput = 0;
  183. if (memory.memoryChanged != true) {
  184. trigInput = Console.getDoubleInput("Enter the input for the trignometric function: ");
  185. } else if (memory.memoryChanged == true) {
  186. String useMemory = Console.getStringInput("Do you want to use the stored memory value? Y or N: ").toLowerCase();
  187. if (useMemory.equals("y")) {
  188. trigInput = memory.memory;
  189. } else if (useMemory.equals("n")) {
  190. trigInput = Console.getDoubleInput("Enter the input for the trignometric function: ");
  191. }
  192. }
  193. double trigAnswer;
  194. if (trigMode.equals("1")) {
  195. trigAnswer = trigMenu.calcSin(trigInput);
  196. //COS
  197. } else if (trigMode.equals("2")) {
  198. trigAnswer = trigMenu.calcCos(trigInput);
  199. //TAN
  200. } else if (trigMode.equals("3")) {
  201. trigAnswer = trigMenu.calcTan(trigInput);
  202. //ARCSIN
  203. } else if (trigMode.equals("4")) {
  204. trigAnswer = trigMenu.calcArcsin(trigInput);
  205. //ARCCOS
  206. } else if (trigMode.equals("5")) {
  207. trigAnswer = trigMenu.calcArccos(trigInput);
  208. //ARCTAN
  209. } else if (trigMode.equals("6")) {
  210. trigAnswer = trigMenu.calcArctan(trigInput);
  211. } else if (trigMode.equals("7")) {
  212. trigAnswer = trigMenu.calcLog(trigInput);
  213. } else if (trigMode.equals("8")) {
  214. break;
  215. } else {
  216. //Error message if input does not match options, loops back to the top so they can try again.
  217. trigAnswer = Double.NaN;
  218. }
  219. String trigAnswerString = Double.toString(trigAnswer);
  220. Console.println(trigAnswerString);
  221. String trigDefRad = Console.getStringInput("Do you want to convert your answer from radians to degrees?"
  222. + "\nEnter Y or N").toLowerCase();
  223. if (trigDefRad.equals("y") ) {
  224. double convertAnswer = trigMenu.toDegrees(trigAnswer);
  225. Console.println(Double.toString(convertAnswer));
  226. }
  227. break;
  228. } else {
  229. break;
  230. }
  231. break;
  232. case "4":
  233. // Quits the calculator
  234. break;
  235. }
  236. }
  237. }
  238. public static void print(String output, Object... args) {
  239. System.out.printf(output, args);
  240. }
  241. public static void println(String output, Object... args) {
  242. print(output + "\n", args);
  243. }
  244. public static String getStringInput(String prompt) {
  245. Scanner scanner = new Scanner(System.in);
  246. println(prompt);
  247. String userInput = scanner.nextLine();
  248. return userInput;
  249. }
  250. public static Integer getIntegerInput(String prompt) {
  251. Scanner scanner = new Scanner(System.in);
  252. println(prompt);
  253. Integer userInput = scanner.nextInt();
  254. return userInput;
  255. }
  256. public static Double getDoubleInput(String prompt) {
  257. Scanner scanner = new Scanner(System.in);
  258. println(prompt);
  259. Double userInput = scanner.nextDouble();
  260. return userInput;
  261. }
  262. }