Results 1 to 8 of 8
- 02-20-2011, 06:49 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Java code problem with three different internet service provider packages.
What to do with this JAVA code?
This is the code that I need to do without using the Joptionpane:
An Internet service provider has three different subscription packages for its customers:
Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
Package B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
Package C: For $19.95 per month unlimited access is provided.
Write a program that calculates a customer's monthly bill. It should ask the user to enter the letter of the package the customer has purchased (A, B, or C) and the number of hours that were used. It should then display the total charges.
This is what I have so far : Cannot use Joptionpaneshowinputdialog
/* A demonstration of how to use Decision Structures
* Taha Ahmed
* Cop2250-U03 Project_2
* #13 on page 174 of the textbook
*
*/
import java.util.Scanner;
/**
This program demonstrates a switch statement.
*/
public class ISP13
{
public static void main(String[] args)
{
char packageLetter;
int hoursUsed;
int regularHours;
int additionalHours;
double monthlyFee;
double additionalHoursFee;
double totalFee;
String input;
Scanner keyboard = new Scanner(System.in);
input = input.toUpperCase();
packageLetter = input.charAt(0);
System.out.println("Enter the letter of the " +
"package you purchased (either A, B, or C.");
System.out.println("Enter the number of hours " +
"you used.");
hoursUsed = Integer.parseInt(input);
switch(packageLetter)
{
case 'A':
monthlyFee = 9.95;
regularHours = 10;
additionalHours = hoursUsed - regularHours;
additionalHoursFee = additionalHours * 2.00;
totalFee = monthlyFee + additionalHoursFee;
System.out.println("The total charges is $" +
totalFee + ".");
break;
case 'B':
monthlyFee = 13.95;
regularHours = 20;
additionalHours = hoursUsed - regularHours;
additionalHoursFee = additionalHours * 1.00;
totalFee = monthlyFee + additionalHoursFee;
System.out.println("The total charges is " + totalFee);
System.out.println("The total charges is $" +
totalFee + ".");
break;
case 'C':
totalFee = 19.95;
System.out.println("The total charges is $" +
totalFee + ".");
break;
default:
System.out.println("Plea… enter either A,B, " +
"or C).");
}
System.exit(0);
}
}
- 02-20-2011, 06:56 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
You have declared and initialised a Scanner, presumably because you want to get user input, but then you neglected to use it anywhere in your code :confused:
- 02-20-2011, 07:38 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Problem here:
input cannot be parsed to an int because it holds a String which does not contain all numerical characters e.g. "93759" it holds whatever package you choose i.e. 'A' or 'B' or something. (NumberFormatException)Java Code:hoursUsed = Integer.parseInt(input); input = keyboard.nextLine(); hoursUsed = input.charAt(0);
- 02-21-2011, 02:11 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
ok can you tell me how to fix this code please
- 02-21-2011, 02:44 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Why are you parsing your input before reading the users input of hours? This will make the program try to parse the last input, the letter A, B or C they typed in before.Java Code:System.out.println("Enter the number of hours " + "you used."); hoursUsed = Integer.parseInt(input); input = keyboard.nextLine(); hoursUsed = input.charAt(0);
Also you dont want to make hoursUsed a char.
try:
Java Code:System.out.println("Enter the number of hours " + "you used."); input = keyboard.nextLine(); hoursUsed = Integer.parseInt(input);
- 02-22-2011, 01:17 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
awesome Dr. Fox thank you so much that worked... now my only problem is when I input hours as 5 for say package a I get a negative value for the charges... I know this has to do with the if else statements however can you please advise me how to input the correct statement should it be something like :
if (hoursUsed < 10)
System.out.println(monthlyFee);
this is the error:
run:
Enter the letter of the package you purchased (either A, B, or C.)
a
Enter the number of hours you used.
5
The total charges is $-0.05000000000000071.
BUILD SUCCESSFUL (total time: 4 seconds)
- 02-22-2011, 01:26 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Have a re look at your calculations for package 'A'
// hoursUsed=5
// regularHours=10;
// additionalHours= 5-10 = -5
// additionalHoursFee = -5*2.00 = -10
totalFee = 9.95 - 10 = -0.05
System.out.println(totalFee);
All your problems begin here when hoursUsed is 5
Java Code:additionalHours = hoursUsed - regularHours;
- 02-22-2011, 02:41 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Java packages / statsic problem - Please help!
By brendan_1986 in forum New To JavaReplies: 14Last Post: 02-09-2011, 01:51 PM -
No Persistence provider problem with Maven
By spac in forum JDBCReplies: 2Last Post: 01-28-2011, 06:22 PM -
JNDI/DNS Service Provider and Syslog
By Jotta in forum NetworkingReplies: 0Last Post: 04-19-2010, 10:05 PM -
How to authenticate code with no internet connect.
By beardawg in forum JavaFXReplies: 0Last Post: 04-13-2010, 08:16 PM -
Help - problem with java when using internet
By badabing in forum New To JavaReplies: 1Last Post: 08-07-2007, 01:39 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks