C# application how could i implement dictionary or hashtable for this?

試著忘記壹切 提交于 2019-12-08 10:40:24

问题


This is my problem, i want to write a basic console application where i enter a date as input, if that date hasnt been entered the application then allow a time to enter a note, i.e for 07/08/2013 for time 5:00 - 7:00 pm enter text blah blah

then application will keep looping, if i enter the same date, i shouldnt be able to enter the same times as above, but i should be able to enter 7:00 to 8 for example.

i was thinking of using dictionary :

Dictionary<string, Booking> BookingDict = new Dictionary<string, Booking>();

and adding date as id, but it seems only one element id can be entered uniquely

can some one please help


回答1:


Do you have to use a list or dictionary? If you will not be searching in it alot and do not have a lot of elements, you are probably best going with a generic list: List<Booking>

Then you can use LINQ to search through the list to retrieve the bookings that you need.




回答2:


i would recommend a dictionary with the key being the date and the value be an array of 24 booleans represent hours. that is only if the booking is for full hours. if it's by the minute then the array will be too big and then another option might be needed.

each cell in the array is true if the hour is booked, for example if <07/08/13, booked[2] = true> means that hour 2-3 is booked on 07/08/13 is booked.

when you get a new booking you'll need to check each hour between the two. if you got hours 4-10 you'll need to check the value in 4,5,6,7,8,9. not the best efficiency i think, but not that bad if no one is booking a full week or so.

sum it up my answer is using

Dictionary<string/DateTime, bool[24]> bookingTbl



回答3:


create a key with two dateTime (the start and end dateTime). When you want to enter a new booking, check (with linq for instance) if there's a known end Datetime that is between your new start and end datetime and do the same with the known start datetime. If there is no overlap, add it.

Here's an example:

Dictionary<TwoUintsKeyInfo,object> test = new Dictionary<TwoUintsKeyInfo, object>();
        test.Add(new TwoUintsKeyInfo { IdOne = 3, IdTwo = 9 }, new object());
        test.Add(new TwoUintsKeyInfo { IdOne = 10, IdTwo = 15 }, new object());

        uint newStartPoint1 = 16,newEndPoint1=20;
        bool mayUse = (from result in test 
                            let newStartPointIsBetweenStartAndEnd = newStartPoint1.Between(result.Key.IdOne,result.Key.IdTwo)
                            let newEndPointIsBetweenStartAndEnd = newEndPoint1.Between(result.Key.IdOne,result.Key.IdTwo)
                            let completeOverlap = result.Key.IdOne < newStartPoint1 && result.Key.IdTwo > newEndPoint1
                            let oldDateWithingNewRange = result.Key.IdOne.Between(newStartPoint1, newEndPoint1) || result.Key.IdTwo.Between(newStartPoint1, newEndPoint1)
                            let FoundOne = 1
                            where newStartPointIsBetweenStartAndEnd || newEndPointIsBetweenStartAndEnd || completeOverlap || oldDateWithingNewRange
                            select FoundOne).Sum()==0;

using:

 public static class LinqExtentions
{
    /// <summary>
    /// Note: For the compare parameters; First the low, than the High
    /// </summary>
    /// <returns>Bool</returns>
    public static bool Between<T1, T2, T3>(this T1 actual, T2 lowest, T3 highest)
        where T1 : IComparable
        where T2 : IConvertible
        where T3 : IConvertible
    {
        return actual.CompareTo(lowest.ToType(typeof(T1), null)) >= 0 &&
               actual.CompareTo(highest.ToType(typeof(T1), null)) <= 0;
    }
}

public class TwoUintsKeyInfo
{
    public uint IdOne { get; set; }
    public uint IdTwo { get; set; }

    public class EqualityComparerTwoUintsKeyInfo : IEqualityComparer<TwoUintsKeyInfo>
    {
        public bool Equals(TwoUintsKeyInfo x, TwoUintsKeyInfo y)
        {
            return x.IdOne == y.IdOne &&
                    x.IdTwo == y.IdTwo;
        }

        public int GetHashCode(TwoUintsKeyInfo x)
        {
            return (Math.Pow(x.IdOne, Math.E) + Math.Pow(x.IdTwo, Math.PI)).GetHashCode();
        }
    }
}

I tested it, seems to be working. Good luck!



来源:https://stackoverflow.com/questions/18097096/c-sharp-application-how-could-i-implement-dictionary-or-hashtable-for-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!