Reputation: 65
I have a problem to solve in Java. Basically, I have to calculate how many KWH customer has had spent last month till today. From 0-300 KWH it is $5 total. From 301 to 1000kwh it is $5 for first 300kwh and then each kwh is $0.03 extra. from 1001 and above it is $0.02 extra for each kwh. I am not sure how to apply those 0.03 and 0.02 on each kwh after 300kwh. Thanks.
Upvotes: 0
Views: 70
Reputation: 33068
Start with $5. This is the amount.
If the total is greater than 300 kwh,
then the amount should be increased by $0.03 per kwh above 300.
If the total is greater than 1000 kwh,
then the amount should be reduced by $0.01 per kwh above 1000.
Start with that pseudo-code. Here's some actual code based on your comments:
int totalReading = ...; // something
// $5 is the minimum cost
double cost = 5.0;
// above 300, the cost is 0.03 per
if (totalReading > 300)
cost += (totalReading - 300) * 0.03;
// above 1000, the cost drops 0.01 per
if (totalReading > 1000)
cost -= (totalReading - 1000) * 0.01;
Upvotes: 1