Nexmo

Jsonstringtool

May 2nd, 2022
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. /**
  2.  * Write a calculator than can execute three operations:
  3.  * - "add"     : to sum up a list of integer numbers
  4.  * - "count"   : to count the amount of substrings in a larger string
  5.  * - "weekday" : to specify the week day of a given date, where 1=Sunday, 2=Monday, ...
  6.  *
  7.  * The input for the calculator is given as a JSON string that needs to be parsed. Each string only
  8.  * contains one command. The output for each calculation is just a long number of the result.
  9.  *
  10.  * Examples for the three operations in JSON are:
  11.  * - {"operator":"add", "operands":[5,8,-3]}" to add the 5+8+(-3). Result: 10
  12.  * - {"operator":"count", "text":"abbaabbababb", "searchToken":"bb"} to count the "bb" is the string. Result: 3
  13.  * - {"operator":"weekday", "date":"1976-03-12"} to extract Friday, thus Result: 6
  14.  *
  15.  * Hint:
  16.  * - You may use any of the installed libraries: JSON-Simple, JUnit 4 and Apache Commons Lang3.
  17.  * - If needed, you are allowed to use the internet to search for documentations and code snippets.
  18.  * - You can run your code as often as you like by pressing "Run" in the lower left corner of this screen.
  19.  * - When everything is correct, the result 10 / 3 / 6 should appear on the console.
  20.  * - Try to use clean coding techniques as much as possible to create a good readable source code.
  21.  *
  22.  * Last remark: All actions you perform in this screen are recorded for a later review by CAS.
  23.  * Please send an email back to CAS as soon as you have finished.
  24.  */
  25.  
  26. import java.io.*;
  27. import org.json.simple.*;
  28. import org.json.simple.parser.*;
  29. // TODO: add additional imports if needed
  30.  
  31. class Calculator {
  32.     public static void main (String[] args) throws Exception {
  33.     String[] expressions = new String[3];    
  34.     expressions[0] = "{\"operator\":\"add\",\"operands\":[5,8,-3]}";
  35.     expressions[1] = "{\"operator\":\"count\",\"text\":\"abbaabbababb\",\"searchToken\":\"bb\"}";
  36.     expressions[2] = "{\"operator\":\"weekday\",\"date\":\"1976-03-12\"}";
  37.    
  38.     for (int i = 0; i < expressions.length; i++) {
  39.       long result = execute(expressions[i]);
  40.       System.out.println(result);
  41.     }
  42.   }
  43.    
  44.   private static long execute(String jsonExpression) throws Exception {
  45.     // TODO: Put in your code here.    
  46.     return 0;      
  47.   }  
  48.  
  49.   // TODO: Add additional helper methods here if needed.
  50. }
Advertisement
Add Comment
Please, Sign In to add comment