This is a part from the code of my application. With the following method i want to calculate the cost of each call. Each call "belongs" to a service. So i call the method in main with argument the phone number of the service. The problem (specified with red color) is that i cannot calculate the cost correctly for the first call that overtakes the free time.
For example, the free time is 30 min and 0,1 cost for every additional minute. The first call lasts 15 min and the second 20. The result must be: for the first call, cost 0, and for the second, cost 0,1*5=0,5.
I think the solution would be something like this (in pseudocode):public void printCalls(String num) { double j = 0; /* total duration */ double y = 0; /* cost of each call*/ for(int i=0; i<Call.calls.size();i++) /* for every call */ { if (Call.calls.get(i).getCallingNumber().equals(num)) /* if the calling number equals the service's number */ { j+= Call.calls.get(i).getCallingDuration(); /* increase j by duration of call*/ if (j <= 30*60) /* if j < free calling time*/ { y = 0; } else y = Call.calls.get(i).getCallingDuration()*0.11; System.out.printf ("%s. %s: %s %s: %s %s: %s %s %s: %s’’ %s: %.2f€\n", "Call" , "From" , Call.calls.get(i).getCallingNumber() , " To" , Call.calls.get(i).getDialedNumber(), " Time" , Call.calls.get(i).getCallingDate(),Call.calls.get(i).getCallingTime(), " Duration" , Call.calls.get(i).getCallingDuration() , " Cost" , y); } } }
But the problem is that i don't know how to get the value of total duration of the previous iteration.if total duration <= free duration cost = 0; else { if total duration [B][U]of the previous iteration (i-1)[/U][/B] <= free duration cost = (duration - (free duration - total duration of the previous iteration) )* cost else duration * cost }
Any Suggestions?