'Compiler Options
Option Explicit On
Option Strict On
Public Class Business
'============================================================
'Module : Business
'============================================================
'Abstract : This is the business tier where the actual rental
'fee/charge is calculated.
'============================================================
Public Enum MyCarType
Economy
Midsize
Luxury
End Enum
'============================================================
'Module level variables
'============================================================
Private mdecDailyRates() As Decimal = {26.95D, 32.95D, 50.95D}
Private mdecMileageCharges() As Decimal = {0.12D, 0.15D, 0.2D}
Private mstrTypeOfCar As String
Private mstrAccountType As String
Private mdecNumberOfDays As Decimal
Private mintStartOdo As Integer
Private mintEndOdo As Integer
Private mdecTotalRent As Decimal
Private mdecMileageCharge As Decimal
Private mdecDiscountRate As Decimal
Private mdecDailyCharge As Decimal
Private mdecDailyRate As Decimal
Public Sub New()
End Sub
'============================================================
'Property : NumberOfDays
'============================================================
'Number of days used to calculate the Rental.
'============================================================
Public Property NumberOfDays() As Decimal
Get
Return mdecNumberOfDays
End Get
Set(ByVal Value As Decimal)
If Value >= 1 And Value <= 180 Then
mdecNumberOfDays = Value
Else
Throw New BusinessException("Days Rented is outside of valid range", "Days Rented")
End If
End Set
End Property
'============================================================
'Property : TypeOfCar
'============================================================
'Car type used to calculate the Rental.
'============================================================
Public Property TypeOfCar() As String
Get
Return mstrTypeOfCar
End Get
Set(ByVal Value As String)
If Value = "Economy" Or Value = "Midsize" Or Value = "Luxury" Then
mstrTypeOfCar = Value
Else
Throw New BusinessException("Type of car is not of valid type", "Car Type")
End If
End Set
End Property
'============================================================
'Property : StartOdometer
'============================================================
'Start Odometer reading used to calculate the Rental.
'============================================================
Public Property StartOdometer() As Integer
Get
Return mintStartOdo
End Get
Set(ByVal Value As Integer)
If Len(Value) >= 0 And Len(Value) <= 6 Then
mintStartOdo = Value
Else
Throw New BusinessException("Start Odometer isn't 6 digits", "Start Odometer")
End If
End Set
End Property
'============================================================
'Property : EndOdometer
'============================================================
'End Odometer reading used to calculate the Rental.
'============================================================
Public Property EndOdometer() As Integer
Get
Return mintEndOdo
End Get
Set(ByVal Value As Integer)
If Len(Value) >= 0 And Len(Value) <= 6 Then
mintEndOdo = Value
Else
Throw New BusinessException("Start Odometer isn't 6 digits", "End Odometer")
End If
End Set
End Property
'============================================================
'Property : AccountType
'============================================================
'Type of Account used to calculate the Rental.
'============================================================
Public Property AccountType() As String
Get
Return mstrAccountType
End Get
Set(ByVal Value As String)
If Value = "Regular" Or Value = "Insurance" Or Value = "Corporate" Then
mstrAccountType = Value
Else
Throw New BusinessException("Account isn't of valid Type", "Account Type")
End If
End Set
End Property
'============================================================
'Property : DailyCharge
'============================================================
'Daily Charge returned after rental is calculated.
'============================================================
Public ReadOnly Property DailyCharge() As Decimal
Get
Return mdecDailyCharge
End Get
End Property
'============================================================
'Property : MileageCharge
'============================================================
'Mileage Charge returned after rental is calculated.
'============================================================
Public ReadOnly Property MileageCharge() As Decimal
Get
Return mdecMileageCharge
End Get
End Property
'============================================================
'Property : DiscountRate
'============================================================
'Discount rate returned after rental is calculated.
'============================================================
Public ReadOnly Property DiscountRate() As Decimal
Get
Return mdecDiscountRate
End Get
End Property
'============================================================
'Property : TotalRent
'============================================================
'Total rent returned after rental is calculated.
'============================================================
Public ReadOnly Property TotalRent() As Decimal
Get
Return mdecTotalRent
End Get
End Property
'============================================================
'Property : DailyRate
'============================================================
'Daily Rate returned after rental is calculated.
'============================================================
Public ReadOnly Property DailyRate() As Decimal
Get
Return mdecDailyRate
End Get
End Property
'============================================================
'Function : CalculateDailyRate
'============================================================
'Calculates the total daily rate based on the dailyratefee and
'the accoutn type.
'============================================================
Private Function CalculateDailyRate(ByVal DailyRate As Decimal, ByVal AccountType As String) As Decimal
Dim decDailyRateCharge As Decimal
Select Case AccountType
Case "Corporate"
decDailyRateCharge = (mdecNumberOfDays * DailyRate)
Case "Insurance"
decDailyRateCharge = (mdecNumberOfDays * DailyRate)
Case "Regular"
decDailyRateCharge = mdecNumberOfDays * DailyRate
Case Else
decDailyRateCharge = 0
MessageBox.Show("Oops you're not supposed to see this", "Christian's Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End Select
Return decDailyRateCharge
End Function
'============================================================
'Function : GetMileageCharge
'============================================================
'Calculates the total mileage charge based on the total miles
'Mileage rate, and the account type.
'============================================================
Private Function GetMileageCharge(ByVal TotalMiles As Decimal, ByVal MileageRate As Decimal, ByVal AccountType As String) As Decimal
Dim decMileageCharge As Decimal
If AccountType <> "Corporate" Then
decMileageCharge = TotalMiles / mdecNumberOfDays
If decMileageCharge > 100 Then
decMileageCharge = (decMileageCharge - 100) * MileageRate
decMileageCharge = decMileageCharge * mdecNumberOfDays
Else
decMileageCharge = 0D
End If
Return decMileageCharge
Else
Return 0
End If
End Function
'============================================================
'Function : CalcDiscount
'============================================================
'Calculates the applicable discount based on the daily rate
'and account type.
'============================================================
Private Function CalcDiscount(ByVal DailyRateCharge As Decimal, ByVal AccountType As String) As Decimal
Dim decDiscountRate As Decimal
Select Case mstrAccountType
Case "Corporate"
decDiscountRate = DailyRateCharge * 0.05D
Case "Insurance"
decDiscountRate = DailyRateCharge * 0.1D
Case "Regular"
Case Else
DailyRateCharge = 0
MessageBox.Show("Oops you're not supposed to see this", "Christian's Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End Select
Return decDiscountRate
End Function
'============================================================
'Sub : CalcTotalRental
'============================================================
'Calculates the Total Rental and sets the output properties.
'============================================================
Public Sub CalcTotalRental()
Dim decMileageRate As Decimal = GetCarTypeMileageRates(mstrTypeOfCar)
Dim decTotalMiles As Decimal = Convert.ToDecimal(mintEndOdo - mintStartOdo)
Dim strAccountType As String = mstrAccountType
mdecDailyRate = GetCarTypeDailyRates(mstrTypeOfCar)
mdecDailyCharge = CalculateDailyRate(mdecDailyRate, strAccountType)
mdecMileageCharge = GetMileageCharge(decTotalMiles, decMileageRate, strAccountType)
mdecDiscountRate = CalcDiscount(mdecDailyCharge, strAccountType)
mdecTotalRent = (mdecDailyCharge + mdecMileageCharge) - mdecDiscountRate
End Sub
'============================================================
'Function : GetCarTypeMileageRates
'============================================================
'Gets the mileage rate based on the type of car.
'============================================================
Private Function GetCarTypeMileageRates(ByVal Car As String) As Decimal
Select Case Car
Case "Economy"
Return mdecMileageCharges(MyCarType.Economy)
Case "Midsize"
Return mdecMileageCharges(MyCarType.Midsize)
Case "Luxury"
Return mdecMileageCharges(MyCarType.Luxury)
Case Else
MessageBox.Show("Oops you're not supposed to see this", "Christian's Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End Select
End Function
'============================================================
'Function : GetCarTypeDailyRates
'============================================================
'Gets the daily rate based on the type of car.
'============================================================
Private Function GetCarTypeDailyRates(ByVal Car As String) As Decimal
Select Case Car
Case "Economy"
Return mdecDailyRates(MyCarType.Economy)
Case "Midsize"
Return mdecDailyRates(MyCarType.Midsize)
Case "Luxury"
Return mdecDailyRates(MyCarType.Luxury)
Case Else
MessageBox.Show("Oops you're not supposed to see this", "Christian's Car Rental", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End Select
End Function
End Class
Public Class BusinessException
Inherits System.ApplicationException
'============================================================
'Module level variables
'============================================================
Private invalidFieldString As String
Sub New(ByVal Message As String, ByVal InvalidField As String)
MyBase.New(Message) 'REQUIRED!!!!
invalidFieldString = InvalidField
End Sub
Public ReadOnly Property FieldInError() As String
Get
Return invalidFieldString
End Get
End Property
End Class