Need If-Else if-Else help on my code
Task Part 1:
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.
Task Part 2: (experiencing problems in this part)
Modify the program you wrote for Programming Challenge 13 (Task 1) so it also calculates and displays the amount of money (Package A customers would save if they purchased Packages B or C, and the amount of money Package B customers would save if they purchased Package C. If there would be no savings, no message should be printed.
The problem that I am experiencing is that I will get a negative number if the price of the monthly bill is less than $19.95.
e.g. I enter 'A' and '14'.
Answers: Monthly bill: $17.95
Savings if you purchase Package B: $4.0
Savings if you purchase Package C: $-2.0
How can I fix this code and not receive the negative number?
This is my code:
Code java:
package internetserviceproviderpart2;
import javax.swing.JOptionPane;
/**
*
* @author Home
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String inputString;
char packageLetter;
int hoursUsed;
int additionalHours = 0;
double totalFee;
double savingAB;
double savingAC;
double savingBC;
inputString = JOptionPane.showInputDialog("Enter the letter of the " +
"package you purchased (either A, B, or C.)");
inputString = inputString.toUpperCase();
packageLetter = inputString.charAt(0);
inputString = JOptionPane.showInputDialog("Enter the number of hours " +
"you used.");
hoursUsed = Integer.parseInt(inputString);
if (packageLetter=='A' && hoursUsed>10)
{
additionalHours = hoursUsed - 10;
totalFee = 9.95 + (additionalHours * 2.00);
savingAB = totalFee - 13.95;
savingAC = totalFee - 19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
" if you purchase Package B.");
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
}
else if (packageLetter=='A' && hoursUsed<=10)
JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
else if (packageLetter=='B' && hoursUsed>20)
{
additionalHours = hoursUsed - 20;
totalFee = 13.95 + (additionalHours * 1.00);
savingBC = totalFee -19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
" if you purchase Package C.");
}
else if (packageLetter=='B' && hoursUsed<=20)
JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
else if (packageLetter=='C')
JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
else
JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
System.exit(0);
}
}
Re: Need If-Else if-Else help on my code
Quote:
Originally Posted by
Plural
Task Part 1:
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.
Task Part 2: (experiencing problems in this part)
Modify the program you wrote for Programming Challenge 13 (Task 1) so it also calculates and displays the amount of money (Package A customers would save if they purchased Packages B or C, and the amount of money Package B customers would save if they purchased Package C. If there would be no savings, no message should be printed.
The problem that I am experiencing is that I will get a negative number if the price of the monthly bill is less than $19.95.
e.g. I enter 'A' and '14'.
Answers: Monthly bill: $17.95
Savings if you purchase Package B: $4.0
Savings if you purchase Package C: $-2.0
How can I fix this code and not receive the negative number?
This is my code:
Code java:
package internetserviceproviderpart2;
import javax.swing.JOptionPane;
/**
*
* @author Home
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String inputString;
char packageLetter;
int hoursUsed;
int additionalHours = 0;
double totalFee;
double savingAB;
double savingAC;
double savingBC;
inputString = JOptionPane.showInputDialog("Enter the letter of the " +
"package you purchased (either A, B, or C.)");
inputString = inputString.toUpperCase();
packageLetter = inputString.charAt(0);
inputString = JOptionPane.showInputDialog("Enter the number of hours " +
"you used.");
hoursUsed = Integer.parseInt(inputString);
if (packageLetter=='A' && hoursUsed>10)
{
additionalHours = hoursUsed - 10;
totalFee = 9.95 + (additionalHours * 2.00);
savingAB = totalFee - 13.95;
savingAC = totalFee - 19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
" if you purchase Package B.");
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
}
else if (packageLetter=='A' && hoursUsed<=10)
JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
else if (packageLetter=='B' && hoursUsed>20)
{
additionalHours = hoursUsed - 20;
totalFee = 13.95 + (additionalHours * 1.00);
savingBC = totalFee -19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
" if you purchase Package C.");
}
else if (packageLetter=='B' && hoursUsed<=20)
JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
else if (packageLetter=='C')
JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
else
JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
System.exit(0);
}
}
double can hold negative values. You could have an inner if statement to tell it to set it to 0.00 or something if the totalFee is less than 19.95.
In fact, it'll do that every time your total fee is less than the amount you're subtracting.
If you have some kind of if statement that will set it so that it will set it to 0 otherwise you will be getting values as low as -$4.00 for 0 additional hours.
\m/
Re: Need If-Else if-Else help on my code
Quote:
Originally Posted by
javapenguin
double can hold negative values. You could have an inner if statement to tell it to set it to 0.00 or something if the totalFee is less than 19.95.
In fact, it'll do that every time your total fee is less than the amount you're subtracting.
If you have some kind of if statement that will set it so that it will set it to 0 otherwise you will be getting values as low as -$4.00 for 0 additional hours.
\m/
I've changed up my code:
Code java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package internetserviceproviderpart2;
import javax.swing.JOptionPane;
/**
*
* @author Home
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String inputString;
char packageLetter;
int hoursUsed;
int additionalHours = 0;
double totalFee = 0;
double savingAB = totalFee - 13.95;
double savingAC = totalFee - 19.95;
double savingBC = totalFee - 19.95;
inputString = JOptionPane.showInputDialog("Enter the letter of the " +
"package you purchased (either A, B, or C.)");
inputString = inputString.toUpperCase();
packageLetter = inputString.charAt(0);
inputString = JOptionPane.showInputDialog("Enter the number of hours " +
"you used.");
hoursUsed = Integer.parseInt(inputString);
if (packageLetter=='A' && hoursUsed>10)
{
additionalHours = hoursUsed - 10;
totalFee = 9.95 + (additionalHours * 2.00);
savingAB = totalFee - 13.95;
savingAC = totalFee - 19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
" if you purchase Package B.");
if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
else
JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
}
else if (packageLetter=='B' && hoursUsed>20)
{
additionalHours = hoursUsed - 20;
totalFee = 13.95 + (additionalHours * 1.00);
savingBC = totalFee - 19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
if(packageLetter=='B' && hoursUsed>20 && totalFee > 19.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
" if you purchase Package C.");
else
JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
}
else if (packageLetter=='C')
JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
else
JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
System.exit(0);
}
}
Now, it is more effective.
But, you said something about a statement that can set the value into 0.0 and what statement would that be?
Re: Need If-Else if-Else help on my code
Quote:
Originally Posted by
Plural
I've changed up my code:
Code java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package internetserviceproviderpart2;
import javax.swing.JOptionPane;
/**
*
* @author Home
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String inputString;
char packageLetter;
int hoursUsed;
int additionalHours = 0;
double totalFee = 0;
double savingAB = totalFee - 13.95;
double savingAC = totalFee - 19.95;
double savingBC = totalFee - 19.95;
inputString = JOptionPane.showInputDialog("Enter the letter of the " +
"package you purchased (either A, B, or C.)");
inputString = inputString.toUpperCase();
packageLetter = inputString.charAt(0);
inputString = JOptionPane.showInputDialog("Enter the number of hours " +
"you used.");
hoursUsed = Integer.parseInt(inputString);
if (packageLetter=='A' && hoursUsed>10)
{
additionalHours = hoursUsed - 10;
totalFee = 9.95 + (additionalHours * 2.00);
savingAB = totalFee - 13.95;
savingAC = totalFee - 19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
" if you purchase Package B.");
if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
else
JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
}
else if (packageLetter=='B' && hoursUsed>20)
{
additionalHours = hoursUsed - 20;
totalFee = 13.95 + (additionalHours * 1.00);
savingBC = totalFee - 19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
if(packageLetter=='B' && hoursUsed>20 && totalFee > 19.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
" if you purchase Package C.");
else
JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
}
else if (packageLetter=='C')
JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
else
JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
System.exit(0);
}
}
Now, it is more effective.
But, you said something about a statement that can set the value into 0.0 and what statement would that be?
Code java:
if (packageLetter=='A' && hoursUsed>10)
{
additionalHours = hoursUsed - 10;
totalFee = 9.95 + (additionalHours * 2.00);
savingAB = totalFee - 13.95;
if (savingAB < 0)
savingAB = 0.00;
else
savingAB = savingAB;
Re: Need If-Else if-Else help on my code
Quote:
Originally Posted by
javapenguin
Code java:
if (packageLetter=='A' && hoursUsed>10)
{
additionalHours = hoursUsed - 10;
totalFee = 9.95 + (additionalHours * 2.00);
savingAB = totalFee - 13.95;
if (savingAB < 0)
savingAB = 0.00;
else
savingAB = savingAB;
This statement got me pretty confused.
Can you include the Message Dialog for me so it can clear up my confusion?
Re: Need If-Else if-Else help on my code
Quote:
Originally Posted by
Plural
This statement got me pretty confused.
Can you include the Message Dialog for me so it can clear up my confusion?
That part for the message dialog need not be altered. The code I gave you will reset the value so that when it is passed to showMessageDialog() it doesn't return a negative number.
Also
Code java:
if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
" if you purchase Package B.");
if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
else
JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
}
You said you had problems with if, else if, else, right?
Well, that last else will go into affect when :
if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
isn't true.
Did you mean:
Code java:
if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
" if you purchase Package B.");
else if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
else
JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
}
Well, it should perhaps go like this: though I'm not sure where the else at the end goes:
Code java:
if(packageLetter=='A' && hoursUsed>10 && totalFee > 13.95)
{
if(packageLetter=='A' && hoursUsed>10 && totalFee > 19.95)
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
else
JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
" if you purchase Package B.");
}
else
JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
}