MainApplication.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import java.util.Scanner;
  2. /**
  3. * Class used perform calculator operations using Calculator methods and Console print methods
  4. *
  5. * @author (Demetrius Murray, Nick Satinover, Zavon Malone)
  6. * @version (10/22/18 v1.4 3:07PM)
  7. */
  8. public class MainApplication {
  9. public static void main(String[] args) {
  10. System.out.println("It's time for the calculator.");
  11. Console.printLegend();
  12. Calculator calc = new Calculator();
  13. TrigFunctions trig = new TrigFunctions();
  14. Bonus bonus = new Bonus();
  15. double currentNum = 0;
  16. double tempNum;
  17. String tempString;
  18. String firstValue = "";
  19. try{
  20. firstValue = Console.inputString();
  21. if(firstValue.matches("[0-9]+")){
  22. currentNum = Double.valueOf(firstValue);
  23. }
  24. else if(firstValue.matches("q"))
  25. {
  26. Console.quit();
  27. }
  28. else if(firstValue.matches("c"))
  29. {
  30. currentNum = Console.displayClear();
  31. currentNum = Double.valueOf(Console.inputString());
  32. }
  33. else{
  34. Console.error();
  35. currentNum = Double.valueOf(Console.inputString());
  36. }
  37. }
  38. catch(Exception a)
  39. {
  40. System.out.println("An error occured with the last request");
  41. System.exit(0);
  42. }
  43. while(String.valueOf(firstValue) != "q") {
  44. try{
  45. switch (Console.getOperator()) {
  46. case '+': //takes string input. if statement places memory value into function, else converts to double
  47. tempString = Console.inputString();
  48. if(tempString.equals("R")){
  49. tempNum = calc.getMemory();
  50. } else {
  51. tempNum = Double.valueOf(tempString);
  52. }
  53. currentNum = calc.add(currentNum, tempNum);
  54. break;
  55. case '-': //takes string input. if statement places memory value into function, else converts to double
  56. tempString = Console.inputString();
  57. if(tempString.equals("R")){
  58. tempNum = calc.getMemory();
  59. } else {
  60. tempNum = Double.valueOf(tempString);
  61. }
  62. currentNum = calc.subtract(currentNum, tempNum);
  63. break;
  64. case '*': //takes string input. if statement places memory value into function, else converts to double
  65. tempString = Console.inputString();
  66. if(tempString.equals("R")){
  67. tempNum = calc.getMemory();
  68. } else {
  69. tempNum = Double.valueOf(tempString);
  70. }
  71. currentNum = calc.multiply(currentNum, tempNum);
  72. break;
  73. case '/': //takes string input. if statement places memory value into function, else converts to double
  74. tempString = Console.inputString();
  75. if(tempString.equals("R")){
  76. tempNum = calc.getMemory();
  77. } else {
  78. tempNum = Double.valueOf(tempString);
  79. }
  80. while(tempNum == 0 ){
  81. System.out.print("Error, cannot divide by zero. Please ");
  82. tempNum = Console.inputDouble();
  83. }
  84. currentNum = calc.divide(currentNum, tempNum);
  85. break;
  86. case '@':
  87. currentNum = calc.square(currentNum);
  88. break;
  89. case '&':
  90. currentNum = calc.squareRoot(currentNum);
  91. break;
  92. case '^': //takes string input. if statement places memory value into function, else converts to double
  93. tempString = Console.inputString();
  94. if(tempString.equals("R")){
  95. tempNum = calc.getMemory();
  96. } else {
  97. tempNum = Double.valueOf(tempString);
  98. }
  99. currentNum = calc.exponent(currentNum, tempNum);
  100. break;
  101. case '!':
  102. if (currentNum == 0){
  103. System.out.println("Error, cannot divide by zero. Press 'c' to clear.");
  104. continue;
  105. }
  106. currentNum = calc.invert(currentNum);
  107. break;
  108. case '#':
  109. currentNum = calc.changeSign(currentNum);
  110. break;
  111. case 'M':
  112. calc.storeMemory(currentNum);
  113. Console.printMemoryValue(calc.getMemory());
  114. continue;
  115. case 'R':
  116. Console.printMemoryValue(calc.getMemory());
  117. break;
  118. case 'C':
  119. calc.clearMemory();
  120. Console.printMemoryValue(calc.getMemory());
  121. break;
  122. case '~':
  123. currentNum = Console.displayChange();
  124. break;
  125. case 'c':
  126. currentNum = Console.displayClear();
  127. currentNum = Double.valueOf(Console.inputString()); //asks for new value
  128. continue; //back to top of loop
  129. case 'l':
  130. Console.printLegend();
  131. break;
  132. case 'q':
  133. Console.quit();
  134. break;
  135. /**
  136. * Below are method calls to the TrigFunction class
  137. */
  138. case 's':
  139. currentNum = trig.sin(currentNum);
  140. break;
  141. case 'x':
  142. currentNum = trig.cosine(currentNum);
  143. break;
  144. case 't':
  145. currentNum = trig.tangent(currentNum);
  146. break;
  147. /*
  148. case 'w':
  149. currentNum = trig.inverseSign(currentNum);
  150. break;
  151. case 'e':
  152. currentNum = trig.inverseCosign(currentNum);
  153. break;
  154. case 'r':
  155. currentNum = trig.inverseTangent(currentNum);
  156. break;
  157. */
  158. /**
  159. * Below are method calls to the Bonus class
  160. */
  161. case 'f':
  162. currentNum = bonus.factorial((int) currentNum);
  163. break;
  164. default:
  165. Console.errorOperator();
  166. }
  167. }
  168. /**
  169. * This block will only execute if any Arithmetic exception occurs in try block
  170. */
  171. catch(ArithmeticException e)
  172. {
  173. System.out.println("You should not divide a number by zero");
  174. }
  175. /**
  176. * This block will only execute if any other exception occurs in try block
  177. */
  178. catch(Exception e)
  179. {
  180. System.out.println("An error occured with the last request");
  181. }
  182. Console.displayValue(currentNum);
  183. }
  184. /*
  185. if (Console.inputString() == "c"){
  186. Console.displayClear();
  187. }
  188. */
  189. }
  190. }