CSC 111 Program 1 Summer 2018 Water Bill Write a Java application that will ask
ID: 3916057 • Letter: C
Question
CSC 111 Program 1 Summer 2018 Water Bill Write a Java application that will ask the user to input the account name, a five digit account number, an account code, and the number of gallons used for the quarter. The water bill will be calculated according to the following rates as listed by the Onondaga County Water Authority. Residential Accounts: Line Fee: Usage Fee: First 10,000 $22.09 Next 13,000 Over 23,000 2.42 per 1000 gallons 3.22 per 1000 gallons 4.03 per 1000 gallons Commercial Accounts: Line Fee Usage Fee: $22.09 2.65 per 1000 gallons Industrial Accounts Line Fee: Usage Fee: First 200,000 $18.41 2.16 per 1000 gallons Next 2,500,000 .73 per 1000 gallons Over 2,700,000 4.03 per 1000 gallons A switch will be used to select the appropriate rate schedule. The program will display an error message if an incorrect choice is made and will loop until a valid choice is made. The program will echo the input data as well as the total bill. The 5 digit account number will include leading zeros when appropriate. A repetition structure will be used to enable the user to calculate more than one bill. Sample output Welcome to the Onondaga Water Bill Calculator Please enter your account name: Sanderson Family Please enter your 5 digit account number: lease enter the number of gallons used this quarter 15278. 65 Please choose from the following account types: 45 H for residential C for commercial I for Industrial Onondaga County Water Bill Account Name: Sanderson Family Account Number: 00045 Account Type: Residential Gallons Used: 15278.65 Amount Due $63.29 Do you have another accoun t to process?Explanation / Answer
ScreenShot
-------------------------------------------------------------------------
Program
//packages for I/O and formatting
import java.text.DecimalFormat;
import java.util.*;
//main class
public class WaterBill {
public static void main(String[] args) {
//Scanner for user input
Scanner sc=new Scanner(System.in);
//variable for check repeatation calculation
char acc=' ';
//Variable for total bill calculation
double totalDue=0.0;
//Formatter
DecimalFormat df=new DecimalFormat("#.##");
//Loop through until user enter 'N'
do {
//due set 0 always before calculation
totalDue=0.0;
//Prompt user to enter details
System.out.println(" Welcome to the Onondaga water bill calculator");
System.out.println();
System.out.print("Please enter your account name: ");
String name=sc.nextLine();
System.out.print("Please enter your 5 digit account number: ");
String number=sc.nextLine();
//Check account number is 5 digit length
while(number.length()!=5) {
System.out.print("Please enter your 5 digit account number: ");
number=sc.nextLine();
}
System.out.print("Please enter the number of gallons used this quarter: ");
double gallon=sc.nextDouble();
System.out.print("Please choose from the following account types: ");
System.out.println();
System.out.println("H for residential C for commercial I for industrial");
char ch=sc.next().charAt(0);
//Loop until entered account type is correct
while((ch!='H'&& ch!='h')&&(ch!='C' && ch!='c')&& (ch!='I'&& ch!='i')) {
System.out.print("Please choose from the following account types: ");
System.out.println();
System.out.println("H for residential C for commercial I for industrial");
ch=sc.next().charAt(0);
}
//Switch used to perform calculation of water bill in each account type seperately
switch(ch) {
//Calculation for residential
case 'H':
case 'h':
totalDue+=22.09;
if(gallon<=10000) {
totalDue+=((gallon/1000)*2.42);
}
else if(gallon>10000 && gallon<=23000) {
totalDue+=((10*2.42)+((gallon-10000)/1000)*3.22);
}
else if(gallon>23000) {
totalDue+=(10*2.42)+(13*3.22)+(((gallon-23000)/1000)*4.03);
}
break;
//Calculation for Commercial
case 'C':
case 'c':
totalDue+=22.09+((gallon/1000)*2.65);
break;
//Calculation for industrial
case 'I':
case 'i':
totalDue+=18.41;
if(gallon<=200000) {
totalDue+=((gallon/1000)*2.16);
}
else if(gallon>200000 && gallon<=4500000) {
totalDue+=((200*2.16)+((gallon-200000)/1000)*1.73);
}
else if(gallon>2700000) {
totalDue+=(200*2.16)+(700*1.73)+(((gallon-2700000)/1000)*4.03);
}
break;
}
//Print formatted bill
System.out.println();
System.out.println("Onondaga County Water Bill");
System.out.println();
System.out.println("Account Name: "+name);
//Proceding space of an account number fill with 0
String accNumber="";
accNumber+=number.replace(' ','0');
System.out.println("Account Number: "+accNumber);
if(ch=='H') {
System.out.println("Account Type: Residential");
}
else if(ch=='C') {
System.out.println("Account Type: Commercial");
}
else if(ch=='I') {
System.out.println("Account Type: Industrial");
}
System.out.println("Gallon Used: "+gallon);
System.out.println("Amount Due: $"+df.format(totalDue));
System.out.println("Do you have another account to process?(Y or N)");
acc=sc.next().charAt(0);
}while(acc=='y'|| acc=='Y');
}
}
------------------------------------------------------------
Output
Welcome to the Onondaga water bill calculator
Please enter your account name: Sanderson Family
Please enter your 5 digit account number: 45
Please enter the number of gallons used this quarter: 15278.65
Please choose from the following account types:
H for residential
C for commercial
I for industrial
h
Onondaga County Water Bill
Account Name: Sanderson Family
Account Number: 00045
Gallon Used: 15278.65
Amount Due: $63.29
Do you have another account to process?(Y or N)
n