algorithm for the program

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

>>Show an algorithm for the program below:

public class electricbill {

public static void main(String[] args) {
int presentReading = 0, previousReading = 0;
Scanner kbd = new Scanner(System.in);
String consumer = ""; // to hold name of consumer
char cType = 'x'; // to hold type of consumer
int nCMUsed; // to hold number of units consumed
int minCMResidential = 12; // to hold cut-off for minimum Bill
// for residential consumers
double minBillResidential = 98; // minimum bill for <= 12 units

float rateResidential = 8.1583F; // cost of 1 unit above the min. consumption
int minCMCommercial = 7; // to hold cut-off for minimum Bill for commercial consumers
double minBillCommercial = 600.00; // minimum bill for <= 20 units used
float rateCommercial = 50.00F; // cost of 1 unit above the min.
// consumption for commercial consumers
double amountDue = 0.0; // to hold the amount due
showIntroduction();
// prompt message for name
System.out.print("Enter the name of the electricity consumer: ");
// input name
consumer = kbd.nextLine();
// store previous reading using method readPreviousReading();
previousReading = readPreviousReading();
// store cuurent reading using method readPresentReading();
presentReading = readPresentReading(previousReading);
// store electricity consumption
nCMUsed = presentReading - previousReading;
// store consumer type
cType = readTypeOfConsumer();
// compute due amount
amountDue = computeAmountDue(nCMUsed, cType, minCMResidential,
minBillResidential, minCMCommercial,
minBillCommercial, rateResidential,
rateCommercial);

showBill(consumer, cType, previousReading, presentReading, nCMUsed,
amountDue);
System.exit(0);
} // end of main method
public static void showBill(String n, char t, int previous, int present,
int c, double amount) {
System.out.println("-------------------------------");
System.out.println("Electricity Billing Statement");
System.out.println("Name of Consumer: " + n);
System.out.print("Type of consumer: ");
if (Character.toLowerCase(t) == 'r')
System.out.println("Residential");
if (Character.toLowerCase(t) == 'c')
System.out.println("Commercial");
System.out.println("Meter reading last month = " + previous +
" units ");
System.out.println("Meter reading this month = " + present +
" units");
System.out.println("Units Consumed = " + c + " units");
System.out.println("Amount Due = ₱ " + amount);
return;
}
public static void showIntroduction() {
System.out.println();
System.out.println("This program will prepare a bill for a electricity " +
"distribution consumer.");
System.out.println("--------------------------------------------------------------------------");
System.out.println("You will be asked to enter the name of the " +
"consumer,\n\tthe electricity meter reading last month, " +
"\n\t and the electricity reading this month.");
System.out.println("---------------------------------------------------------------------------");
return; // optional since return type of the method is void
}
public static int readPreviousReading() {
int reading = 0;
Scanner kbd = new Scanner(System.in);
do {
System.out.print("Enter the meter reading last month: ");
// input previous reading
reading = Integer.parseInt(kbd.nextLine());
// if less than 0
if (reading < 0) {
System.out.println("The meter reading cannot be negative.");
}
} while (reading < 0); // loop until value is positive
return reading;
}
public static int readPresentReading(int previous) {
int reading = 0;
Scanner kbd = new Scanner(System.in);
do {
System.out.print("Enter the meter reading for this month: ");
// input current reading
reading = Integer.parseInt(kbd.nextLine());
// check if current reading is less than previous reading
if (reading < previous) {
System.out.println("Invalid datum entry! The present reading " +
"must be greater than the previous reading.");
}
} while (reading < previous); // loop until current > previous
return reading;
}
public static char readTypeOfConsumer() {
char t = 'x';
Scanner kbd = new Scanner(System.in);
do {
System.out.print("Enter the type of the consumer (type r " +
"for residential or c for commercial): ");
// input consumer type
String input = kbd.nextLine();
// store first character of input
t = input.charAt(0);
if (t != 'r' && t != 'R' && t != 'c' && t != 'C') {
System.out.println("The valid types are r for residential " +
"and c for commercial.");
}
} while (t != 'r' && t != 'R' && t != 'c' && t != 'C'); // loop until type is valid
return t;
}
public static double computeAmountDue(int c, char t, int min1, double minB1,
int min2, double minB2, float rate1,

float rate2) {

double amount = 0;
switch (t) {
case 'r':
case 'R':
if (c <= min1) {
amount = minB1;
} else {
amount = minB1 + (c - min1) * rate1;
}
break;
case 'c':
case 'C':
if (c <= min2) {
amount = minB2;
} else {
amount = minB2 + (c - min2) * rate2;
}
}
return amount;
}
} // end of class

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Top down approach design
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education