Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will be developing a phone record analysis program as described below. Your

ID: 3600958 • Letter: Y

Question

You will be developing a phone record analysis program as described below. Your program reads phone call information from a text file name “phoneCalls.txt”. Each record in the file has the following format 480-678-1234/10/10-08-2017/Josh William/2.45 Program reads record at a time from the file and generates an array list of phone call objects. The phone call object has the destination phone number, duration, call date, callee name (person who called by the caller), and the cost of the call. You can add required methods phone call object based on your design. Once the object is created, it will be placed in an array list called phone calls. Then, your program can perform following operations A) Search a call based on the destination number B) Display the longest call information C) Display the shortest call information D) Sort the phone calls array list based on the call duration and display calls in the sorted order (display the destination phone number and the duration) E) Prepare a phone bill and store the phone bill in the “phone bill.txt” file. Format of the “phone_bills.txt” is given below . F) Exit the program Destination number Duration Call Date Callee Name Cost

Explanation / Answer

Please find below code:

PhoneMain.java

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Date;

import java.util.List;

import java.util.Scanner;

public class PhoneMain {

public static void main(String[] args) throws IOException, ParseException {

// TODO Auto-generated method stub

File f = new File(args[0]);

FileReader fileReader = new FileReader(f);

BufferedReader bufferedReader = new BufferedReader(fileReader);

//StringBuffer stringBuffer = new StringBuffer();

String line;

PhoneObject phn;

List<PhoneObject> myList = new ArrayList<PhoneObject>();

List<PhoneObject> dummy = new ArrayList<PhoneObject>();

while ((line = bufferedReader.readLine()) != null) {

/*stringBuffer.append(line);

stringBuffer.append(" ");*/

String vals[]=line.split(",");

String destinationNumber = vals[0];

double callTime = Double.valueOf(vals[1]);

String date = vals[2];

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

Date startDate = df.parse(date);

String callerName = vals[3];

double cost = Double.valueOf(vals[4]);

phn = new PhoneObject(destinationNumber, callTime, startDate, callerName, cost);

myList.add(phn);

}

fileReader.close();

dummy = myList;

boolean flag=true;

Scanner sc=new Scanner(System.in);

String care="";

do{

System.out.println("Menu");

System.out.println("A) Search a call based on the destination number");

System.out.println("B) Display the longest call information");

System.out.println("C) Display the shortest call information ");

System.out.println("D) Sort the phone calls array list based on the call duration and display calls in the sorted order");

System.out.println("E) Prepare a phone bill and store the phone bill");

System.out.println("F) Exit the program");

care=sc.nextLine();

switch (care) {

case "A":

System.out.println("Search a call based on the destination number");

System.out.println("Enter Destination Number:");

String phnNum = sc.nextLine();

String found="";

for(PhoneObject ph:myList){

String number = ph.getDestPhnNum();

if(number.equalsIgnoreCase(phnNum)){

found=ph.toString();

}

}

if(!found.isEmpty()){

System.out.println(found);

}else{

System.out.println("Not Found");

}

break;

case "B":

System.out.println("Longest call information");

Collections.sort(dummy, new Comparator<PhoneObject>() {

@Override

public int compare(PhoneObject c1, PhoneObject c2) {

return Double.compare(c1.getCallTime(), c2.getCallTime());

}

});

phn = dummy.get(dummy.size()-1);

System.out.println(phn.toString());

break;

case "C":

System.out.println("Shortest call information");

Collections.sort(dummy, new Comparator<PhoneObject>() {

@Override

public int compare(PhoneObject c1, PhoneObject c2) {

return Double.compare(c1.getCallTime(), c2.getCallTime());

}

});

phn = dummy.get(0);

System.out.println(phn.toString());

break;

case "D":

System.out.println("Display Calls based on call duration");

Collections.sort(dummy, new Comparator<PhoneObject>() {

@Override

public int compare(PhoneObject c1, PhoneObject c2) {

return Double.compare(c1.getCallTime(), c2.getCallTime());

}

});

for(PhoneObject ph:dummy){

System.out.println(ph.toString());

}

break;

case "E":

System.out.println("Write to a file");

String text = "";

BufferedWriter output = null;

try {

File file = new File("phone_bills.txt");

output = new BufferedWriter(new FileWriter(file));

double totalBill=0.0;

StringBuffer sff = new StringBuffer();

for(PhoneObject p1:myList){

text = p1.toString()+" ";

sff.append(text);

totalBill+=p1.getCallCost();

}

output.write(sff.toString());

output.write("Total Bill::"+totalBill);

} catch ( IOException e ) {

e.printStackTrace();

} finally {

if ( output != null ) {

output.close();

}

}

break;

case "F":

System.out.println("Bye Bye");

flag=false;

break;

default:

System.out.println("INVALID Selection Try again!");

}

}while(flag);

}

}

PhoneObject.java

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

public class PhoneObject {

private String destPhnNum;

private double callTime;

private Date callDate;

private String callerName;

private double callCost;

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

public PhoneObject(String destPhnNum, double callTime, Date callDate, String callerName, double callCost) {

super();

this.destPhnNum = destPhnNum;

this.callTime = callTime;

this.callDate = callDate;

this.callerName = callerName;

this.callCost = callCost;

}

public String getDestPhnNum() {

return destPhnNum;

}

public void setDestPhnNum(String destPhnNum) {

this.destPhnNum = destPhnNum;

}

public double getCallTime() {

return callTime;

}

public void setCallTime(double callTime) {

this.callTime = callTime;

}

public Date getCallDate() {

return callDate;

}

public void setCallDate(Date callDate) {

this.callDate = callDate;

}

public String getCallerName() {

return callerName;

}

public void setCallerName(String callerName) {

this.callerName = callerName;

}

public double getCallCost() {

return callCost;

}

public void setCallCost(double callCost) {

this.callCost = callCost;

}

@Override

public String toString() {

return "PhoneObject [destPhnNum=" + destPhnNum + ", callTime=" + callTime + ", callDate=" + df.format(callDate)

+ ", callerName=" + callerName + ", callCost=" + callCost + "]";

}

}

Output:

Menu
A) Search a call based on the destination number
B) Display the longest call information
C) Display the shortest call information
D) Sort the phone calls array list based on the call duration and display calls in the sorted order
E) Prepare a phone bill and store the phone bill
F) Exit the program
D
Display Calls based on call duration
PhoneObject [destPhnNum=268-567-2312, callTime=2.16, callDate=14-08-2017, callerName=Krishna, callCost=0.45]
PhoneObject [destPhnNum=480-678-1234, callTime=10.0, callDate=10-08-2017, callerName=Josh William, callCost=2.45]
PhoneObject [destPhnNum=123-678-2215, callTime=11.26, callDate=12-08-2017, callerName=Sona u, callCost=3.15]
PhoneObject [destPhnNum=785-890-4545, callTime=56.0, callDate=15-08-2017, callerName=Kumar, callCost=52.45]
PhoneObject [destPhnNum=145-678-4332, callTime=99.11, callDate=13-08-2017, callerName=Agarqal, callCost=122.45]
Menu
A) Search a call based on the destination number
B) Display the longest call information
C) Display the shortest call information
D) Sort the phone calls array list based on the call duration and display calls in the sorted order
E) Prepare a phone bill and store the phone bill
F) Exit the program

Input file:

480-678-1234,10,10-08-2017,Josh William,2.45
123-678-2215,11.26,12-08-2017,Sona u,3.15
145-678-4332,99.11,13-08-2017,Agarqal,122.45
268-567-2312,2.16,14-08-2017,Krishna,0.45
785-890-4545,56.0,15-08-2017,Kumar,52.45

It will continue until u pree F. Please execute the java program and give your inpuy files as command line arguments.

Thank you. All the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote