Paste Search Dynamic
Restaurant
  1. using system;
  2. using system.Collections.Generic;
  3. using system.Linq;
  4.  
  5. public class Restaurant
  6. {
  7.         public WeekCollection<OpeningHour> OpeningHours { get; private set; }
  8.  
  9.         public Restaurant()
  10.         {
  11.                 // No opening hours available for restaurant
  12.         }
  13.  
  14.         public Restaurant(OpeningHour monday, OpeningHour tuesday, OpeningHour wednesday, OpeningHour thursday, OpeningHour friday, OpeningHour saturday, OpeningHour sunday)
  15.         {
  16.                 OpeningHours = new WeekCollection<OpeningHour>(monday, tuesday, wednesday, thursday, friday, saturday, sunday);
  17.         }
  18.  
  19.         public string GetOpeningHours()
  20.         {
  21.                 var groupedBy = OpeningHours.GetDict().GroupBy(
  22.                         c => new {
  23.                                 c.Value.OpeningTime,
  24.                                 c.Value.ClosingTime
  25.                         }).ToList();
  26.  
  27.                 foreach (var item in groupedBy)
  28.                 {
  29.                         foreach (var a in item)
  30.                         {
  31.                                 //Day
  32.                                 string Day = a.Key + "";
  33.                                 Day = Day.Substring(0, 3);
  34.                                 Console.WriteLine(Day);
  35.                         }
  36.                         //Get Time
  37.                         string time = item.Key + "";
  38.                         string opntime = time.Substring(1, 3);
  39.                         Console.WriteLine(opntime + "-" ); //item.Key
  40.                 }
  41.                 return "";
  42.  
  43.  
  44.         }
  45. }
  46.  
  47. public class OpeningHour : IComparable<OpeningHour>
  48. {
  49.         public TimeSpan OpeningTime { get; private set; }
  50.         public TimeSpan ClosingTime { get; private set; }
  51.  
  52.         public OpeningHour(TimeSpan openingTime, TimeSpan closingTime)
  53.         {
  54.                 OpeningTime = openingTime;
  55.                 ClosingTime = closingTime;
  56.         }
  57.  
  58.         public OpeningHour(int openingHour, int closingHour)
  59.         {
  60.                 OpeningTime = TimeSpan.FromHours(openingHour);
  61.                 ClosingTime = TimeSpan.FromHours(closingHour);
  62.         }
  63.  
  64.         public int CompareTo(OpeningHour other)
  65.         {
  66.                 if (OpeningTime == other.OpeningTime && ClosingTime == other.ClosingTime)
  67.                         return 0;
  68.                 else
  69.                         return 1;
  70.         }
  71.  
  72. }
  73.  
  74. public class WeekCollection<T>
  75. {
  76.         private Dictionary<DayOfWeek, T> _collection;
  77.  
  78.         public WeekCollection(T sunday, T monday, T tuesday, T wednesday, T thursday, T friday, T saturday)
  79.         {
  80.                 _collection = new Dictionary<DayOfWeek, T>();
  81.                 _collection.Add(DayOfWeek.Sunday, sunday);
  82.                 _collection.Add(DayOfWeek.Monday, monday);
  83.                 _collection.Add(DayOfWeek.Tuesday, tuesday);
  84.                 _collection.Add(DayOfWeek.Wednesday, wednesday);
  85.                 _collection.Add(DayOfWeek.Thursday, thursday);
  86.                 _collection.Add(DayOfWeek.Friday, friday);
  87.                 _collection.Add(DayOfWeek.Saturday, saturday);
  88.         }
  89.  
  90.         public T Get(DayOfWeek dayOfWeek)
  91.         {
  92.                 return _collection[dayOfWeek];
  93.         }
  94.  
  95.         public Dictionary<DayOfWeek, T> GetDict()
  96.         {
  97.                 return _collection;
  98.         }
  99.  
  100. }
  101.  
  102. public class Test
  103. {
  104.         public static void Main()
  105.         {
  106.                 var restaurant = new Restaurant(
  107.                         new OpeningHour(8, 16), // Sunday
  108.                         new OpeningHour(8, 17), // Monday
  109.                         new OpeningHour(8, 18), // Tuesday
  110.                         new OpeningHour(8, 19), // Wednesday
  111.                         new OpeningHour(8, 20), // Thursday
  112.                         new OpeningHour(8, 21), // Friday
  113.                         new OpeningHour(8, 19)  // Saturday
  114.                 );
  115.  
  116.                 //Console.WriteLine ("asdsa");
  117.                 Console.WriteLine(restaurant.GetOpeningHours());
  118.         }
  119. }
Parsed in 0.023 seconds