collections
Sat May 04 2024 04:59:20 GMT+0000 (Coordinated Universal Time)
Saved by
@cms
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<CallLog> callLogs = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("weeklycall.csv"))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 4) {
String name = parts[0];
String dialledNumber = parts[1];
int duration = Integer.parseInt(parts[2]);
Date dialledDate = new SimpleDateFormat("yyyy-MM-dd").parse(parts[3]);
callLogs.add(new CallLog(name, dialledNumber, duration, dialledDate));
}
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
Collections.sort(callLogs);
Collections.reverse(callLogs);
System.out.println("Call-Logs");
System.out.println("Caller Name Duration");
for (CallLog log : callLogs) {
System.out.println(log);
}
}
}
//CallLog.java
import java.util.Date;
public class CallLog implements Comparable<CallLog> {
private String name;
private String dialledNumber;
private int duration;
private Date dialledDate;
public CallLog(String name,String d,int du,Date di){
this.name=name;
this.dialledNumber=d;
this.duration=du;
this.dialledDate=di;
}
@Override
public int compareTo(CallLog other) {
return this.name.compareTo(other.name);
}
@Override
public String toString() {
return name + "(+91-"+this.dialledNumber+") " + duration + " Seconds";
}
}
content_copyCOPY
Comments