Answer to Question #233873 in Programming & Computer Science for Selma Shigwedha

Question #233873
Think of task 3 above, in real life the total cost is not provide but rather calculated by
the system/program in place. Given the following facts create a program to calculate the cost give
the arrival and departure times:
Duration Cost
0 to 30 minutes N$ 0
0.5h to 8 hours N$ 2.50 per 30 minutes
Above 8 hours N$ 80.00
Sample run 1:
Java lab01_task05 N88W 8h30 9h45
Output:
Vehicle Details
N88W
++++++++++++++++++++++
Arrival 8h30
Departure 9h45
++++++++++++++++++++++
Total Cost: N$ 6.25
++++++++++++++++++++++
1
Expert's answer
2021-09-07T14:16:42-0400
public class lab01_task05 {


	public static void main(String[] args) {


		String[] arrivalDate = args[1].split("h");
		String[] departureDate = args[2].split("h");


		int hoursArrival = Integer.parseInt(arrivalDate[0]);
		int minutesArrival = Integer.parseInt(arrivalDate[1]);


		int hoursDeparture = Integer.parseInt(departureDate[0]);
		int minutesDeparture = Integer.parseInt(departureDate[1]);


		if (minutesDeparture < minutesArrival) {
			hoursDeparture--;
			minutesDeparture += 60;
		}


		int diffMinutes = minutesDeparture - minutesArrival;
		int diffHours = hoursDeparture - hoursArrival;
		
		int totalMinutes = diffMinutes + diffHours * 60;


		// Duration Cost
		float totalCost = 0;
		// 0 to 30 minutes N$ 0
		if (totalMinutes > 0 && totalMinutes < 30) {
			totalCost=0;
		}
		// 0.5h to 8 hours N$ 2.50 per 30 minutes
		if (totalMinutes >= 30 && totalMinutes < 480) {
			totalCost=totalMinutes*2.5f/30.0f;
		}
		// Above 8 hours N$ 80.00
		if (totalMinutes>= 480) {
			totalCost=80.00f;
		}
		
		System.out.println("Vehicle Details");
		System.out.println(args[0]);
		System.out.println("++++++++++++++++++++++");
		System.out.println("Arrival " + args[1]);
		System.out.println("Departure " + args[2]);
		System.out.println("++++++++++++++++++++++");
		System.out.println("Total Cost: N$ " + String.format("%.2f", totalCost));
		System.out.println("++++++++++++++++++++++");


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS