ZhenyaDudko

Untitled

Sep 4th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import org.json.simple.JSONObject;
  2. import org.json.simple.JSONArray;
  3. import org.json.simple.parser.JSONParser;
  4. import org.json.simple.parser.ParseException;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.*;
  10. import java.text.SimpleDateFormat;
  11.  
  12. public class Main {
  13.  
  14. static class MyJSONComparator implements Comparator<JSONObject> {
  15.  
  16. @Override
  17. public int compare(JSONObject o1, JSONObject o2) {
  18. Long a = (Long) o1.get("id");
  19. Long b = (Long) o2.get("id");
  20. return a.compareTo(b);
  21. }
  22. }
  23.  
  24. public static void main(String[] args) {
  25. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  26. try {
  27. String in = bufferedReader.readLine();
  28. JSONParser parser = new JSONParser();
  29. JSONArray goods = (JSONArray) parser.parse(in);
  30.  
  31. int price_less = 0, price_greater = 0;
  32. String name_contains = "";
  33. Date date_before = null, date_after = null;
  34.  
  35. String pattern = "dd.MM.yyyy";
  36. SimpleDateFormat simple_date_format = new SimpleDateFormat(pattern);
  37.  
  38. for (int i = 0; i < 5; i++) {
  39. in = bufferedReader.readLine();
  40. String[] parsed = in.split(" ");
  41. if (Objects.equals(parsed[0], "NAME_CONTAINS")) {
  42. name_contains = parsed[1].toLowerCase();
  43. } else if (Objects.equals(parsed[0], "PRICE_GREATER_THAN")) {
  44. price_greater = Integer.parseInt(parsed[1]);
  45. } else if (Objects.equals(parsed[0], "PRICE_LESS_THAN")) {
  46. price_less = Integer.parseInt(parsed[1]);
  47. } else if (Objects.equals(parsed[0], "DATE_BEFORE")) {
  48. date_before = simple_date_format.parse(parsed[1]);
  49. } else if (Objects.equals(parsed[0], "DATE_AFTER")) {
  50. date_after = simple_date_format.parse(parsed[1]);
  51. }
  52. }
  53. bufferedReader.close();
  54.  
  55. ArrayList<JSONObject> list = new ArrayList<>();
  56. for (Object good : goods) {
  57. JSONObject cur_good = (JSONObject) good;
  58. String name = ((String) cur_good.get("name")).toLowerCase();
  59. Date date = simple_date_format.parse((String) cur_good.get("date"));
  60. Long price = (Long) cur_good.get("price");
  61. if (name.contains(name_contains)
  62. && (date.after(date_after) || date.equals(date_after))
  63. && (date.before(date_before) || date.equals(date_before))
  64. && price >= price_greater
  65. && price <= price_less) {
  66. list.add(cur_good);
  67. }
  68. }
  69. list.sort(new MyJSONComparator());
  70.  
  71. JSONArray result = new JSONArray();
  72. for (JSONObject good : list) {
  73. result.add(good);
  74. }
  75. System.out.println(result.toString());
  76.  
  77. } catch (IOException | ParseException | java.text.ParseException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment