Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.Duration;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.Comparator;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- class TelcoApp {
- Map<String,Call> calls;
- Map<String, List<Call>> callsPerPerson;
- public TelcoApp() {
- this.calls= new HashMap<>();
- this.callsPerPerson = new HashMap<>();
- }
- public void addCall(String uuid, String dialer, String receiver, long timestamp) {
- Call newCall = new Call(uuid, dialer, receiver, timestamp);
- calls.putIfAbsent(uuid,newCall);
- callsPerPerson.putIfAbsent(dialer, new ArrayList<>());
- callsPerPerson.putIfAbsent(receiver, new ArrayList<>());
- callsPerPerson.get(dialer).add(newCall);
- callsPerPerson.get(receiver).add(newCall);
- }
- public void updateCall(String uuid, long timestamp, String action) {
- if(action.equals(ActionType.ANSWER.name())){
- calls.get(uuid).setTimestamp_answer(timestamp);
- }
- else if(action.equals(ActionType.END.name())){
- calls.get(uuid).setTimestamp_end(timestamp);
- }
- else if(action.equals(ActionType.HOLD.name())){
- calls.get(uuid).addTimestampHold(timestamp);
- }
- else{
- calls.get(uuid).addTimestampResume(timestamp);
- }
- }
- public void printChronologicalReport(String phoneNumber) {
- for(Call call : callsPerPerson.get(phoneNumber)) {
- String caller= "R";
- String callerNumber = call.dialer;
- if(call.dialer.equals(phoneNumber)){
- callerNumber = call.receiver;
- caller = "D";
- }
- String timestampEnd = String.valueOf(call.timestamp_end);
- String timestampAnswer = String.valueOf(call.timestamp_answer);
- if(call.getCallDuration().equals("00:00")){
- timestampAnswer = timestampEnd;
- timestampEnd = "MISSED CALL";
- }
- System.out.println(String.format("%s %s %s %s %s",
- caller,callerNumber,timestampAnswer,timestampEnd,call.getCallDuration()));
- }
- }
- public void printReportByDuration(String phoneNumber) {
- List<Call> callsByGivenPhoneNumber = callsPerPerson.get(phoneNumber)
- .stream().sorted().collect(Collectors.toList());
- for(Call call : callsByGivenPhoneNumber){
- String caller= "R";
- String callerNumber = call.dialer;
- if(call.dialer.equals(phoneNumber)){
- callerNumber = call.receiver;
- caller = "D";
- }
- String timestampEnd = String.valueOf(call.timestamp_end);
- String timestampAnswer = String.valueOf(call.timestamp_answer);
- if(call.getCallDuration().equals("00:00")){
- timestampAnswer = timestampEnd;
- timestampEnd = "MISSED CALL";
- }
- System.out.println(String.format("%s %s %s %s %s",
- caller,callerNumber,timestampAnswer,timestampEnd,call.getCallDuration()));
- }
- }
- public void printCallsDuration() {
- Map<String,Long> summmedCallsBetweenTwoContacts = calls.values().stream().sorted().collect(Collectors.groupingBy(
- i->i.dialer+" <-> "+i.receiver,
- Collectors.summingLong(Call::getCallDurationLong)
- ));
- Map<String,String> summmedCallsBetweenTwoContactsAdapted = summmedCallsBetweenTwoContacts.entrySet().stream()
- .collect(Collectors.toMap(
- entry->entry.getKey(),
- entry->DurationConverter.convert(entry.getValue()),
- (e1,e2)->e1,
- LinkedHashMap::new
- ));
- summmedCallsBetweenTwoContactsAdapted.entrySet()
- .stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
- .forEach(i-> System.out.println(String.format("%s : %s",i.getKey(),i.getValue())));
- }
- }
- enum ActionType {
- ANSWER,
- RESUME,
- HOLD,
- END
- }
- class Call implements Comparable<Call> {
- String uuid;
- String dialer;
- String receiver;
- long timestamp;
- long timestamp_answer;
- long timestamp_end;
- List<Long> timestamps_hold;
- List<Long> timestamps_resume;
- public Call(String uuid, String dialer, String receiver, long timestamp) {
- this.uuid = uuid;
- this.dialer = dialer;
- this.receiver = receiver;
- this.timestamp = timestamp;
- this.timestamp_answer = 0;
- this.timestamp_end = 0;
- this.timestamps_hold = new ArrayList<>();
- this.timestamps_resume= new ArrayList<>();
- }
- public void addTimestampHold(long timestamp) {
- this.timestamps_hold.add(timestamp);
- }
- public void addTimestampResume(long timestamp) {
- this.timestamps_resume.add(timestamp);
- }
- public void setTimestamp_answer(long timestamp_answer) {
- this.timestamp_answer = timestamp_answer;
- }
- public void setTimestamp_end(long timestamp_end) {
- this.timestamp_end = timestamp_end;
- }
- public long getTimestamp() {
- return timestamp;
- }
- public long getCallDurationLong(){
- if(timestamp_answer==0)
- return 0;
- if(timestamps_hold.size()==0)
- return timestamp_end-timestamp_answer;
- if(timestamps_resume.size()==0)
- return timestamps_hold.get(0)-timestamp_answer;
- long durationOnHold = 0;
- for(int i=0;i<timestamps_resume.size();i++){
- durationOnHold+= (timestamps_resume.get(i)-timestamps_hold.get(i));
- }
- if(timestamps_hold.size()>timestamps_resume.size())
- durationOnHold+= timestamp_end-timestamps_hold.get(timestamps_hold.size()-1);
- return timestamp_end-timestamp_answer-durationOnHold;
- }
- public String getCallDuration(){
- if(timestamp_answer==0)
- return "00:00";
- if(timestamps_hold.size()==0)
- return DurationConverter.convert(timestamp_end-timestamp_answer);
- if(timestamps_resume.size()==0)
- return DurationConverter.convert(timestamps_hold.get(0)-timestamp_answer);
- long durationOnHold = 0;
- for(int i=0;i<timestamps_resume.size();i++){
- durationOnHold+= (timestamps_resume.get(i)-timestamps_hold.get(i));
- }
- if(timestamps_hold.size()>timestamps_resume.size())
- durationOnHold+= timestamp_end-timestamps_hold.get(timestamps_hold.size()-1);
- return DurationConverter.convert(timestamp_end-timestamp_answer-durationOnHold);
- }
- @Override
- public int compareTo(Call o) {
- return Comparator.comparing(Call::getCallDuration).reversed().thenComparing(Call::getTimestamp,Comparator.reverseOrder()).compare(this,o);
- }
- }
- public class TelcoTest2 {
- public static void main(String[] args) {
- TelcoApp app = new TelcoApp();
- Scanner sc = new Scanner(System.in);
- while (sc.hasNextLine()) {
- String line = sc.nextLine();
- String[] parts = line.split("\\s+");
- String command = parts[0];
- if (command.equals("addCall")) {
- String uuid = parts[1];
- String dialer = parts[2];
- String receiver = parts[3];
- long timestamp = Long.parseLong(parts[4]);
- app.addCall(uuid, dialer, receiver, timestamp);
- } else if (command.equals("updateCall")) {
- String uuid = parts[1];
- long timestamp = Long.parseLong(parts[2]);
- String action = parts[3];
- app.updateCall(uuid, timestamp, action);
- } else if (command.equals("printChronologicalReport")) {
- String phoneNumber = parts[1];
- app.printChronologicalReport(phoneNumber);
- } else if (command.equals("printReportByDuration")) {
- String phoneNumber = parts[1];
- app.printReportByDuration(phoneNumber);
- } else {
- app.printCallsDuration();
- }
- }
- }
- }
- class DurationConverter {
- public static String convert(long duration) {
- long minutes = duration / 60;
- duration %= 60;
- return String.format("%02d:%02d", minutes, duration);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement