Hello need help inserting something like this code to change
Hello need help inserting something like this code to change the price when the date is enter from a certain date \"
if (month == 12 | month == 1 | month == 2)
rate = .10;
else if (month >= 3 && month < 6)
rate = .12;
else
rate = .15;
\"
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class self2 {
public static void main(String[] args) {
// Construction
double rate = 0;
double overCharge = 1.10;
double excessCharge = 1.25;
double tier1 = 350;
double tier2 = 500;
double over = 0;
double excess = 0;
double total = 0;
// Welcome
System.out.println(\"Southwet Power & Light \ Billing Statement\");
Scanner sc = new Scanner(System.in);
String choice = \"y\";
while (choice.equalsIgnoreCase(\"y\")) {
// get the input from the user
System.out.println(\"Please enter your name (Last,First) > \");
String name = sc.nextLine();
System.out.println(\"Meter reading date >\");
String month = sc.nextLine();
Scanner sc1 = new Scanner(System.in); // Scanner #1 for KW used
// input
double kwusedEntered = 0;
double base = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(\"Electricity Used (KW) > \");
kwusedEntered = sc1.nextInt();
isValid = true;
// Get current date and time
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(\"MM/dd/yyyy\");
System.out.println(\"Date printed: \" + sdf.format(now));
System.out.println(\"Name:\" + name);
System.out.println(\"Meter reading date : \" + month);
System.out.println(\"Electricity used (KW) : \" + kwusedEntered);
// Validate Date Entered
String billingDate = null;
boolean valid = false;
while (!valid) {
try {
Scanner scandate = new Scanner(System.in); // Scanner #1
// for
// scanning
// date
System.out.print(\"Meter reading date > \");
billingDate = scandate.nextLine();
// Charges
if (kwusedEntered >= 0 && kwusedEntered <= 350) {
base = kwusedEntered * rate;
over = 0;
excess = 0;
} else if (kwusedEntered > 350 && kwusedEntered <= 500) {
base = tier1 * rate;
over = (kwusedEntered - tier1) * (rate * overCharge);
excess = 0;
} else if (kwusedEntered > 500) {
base = tier1 * rate;
over = (tier2 - tier1) * (rate * overCharge);
excess = (kwusedEntered - tier2) * (rate * excessCharge);
}
base = Math.round(base * 100) / 100;
over = Math.round(over * 100) / 100;
excess = Math.round(excess * 100) / 100;
total = base + over + excess;
System.out.println(\"Baseline charge \" + base);
System.out.println(\"Over-baseline charge \" + over);
System.out.println(\"Excess charge\" + excess);
System.out.println(\"total amount due \" + total);
System.out.println(\"Calculate another bill (y/n)\");
choice = sc.next();
System.out.println(\"\ Thank you for letting us serve you!\") ;
}
}
}
}
Solution
Hi,
I have inserted your code in approprate place. Highlighted the code changes below.
self2.java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class self2 {
public static void main(String[] args) {
// Construction
double rate = 0;
double overCharge = 1.10;
double excessCharge = 1.25;
double tier1 = 350;
double tier2 = 500;
double over = 0;
double excess = 0;
double total = 0;
// Welcome
System.out.println(\"Southwet Power & Light \ Billing Statement\");
Scanner sc = new Scanner(System.in);
String choice = \"y\";
while (choice.equalsIgnoreCase(\"y\")) {
// get the input from the user
System.out.println(\"Please enter your name (Last,First) > \");
String name = sc.nextLine();
System.out.println(\"Meter reading date(MM/dd/yyyy) >\");
String date = sc.nextLine();
Scanner sc1 = new Scanner(System.in); // Scanner #1 for KW used
// input
double kwusedEntered = 0;
double base = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(\"Electricity Used (KW) > \");
kwusedEntered = sc1.nextInt();
isValid = true;
// Get current date and time
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(\"MM/dd/yyyy\");
System.out.println(\"Date printed: \" + sdf.format(now));
System.out.println(\"Name:\" + name);
System.out.println(\"Meter reading date : \" + date);
System.out.println(\"Electricity used (KW) : \" + kwusedEntered);
String monthStr = date.substring(0, 2);
int month = Integer.parseInt(monthStr);
if (month == 12 || month == 1 || month == 2)
rate = 0.10;
else if (month >= 3 && month < 6)
rate = 0.12;
else
rate = 0.15;
// Validate Date Entered
String billingDate = null;
boolean valid = false;
while (!valid) {
try {
Scanner scandate = new Scanner(System.in); // Scanner #1
// for
// scanning
// date
System.out.print(\"Meter reading date(MM/dd/yyyy) > \");
billingDate = scandate.nextLine();
// Charges
if (kwusedEntered >= 0 && kwusedEntered <= 350) {
base = kwusedEntered * rate;
over = 0;
excess = 0;
} else if (kwusedEntered > 350 && kwusedEntered <= 500) {
base = tier1 * rate;
over = (kwusedEntered - tier1) * (rate * overCharge);
excess = 0;
} else if (kwusedEntered > 500) {
base = tier1 * rate;
over = (tier2 - tier1) * (rate * overCharge);
excess = (kwusedEntered - tier2) * (rate * excessCharge);
}
base = Math.round(base * 100) / 100;
over = Math.round(over * 100) / 100;
excess = Math.round(excess * 100) / 100;
total = base + over + excess;
System.out.println(\"Baseline charge \" + base);
System.out.println(\"Over-baseline charge \" + over);
System.out.println(\"Excess charge\" + excess);
System.out.println(\"total amount due \" + total);
System.out.println(\"Calculate another bill (y/n)\");
choice = sc.next();
System.out.println(\"\ Thank you for letting us serve you!\") ;
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
}
Output:
Southwet Power & Light
Billing Statement
Please enter your name (Last,First) >
Suresh Murapaka
Meter reading date(MM/dd/yyyy) >
12/01/2015
Electricity Used (KW) > 500
Date printed: 10/13/2016
Name:Suresh Murapaka
Meter reading date : 12/01/2015
Electricity used (KW) : 500.0
Meter reading date > 12/01/2015
Baseline charge 35.0
Over-baseline charge 16.0
Excess charge0.0
total amount due 51.0
Calculate another bill (y/n)
n
Thank you for letting us serve you!
Meter reading date >





