84.0k views
5 votes
Residential and business customers are paying different rates for water usage. Residential customers pay $0.005 per gallon for the first 6000 gallons. If the usage is more than 6000 gallons, the rate will be $0.007 per gallon after the first 6000 gallons. Business customers pay $0.006 per gallon for the first 8000 gallons. If the usage is more than 8000 gallons, the rate will be $0.008 per gallon after the first 8000 gallons. For example, a residential customer who has used 9000 gallons will pay $30 for the first 6000 gallons ($0.005 * 6000), plus $21 for the other 3000 gallons ($0.007 * 3000). The total bill will be $51. A business customer who has used 9000 gallons will pay $48 for the first 8000 gallons ($0.006 * 8000), plus $8 for the other 1000 gallons ($0.008 * 1000). The total bill will be $56. Write a program to do the following. Ask the user which type the customer it is and how many gallons of water have been used. Calculate and display the bill.

User Gui Silva
by
6.2k points

1 Answer

2 votes

Answer:

// here is program in Java.

// import package

import java.util.*;

// class definition

class Main

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// variable

int gall;

double cost=0;

// object to read value from user

Scanner scr=new Scanner(System.in);

// ask user to enter type

System.out.print("Enter customer type (R for residential or B for business ):");

// read type of customer

char t=scr.next().charAt(0);

if(t=='r'||t=='R')

{

System.out.print("enter the number of gallons:");

//read number of gallons

gall=scr.nextInt();

// if number of gallons are less or equal to 6000

if(gall<=6000)

{

// calculate cost

cost=gall*0.007;

// print cost

System.out.println("total cost is: "+cost);

}

else

{

// calculate cost

cost=(6000*0.005)+((gall-6000)*0.007);

// print cost

System.out.println("total cost is: "+cost);

}

}

else if(t=='b'||t=='B')

{

System.out.print("enter the number of gallons:");

//read number of gallons

gall=scr.nextInt();

// if number of gallons are less or equal to 8000

if(gall<=8000)

{

// calculate cost

cost=gall*0.006;

// print cost

System.out.println("total cost is: "+cost);

}

else

{// calculate cost

cost=(8000*0.006)+((gall-8000)*0.008);

// print cost

System.out.println("total cost is: "+cost);

}

}

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Ask user to enter the type of customer and assign it to variable "t" with scanner object.If the customer type is business then read the number of gallons from user and assign it to variable "gall". Then calculate cost of gallons, if gallons are less or equal to 8000 then multiply it with 0.006.And if gallons are greater than 8000, cost for first 8000 will be multiply by 0.006 and for rest gallons multiply with 0.008.Similarly if customer type is residential then for first 6000 gallons cost will be multiply by 0.005 and for rest it will multiply by 0.007. Then print the cost.

Output:

Enter customer type (R for residential or B for business ):r

enter the number of gallons:8000

total cost is: 44.0

User Thomas Deniau
by
6.3k points