Dimitrija

Untitled

Dec 3rd, 2021
654
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.dimitrija.LogProcessorTest;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6.  
  7.  
  8. interface Log {
  9.  
  10.     //todo: complete interface
  11.     int getLogId();
  12.     String getLogMessage();
  13.     double getLogProblemValue();
  14.     int getStatusCode();
  15. }
  16.  
  17. class SystemALog implements Log {
  18.  
  19.     private int logId;
  20.     private String logMessage;
  21.  
  22.     public SystemALog(int logId, String logMessage) {
  23.         this.logId = logId;
  24.         this.logMessage = logMessage;
  25.     }
  26.  
  27.     @Override
  28.     public int getLogId() {
  29.         return logId;
  30.     }
  31.  
  32.     @Override
  33.     public String getLogMessage() {
  34.         return logMessage;
  35.     }
  36.  
  37.     @Override
  38.     public double getLogProblemValue() {
  39.         return logMessage.hashCode() + 2.45 * logId;
  40.     }
  41.  
  42.     //todo: implement method
  43.     @Override
  44.     public int getStatusCode() {
  45.         return 0;
  46.     }
  47.  
  48.     @Override
  49.     public String toString() {
  50.         return "SystemALog{" +
  51.                 "logId=" + logId +
  52.                 ", logMessage='" + logMessage + '\'' +
  53.                 '}';
  54.     }
  55. }
  56.  
  57. class SystemBLog implements Log {
  58.  
  59.     private String logId;
  60.     private String logMessage;
  61.  
  62.     public SystemBLog(String logId, String logMessage) {
  63.         this.logId = logId;
  64.         this.logMessage = logMessage;
  65.     }
  66.  
  67.     @Override
  68.     public int getLogId() {
  69.         return logId.hashCode();
  70.     }
  71.  
  72.     @Override
  73.     public String getLogMessage() {
  74.         return logMessage;
  75.     }
  76.  
  77.     @Override
  78.     public double getLogProblemValue() {
  79.         return logMessage.hashCode() * 3.45 + getLogId() * 1.35;
  80.     }
  81.  
  82.     //todo: implement method
  83.     @Override
  84.     public int getStatusCode() {
  85.         return 0;
  86.     }
  87.  
  88.     @Override
  89.     public String toString() {
  90.         return "SystemBLog{" +
  91.                 "logId='" + logId + '\'' +
  92.                 ", logMessage='" + logMessage + '\'' +
  93.                 '}';
  94.     }
  95. }
  96.  
  97. interface LogProcessor<R extends Log> {
  98.     R compute(ArrayList<R> Logs);
  99. }
  100.  
  101. class Logs<T extends Log>{
  102.  
  103.     //todo: add fields
  104.     private ArrayList<T> logs;
  105.     //todo: constructor
  106.  
  107.     public Logs(ArrayList<T> logs) {
  108.         logs = new ArrayList<>();
  109.     }
  110.  
  111.     public <R extends Log> void process(LogProcessor<T> processor) {
  112.         System.out.println(processor.compute(logs));
  113.     }
  114. }
  115.  
  116. public class LogProcessorTest<T extends Log> {
  117.  
  118.     public static void main(String[] args) {
  119.  
  120.         ArrayList<SystemALog> systemALogsList = new ArrayList<>();
  121.         ArrayList<SystemBLog> systemBLogsList = new ArrayList<>();
  122.  
  123.         String[] logTexts = {"STATUS CODE 500 SERVER ERROR", "STATUS CODE 304", "STATUS CODE 200 OK", "DATABASE DOWN ERROR", "SERVICE X NOT RUNNING",
  124.                 "STATUS CODE 201", "STATUS CODE 302", "SERVICE Z NOT RUNNING", "AUTHENTICATION ERROR", "AUTHORIZATION ERROR"};
  125.  
  126.         Scanner sc = new Scanner(System.in);
  127.         int countSystemALogs;
  128.         countSystemALogs = Integer.parseInt(sc.nextLine());
  129.         while (countSystemALogs > 0) {
  130.             int id = Integer.parseInt(sc.nextLine());
  131.             systemALogsList.add(new SystemALog(id, logTexts[id]));
  132.             --countSystemALogs;
  133.         }
  134.  
  135.         int countSystemBLogs;
  136.         countSystemBLogs = Integer.parseInt(sc.nextLine());
  137.         while (countSystemBLogs > 0) {
  138.             String line = sc.nextLine();
  139.             String[] parts = line.split(":");
  140.             String text = Arrays.stream(parts).skip(1).collect(Collectors.joining(" "));
  141.             systemBLogsList.add(new SystemBLog(parts[0], text));
  142.             --countSystemBLogs;
  143.         }
  144.  
  145.         Logs<SystemALog> logsA = new Logs<>(systemALogsList);
  146.  
  147.         //todo: first processor
  148.         LogProcessor<SystemALog> firstProcessor = (logs) -> {
  149.  
  150.         System.out.println(logs.stream().filter(x-> x.getLogMessage().contains("ERROR")).count());
  151.  
  152.         //NEZNAM STO DA VRATI ME FAKJA PANIKA
  153.         return null;
  154.         };
  155.  
  156.                 System.out.println("----- RESULTS FROM THE FIRST PROCESSOR -----");
  157.         logsA.process(firstProcessor);
  158.  
  159.         //todo: second processor
  160.         LogProcessor<SystemALog> secondProcessor =(logs) -> {
  161.             DoubleSummaryStatistics doubleSummaryStatistics = logs.stream().mapToDouble(SystemALog::getLogProblemValue).summaryStatistics();
  162.             System.out.println(doubleSummaryStatistics);
  163.             return null;
  164.         };
  165.                 System.out.println("----- RESULTS FROM THE SECOND PROCESSOR -----");
  166.         logsA.process(secondProcessor);
  167.  
  168.         Logs<SystemBLog> logsB = new Logs<>(systemBLogsList);
  169.  
  170.         //todo: third processor
  171.         LogProcessor<SystemBLog> thirdProcessor =(logs) -> {
  172.             logs =  logs.stream().sorted().collect(Collectors.toCollection(ArrayList::new));
  173.             System.out.println(logs);
  174.            return null;
  175.         };
  176.                 System.out.println("----- RESULTS FROM THE THIRD PROCESSOR -----");
  177.         logsB.process(thirdProcessor);
  178.  
  179.         //TODO fourth processor
  180.         LogProcessor<SystemBLog> fourthProcessor =(logs) -> {
  181.             double totalProblems = logs.stream().mapToDouble(SystemBLog::getLogProblemValue).sum();
  182.             System.out.println(totalProblems);
  183.             return null;
  184.         };
  185.                 System.out.println("----- RESULTS FROM THE FOURTH PROCESSOR -----");
  186.         logsB.process(fourthProcessor);
  187.     }
  188. }
  189.  
RAW Paste Data