Question: JAVA. I have this code that asks the user to enter the day of the week, their vehicle's time of arrival, and their departure time

JAVA. I have this code that asks the user to enter the day of the week, their vehicle's time of arrival, and their departure time to calculate the amount charged for their parking. I need some help modifying so that it doesn't crash when an input that is not what I look for is set. The first question only accepts the days of the week strings or their first three letters as it's in the code, the second and third questions only accept numbers, and if a special character or something else is used when it is not supposed to be that, to be labeled as "Invalid input" or something similar, please. If you could leave an explanation as well

Code:

import java.util.Scanner; public class ParkingCharge { //declaration of member varaibles of the class private String day; private int arrTime; private int deptTime; private int durTime; private float parkingCharge; private float rate; private boolean valid; //parametrized constructor ParkingCharge(String day,int arrTime,int deptTime){ this.day=day; this.arrTime=arrTime; this.deptTime=deptTime; //checking the day of week switch(this.day){ case "MON": this.rate=(float)1.25; this.valid = true; break; case "TUE": this.rate=(float)1.25; this.valid = true; break; case "WED": this.rate=(float)1.25; this.valid = true; break; case "THU": this.rate=(float)1.25; this.valid = true; break; case "FRI": this.rate=(float)1.25; this.valid = true; break; case "SAT": this.rate=(float)0.50; this.valid = true; break; case "SUN": this.rate=(float)0.50; this.valid = true; break; default : this.valid = false; break; } } //this is a method to calculate total parking charge void calculateCharge(){ int arrMin,deptMin,durMin,numOfIntervals,reminder; arrMin=(this.arrTime%100)+(this.arrTime/100)*60; deptMin=(this.deptTime%100)+(this.deptTime/100)*60; this.durTime = deptMin-arrMin; if(this.durTime<0){ System.out.println("invalid Arrival/Departure Time"); } else{ numOfIntervals = this.durTime/15; reminder = this.durTime%15; if (reminder!=0) numOfIntervals+=1; this.parkingCharge = (float)(numOfIntervals * this.rate); if (this.parkingCharge > 15) this.parkingCharge=(float)15.0; } } public static void main(String args[]){ String day; int arrTime,deptTime; Scanner sc =new Scanner(System.in); //taking inputs from the user System.out.print("Please enter the day of the week(first three letters of the day like \"mon\" \"for monday\"):"); day=sc.nextLine(); System.out.print("Please Enter the vehicle's arrival time:"); arrTime = sc.nextInt(); System.out.print("please enter its departure time:"); deptTime = sc.nextInt(); day = day.toUpperCase(); //creating the object of the class ParkingCharge p1=new ParkingCharge(day,arrTime,deptTime); if(p1.valid){ //calling the method using object p1.calculateCharge(); //displaying the output System.out.println("Day of week:"+p1.day); System.out.println("Parking duration in minutes:"+p1.durTime+", rate:$"+p1.rate); System.out.println("Amount charged:$"+p1.parkingCharge); } else System.out.println("Invalid day"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!