123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Write a description of class Operations here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.util.Scanner;
  8. public class Operations
  9. {
  10. /**
  11. * prompting a user for a command solving for that command
  12. *
  13. */
  14. public static void printHelp(){
  15. System.out.println("\n? | + | - | * | / |");
  16. System.out.println("^ | sqrt | inverse |");
  17. System.out.println("cos | sine | tan | cos-1 | sin-1 | m+ | m- | MRC");
  18. System.out.println("log | ln | ex |");
  19. System.out.println("To exit the program enter exit");
  20. System.out.println("to clear display enter clrDisplay\n");
  21. }
  22. public static double runCommand()
  23. {
  24. // put your code here
  25. Scanner input = new Scanner(System.in);
  26. double result = 0.0;
  27. //String userInput = scanner.nextLine();
  28. scienticFunctions ScientificFunctions = new scienticFunctions();
  29. Memory memory = new Memory();
  30. DiplayMode displayMode = new DiplayMode();
  31. bonusFunctions bonusFunctions = new bonusFunctions();
  32. calculatorCoreFunctions calculatorCoreFunctions = new calculatorCoreFunctions();
  33. // Calculator calc = new ....
  34. while (true) {
  35. System.out.print("Enter a command: ");
  36. String command = input.next();
  37. if (command.equalsIgnoreCase("exit")) {
  38. System.out.print("Thanks for calculating with us!");
  39. break;
  40. }
  41. else if (command.equalsIgnoreCase("+")){
  42. if (result != 0){
  43. double numInput = Console.getDoubleInput("Enter number");
  44. result = calculatorCoreFunctions.add(result, numInput);
  45. }
  46. else{
  47. double firstNumber = Console.getDoubleInput("Enter first number");
  48. double secondNumber = Console.getDoubleInput("Enter second number");
  49. result = calculatorCoreFunctions.add(firstNumber, secondNumber);
  50. }
  51. Console.printResult(result);
  52. }
  53. else if (command.equalsIgnoreCase("-")){
  54. if (result != 0){
  55. double numInput = Console.getDoubleInput("Enter number");
  56. result = calculatorCoreFunctions.subtract(result, numInput);
  57. }
  58. else{
  59. double firstNumber = Console.getDoubleInput("Enter first number");
  60. double secondNumber = Console.getDoubleInput("Enter second number");
  61. result = calculatorCoreFunctions.subtract(firstNumber, secondNumber);;
  62. }
  63. Console.printResult(result);
  64. }
  65. else if (command.equalsIgnoreCase("*")){
  66. if (result != 0){
  67. double numInput = Console.getDoubleInput("Enter number");
  68. result = calculatorCoreFunctions.multiply(result, numInput);
  69. }
  70. else{
  71. double firstNumber = Console.getDoubleInput("Enter first number");
  72. double secondNumber = Console.getDoubleInput("Enter second number");
  73. result = calculatorCoreFunctions.multiply(firstNumber, secondNumber);;
  74. }
  75. Console.printResult(result);
  76. }
  77. else if (command.equalsIgnoreCase("/")){
  78. if (result != 0){
  79. double numInput = Console.getDoubleInput("Enter number");
  80. result = calculatorCoreFunctions.divide(result, numInput);
  81. }
  82. else{
  83. double firstNumber = Console.getDoubleInput("Enter first number");
  84. double secondNumber = Console.getDoubleInput("Enter second number");
  85. result = calculatorCoreFunctions.divide(firstNumber, secondNumber);;
  86. }
  87. Console.printResult(result);
  88. }
  89. else if (command.equalsIgnoreCase("^")){
  90. if (result != 0){
  91. double numInput = Console.getDoubleInput("Enter number");
  92. result = calculatorCoreFunctions.exponent(result, numInput);
  93. }
  94. else{
  95. double firstNumber = Console.getDoubleInput("Enter first number");
  96. double secondNumber = Console.getDoubleInput("Enter second number");
  97. result = calculatorCoreFunctions.exponent(firstNumber, secondNumber);;
  98. }
  99. Console.printResult(result);
  100. }
  101. else if (command.equalsIgnoreCase("sqrt")) {
  102. if (result != 0){
  103. result = calculatorCoreFunctions.sqrt(result);
  104. }
  105. else{
  106. result = calculatorCoreFunctions.sqrt(Console.getDoubleInput("Enter number"));
  107. }
  108. Console.printResult(result);
  109. }
  110. else if (command.equalsIgnoreCase("inverse")) {
  111. if (result != 0){
  112. result = calculatorCoreFunctions.inverse(result);
  113. }
  114. else{
  115. result = calculatorCoreFunctions.inverse(Console.getDoubleInput("Enter number"));
  116. }
  117. Console.printResult(result);
  118. }
  119. else if (command.equalsIgnoreCase("sine")) {
  120. if (result != 0){
  121. result = ScientificFunctions.sine(result);
  122. }
  123. else{
  124. result = ScientificFunctions.sine(Console.getDoubleInput("Enter number"));
  125. }
  126. Console.printResult(result);
  127. }
  128. else if (command.equalsIgnoreCase("cos")) {
  129. if (result != 0){
  130. result = ScientificFunctions.cosine(result);
  131. }
  132. else{
  133. result = ScientificFunctions.cosine(Console.getDoubleInput("Enter number"));
  134. }
  135. Console.printResult(result);
  136. }
  137. else if (command.equalsIgnoreCase("tan")) {
  138. if (result != 0){
  139. result = ScientificFunctions.tangent(result);
  140. }
  141. else{
  142. result = ScientificFunctions.tangent(Console.getDoubleInput("Enter number"));
  143. }
  144. Console.printResult(result);
  145. }
  146. else if (command.equalsIgnoreCase("cos-1")) {
  147. if (result != 0){
  148. result = ScientificFunctions.inverseCosine(result);
  149. }
  150. else{
  151. result = ScientificFunctions.inverseCosine(Console.getDoubleInput("Enter number"));
  152. }
  153. Console.printResult(result);
  154. }
  155. else if (command.equalsIgnoreCase("sin-1")) {
  156. if (result != 0){
  157. result = ScientificFunctions.inverseSine(result);
  158. }
  159. else{
  160. result = ScientificFunctions.inverseSine(Console.getDoubleInput("Enter number"));
  161. }
  162. Console.printResult(result);
  163. }
  164. else if (command.equalsIgnoreCase("tan-1")) {
  165. if (result != 0){
  166. result = ScientificFunctions.inverseTangent(result);
  167. }
  168. else{
  169. result = ScientificFunctions.inverseTangent(Console.getDoubleInput("Enter number"));
  170. }
  171. Console.printResult(result);
  172. }
  173. else if(command.equalsIgnoreCase("M+")){
  174. memory.storeInMemory(result);
  175. Console.printResult(result);
  176. }
  177. else if (command.equalsIgnoreCase("M-")){
  178. Console.printResult(memory.clearMemory());
  179. }
  180. else if (command.equalsIgnoreCase("MRC")){
  181. Console.printResult(memory.retrieveFromMemory());
  182. }
  183. else if (command.equalsIgnoreCase("switchmode")){
  184. displayMode.setResult(result);
  185. String mode = displayMode.enterMode();
  186. //System.out.println(mode);
  187. displayMode.switchDisplayMode(mode);
  188. }
  189. else if (command.equalsIgnoreCase("log")){
  190. Console.printResult(bonusFunctions.LogBaseTenOfAnumber(result));
  191. }
  192. else if (command.equalsIgnoreCase("ln")){
  193. Console.printResult(bonusFunctions.naturalLogLn(result));
  194. }
  195. else if (command.equalsIgnoreCase("ex")){
  196. Console.printResult(bonusFunctions.exponential(result));
  197. }
  198. else if(command.equalsIgnoreCase("?")){
  199. printHelp();
  200. }
  201. else if(command.equalsIgnoreCase("clrDisplay")){
  202. result = 0.0;
  203. }
  204. else{
  205. Console.println("Invalid command please press ? for help");
  206. }
  207. //firstNumber = result;
  208. // and on and on
  209. }
  210. return result;
  211. }
  212. //Add CommentCollapse 
  213. }