Question:
Help With a Java Program?
anonymous
2008-04-09 19:59:15 UTC
An Internet service provider has three different service packages for its customers:

A: For $9.95 per month 10 hours of access are provided. Additional hours are
$2.00 per hour.
B: For $13.95 per month 20 hours of access are provided. Additional hours are
$1.00 per hour.
C: For $19.95 per month unlimited access is provided.

Write an interactive Java program (e.g. Bill.java) that calculates a customer's monthly bill. It should ask the user to enter the letter of the service package the customer has purchased (A, B, or C) and the number of hours (integer) that were used. It should then display the total charges. If an invalid package (other than (A, B, or C) or invalid number of hours (<0 or > 30*24) is input, the program should report it and ask for new input until the valid input is given.The program should use constants (declared with the modifier final) to represent the
base rates, per hour rates and base hours.
Three answers:
dan131m
2008-04-09 20:08:03 UTC
The only loop you need is the input loop, and for that I'd suggest a do/while loop since you want to loop while the user has not yet chosen to abort the program.
anonymous
2008-04-10 10:02:29 UTC
hmmm, just testing for ABC or if we have a number takes about 30 minutes, but adding that third range condition made it a little tougher. I disagree with the requirement for constants. static methods for aPlan, bPlan, cPlan can hold the math and values. You have to know the switch() branch will ONLY accept a primitive, not a String...



static Scanner scanner;

static char choice;



public static void main(String args[]) {



scanner = new Scanner(System.in);

boolean run = true;



while (run) {

System.out.println("W E L C O M E");



choice =

checkMenuEntry(

"Please select from the following [A B C D]: ");

billEntry(choice);

.....

System.out.print("Would you like to do another? [ y / n ]: ");

run = scanner.next().

equalsIgnoreCase("y") ? true : false;



}

========

public static Character checkMenuEntry(String msg) {

boolean suspectItem = true;

String entry = "";

while (suspectItem) {

System.out.print(msg);

entry = scanner.next();



if (validateMenuEntry(entry)) {

System.out.println("that is a correct choice");

return entry.charAt(0);

} else {

System.out.println("Incorrect entry, try again...");

}

}

return entry.charAt(0);

}

=======

public static boolean validateMenuEntry(String menuItem) {

return (menuItem.equalsIgnoreCase("A") ||

menuItem.equalsIgnoreCase("B") ||

menuItem.equalsIgnoreCase("C"));

}

=======



public static void billEntry(char choice) {

float bill = 0.0f;

float aHours = 0.0f;



switch (choice) {

case 'a':

System.out.println(">> Company A <<");

aHours = checkNumber("Enter total hours: ");

bill = typeA(aHours);



break;

=========

this is a good, lite, exercise in validation, 150 lines of code, I can't put it all in this window.
Yahoo! Answerer
2008-04-09 20:07:04 UTC
It would be my pleasure to do your homework! -.-



char pack = readChar("Enter package: ");

while(pack!= 'A' && char!='B' && char!='C')

pack = readChar("Enter package: ");

int d = readDouble("Enter number of hours: ");

while(d<0||d>30*24)

d = readDouble("Enter number of hours: ");



double total;



if(char=='A')

total = 9.95;

else if(char=='B')

total = 13.95;

else

total = 19.95;



if(char=='A'){

d-=10;

while(d>0){ total+=2; d--;}

}else if(char=='B'){

d-=20; while(d>0){total+=1; d--;}



System.out.println("The bill is: $"+total);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...