Assingment must be done in Java: You will be developing a phone record analysis
ID: 3600821 • Letter: A
Question
Assingment must be done in Java:
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
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
Ex.
Here is the data
Thank You!
480-678-1234/10/10-08-2017/Josh Wiliam/2.45, Destination number Duration Call Date Callee Name CostExplanation / Answer
Hello, I developed everything as per your requirements. In my answer, I created two classes PhoneCall.java and Test.java. In PhoneCall.java ; all the required attributes of a phone call are defined as variables. A parameterized constructor ,a default constructor as well as getters and setters are defined. Along with that, a toString() method is defined which will return a string containing all the details. In the Test.java I created an ArrayList of PhoneCall objects, defined two File objects.; and a Scanner object to get the user input. In the main function ; the arraylist is initialized and the menu is shown. I have developed everything in separate functions, i.e separate methods are defined for displaying the menu, finding the longest call, shortest call, sorting the list, saving the bill etc. Comments are included in everything which will help you to understand things easily. Thank you J
//PhoneCall.java file
import java.text.SimpleDateFormat;
import java.util.Date;
public class PhoneCall {
private String dest_no;
private int duration;
private Date date;
private String callee_name;
private float cost;
public PhoneCall(String dest_no, int duration, Date date,
String callee_name, float cost) {
/**
* Parameterized constructor
*/
this.dest_no = dest_no;
this.duration = duration;
this.date = date;
this.callee_name = callee_name;
this.cost = cost;
}
public PhoneCall() {
/**
* Default constructor
*/
}
/**
* Following are getter and setter methods
* for all the variables
*/
public String getDest_no() {
return dest_no;
}
public void setDest_no(String dest_no) {
this.dest_no = dest_no;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getCallee_name() {
return callee_name;
}
public void setCallee_name(String callee_name) {
this.callee_name = callee_name;
}
public float getCost() {
return cost;
}
public void setCost(float cost) {
this.cost = cost;
}
/**
* the following method will return a string containing all
* the details of the phone call
*/
@Override
public String toString() {
String str="Destination: "+dest_no;
str+=" Duration: "+duration+" minutes";
str+=" Date: "+new SimpleDateFormat("dd/mm/yyyy").format(date);/*formatting the date*/
str+=" Callee Name: "+callee_name;
str+=" Call Cost: "+cost;
return str;
}
}
//Test.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Scanner;
public class Test {
/**
* An ArrayList of PhoneCall objects
*/
static ArrayList<PhoneCall> phonecalls;
/**
* A file to get the phone call details
*/
static File phonecalldetails=new File("phoneCalls.txt");
/**
* A file to save the phone bill
*/
static File phonebilldetails=new File("phone_bills.txt");
/**
* Scanner object to get the user input
*/
static Scanner scanner=new Scanner(System.in);
public static void main(String[] args) {
phonecalls=new ArrayList<PhoneCall>();
loadPhoneCalls(); /*fetching the phonecall details*/
displayMenu(); /*displaying the menu*/
}
/**
* This method will load the phone call details from the file
* to the arraylist
*/
public static void loadPhoneCalls(){
try {
Scanner scanner=new Scanner(phonecalldetails);
while(scanner.hasNextLine()){
String line=scanner.nextLine();
String[] cols=line.split("/");/*splitting the line based on '/' character*/
String dest_number=cols[0];
int minutes=Integer.parseInt(cols[1]);
Date date=new SimpleDateFormat("dd-mm-yyyy").parse(cols[2]);
/**
* SimpleDateFormat is used to parse a Date from string in required format
* and vice versa
*/
String name=cols[3];
float cost=Float.parseFloat(cols[4]);
PhoneCall object=new PhoneCall(dest_number,minutes,date,name,cost);
phonecalls.add(object); /*adding it into the list*/
}
} catch (FileNotFoundException e) {
/**
* file not found
*/
e.printStackTrace();
}catch (Exception e) {
/**
* Some error occured due to invalid file structure
*/
}
}
/**
* This method will show a menu; and it will keep looping
* until user presses 6
*/
public static void displayMenu(){
String menu="1. Search a call based on the destination number";
menu+=" 2. Display the longest call information";
menu+=" 3. Display the shortest call information";
menu+=" 4. Display the sorted call information, based on call duration";
menu+=" 5. Save the Phone bill";
menu+=" 6. Exit the program";
menu+=" Enter your choice";
int choice=0;
while(choice!=6){
System.out.println(menu);
try{
choice=Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1:searchCall();
break;
case 2:longestCallDetails();
break;
case 3:shortestCallDetails();
break;
case 4:sortByCallDuration();
break;
case 5:saveBill();
break;
case 6:System.out.println("Thanks, Bye..");
break;
default: throw new Exception(); /*throws the exception*/
}
}catch (Exception e) {
/**
* Invalid input
*/
System.err.println("please enter a valid input only");
displayMenu();/* showing the menu again*/
}
}
}
/**
* Method to search a call using dest number
*/
public static void searchCall(){
System.out.println("Enter the destination number");
String dest=scanner.nextLine();
boolean found=false;
System.out.println("Search results:");
for (PhoneCall call : phonecalls) {/*iterating through the list*/
if(call.getDest_no().equalsIgnoreCase(dest)){
System.out.println(call.toString());
found=true;
}
}
if(!found){
System.out.println("No matching results");
}
}
/**
* method to find the longest call
*/
public static void longestCallDetails(){
System.out.println("Longest call :");
PhoneCall longest=null;
for (PhoneCall call : phonecalls) {/*iterating through the list*/
if(longest==null){ /* assuming first object as the longest call*/
longest=call;
}
if(call.getDuration()>longest.getDuration()){
/**
* if the current object in loop holds a longer duration than
* the assumed one,making it as the longest
*/
longest=call;
}
}
if(longest!=null){
System.out.println(longest.toString());
}else{
System.out.println("No calls found");
}
}
/**
* method to find the shortest call
*/
public static void shortestCallDetails(){
System.out.println("Shortest call :");
PhoneCall shortest=null;
for (PhoneCall call : phonecalls) {/*iterating through the list*/
if(shortest==null){ /* assuming first object as the longest call*/
shortest=call;
}
if(call.getDuration()<shortest.getDuration()){
/**
* if the current object in loop holds a shorter duration than
* the assumed one,making it as the longest
*/
shortest=call;
}
}
if(shortest!=null){
System.out.println(shortest.toString());
}else{
System.out.println("No calls found");
}
}
/**
* method to sort the list
*/
public static void sortByCallDuration(){
Collections.sort(phonecalls,new Comparator<PhoneCall>() {
/**
* sorting based on call duration
*/
public int compare(PhoneCall a, PhoneCall b) {
if(a.getDuration()<b.getDuration()){
return 1;
}else{
return -1;
}
}
});
System.out.println("Destination Duration");
for (PhoneCall call : phonecalls) {
System.out.println(call.getDest_no()+" "+call.getDuration());
}
}
/**
* method to save the bill
*/
public static void saveBill(){
try {
FileWriter writer=new FileWriter(phonebilldetails);
writer.write("Destination Number Duration Cost");
float total=0;
for (PhoneCall call : phonecalls) {
String str=" "+call.getDest_no()+" "+call.getDuration()+" "+call.getCost();
writer.append(str);
total+=call.getCost();
}
String cost=" Total: $"+total;
writer.append(cost);
System.out.println("Bill saved successfully; file name: phone_bills.txt");
writer.close();
} catch (IOException e) {
/**
* Input/Output error happened
*/
}
}
}
//Output
1. Search a call based on the destination number
2. Display the longest call information
3. Display the shortest call information
4. Display the sorted call information, based on call duration
5. Save the Phone bill
6. Exit the program
Enter your choice
1
Enter the destination number
602-578-1000
Search results:
Destination: 602-578-1000
Duration: 12 minutes
Date: 10/05/2017
Callee Name: James Nash
Call Cost: 3.15
1. Search a call based on the destination number
2. Display the longest call information
3. Display the shortest call information
4. Display the sorted call information, based on call duration
5. Save the Phone bill
6. Exit the program
Enter your choice
2
Longest call :
Destination: 480-378-1235
Duration: 24 minutes
Date: 10/03/2017
Callee Name: Michael Smith
Call Cost: 10.85
1. Search a call based on the destination number
2. Display the longest call information
3. Display the shortest call information
4. Display the sorted call information, based on call duration
5. Save the Phone bill
6. Exit the program
Enter your choice
3
Shortest call :
Destination: 515-478-4234
Duration: 1 minutes
Date: 10/03/2017
Callee Name: Robert William
Call Cost: 0.5
1. Search a call based on the destination number
2. Display the longest call information
3. Display the shortest call information
4. Display the sorted call information, based on call duration
5. Save the Phone bill
6. Exit the program
Enter your choice
4
Destination Duration
480-378-1235 24
602-578-1000 12
480-278-6236 11
480-678-1234 10
212-668-2335 5
515-478-4234 1
1. Search a call based on the destination number
2. Display the longest call information
3. Display the shortest call information
4. Display the sorted call information, based on call duration
5. Save the Phone bill
6. Exit the program
Enter your choice
5
Bill saved successfully; file name: phone_bills.txt
1. Search a call based on the destination number
2. Display the longest call information
3. Display the shortest call information
4. Display the sorted call information, based on call duration
5. Save the Phone bill
6. Exit the program
Enter your choice
6
Thanks, Bye..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.