Chelsea
Java Compolation
1 Answer(s)      5 years and 7 months ago
Posted in : Java Beginners
How do I correctly write a program that calculates a customer's monthly bill?

This is what I have:

import java.util.Scanner;
import java.text.DecimalFormat;
public class Bill
{
public static void main(String[] args)
{
final double BASE_RATE_A = 9.95;
final double BASE_RATE_B = 13.95;
final double BASE_RATE_C = 19.95;
final double BASE_HOUR_RATE_A = 10;
final double BASE_HOUR_RATE_B = 20;
final double ADD_HOUR_RATE_A = 2.00;
final double ADD_HOUR_RATE_B = 2.00;
String servicePackage;
int hours;
double charges;

Scanner kb = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("$##.00");

System.out.print("Service package (A, B, or C): ");
servicePackage = kb.nextLine();
System.out.print("Number of hours: ");
hours = kb.nextInt();

kb.nextLine();

if(servicePackage == "A")
{
if(hours>0&&hours<=10)
{
charges = BASE_RATE_A;
System.out.println("Total charges: "+fmt.format(charges));
System.exit(0);
}
else if(hours>10&&hours<=30*24)
{
charges = BASE_RATE_A+(hours*ADD_HOUR_RATE_A);
System.out.println("Total charges: "+fmt.format(charges));
System.exit(0);
}
else
{
System.out.print("invalid input, please enter number of "
+ "hours between 0 and 720 ");
hours = kb.nextInt();
}
}
else
{
System.out.print("invalid input, please enter (A, B or C): ");
}
}
}
Advertisement
View Answers

February 15, 2010 at 11:10 AM


Hi Friend,

Try the following:

import java.util.Scanner;
import java.text.DecimalFormat;
public class Bill
{
public static void main(String[] args)
{
final double BASE_RATE_A = 9.95;
final double BASE_RATE_B = 13.95;
final double BASE_RATE_C = 19.95;
final double BASE_HOUR_RATE_A = 10;
final double BASE_HOUR_RATE_B = 20;
final double ADD_HOUR_RATE_A = 2.00;
final double ADD_HOUR_RATE_B = 2.00;
String servicePackage;
int hours;
double charges;

Scanner kb = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("$##.00");

System.out.print("Service package (A, B, or C): ");
servicePackage = kb.nextLine();
System.out.print("Number of hours: ");
hours = kb.nextInt();
kb.nextLine();
if(servicePackage.equals("A"))
{
if(hours>0&&hours<=10)
{
charges = BASE_RATE_A;
System.out.println("Total charges: "+fmt.format(charges));
System.exit(0);
}
else if(hours>10&&hours<=30*24)
{
charges = BASE_RATE_A+(hours*ADD_HOUR_RATE_A);
System.out.println("Total charges: "+fmt.format(charges));
System.exit(0);
}
else
{
System.out.print("invalid input, please enter number of "+ "hours between 0 and 720 ");
hours = kb.nextInt();
}
}
else
{
System.out.print("invalid input, please enter (A, B or C): ");
}
}
}

Thanks









Related Tutorials/Questions & Answers:
jav compolation error - Java Beginners
jav compolation error  find symbol error
Java Compolation - Java Beginners
Java Compolation  How do I correctly write a program that calculates a customer's monthly bill? This is what I have: import java.util.Scanner; import java.text.DecimalFormat; public class Bill { public static void
Advertisements
java
java  diff bt core java and java
java
java  what is java
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java