使用DayOfWeek使挑战更有效

问题描述

我遇到了这个挑战,要求根据提供的时间表打印出字符串。这是一个示例:

var restaurant = new Restaurant(
            new openingHour(8,16),// Sunday
            new openingHour(8,17),// Monday
            new openingHour(8,// Tuesday
            new openingHour(8,// Wednesday
            new openingHour(8,// Thursday
            new openingHour(8,// Friday
            new openingHour(8,16)  // Saturday
        );

预期的输出结果 =“周日,周四-周六:8-16,周一-周三:8-17”

我所做的基本上是:

  • 创建日期,开放时间和关闭时间列表
  • 创建日期的哈希集,以便我可以比较日期
  • 根据HashSet和Days创建一个for循环
  • 分隔开始,中间和结尾
  • 根据营业时间和营业时间以及两天之间的间隔来合并结果

我已经尽力了,但是我知道我的代码根本不是高效的,而是凌乱的。我正在尝试提高我的C#技能,请帮助。这是我的凌乱代码

namespace Livit
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Restaurant
    {
        public WeekCollection<openingHour> openingHours { get; private set; }

        public Restaurant() {
            // No opening hours available for restaurant
        }

        public Restaurant(openingHour monday,openingHour tuesday,openingHour wednesday,openingHour thursday,openingHour friday,openingHour saturday,openingHour sunday)
        {
            openingHours = new WeekCollection<openingHour>(monday,tuesday,wednesday,thursday,friday,saturday,sunday);
        }
        
        // THE EMPHASIS OF THE CHALLANGE IS THIS FUNCTION RIGHT HERE!!!
        // Parse the date into desired format
        public string DateParser(List<DayOfWeek> days,List<TimeSpan> openHours,List<TimeSpan> closeHours)
        {
          HashSet<string> availableRanges = new HashSet<string>();
          List<string> timeRanges = new List<string>();
          DayOfWeek current = DayOfWeek.Sunday;
          string result = "";
          for (int i = 0 ; i < days.Count; i++){
            string timeRange = openHours[i].ToString().Substring(1,1)+'-'+closeHours[i].ToString().Substring(0,2);
            availableRanges.Add(timeRange);
            timeRanges.Add(timeRange);
          }
          List<string> arToList= availableRanges.ToList();
          for (int i = 0 ; i < arToList.Count; i++)
            {
              for (int j = 0 ; j < timeRanges.Count; j++){
                if(timeRanges[j] == arToList[i]){
                  
                  
                  // First Item
                  if(j==0 ){
                    result += days[j].ToString().Substring(0,3);
                  }
                  // Last Item
                  else if(j==timeRanges.Count-1){
                    char last = result.Last();
                    if(last != ' '){
                        result += " - ";
                      } 
                    result += days[j].ToString().Substring(0,3);
                  }
                  // Everything in the middle
                  else{
                    if(days[j]-current > 1){
                      result += ",";
                    }
                    if(timeRanges[j] != timeRanges[j-1] ){
                      result += days[j].ToString().Substring(0,3);
                    } else if (timeRanges[j] == timeRanges[j-1]){
                      char last = result.Last();
                      if(last != ' '){
                        result += " - ";
                      } 
                      if(timeRanges[j] != timeRanges[j+1]){
                        result += days[j].ToString().Substring(0,3);
                      }
                    }
                  }
                  
                  current = days[j];
                } 
              }
              
              result += ": " + arToList[i];
              if(i!=arToList.Count-1){
                result += ",";
              }
            } 
          Console.WriteLine(result);
          return result;
        }

        public string GetopeningHours()
        {
            // Declare List for each attribute
            List<DayOfWeek> days = new List<DayOfWeek>();
            List<TimeSpan> openHours = new List<TimeSpan>();
            List<TimeSpan> closeHours = new List<TimeSpan>();
            // Call the opening and closing hours from each day and Feed into new array
            foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek)).OfType<DayOfWeek>().ToList()) {
              TimeSpan openHour = openingHours.Get(day).openingTime;
              TimeSpan closeHour = openingHours.Get(day).ClosingTime;
              days.Add(day);
              openHours.Add(openHour);
              closeHours.Add(closeHour);
            }
            return DateParser(days,openHours,closeHours);
            
            
            
            throw new NotImplementedException();
        }
    }

    public class openingHour
    {
        public TimeSpan openingTime { get; private set; }
        public TimeSpan ClosingTime { get; private set; }

        public openingHour(TimeSpan openingTime,TimeSpan closingTime)
        {
            openingTime = openingTime;
            ClosingTime = closingTime;
        }

        public openingHour(int openingHour,int closingHour)
        {
            openingTime = TimeSpan.FromHours(openingHour);
            ClosingTime = TimeSpan.FromHours(closingHour);
        }

    }

    public class WeekCollection<T>
    {
        private Dictionary<DayOfWeek,T> _collection;

        public WeekCollection(T sunday,T monday,T tuesday,T wednesday,T thursday,T friday,T saturday)
        {
            _collection = new Dictionary<DayOfWeek,T>();
            _collection.Add(DayOfWeek.Sunday,sunday);
            _collection.Add(DayOfWeek.Monday,monday);
            _collection.Add(DayOfWeek.Tuesday,tuesday);
            _collection.Add(DayOfWeek.Wednesday,wednesday);
            _collection.Add(DayOfWeek.Thursday,thursday);
            _collection.Add(DayOfWeek.Friday,friday);
            _collection.Add(DayOfWeek.Saturday,saturday);
        }

        public T Get(DayOfWeek dayOfWeek)
        {
            return _collection[dayOfWeek];
        }

    }
}

目前,我仍在尝试寻找一种更好的方法来应对这一挑战。任何帮助都会得到应用。

P.S。我强调了发生串联的部分,这部分基本上是整个挑战的重点

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)