【问题标题】:Hotel reservation price code weekend price (Logic implementation issue)酒店预订价格代码周末价格(逻辑实现问题)
【发布时间】:2020-11-13 07:10:00
【问题描述】:

当用户预订周五和周六当天的周末额外费用时,我尝试多收取 30 美元,但我无法获得当天的费用,也无法添加我的额外费用。 到目前为止,我已经这样做了:

    private void btnCalculate_Click(object sender, EventArgs e)
            {
                DateTime dtArrivalDate;
                DateTime.TryParse(txtArrivalDate.Text, out dtArrivalDate);
                DateTime dtDepartureDate;
                DateTime.TryParse(txtDepartureDate.Text, out dtDepartureDate);
                TimeSpan tsDuration = dtDepartureDate.Subtract(dtArrivalDate);
                int costPerNight = 120;
                for (DateTime date = dtArrivalDate; date <= dtDepartureDate; date = date.AddDays(1))
                {
                    DayOfWeek dw = date.DayOfWeek;
                    string days = dw.ToString();
                    int weekendCost = 0;
                    if (dw == DayOfWeek.Friday || dw == DayOfWeek.Saturday)
                    {
                        weekendCost = weekendCost + 30;
                        dw++;
                        int totalCost = tsDuration.Days * costPerNight + weekendCost;
                        int avgPricePerNight = (tsDuration.Days * costPerNight + weekendCost) / tsDuration.Days;
                        txtNights.Text = tsDuration.Days.ToString("n");
                        txtTotalPrice.Text = totalCost.ToString("n");
                        txtAvgPrice.Text = avgPricePerNight.ToString("n");
                    }
                    else
                    {
                        int totalCost = tsDuration.Days * costPerNight;
                        int avgPricePerNight = (tsDuration.Days * costPerNight) / tsDuration.Days;
                        txtNights.Text = tsDuration.Days.ToString("n");
                        txtTotalPrice.Text = totalCost.ToString("n");
                        txtAvgPrice.Text = avgPricePerNight.ToString("n");
                    }
                }    
            }

【问题讨论】:

  • 我删除了你的 [visual-studio-201] 标签,因为这个问题与 Visual Studio 无关。
  • 你能告诉你在这里看到什么问题吗?

标签: c# logic


【解决方案1】:
for (DateTime date = dtArrivalDate; date <= dtDepartureDate; date = date.AddDays(1))

与您的问题没有直接关系,但应该是date &lt; dtDepartureData

无论如何,问题在于您每天都检查,但在循环中您重新计算并显示价格。更好的代码应该是这样的(保持你每天迭代的基本想法):

int costPerNight = 120;
int weekendCost = 0;
for (DateTime date = dtArrivalDate; date < dtDepartureDate; date = date.AddDays(1))
{
    DayOfWeek dw = date.DayOfWeek;
    if (dw == DayOfWeek.Friday || dw == DayOfWeek.Saturday)
    {
        weekendCost = weekendCost + 30;
    }
}
int totalCost = tsDuration.Days * costPerNight + weekendCost;
int avgPricePerNight = (tsDuration.Days * costPerNight + weekendCost) / tsDuration.Days;
txtNights.Text = tsDuration.Days.ToString("n");
txtTotalPrice.Text = totalCost.ToString("n");
txtAvgPrice.Text = avgPricePerNight.ToString("n");

我们计算每个周末的天数,然后在循环结束后,计算总价格。

顺便说一句,这些 30120 值应替换为命名常量以提高可读性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多