pyuntae
pyuntae

Reputation: 832

C# Why is my calculations not adding correctly?

I am making a form that calculates reservation dates. On Fridays and Saturdays the charge for a room is $150 while on the rest of the days the charge is $120. I have used a while loop in order to set this up but for some reason it keeps calculating the wrong prices.

What it should look like:

enter image description here

What it looks like:

enter image description here

Here is my code:

int nights = 0;
int price = 0;

private void btnCalculate_Click(object sender, EventArgs e)
    {
        DateTime arrivalDate = DateTime.Parse(txtArrivalDate.Text);
        DateTime departureDate = DateTime.Parse(txtDepartureDate.Text);
        TimeSpan ts = departureDate.Subtract(arrivalDate);
        nights = ts.Days;

        while (arrivalDate != departureDate)
        {
            DayOfWeek dow = arrivalDate.DayOfWeek;
            if (dow == DayOfWeek.Friday || 
                dow == DayOfWeek.Saturday)
            {
                price = 150;
            }
            else
            {
                price = 120;
            }

            arrivalDate = arrivalDate.AddDays(1);

        }

        txtNights.Text = nights.ToString();
        int totalPrice = price * nights;
        txtTotalPrice.Text = totalPrice.ToString();
        int average = totalPrice / nights;
        txtAvgPrice.Text = average.ToString();
        txtArrivalDate.Focus();
    }

Upvotes: 0

Views: 223

Answers (2)

tmaj
tmaj

Reputation: 34987

You're not doing anything with the price.

It should be used in the while loop.

Also, you should be using .TotalDays, not .Days.

Something like:

            public static (decimal price, int nights) GetPrice
        (DateTime arrivalDate, DateTime departureDate)
    {
        //This code assumes there is no time component in the dates provided 

        if(departureDate < arrivalDate )
        {
            throw new ArgumentException("Arrival after Departure");
        }

        if (departureDate.Date == arrivalDate.Date)
        {
            //TODO
            return (0, 0);
        }

        Decimal totalPrice = 0;
        DateTime day = arrivalDate;
        while (day != departureDate)
        {
            totalPrice += GetRate(day.DayOfWeek);
            day = day.AddDays(1);
        }
        return (totalPrice, (int)(departureDate - arrivalDate).TotalDays);
    }

    private static decimal GetRate(DayOfWeek dow)
    {
        return (dow == DayOfWeek.Friday || dow == DayOfWeek.Saturday)
            ? 150
            : 120;
    }

Upvotes: -3

Bivo Kasaju
Bivo Kasaju

Reputation: 1233

In short, int totalPrice = price * nights; this line should be removed, and in the while loop, price += 120 or price += 150 in each condition. totalPrice can simply be replaced by price.

You're not using the price set in while loop. price is set to 120 or 150 but then overwritten by the next value (previous value is completely ignored). So once your code gets out of the while loop, the latest price set is used and multiplied by total nights.

So what your code is doing is taking the price of the last day (2/1/2016 in this case) and multiplying it by total nights. What it should be doing is keeping a running total of price inside the loop.

Upvotes: 5

Related Questions