I\'m working on a Java invoice where a company charges more per service call dep
ID: 3787696 • Letter: I
Question
I'm working on a Java invoice where a company charges more per service call depending upon the day of the week and time of day. The basic service rate is $52 per hour for weekdays, $76 on Saturdays, and $124 on Sundays. Basic or day rates are from 8:00 a.m. until 5 p.m. Evening rates are billed at 50% above the basic rate.
A client is supposed to enter the date of service and hours and then I'm supposed to determine what day of the week their input falls on, which I already did. However, what do I do so the client is billed for the day of the week that was output? In other words, if the output is Monday from 1:00 PM - 3:00 PM, how do I make sure they're charged for weekday day rates and not weekend rates or weekday evening rates?
I also need to be able to calculate day AND evening hours at the same time. For example, if service is requested from 4:00 PM to 7:00 PM (1 day hour and 2 evening hours), how would I write that in java?
Explanation / Answer
Hi,
Please see below the Java Class. Please comment for any comments/feedbacks.
Thanks,
Anita
CallService.java
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class CallService {
private static double rate;
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
//Reading the date from user
System.out.println("Enter the date of service in MM/dd/yyyy Format");
String date = scan.nextLine();
String day =getDay(date); //Gettingthe day for the date
int hours=0;
//Setting the rate based on the day
if("Sun".equalsIgnoreCase(day)){
rate = 124;
}
else if("Sat".equalsIgnoreCase(day)){
rate = 76;
}
else{
rate = 52;
}
//reading the start time and anatomy of the service
System.out.println("Service Requested from :");
String startTimeStr = scan.nextLine();
int startTime=Integer.valueOf(startTimeStr);
System.out.println("AM or PM:");
String AmOrPM = scan.nextLine();
if("PM".equalsIgnoreCase(AmOrPM)){
startTime = startTime +12;
}
System.out.println("Service Requested to :");
String endTimeStr = scan.nextLine();
int endTime = Integer.valueOf(endTimeStr);
System.out.println("AM or PM:");
String AmOrPMTo = scan.nextLine();
if("PM".equalsIgnoreCase(AmOrPMTo)){
endTime = endTime +12;
}
//Calculating the rate
if(startTime>=8 && endTime<=17){
hours = endTime-startTime;
rate= rate * hours; // basic rate
}
if (endTime>17){
//basic rate
hours = 17-startTime;
rate= rate * hours;
int evngHrs= endTime- 18;
//evng hors rate
double evngRate = (rate + (rate *.5)) * evngHrs;
rate = rate+ evngRate;
}
System.out.println("Total Bill is : "+rate);
}
public static String getDay(String date){
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date dt;
String day="";
try {
dt = df.parse(date);
SimpleDateFormat simpleDateformat = new SimpleDateFormat("E"); // the day of the week abbreviated
day = simpleDateformat.format(dt);
System.out.println(day);
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}
}
Sample output:
Enter the date of service in MM/dd/yyyy Format
01/11/2017
Wed
Service Requested from :
8
AM or PM:
AM
Service Requested to :
8
AM or PM:
PM
Total Bill is : 1872.0