Dimitrija

Untitled

Dec 3rd, 2021
692
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.dimitrija.LogProcessor;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6.  
  7.  
  8. /**
  9.  * Да се имплементира класата LogProcessor со единствен метод:
  10.  *
  11.  *     void readLines(InputStream is, OutputStream os, int processId) којшто од влезен поток ќе ги прочита сите логови (секој од нив во нов ред), а на излезен поток ќе ги испечати сите логови кои припаѓаат на процесот со processId сортирани според текстот на логот во растечки редослед. Текстот на логот е делот кој се наоѓа по знакот ":" во секој лог.
  12.  *
  13.  * Задачата мора да ги помине сите тест примери за да се продолжи на главниот дел од испитот.
  14.  *
  15.  *
  16.  * For example:
  17.  * Input    Result
  18.  *
  19.  * 99900:STATUS CODE 200 OK
  20.  * 78976:AUTHENTICATION ERROR
  21.  * 12345:STATUS CODE 201
  22.  * 12334:DATABASE DOWN
  23.  *
  24.  *
  25.  * 99900:STATUS CODE 200 OK
  26.  * 78976:AUTHENTICATION ERROR
  27.  * 12345:STATUS CODE 201
  28.  * 12334:DATABASE DOWN
  29.  *
  30.  *
  31.  *
  32.  * 12334:DATABASE DOWN
  33.  *
  34.  *
  35.  *
  36.  * 12334:DATABASE DOWN
  37.  *
  38.  *
  39.  *
  40.  *
  41.  * 12334:SERVICE Z NOT RUNNING
  42.  * 78999:SERVICE X NOT RUNNING
  43.  * 12334:STATUS CODE 302
  44.  * 99900:SERVICE X NOT RUNNING
  45.  * 78976:STATUS CODE 304
  46.  * 12345:DATABASE DOWN
  47.  *
  48.  *
  49.  *
  50.  * 12334:SERVICE Z NOT RUNNING
  51.  * 12334:STATUS CODE 302
  52.  *
  53.  *
  54.  *
  55.  * 12334:SERVICE Z NOT RUNNING
  56.  * 12334:STATUS CODE 302
  57.  *
  58.  *
  59.  *
  60.  *
  61.  * 12334:DATABASE DOWN
  62.  * 34567:STATUS CODE 201
  63.  * 99900:STATUS CODE 200 OK
  64.  * 12334:STATUS CODE 302
  65.  * 35677:STATUS CODE 304
  66.  * 78999:STATUS CODE 302
  67.  * 23456:AUTHORIZATION ERROR
  68.  * 12345:STATUS CODE 500
  69.  *
  70.  *
  71.  *
  72.  * 12334:DATABASE DOWN
  73.  * 12334:STATUS CODE 302
  74.  *
  75.  *
  76.  *
  77.  * 12334:DATABASE DOWN
  78.  * 12334:STATUS CODE 302
  79.  *
  80.  *
  81.  *
  82.  *
  83.  * 12334:SERVICE Z NOT RUNNING
  84.  * 12334:SERVICE X NOT RUNNING
  85.  * 12345:AUTHENTICATION ERROR
  86.  * 23456:SERVICE Z NOT RUNNING
  87.  * 35677:STATUS CODE 304
  88.  * 12334:STATUS CODE 304
  89.  * 12345:STATUS CODE 200 OK
  90.  * 78976:SERVICE Z NOT RUNNING
  91.  * 35677:AUTHENTICATION ERROR
  92.  *
  93.  *
  94.  *
  95.  * 12334:SERVICE X NOT RUNNING
  96.  * 12334:SERVICE Z NOT RUNNING
  97.  * 12334:STATUS CODE 304
  98.  *
  99.  *
  100.  *
  101.  * 12334:SERVICE X NOT RUNNING
  102.  * 12334:SERVICE Z NOT RUNNING
  103.  * 12334:STATUS CODE 304
  104.  */
  105. class Log implements Comparable<Log>{
  106.     @Override
  107.     public String toString() {
  108.         return ID + ":" + statusLog;
  109.     }
  110.  
  111.     private int ID;
  112.     private String statusLog;
  113.  
  114.     public Log(int ID, String status) {
  115.         this.ID = ID;
  116.         this.statusLog = status;
  117.     }
  118.  
  119.     public static Log createALog(String s){
  120.         String[] strings = s.split(":");
  121.         int ID = Integer.parseInt(strings[0]);
  122.         String statusLog = strings[1];
  123.         return new Log(ID,statusLog);
  124.     }
  125.  
  126.     public int getID() {
  127.         return ID;
  128.     }
  129.  
  130.     @Override
  131.     public int compareTo(Log o) {
  132.         return statusLog.compareTo(o.statusLog);
  133.     }
  134. }
  135.  
  136. class LogProcessor{
  137.  
  138.     private List<Log> logList;
  139.  
  140.     void readLines(InputStream is, OutputStream os, int processId) throws IOException {
  141.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
  142.         if (bufferedReader == null){
  143.             throw new IOException();
  144.         }
  145.         logList = bufferedReader.lines()
  146.                 .map(x->Log.createALog(x)).collect(Collectors.toList());
  147.  
  148.         PrintWriter printWriter = new PrintWriter(os);
  149.         if (printWriter == null){
  150.             throw new IOException();
  151.         }
  152.         logList.stream().filter(x->x.getID() == processId).sorted().forEach(printWriter::println);
  153.  
  154.         printWriter.flush();
  155.     }
  156.  
  157.  
  158. }
  159.  
  160. public class LogProcessorTest {
  161.     public static void main(String[] args) {
  162.         LogProcessor lineProcessor = new LogProcessor();
  163.  
  164.         try {
  165.             lineProcessor.readLines(System.in, System.out, 12334);
  166.         } catch (IOException e) {
  167.             e.printStackTrace();
  168.         }
  169.     }
  170. }
RAW Paste Data