重构其他if语句从周三返回当前周

时间:2014-09-18 15:21:09

标签: c# if-statement

我非常担心CurrentRentWeek.cs中的代码是否具有面向未来的能力,是否有这么多其他if语句的好习惯?如果不是,重构它的最佳方法是什么?

MainWindow.Xaml.cs

    public MainWindow()
    {
        InitializeComponent();

        // Set current rent week
        var datecheckObject = new CurrentRentWeek();
        CurrentRentWeekTextBlock.Text = datecheckObject.DateCheck(CurrentRentWeekTextBlock.Text);
    }

CurrentRentWeek.cs

    public string DateCheck(string rentWeek)
    {
        if (_today.DayOfWeek == DayOfWeek.Monday)
        {
            _cRentWeekStart = _today.AddDays(-5);
            _cRentWeekEnd = _today.AddDays(2);
            rentWeek = "Current Rent Week: " + _cRentWeekStart.ToString("dd/MM/yyyy") + " - " +
                       _cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else if (_today.DayOfWeek == DayOfWeek.Tuesday)
        {
            _cRentWeekStart = _today.AddDays(-6);
            _cRentWeekEnd = _today.AddDays(1);
            rentWeek = "Current Rent Week: " + _cRentWeekStart.ToString("dd/MM/yyyy") + " - " +
                       _cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else if (_today.DayOfWeek == DayOfWeek.Wednesday)
        {
            _cRentWeekStart = _today.AddDays(0);
            _cRentWeekEnd = _today.AddDays(7);
            rentWeek = "Current Rent Week: " + _cRentWeekStart.ToString("dd/MM/yyyy") + " - " +
                       _cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else if (_today.DayOfWeek == DayOfWeek.Thursday)
        {
            _cRentWeekStart = _today.AddDays(-1);
            _cRentWeekEnd = _today.AddDays(6);
            rentWeek = "Current Rent Week: " + _cRentWeekStart.ToString("dd/MM/yyyy") + " - " +
                       _cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else if (_today.DayOfWeek == DayOfWeek.Friday)
        {
            _cRentWeekStart = _today.AddDays(-2);
            _cRentWeekEnd = _today.AddDays(5);
            rentWeek = "Current Rent Week: " + _cRentWeekStart.ToString("dd/MM/yyyy") + " - " +
                       _cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else if (_today.DayOfWeek == DayOfWeek.Saturday)
        {
            _cRentWeekStart = _today.AddDays(-3);
            _cRentWeekEnd = _today.AddDays(4);
            rentWeek = "Current Rent Week: " + _cRentWeekStart.ToString("dd/MM/yyyy") + " - " +
                       _cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else if (_today.DayOfWeek == DayOfWeek.Sunday)
        {
            _cRentWeekStart = _today.AddDays(-4);
            _cRentWeekEnd = _today.AddDays(3);
            rentWeek = "Current Rent Week: " + _cRentWeekStart.ToString("dd/MM/yyyy") + " - " +
                       _cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else
        {
            rentWeek = "";
        }
        return rentWeek;
    }

3 个答案:

答案 0 :(得分:5)

您可以从广义函数开始,以获得任何给定日期的星期开始:

public static DateTime StartOfWeek(DateTime date)
{
    while (date.DayOfWeek != DayOfWeek.Wednesday)
        date = date.AddDays(-1);
    return date;
}

然后你可以简单地调用该方法,添加固定的天数以到达周末,并为这些日期创建字符串:

public string DateCheck()
{
    var startOfWeek = StartOfWeek(_today);
    var endOfWeek = startOfWeek.AddDays(7);
    return string.Format("Current Rent Week: {0} - {1}",
        startOfWeek.ToString("dd/MM/yyyy"),
        endOfWeek.ToString("dd/MM/yyyy"));
}

答案 1 :(得分:1)

您希望Wednesday是本周的开始吗?你可以用这个:

int daysDiff = (int)_today.DayOfWeek - (int)DayOfWeek.Wednesday;
if (daysDiff >= 0)
    _cRentWeekStart = _today.AddDays(-daysDiff);
else
    _cRentWeekStart = _today.AddDays(-(7 + daysDiff));
_cRentWeekEnd = _cRentWeekStart.AddDays(7);

如果今天是"更少"这将返回上周的上周三。比周三似乎更合适。

答案 2 :(得分:0)

我认为转换声明会更清楚你的情况并节省时间,但我怀疑这是一个问题。

switch (_today.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    _cRentWeekStart = _today.AddDays(-5);
                    _cRentWeekEnd = _today.AddDays(2);
                    rentWeek = "Current Rent Week: " + _cRentWeekStart.ToString("dd/MM/yyyy") + " - " +_cRentWeekEnd.ToString("dd/MM/yyyy");                        
                    break;

                case DayOfWeek.Tuesday:
                    //...
                    break;
                //rest of cases
            }