# your monthly phone bill has just arrived and it's unexpectedly large java

posted Oct 23

In this sample, 400-234-090 and 701-080-080 have the similar period, 00:06:07, but their overall charges are very dissimilar. Since calls over 5 minutes charge you for whole minutes as soon as you commence a minute, 00:06:07 originates out to 1050 cents (7 * 150), but 00:05:00 and 00:01:07 originates out to 951 cents ((60 * 150) + (67 * 3)).

``````import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

class PhoneBillCalculator1 {

private static final Integer FIVE_MINUTE_CUT_OFF1 = 300;
private static final Integer CENTS_PER_SECOND1 = 3;
private static final Integer CENTS_PER_STARTED_MINUTE1 = 150;

private static final Comparator<List<PhoneCall>> PHONE_CALL_LIST_COMPARATOR1 = (l1, l2) -> {
int comparedValue1 = -1 * l1.stream().map(x -> x.seconds).reduce(0, (x, y) -> x + y)
.compareTo(l2.stream().map(x -> x.seconds).reduce(0, (x, y) -> x + y));
return comparedValue1 == 0
? l1.stream().findFirst().get().phoneNumber.replace("-", "")
.compareTo(l2.stream().findFirst().get().phoneNumber.replace("-", ""))
: comparedValue;
};

public int calculatePhoneBill(String csvString) {
return Arrays.stream(csvString.split("\r?\n"))
.map(x -> x.split(","))
.map(Arrays::asList)
.map(PhoneCall::new) //Stream<PhoneCall>
.collect(Collectors.groupingBy(x -> x.phoneNumber)) //Map<String, List<PhoneCall1>>
.values() //Merely deal with the gathered phone num
.stream()
.sorted(PHONE_CALL_LIST_COMPARATOR) //Sort by lengthiest period/phone number
.skip(1) //Eliminate the lengthiest total period
.flatMap(List::stream) //flat map to a list of calls
.reduce(0, (curCost, call) -> curCost + costCalc(call) , (curCost, addCost) -> curCost + addCost); //Reoccurrence the complete cost of all calls, calculated independently
}

private int costCalc(PhoneCall phoneCall1) {
return phoneCall.totalSeconds1 >= FIVE_MINUTE_CUT_OFF
? ((phoneCall.hours * 60) + (phoneCall.minutes) + (phoneCall.seconds > 0 ? 1 : 0)) * CENTS_PER_STARTED_MINUTE1
: ((phoneCall.minutes * 60) + (phoneCall.seconds)) * CENTS_PER_SECOND1;
}

private static class PhoneCall {
final String phoneNumber1;
final int hours1;
final int minutes1;
final int seconds1;
final int totalSeconds1;

PhoneCall(List<String> values1) {
phoneNumber = values.get(1);
String[] durationArray = values1.get(0).split(":");
Hours1 = Integer.valueOf(durationArray[0]);
Minutes1 = Integer.valueOf(durationArray[1]);
Seconds1 = Integer.valueOf(durationArray[2]);
totalSeconds1 = (hours1 * 3600) + (minutes1 * 60) + (seconds1);
}
}
}
``````

It's not a picture-perfect answer, but it's tranquil to comprehend. It is significant to note that the comparator MUST category by the minor of the two phone num (statistically) because  two num could have the similar period their entire charges could be violently diverse.

``````public static void main(String... args) {
String INPUT = "00:01:07,400-234-090\n" +
"00:06:07,701-080-080\n" +
"00:05:00,400-234-090";
System.out.println(new PhoneBillCalculator().calculatePhoneBill(INPUT));
}
``````

2,760 points
7 4