Created
October 20, 2013 07:26
-
-
Save anonymous/7066003 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package brent; | |
import java.text.NumberFormat; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.*; | |
public class Brent { | |
public static void main(String[] args) | |
{ | |
// Billing Statement Header | |
System.out.println("Southwest Power and Light"+"\tElectric Utility Charges"); | |
System.out.println("--------------------------------------------------------"); | |
Scanner sc = new Scanner(System.in); | |
Scanner scan = new Scanner(System.in); | |
Scanner scanD = new Scanner(System.in); | |
//perform invoice cal until choic isnt't equal to "y" or "Y" | |
String choice = "y"; | |
while (choice.equalsIgnoreCase("y")) | |
{ | |
//Declare Variables | |
String userName="", dateIn=""; | |
int month=0, date=0, year=0, KwUsed=0; | |
Date now = new Date(); | |
SimpleDateFormat todaysDate = new SimpleDateFormat("MM/dd/yyyy"); | |
System.out.println("Date: " + todaysDate.format(now)); | |
System.out.print("Please enter your name (Last, First): "); | |
userName = scan.nextLine(); | |
// Loop until dateIn is valid | |
boolean validDate = false; | |
while (!validDate) | |
{ | |
try | |
{ | |
System.out.print("Meter reading date (mm/dd/yyyy): "); | |
dateIn = scan.nextLine(); | |
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); | |
sdf.setLenient(false); | |
sdf.parse(dateIn); | |
validDate = true; | |
} | |
catch (ParseException invalidDate) | |
{ | |
System.out.println("Please enter a valid date."); | |
} | |
} | |
try (Scanner scanDate = new Scanner(dateIn)) { | |
scanDate.useDelimiter("/"); | |
month = scanDate.nextInt(); | |
date = scanDate.nextInt(); | |
year = scanDate.nextInt(); | |
} | |
// Loop until KW is valid | |
boolean valid = false; | |
{ | |
try | |
{ | |
System.out.print("Electricity used (KW): "); | |
KwUsed = scanD.nextInt(); | |
valid = true; | |
} | |
catch (InputMismatchException e) | |
{ | |
scanD.next(); | |
System.out.println("Please enter KW value."); | |
continue; // check here | |
} | |
} | |
//Calculate base rate via Meter Read Date | |
double baseRate = 0; | |
switch (month) | |
{ | |
case 1: //January | |
baseRate=0.10;break; | |
case 2: //February | |
baseRate=0.10;break; | |
case 3: //March | |
baseRate=0.12;break; | |
case 4: //April | |
baseRate=0.12;break; | |
case 5: //May | |
baseRate=0.12;break; | |
case 6: //June | |
baseRate=0.15;break; | |
case 7: //July | |
baseRate=0.15;break; | |
case 8: //August | |
baseRate=0.15;break; | |
case 9: //September | |
baseRate=0.15;break; | |
case 10: //October | |
baseRate=0.15;break; | |
case 11: //November | |
baseRate=0.15;break; | |
case 12: //December | |
baseRate=0.10;break; | |
} | |
//Currency Format | |
NumberFormat currency = NumberFormat.getCurrencyInstance(); | |
double totalAmountDue = 0; | |
double baseLineCharge = 0; | |
double baseCharge = (baseRate*KwUsed); | |
double overBaselineCharge = 0; | |
double excessCharge = 0; | |
if(KwUsed<=350) | |
{ | |
baseLineCharge = baseCharge; | |
overBaselineCharge = 0; | |
excessCharge = 0; | |
} | |
if(KwUsed>350 & KwUsed<501) | |
{ | |
baseLineCharge = baseRate*350; | |
overBaselineCharge = (KwUsed-350)*(baseRate*.10+baseRate); | |
excessCharge = 0; | |
} | |
if(KwUsed>500) | |
{ | |
baseLineCharge = baseRate*350; | |
overBaselineCharge = (150)*(baseRate*.10+baseRate); | |
excessCharge = (KwUsed-500)*(baseRate*.25+baseRate); | |
} | |
totalAmountDue = baseLineCharge + overBaselineCharge + excessCharge; | |
//Print Output | |
System.out.println("\nName: "+ userName); | |
System.out.println("Meter Reading Date: "+ month + "/" + date + "/" + year); | |
System.out.println("Electricity Used (KW): "+ KwUsed); | |
System.out.println("Baseline charge: "+ currency.format(baseLineCharge)); | |
System.out.println("Over-Baseline charge: "+ currency.format(overBaselineCharge)); | |
System.out.println("Excess Charge: "+ currency.format(excessCharge)); | |
System.out.println("Total amount due: "+ currency.format(totalAmountDue)); | |
System.out.println("Thank you for using Southwest Power & Light"); | |
System.out.print("Continue? (y/n): "); | |
choice = sc.next(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment