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

Your monthly phone bill has just arrived, and it\'s unexpectedly large. k2 You d

ID: 3860597 • Letter: Y

Question

Your monthly phone bill has just arrived, and it's unexpectedly large. k2 You decide to verify the amount by recalculating the bill based on k 2 your phone cll lgs and the phone companyýs charges. your phone call logs and the phone company's charges The logs are given as a string S consisting of N lines separated by end-of-line characters (ASClIl code 10). Each line describes one phone call using the following format: "hh:mm: ss, nnn-nnn-nnn", where "hh :mm: ss" denotes the duration of the call (in "hh" hours, "mm" minutes and "ss" seconds) and "nnn-nnn-nnn" denotes the 9-digit phone number of the recipient (with no leading zeros) Each call is billed separately. The billing rules are as follows: . If the call was shorter than 5 minutes, then you pay 3 cents for every started second of the call (e.g. for duration "00:01:07" you pay 67* 3 201 cents). e If the call was at least 5 minutes long. then you pay 150 cents for every started minute of the call (e.g. for duration "00:05:00" you pay 5 150 -750 cents and for duration 00:05: 01 you pay 6 150 900 Alinalsto the phone number that has cents) . All calls to the phone number that has the longest total duration of calls are free. In the case of a tie, if more than one phone number shares the longest total duration, the promotion is applied only to the phone number whose numerical value is the smallest among these phone numbers Custom test cases 0/10 +

Explanation / Answer

Given below is the code for the question with output . Please rate the answer if it helped. Thank you.


class PhoneCall{
private String phoneNum;
private int totalDuration;
private int totalCost;

public PhoneCall(String phNum, int dur, int cost){
phoneNum = phNum;
totalDuration = dur;
totalCost = cost;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public int getDuration() {
return totalDuration;
}
public void setDuration(int duration) {
this.totalDuration = duration;
}
public int getCost() {
return totalCost;
}
public void setCost(int cost) {
this.totalCost = cost;
}

public void add(int duration, int cost)
{
this.totalDuration += duration;
this.totalCost += cost;
}

}
public class Solution {
private int getDurationInSecs(String duration)
{
String[] tokens = duration.split(":");
int seconds = 0;
seconds += Integer.parseInt(tokens[0]) * 60 * 60 ; //convert hours to secs
seconds += Integer.parseInt(tokens[1]) * 60; //convert mins to secs
seconds += Integer.parseInt(tokens[2]) ; //add secs
return seconds;

}

private int getCost(int callSecs)
{
int fiveMins = 5 * 60;
double //make it double so that we don't do pure int division
if(callSecs < fiveMins)
return callSecs * 3; //3 cents for each second
else{
int startedMins =(int) Math.ceil(callSecs / oneMinute );
return startedMins * 150;
}
}

private PhoneCall findPhoneNum(String phoneNo, PhoneCall[] calls, int n){
for(int i = 0; i < n; i++)
if(calls[i].getPhoneNum().equals(phoneNo))
return calls[i];

return null;
}

private PhoneCall findLongestDuration(PhoneCall[] calls, int n)
{
int maxIdx = 0;
for(int i = 1; i < n; i++){
if(calls[i].getDuration() > calls[maxIdx].getDuration())
maxIdx = i;
else if(calls[i].getDuration() == calls[maxIdx].getDuration()){ //if its a tie
//chose the phone number which is smallest
if(calls[i].getPhoneNum().compareTo(calls[maxIdx].getPhoneNum()) < 0)
maxIdx = i;
}
}
return calls[maxIdx];
}

public int solution(String S){
  
String lines[] = S.split(" ");
PhoneCall[] calls = new PhoneCall[lines.length];
int count = 0;
int totalCost = 0;
for(int i = 0; i < lines.length; i++)
{
  
String tokens[] = lines[i].split(",");
int duration = getDurationInSecs(tokens[0].trim());
int cost = getCost(duration);
String phNum = tokens[1].trim();
PhoneCall ph = findPhoneNum(phNum, calls, count);
if(ph == null)
calls[count++] = new PhoneCall(phNum, duration, cost);
else
ph.add(duration, cost);

totalCost += cost;
}

//return the total cost leaving out the longest duration phone number
PhoneCall longest = findLongestDuration(calls, count);
System.out.println("Free calls to " + longest.getPhoneNum());
totalCost -= longest.getCost();
return totalCost;
}

public static void main(String[] args) {
Solution solution = new Solution();
String calls = "00:01:07,400-234-090" + " " +
"00:05:01,701-080-080" + " " +
"00:05:00,400-234-090";

System.out.println("calls made " + calls);
System.out.println("Cost for calls " + solution.solution(calls));
}
}

output

calls made
00:01:07,400-234-090
00:05:01,701-080-080
00:05:00,400-234-090
Free calls to 400-234-090
Cost for calls 900