123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- public class Calculations
- {
- public static Console console = new Console();
- public static ArrayList<String> library = new ArrayList<String>();
- public static String invalidArgumentsAmountError = "You have entered an invalid amount of arguments. Please only use two arguments, or switch to PEMDAS";
- public static Pemdas pemdas = new Pemdas();
-
- public Calculations(){
-
- library.add("multiply");
- library.add("divide");
- library.add("subtract");
- library.add("add");
- library.add("COS");
- library.add("SIN");
- library.add("TAN");
- library.add("invertS");
- library.add("invertN");
- library.add("factorial");
- library.add("switchunit");
- library.add("clear");
- library.add("storeM");
- library.add("resetM");
- library.add("recallM");
- library.add("displayM");
-
- }
-
- public static String getCommand(String userInput)
- {
- boolean foundCommand = false;
- String command = "";
- String result = "";
-
- String[] inputArr = userInput.split(" ");
- for(int i = 0; i < library.size(); i ++)
- {
- String currCommand = library.get(i);
- if(currCommand.equalsIgnoreCase(inputArr[0]))
- {
- command = currCommand;
- foundCommand = true;
- break;
- }
- }
-
- switch(command)
- {
- case "multiply":
- result = Multiply(userInput);
- break;
-
- case "divide":
- result = Divide(userInput);
- break;
-
- case "add":
- result = Add(userInput);
- break;
-
- case "subtract":
- result = Subtract(userInput);
- break;
-
- case "invertS":
- result = InvertSign(userInput);
- break;
-
- case "invertN":
- result = InvertNumber(userInput);
- break;
-
- case "clear":
- Clear();
- break;
- }
- return result;
- }
-
- public static ArrayList<Double> getNumbers(String userInput){
- ArrayList<Double> results = new ArrayList<Double>();
-
- //takes in a string
- //finds the first occurence of a number
- //store it as a double ( left )
- //add this to the arraylist
- //find the second occurence of a number
- //store it as a double (right)
- //add this to the array list
- //if the size of the arraylist is 2
- //return the array list
- //else return null'
-
- Pattern pattern = Pattern.compile("\\s?\\-?\\.?\\d+\\.?(\\d+)?\\s?");
- Matcher matcher = pattern.matcher(userInput);
-
- while(matcher.find()){
- String number = matcher.group().trim();
- Double deci = Double.valueOf(number);
-
- results.add(deci);
- }
-
- if(results.size() == 2)
- {
- return results;
- }
- return null;
- }
-
- public static ArrayList<Double> getNumber(String userInput){
- ArrayList<Double> results = new ArrayList<Double>();
-
- //takes in a string
- //finds the first occurence of a number
- //store it as a double ( left )
- //add this to the arraylist
- //find the second occurence of a number
- //store it as a double (right)
- //add this to the array list
- //if the size of the arraylist is 2
- //return the array list
- //else return null'
-
- Pattern pattern = Pattern.compile("\\s?\\-?\\.?\\d+\\.?(\\d+)?\\s?");
- Matcher matcher = pattern.matcher(userInput);
-
- while(matcher.find()){
- String number = matcher.group().trim();
- Double deci = Double.valueOf(number);
-
- results.add(deci);
-
- }
-
- if(results.size() == 1)
- {
- System.out.println(results.get(0));
- return results;
- }
-
- return null;
- }
-
- public static String Multiply(String userInput)
- {
- String result = "";
- ArrayList<Double> results = getNumbers(userInput);
- if(results != null)
- {
- Double product = results.get(0) * results.get(1);
- result = String.valueOf(product);
- return result;
- } else {
- return invalidArgumentsAmountError;
- }
- }
-
- public static String Divide(String userInput)
- {
- //perform the operation
- //convert to string
- //return string
- String result = "";
- ArrayList<Double> results = getNumbers(userInput);
- if(results != null)
- {
- Double quotient = results.get(0) / results.get(1);
- result = String.valueOf(quotient);
- return result;
- } else {
- return invalidArgumentsAmountError;
- }
-
-
- }
-
- public static String Add(String userInput)
- {
- String result = "";
- ArrayList<Double> results = getNumbers(userInput);
- if(results != null)
- {
- Double sum = results.get(0) + results.get(1);
- result = String.valueOf(sum);
- return result;
- } else {
- return invalidArgumentsAmountError;
- }
-
- }
-
- public static String Subtract(String userInput)
- {
- //perform the operation
- //convert to string
- //return string
- String result = "";
- ArrayList<Double> results = getNumbers(userInput);
- if(results != null)
- {
- Double difference = results.get(0) - results.get(1);
- result = String.valueOf(difference);
- return result;
- } else {
- return invalidArgumentsAmountError;
- }
-
- }
-
- public static String InvertSign(String userInput)
- {
- ArrayList <Double> number1 = getNumber(userInput);
- return number.get(0) * -1;
- //perform the operation
- //convert to string
- //return string
- return null;
- }
-
- public static String InvertNumber(String userInput)
- {
- //perform the operation
- //convert to string
- //return string
- return null;
- }
-
- public static void Clear()
- {
- //reset the default value to 0
- }
- }
|