calculation total and display

chageman

Member
Joined
Oct 5, 2019
Messages
9
Programming Experience
Beginner
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:

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:
Look closer at line 92 and the direction of assignment. Ignore the comment on line 91. (As an aside, this is why most programming style guides recommend not commenting trivial or self-evident code. Trivial code should be easily understood and not require explanation. All it does is add noise instead of clarifying the code.)
I just wanted to say thank you very much for telling me to check out line 92. I appreciate your help!
 
I debugged the code but there are no errors found.
You said this several times but I can't see how it's true. What exactly did you do to debug it. Did you set breakpoints? Did you step through the code line by line? Did you examine the state (values of relevant variables and other expressions) at every step? If not then you didn't debug, or not properly at least. Debugging is not simply running the code and seeing what happens from a user's perspective. If you debug properly then you can compare the state of the application to your expectations at every step. If they match and you still don't get the expected result then obviously your expectations are the problem and you need to reevaluate them. If the actual state differs from your expectations at any point then you have located an issue and then you have something to actually investigate. Reading code over and over is generally pointless. If you can't detect the issue by eye on the first couple of attempts, you're unlikely to at all. You need to watch the code in action.

Looking back at your original code, lines 89 and 92 both set the value of RentalTotal but don't set the value of RentalCharge. If you had actually stepped through those two lines, it seems hard to believe that what they did would actually match your expectations, which is why I can't believe that you actually debugged that code. If you did and you expected to see one variable set twice on subsequent lines then, as I said, the fact that the code did everything you expected and still produced incorrect results should have told you that your expectations were wrong. In that case, you go back to the drawing board and reassess your expectations, which means picking up a pen and paper and writing down what you expected the code to do, step by step. Once you have an actual algorithm written down, then you can write code to implement that algorithm. The reason that people make mistakes like this is that they don't have a clear idea of what the code is actually supposed to do when they write it. Now that it's been pointed out, you'll be better able to look for and find such issues yourself in future. Debugging is a huge chunk of software development.
 
Last edited:
Back
Top Bottom