| Tier | Usage (% of baseline) |
|
| 1st tier | up to 100% |
|
| 2nd tier | 100% to 130% |
|
| 3rd tier | 130% to 200% |
|
| 4th tier | 200% to 300% |
|
| 5th tier | more than 300% |
|
There are two inputs to the program which are found on the
customer's
utility bill: the Baseline Quantity and Actual Usage.
1 /**
2 * UtilBill models a utility bill.
3 */
4 public class UtilBill
5 {
6 // formatter to currency format
7 java.text.DecimalFormat df = new java.text.DecimalFormat("00.##");
8
9 // format a double value as currency with two decimal places.
10 private String toFixed(double value)
11 {
12 return df.format(Math.floor(100*value)/100);
13 }
14
15 /** Calculate the utility bill */
16 public String doCalcs(double baseline, double actualUsage)
17 {
18 // Reset all working variables to zero
19 double C = 0;
20 double G = 0;
21 double K = 0;
22 double O = 0;
23 double P = 0;
24
25 // Compute usage at each of the different tiers
26 // Baseline usage or below
27 if ( baseline > actualUsage )
28 {
29 C = actualUsage;
30 }
31 else
32 {
33 C = baseline;
34 // Tier 2 usage
35 double D = 1.3 * baseline;
36 double E = actualUsage - baseline;
37 double F = D - baseline;
38 if (E > F)
39 G = F;
40 else
41 G = E;
42 // Tier 3 usage
43 if (actualUsage > D)
44 {
45 double H = 2 * baseline;
46 double I = actualUsage - D;
47 double J = H - D;
48 if (I > J)
49 K = J;
50 else
51 K = I;
52 // Tier 4 usage
53 if (actualUsage > H)
54 {
55 double L = 3 * baseline;
56 double M = actualUsage - H;
57 double N = L - H;
58 if (M > N)
59 O = N;
60 else
61 O = M;
62 // Tier 5 usage
63 if (actualUsage > L)
64 {
65 P = actualUsage - L;
66 }
67 }
68 }
69 }
70
71 // compute total of costs at all tiers
72 return toFixed(C * .1301 + G * .1516 + K * .1966 + O
73 * .2366 + P * .2594);
74
75 } // end doCalcs
76
77 // Get the command line args and do the calculations
78 public static void main(String args[])
79 {
80
81 double Actual=0.0;
82 double Baseline=0.0;
83 try
84 {
85 // Obtain input values from the HTML form
86 Baseline = new Double (args[0]);
87 Actual = new Double(args[1]);
88 }
89 catch (Exception ex)
90 {
91 System.out.println("arguments not formatted properly");
92 System.exit(1);
93 }
94 // validate user input
95 if (Actual < 0 || Baseline < 0)
96 {
97 System.out.println("arguments not formatted properly");
98 }
99 else
100 {
101 UtilBill app = new UtilBill();
102 String result = app.doCalcs(Baseline, Actual);
103 System.out.println("\t\tTotal: $" + result);
104 }
105
106 }
107
108 }