Advertisement
dzocesrce

[NP] Дирекција за безбедност и контраразузнавање

Apr 29th, 2025
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.52 KB | None | 0 0
  1.  
  2. import java.time.Duration;
  3. import java.time.LocalDateTime;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6. import java.util.Comparator;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.stream.Collectors;
  12.  
  13. class TelcoApp {
  14.     Map<String,Call> calls;
  15.     Map<String, List<Call>> callsPerPerson;
  16.  
  17.     public TelcoApp() {
  18.         this.calls= new HashMap<>();
  19.         this.callsPerPerson = new HashMap<>();
  20.     }
  21.  
  22.     public void addCall(String uuid, String dialer, String receiver, long timestamp) {
  23.         Call newCall = new Call(uuid, dialer, receiver, timestamp);
  24.         calls.putIfAbsent(uuid,newCall);
  25.         callsPerPerson.putIfAbsent(dialer, new ArrayList<>());
  26.         callsPerPerson.putIfAbsent(receiver, new ArrayList<>());
  27.         callsPerPerson.get(dialer).add(newCall);
  28.         callsPerPerson.get(receiver).add(newCall);
  29.     }
  30.  
  31.     public void updateCall(String uuid, long timestamp, String action) {
  32.         if(action.equals(ActionType.ANSWER.name())){
  33.             calls.get(uuid).setTimestamp_answer(timestamp);
  34.         }
  35.         else if(action.equals(ActionType.END.name())){
  36.             calls.get(uuid).setTimestamp_end(timestamp);
  37.         }
  38.         else if(action.equals(ActionType.HOLD.name())){
  39.             calls.get(uuid).addTimestampHold(timestamp);
  40.         }
  41.         else{
  42.             calls.get(uuid).addTimestampResume(timestamp);
  43.         }
  44.  
  45.     }
  46.  
  47.     public void printChronologicalReport(String phoneNumber) {
  48.         for(Call call : callsPerPerson.get(phoneNumber)) {
  49.             String caller= "R";
  50.             String callerNumber = call.dialer;
  51.             if(call.dialer.equals(phoneNumber)){
  52.                 callerNumber = call.receiver;
  53.                 caller = "D";
  54.             }
  55.             String timestampEnd = String.valueOf(call.timestamp_end);
  56.             String timestampAnswer = String.valueOf(call.timestamp_answer);
  57.             if(call.getCallDuration().equals("00:00")){
  58.                 timestampAnswer = timestampEnd;
  59.                 timestampEnd = "MISSED CALL";
  60.             }
  61.  
  62.             System.out.println(String.format("%s %s %s %s %s",
  63.                     caller,callerNumber,timestampAnswer,timestampEnd,call.getCallDuration()));
  64.         }
  65.     }
  66.  
  67.     public void printReportByDuration(String phoneNumber) {
  68.         List<Call> callsByGivenPhoneNumber = callsPerPerson.get(phoneNumber)
  69.                 .stream().sorted().collect(Collectors.toList());
  70.         for(Call call : callsByGivenPhoneNumber){
  71.             String caller= "R";
  72.             String callerNumber = call.dialer;
  73.             if(call.dialer.equals(phoneNumber)){
  74.                 callerNumber = call.receiver;
  75.                 caller = "D";
  76.             }
  77.             String timestampEnd = String.valueOf(call.timestamp_end);
  78.             String timestampAnswer = String.valueOf(call.timestamp_answer);
  79.             if(call.getCallDuration().equals("00:00")){
  80.                 timestampAnswer = timestampEnd;
  81.                 timestampEnd = "MISSED CALL";
  82.             }
  83.  
  84.             System.out.println(String.format("%s %s %s %s %s",
  85.                     caller,callerNumber,timestampAnswer,timestampEnd,call.getCallDuration()));
  86.         }
  87.         }
  88.  
  89.     public void printCallsDuration() {
  90.         Map<String,Long> summmedCallsBetweenTwoContacts = calls.values().stream().sorted().collect(Collectors.groupingBy(
  91.                 i->i.dialer+" <-> "+i.receiver,
  92.                 Collectors.summingLong(Call::getCallDurationLong)
  93.                 ));
  94.  
  95.         Map<String,String> summmedCallsBetweenTwoContactsAdapted = summmedCallsBetweenTwoContacts.entrySet().stream()
  96.                 .collect(Collectors.toMap(
  97.                 entry->entry.getKey(),
  98.                 entry->DurationConverter.convert(entry.getValue()),
  99.                 (e1,e2)->e1,
  100.                 LinkedHashMap::new
  101.         ));
  102.  
  103.         summmedCallsBetweenTwoContactsAdapted.entrySet()
  104.                 .stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
  105.                 .forEach(i-> System.out.println(String.format("%s : %s",i.getKey(),i.getValue())));
  106.  
  107.     }
  108. }
  109.  
  110.  
  111. enum ActionType {
  112.     ANSWER,
  113.     RESUME,
  114.     HOLD,
  115.     END
  116. }
  117.  
  118. class Call implements Comparable<Call> {
  119.     String uuid;
  120.     String dialer;
  121.     String receiver;
  122.     long timestamp;
  123.     long timestamp_answer;
  124.     long timestamp_end;
  125.     List<Long> timestamps_hold;
  126.     List<Long> timestamps_resume;
  127.  
  128.     public Call(String uuid, String dialer, String receiver, long timestamp) {
  129.         this.uuid = uuid;
  130.         this.dialer = dialer;
  131.         this.receiver = receiver;
  132.         this.timestamp = timestamp;
  133.         this.timestamp_answer = 0;
  134.         this.timestamp_end = 0;
  135.         this.timestamps_hold = new ArrayList<>();
  136.         this.timestamps_resume= new ArrayList<>();
  137.     }
  138.  
  139.     public void addTimestampHold(long timestamp) {
  140.         this.timestamps_hold.add(timestamp);
  141.     }
  142.  
  143.     public void addTimestampResume(long timestamp) {
  144.         this.timestamps_resume.add(timestamp);
  145.     }
  146.  
  147.     public void setTimestamp_answer(long timestamp_answer) {
  148.         this.timestamp_answer = timestamp_answer;
  149.     }
  150.  
  151.     public void setTimestamp_end(long timestamp_end) {
  152.         this.timestamp_end = timestamp_end;
  153.     }
  154.    
  155.         public long getTimestamp() {
  156.         return timestamp;
  157.     }
  158.  
  159.     public long getCallDurationLong(){
  160.         if(timestamp_answer==0)
  161.             return 0;
  162.         if(timestamps_hold.size()==0)
  163.             return timestamp_end-timestamp_answer;
  164.         if(timestamps_resume.size()==0)
  165.             return timestamps_hold.get(0)-timestamp_answer;
  166.  
  167.         long durationOnHold = 0;
  168.         for(int i=0;i<timestamps_resume.size();i++){
  169.             durationOnHold+= (timestamps_resume.get(i)-timestamps_hold.get(i));
  170.         }
  171.         if(timestamps_hold.size()>timestamps_resume.size())
  172.             durationOnHold+= timestamp_end-timestamps_hold.get(timestamps_hold.size()-1);
  173.         return timestamp_end-timestamp_answer-durationOnHold;
  174.     }
  175.  
  176.     public String getCallDuration(){
  177.         if(timestamp_answer==0)
  178.             return "00:00";
  179.         if(timestamps_hold.size()==0)
  180.             return DurationConverter.convert(timestamp_end-timestamp_answer);
  181.         if(timestamps_resume.size()==0)
  182.             return DurationConverter.convert(timestamps_hold.get(0)-timestamp_answer);
  183.  
  184.         long durationOnHold = 0;
  185.         for(int i=0;i<timestamps_resume.size();i++){
  186.             durationOnHold+= (timestamps_resume.get(i)-timestamps_hold.get(i));
  187.         }
  188.         if(timestamps_hold.size()>timestamps_resume.size())
  189.             durationOnHold+= timestamp_end-timestamps_hold.get(timestamps_hold.size()-1);
  190.         return DurationConverter.convert(timestamp_end-timestamp_answer-durationOnHold);
  191.     }
  192.  
  193.  
  194. @Override
  195.     public int compareTo(Call o) {
  196.         return Comparator.comparing(Call::getCallDuration).reversed().thenComparing(Call::getTimestamp,Comparator.reverseOrder()).compare(this,o);
  197.     }
  198. }
  199.  
  200.  
  201. public class TelcoTest2 {
  202.     public static void main(String[] args) {
  203.         TelcoApp app = new TelcoApp();
  204.  
  205.         Scanner sc = new Scanner(System.in);
  206.  
  207.         while (sc.hasNextLine()) {
  208.             String line = sc.nextLine();
  209.             String[] parts = line.split("\\s+");
  210.             String command = parts[0];
  211.  
  212.             if (command.equals("addCall")) {
  213.                 String uuid = parts[1];
  214.                 String dialer = parts[2];
  215.                 String receiver = parts[3];
  216.                 long timestamp = Long.parseLong(parts[4]);
  217.                 app.addCall(uuid, dialer, receiver, timestamp);
  218.             } else if (command.equals("updateCall")) {
  219.                 String uuid = parts[1];
  220.                 long timestamp = Long.parseLong(parts[2]);
  221.                 String action = parts[3];
  222.                 app.updateCall(uuid, timestamp, action);
  223.             } else if (command.equals("printChronologicalReport")) {
  224.                 String phoneNumber = parts[1];
  225.                 app.printChronologicalReport(phoneNumber);
  226.             } else if (command.equals("printReportByDuration")) {
  227.                 String phoneNumber = parts[1];
  228.                 app.printReportByDuration(phoneNumber);
  229.             } else {
  230.                 app.printCallsDuration();
  231.             }
  232.         }
  233.  
  234.     }
  235. }
  236.  
  237. class DurationConverter {
  238.     public static String convert(long duration) {
  239.         long minutes = duration / 60;
  240.         duration %= 60;
  241.         return String.format("%02d:%02d", minutes, duration);
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement