Write a program that calculates and prints the bill for a cellular telephone com
ID: 3691345 • Letter: W
Question
Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of service: regular and premium. Its rates vary, depending on the type of service. The rates are computed as follows: a. For calls made from 6:00 a.m. to 6:00 p.m., the first 75 minutes are free; charges for more than 75 minutes are $0.10 per minute. b. For calls made from 6:00 p.m. to 6:00 a.m., the first 100 minutes are free; charges for more than 100 minutes are $0.05 per minute. Your program should prompt the user to enter an account number, a service code (type char), and the number of minutes the service was used. A service code of r or R means regular service; a service code of p or P means premium service. Treat any other character as an error. Your program should output the account number, type of service, number of minutes the telephone service was used, and the amount due from the user. For the premium service, the customer may be using the service during the day and the night. Therefore, to calculate the bill, you must ask the user to input the number of minutes the service was used during the day and the number of minutes the service was used during the night.Explanation / Answer
package com.chegg.testing;
import java.io.IOException;
import java.util.Scanner;
public class CellularBill {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int acctNum = 0;
char serCd = 0;
int minutes = 0;
String choice = null;
boolean validAcct = false;
boolean validSerCd = false;
boolean validMinutes = false;
double totalCharge = 0;
double dayCharges = 0;
double nightCharges = 0;
boolean minCharges = false;
// Reading account number from user
do {
try {
System.out.println("Enter account number:");
acctNum = Integer.parseInt(sc.next());
validAcct = true;
} catch (NumberFormatException nfe) {
System.out.println("Account number should be numeric only.");
validAcct = false;
}
} while (!validAcct);
// Reading service type code from user
String tempSc;
do {
System.out.println("Please enter service code (P/R)");
if ((tempSc = sc.next()).length() > 1) {
System.out.println("The service code entered was wrong.");
validSerCd = false;
} else {
serCd = tempSc.charAt(0);
choice = String.valueOf(serCd).toLowerCase();
if (!choice.equals("p") && !choice.equals("r")) {
validSerCd = false;
System.out.println("The service code entered was wrong.");
} else {
validSerCd = true;
}
}
} while (!validSerCd);
// Reading minutes used from the user
do {
try {
System.out
.println("Please enter the minutes the service was used:");
minutes = Integer.parseInt(sc.next());
validMinutes = true;
} catch (NumberFormatException nfe) {
System.out.println("Account number should be numeric only.");
validMinutes = false;
}
} while (!validMinutes);
if (validAcct && validSerCd && validMinutes) {
switch (choice) {
case "p":
// Reading day minutes from the user
System.out
.println("Please enter the minutes the service was used during the day:");
int day = Integer.parseInt(sc.next());
// Reading night minutes from the user
System.out
.println("Please enter the minutes the service was used during the night:");
int night = Integer.parseInt(sc.next());
// Checking if the minutes entered by the user is equal to
// entered day and night minutes
if (minutes == day + night) {
// Getting Day minutes from minutes
int tempDay = minutes - night;
if (tempDay <= 75) {
minCharges = true;
} else {
int tempChrag = tempDay - 75;
dayCharges = tempChrag * 0.10;
}
// Getting Night minutes from minutes
int tempNight = minutes - day;
if (tempNight <= 100) {
minCharges = true;
} else {
int tempChrag = tempNight -100;
nightCharges = tempChrag * 0.05;
}
// checking for minimum charged to be applied
if (minCharges) {
totalCharge = 25;
} else {
totalCharge = 25+dayCharges + nightCharges;
}
} else {
System.out
.println("Total Minutes entered is not equal to sum of day and night minutes");
}
break;
case "r":
if (minutes <= 50) {
totalCharge = 10;
} else {
int rem = minutes - 50;
totalCharge = 10 + rem * 0.20;
}
break;
}
System.out.println("Account Number: " + acctNum
+ " Type of service: " + serCd
+ " Number of minutes the telephone service was used: "
+ minutes + " The amount due from the user: $"
+ totalCharge);
}
sc.close();
}
}
Output:
Enter account number:
306421330
Please enter service code (P/R)
P
Please enter the minutes the service was used:
200
Please enter the minutes the service was used during the day:
80
Please enter the minutes the service was used during the night:
120
Account Number: 306421330
Type of service: P
Number of minutes the telephone service was used: 200
The amount due from the user: $26.5
Enter account number:
306421330
Please enter service code (P/R)
r
Please enter the minutes the service was used:
100
Account Number: 306421330
Type of service: r
Number of minutes the telephone service was used: 100
The amount due from the user: $20.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.