Calculations.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import java.util.ArrayList;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class Calculations
  5. {
  6. public static Console console = new Console();
  7. public static ArrayList<String> library = new ArrayList<String>();
  8. public static String invalidArgumentsAmountError = "You have entered an invalid amount of arguments. Please only use two arguments, or switch to PEMDAS";
  9. public static Pemdas pemdas = new Pemdas();
  10. public Calculations(){
  11. library.add("multiply");
  12. library.add("divide");
  13. library.add("subtract");
  14. library.add("add");
  15. library.add("COS");
  16. library.add("SIN");
  17. library.add("TAN");
  18. library.add("invertS");
  19. library.add("invertN");
  20. library.add("factorial");
  21. library.add("switchunit");
  22. library.add("clear");
  23. library.add("storeM");
  24. library.add("resetM");
  25. library.add("recallM");
  26. library.add("displayM");
  27. }
  28. public static String getCommand(String userInput)
  29. {
  30. boolean foundCommand = false;
  31. String command = "";
  32. String result = "";
  33. String[] inputArr = userInput.split(" ");
  34. for(int i = 0; i < library.size(); i ++)
  35. {
  36. String currCommand = library.get(i);
  37. if(currCommand.equalsIgnoreCase(inputArr[0]))
  38. {
  39. command = currCommand;
  40. foundCommand = true;
  41. break;
  42. }
  43. }
  44. switch(command)
  45. {
  46. case "multiply":
  47. result = Multiply(userInput);
  48. break;
  49. case "divide":
  50. result = Divide(userInput);
  51. break;
  52. case "add":
  53. result = Add(userInput);
  54. break;
  55. case "subtract":
  56. result = Subtract(userInput);
  57. break;
  58. case "invertS":
  59. result = InvertSign(userInput);
  60. break;
  61. case "invertN":
  62. result = InvertNumber(userInput);
  63. break;
  64. case "clear":
  65. Clear();
  66. break;
  67. }
  68. return result;
  69. }
  70. public static ArrayList<Double> getNumbers(String userInput){
  71. ArrayList<Double> results = new ArrayList<Double>();
  72. //takes in a string
  73. //finds the first occurence of a number
  74. //store it as a double ( left )
  75. //add this to the arraylist
  76. //find the second occurence of a number
  77. //store it as a double (right)
  78. //add this to the array list
  79. //if the size of the arraylist is 2
  80. //return the array list
  81. //else return null'
  82. Pattern pattern = Pattern.compile("\\s?\\-?\\.?\\d+\\.?(\\d+)?\\s?");
  83. Matcher matcher = pattern.matcher(userInput);
  84. while(matcher.find()){
  85. String number = matcher.group().trim();
  86. Double deci = Double.valueOf(number);
  87. results.add(deci);
  88. }
  89. if(results.size() == 2)
  90. {
  91. return results;
  92. }
  93. return null;
  94. }
  95. public static ArrayList<Double> getNumber(String userInput){
  96. ArrayList<Double> results = new ArrayList<Double>();
  97. //takes in a string
  98. //finds the first occurence of a number
  99. //store it as a double ( left )
  100. //add this to the arraylist
  101. //find the second occurence of a number
  102. //store it as a double (right)
  103. //add this to the array list
  104. //if the size of the arraylist is 2
  105. //return the array list
  106. //else return null'
  107. Pattern pattern = Pattern.compile("\\s?\\-?\\.?\\d+\\.?(\\d+)?\\s?");
  108. Matcher matcher = pattern.matcher(userInput);
  109. while(matcher.find()){
  110. String number = matcher.group().trim();
  111. Double deci = Double.valueOf(number);
  112. results.add(deci);
  113. }
  114. if(results.size() == 1)
  115. {
  116. System.out.println(results.get(0));
  117. return results;
  118. }
  119. return null;
  120. }
  121. public static String Multiply(String userInput)
  122. {
  123. String result = "";
  124. ArrayList<Double> results = getNumbers(userInput);
  125. if(results != null)
  126. {
  127. Double product = results.get(0) * results.get(1);
  128. result = String.valueOf(product);
  129. return result;
  130. } else {
  131. return invalidArgumentsAmountError;
  132. }
  133. }
  134. public static String Divide(String userInput)
  135. {
  136. //perform the operation
  137. //convert to string
  138. //return string
  139. String result = "";
  140. ArrayList<Double> results = getNumbers(userInput);
  141. if(results != null)
  142. {
  143. Double quotient = results.get(0) / results.get(1);
  144. result = String.valueOf(quotient);
  145. return result;
  146. } else {
  147. return invalidArgumentsAmountError;
  148. }
  149. }
  150. public static String Add(String userInput)
  151. {
  152. String result = "";
  153. ArrayList<Double> results = getNumbers(userInput);
  154. if(results != null)
  155. {
  156. Double sum = results.get(0) + results.get(1);
  157. result = String.valueOf(sum);
  158. return result;
  159. } else {
  160. return invalidArgumentsAmountError;
  161. }
  162. }
  163. public static String Subtract(String userInput)
  164. {
  165. //perform the operation
  166. //convert to string
  167. //return string
  168. String result = "";
  169. ArrayList<Double> results = getNumbers(userInput);
  170. if(results != null)
  171. {
  172. Double difference = results.get(0) - results.get(1);
  173. result = String.valueOf(difference);
  174. return result;
  175. } else {
  176. return invalidArgumentsAmountError;
  177. }
  178. }
  179. public static String InvertSign(String userInput)
  180. {
  181. ArrayList <Double> number1 = getNumber(userInput);
  182. return number.get(0) * -1;
  183. //perform the operation
  184. //convert to string
  185. //return string
  186. return null;
  187. }
  188. public static String InvertNumber(String userInput)
  189. {
  190. //perform the operation
  191. //convert to string
  192. //return string
  193. return null;
  194. }
  195. public static void Clear()
  196. {
  197. //reset the default value to 0
  198. }
  199. }