Hi all, I am trying to calculate the total car rental charge here and then have the amount display when the total charge button is clicked on my form. It keeps showing up as 0. I have been troubleshooting with a friend as well and we cant figure it out. Anyone able to help?
here is my class code:
Here is the code for my button trying to call the calculation to show when the button is clicked:
thank you very much for any input!!!
here is my class code:
C#:
public class CarRental
{
#region "Properties/Fields"
//5 Instance Properties
//auto implemented publice get, set
public string CustomerName { get; set; }
//auto implemented public get, private set
public decimal RentalCharge { get; private set; }
// three private fields
private int beginOdmometerReading;
private int daysRented;
private int endOdometerReading;
public int BeginOdometerReading
{
get
{
return beginOdmometerReading;
}
set
{
beginOdmometerReading = value;
}
}
public int EndOdometerReading
{
get
{
return endOdometerReading;
}
set
{
endOdometerReading = value;
}
}
public int DaysRented
{
get
{
return daysRented;
}
set
{
daysRented = value;
}
}
#endregion
#region "Cunstructors"
//Overloaded Constructor to instantiate object and set four properties
public CarRental(string customerName, int beginOdometerReading, int endOdometerReading, int daysRented)
{
CustomerName = customerName;
BeginOdometerReading = beginOdometerReading;
EndOdometerReading = endOdometerReading;
DaysRented = daysRented;
CalculateCost();
}
// Default Constructor
public CarRental()
{
}
#endregion
#region "Methods"
//Private Instance Method to Calculate rental charge and set crental charge property
private void CalculateCost()
{
//Constant Variables
const decimal costPerDay = 29.50m;
const decimal costPerMile = 0.45m;
decimal RentalTotal;
//Calculate Cost
RentalTotal = ((EndOdometerReading - BeginOdometerReading) * costPerMile) + (costPerDay * daysRented);
//set the RentalCharge Property Value
RentalTotal = RentalCharge;
}
#endregion
}
Here is the code for my button trying to call the calculation to show when the button is clicked:
C#:
private void btnCharge_Click(object sender, EventArgs e)
{
// Display Rental Car Charge amount in Label "Charge"
CarRental aCarRental = new CarRental(txtName.Text, (int)nBeginOdometer.Value, (int)nEndOdometer.Value, (int)nDaysRented.Value);
lblCharge.Text = aCarRental.RentalCharge.ToString();
}
thank you very much for any input!!!
Last edited by a moderator: