/// <summary> /// Parameterized Constructor. /// </summary> /// <param name="rideType">Type of the ride.</param> public InvoiceGenerator(RideType rideType) { /// Initialising the Ride Type as Supported from the ride type class enum this.rideType = rideType; /// Catching the Custom Exception for the invalid ride type- Unsupported from the cab service requirements try { /// Initialising hte default value for the NORMAL Ride Type if (rideType.Equals(RideType.NORMAL)) { this.MINIMUM_COST_PER_KM = 10; this.COST_PER_KM = 1; this.MINIMUM_FARE = 5; } /// Initialising the default value for the PREMIUM Ride Type else if (rideType.Equals(RideType.PREMIUM)) { this.MINIMUM_COST_PER_KM = 15; this.COST_PER_KM = 2; this.MINIMUM_FARE = 20; } } catch (CabInvoiceException) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_RIDE_TYPE, "The Passed Ride Type is Not Valid"); } }
//Creating parameterized constructor of the Invoice Generator Class. public InvoiceGenerator(RideType rideType) { this.rideType = rideType; //Exception handling for the invalid ride type. try { //Checking the ride type is equal Normal or not. if (rideType.Equals(RideType.NORMAL)) { this.costPerKm = 1; this.minimumCostPerKm = 10; this.minimumFare = 5; } /// Initialising the default value for the PREMIUM Ride Type else if (rideType.Equals(RideType.PREMIUM)) { this.minimumCostPerKm = 15; this.costPerKm = 2; this.minimumFare = 20; } } catch (CabInvoiceException) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_RIDE_TYPE, "Invalid Ride Type"); } }
// Calculates the fare. // Create Method // Invalid Time public double CalculateFare(double distance, int time) { double totalFare = 0; try { totalFare = distance * MINIMUM_COST_PER_KM + time * COST_PER_TIME; } catch (CabInvoiceException) { if (rideType.Equals(null)) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_RIDE_TYPE, "Invalid ride type"); } if (distance <= 0) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_DISTANCE, "Invalid Distance"); } if (distance >= 0) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_TIME, "Invalid Time"); } } return(Math.Max(totalFare, MINIMUM_FARE)); }
public Category GetRideValue(RideType rideType) { if (rideType.Equals(RideType.NORMAL)) { return(new Category(10, 1, 5)); } if (rideType.Equals(RideType.PREMIUM)) { return(new Category(15, 2, 20)); } else { return(null); } }
//private method for setting the charges for a each type of ride private static void SetCharges(RideType rideType) { if (rideType.Equals(RideType.NORMAL)) { CHARGE_PER_KM = 10; CHARGE_PER_MIN = 1; MINIMUM_FARE = 5; } else if (rideType.Equals(RideType.PREMIUM)) { CHARGE_PER_KM = 15; CHARGE_PER_MIN = 2; MINIMUM_FARE = 20; } else { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_RIDE_TYPE, "Invalid Ride Type"); } }
/// <summary> /// Calculate the total fare for the given distance and time /// </summary> /// <param name="distance"></param> /// <param name="time"></param> /// <returns></returns> public double CalculateFare(double distance, int time, RideType rideType) { if (rideType.Equals(RideType.NORMAL)) { return(SetType(CostPerKmForNormal, CostPerMinuteForNormal, MinimumFareForNormal, distance, time)); } else { return(SetType(CostPerKmForPremium, CostPerMinuteForPremium, MinimumFareForPremium, distance, time)); } }
/// <summary> /// Parameterised constructor /// </summary> /// <param name="rideType"></param> public InvoiceGenerator(RideType rideType) { this.rideType = rideType; if (rideType.Equals(RideType.NORMAL)) { this.COST_PER_KM = 10; this.COST_PER_MIN = 1; this.MIN_FARE = 5; } else if (rideType.Equals(RideType.PREMIUM)) { this.COST_PER_KM = 15; this.COST_PER_MIN = 2; this.MIN_FARE = 20; } else { throw new CabInvoiceException(CabInvoiceException.Type.INVALID_RIDE_TYPE, "Ride type is Invalid"); } }
//Initializes a new instance of the class. //Creating Method public InvoiceGenerator(RideType rideType) { this.rideType = rideType; this.rideRepository = new RideRepository(); try { if (rideType.Equals(RideType.PREMIUM)) { this.MINIMUM_COST_PER_KM = 15; this.COST_PER_TIME = 2; this.MINIMUM_FARE = 20; } else if (rideType.Equals(RideType.NORMAL)) { this.MINIMUM_COST_PER_KM = 10; this.COST_PER_TIME = 1; this.MINIMUM_FARE = 5; } } catch (CabInvoiceException) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_RIDE_TYPE, "Invalid Ride Type"); } }
/// <summary> /// UC-1. /// Method to Compute the total fare of the cab journey when passed with distance and time. /// </summary> /// <param name="distance">The distance.</param> /// <param name="time">The time.</param> /// <returns></returns> /// <exception cref="CabInvoiceException"> /// Invalid ride type /// or /// Invalid distance /// or /// Invalid time /// </exception> public double CalculateFare(double distance, int time) { double totalFare = 0; totalFare = distance * MINIMUM_COST_PER_KM + time * COST_PER_KM; /// Checks if the ride is null. If ride is null will throw exception if (rideType.Equals(null)) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_RIDE_TYPE, "Invalid ride type"); } /// Checks if distance is less than zero if distance is zero will throw exception. if (distance <= 0) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_DISTANCE, "Invalid distance"); } /// Checks if time is less than zero if time is zero will throw exception. if (time <= 0) { throw new CabInvoiceException(CabInvoiceException.ExceptionType.INVALID_TIME, "Invalid time"); } /// Returns the max value. return(Math.Max(totalFare, MINIMUM_FARE)); }