// This method is for altering an already existing event in the list. It uses the same parameters as the CreateEvent method above.
        public bool AlterEvent(int key, string start, string end, string address, string comment, int hearse)
        {
            // We instansiate a new _Event object of the CalendarEntry class followed by the data which the object shall contain.
            CalendarEntry _Event = new CalendarEntry(0, DateTime.Now, DateTime.Now, "", "", status.Deleted, null);

            // We run a foreach loop, checking every object(i) of the CalendarEntry type in the list of events.
            foreach (CalendarEntry i in Eventslist)
            {
                // If loop to check if the item's key is equal to the key given in the method parameter, then that event referred to in the AlterEvent method is set to be equal to the _Event object.
                if (i.Key == key)
                {
                    _Event = i;
                }
            }

            // If the key given in the AlterEvent method is equal to 0, then that key is not found.
            if (_Event.Key == 0)
            {
                throw new IndexOutOfRangeException("Identifikationsnøglen blev ikke fundet.");
            }

            // If the hearse is equal to 0, set its value to null.
            if (hearse == 0)
            {
                _Event.Hearse = null;
            }

            // If it is not 0, call the GetHearse method to assign a hearse to the event.
            else
            {
                _Event.Hearse = HearseRepo.GetHearse(hearse);
            }

            // if the start value is not equal to 0, continue the code block.
            if (!(start == null))
            {
                // A variable of the DateTime datatype is made.
                DateTime ostart;

                // If loop to attempt to parse the value of the start parameter to the ostart variable.
                if (DateTime.TryParse(start, out ostart))
                {
                    // If the parse succeeded; set the boolean, Available, to true.
                    bool Available = true;

                    // Run a foreach loop to check every calendarEntry(e) in the Event list.
                    //foreach (CalendarEntry e in Eventslist)
                    //{

                    //// If the entry(e) is equal to the _Event.Hearse AND is not equal to; the ostart value being before the Start parameter OR later than the End parameter, return false.
                    //    if (e.Hearse == _Event.Hearse && !(ostart<e.Start||ostart>e.End) )
                    //    {
                    //        Available = false;
                    //    }
                    //}

                    // If not, simply set the Start value to the ostart variable.
                    if (Available)
                    {
                        _Event.Start = ostart;
                    }
                    else
                    {
                        Available = true;
                    }
                }

                // If the Event.Start is not equal to the ostart variable, return false.
                if (!(_Event.Start == ostart))
                {
                    return(false);
                }
            }

            // If the end is not equal to null, continue the code block.
            if (!(end == null))
            {
                // A DateTime variable, oend, is made
                DateTime oend;

                // Try to parse the end value to the oend variable.
                if (DateTime.TryParse(end, out oend))
                {
                    // If it succeeded; set the boolean, Available, to true.
                    bool Available = true;

                    // Foreach loop of calendarEntry(e) in the event list
                    //foreach (CalendarEntry e in Eventslist)
                    //{

                    //// If the entry(e) is equal to the _Event.Hearse AND is not equal to; the oend value being before the Start parameter OR later than the End parameter, return false.
                    //    if (e.Hearse == _Event.Hearse && !(oend > e.Start || oend < e.End))
                    //    {
                    //        Available = false;
                    //    }
                    //}


                    if (Available)
                    {
                        _Event.Start = oend;
                    }
                    else
                    {
                        Available = true;
                    }
                }
                if (_Event.End == oend)
                {
                    return(true);
                }
            }

            // If the address is not equal to null; set the address as the Event.Address.
            if (!(address == null))
            {
                _Event.Address = address;
            }

            // If the comment is not equal to null; set the comment as the Event.Comment.
            if (!(comment == null))
            {
                _Event.Comment = comment;
            }

            // If the _Event.Status is not equal to status.NewlyMade; set the _Event.Status as the Status.Changed.
            if (!(_Event.Status == status.NewlyMade))
            {
                _Event.Status = status.Changed;
            }
            foreach (CalendarEntry i in Eventslist)
            {
                // If loop to check if the item's key is equal to the key given in the method parameter, then that event referred to in the AlterEvent method is set to be equal to the _Event object.
                if (i.Key == key)
                {
                    i.Status  = _Event.Status;
                    i.Hearse  = _Event.Hearse;
                    i.Address = _Event.Address;
                    i.Comment = _Event.Comment;
                    i.End     = _Event.End;
                    i.Start   = _Event.Start;
                }
            }
            return(true);
        }
Beispiel #2
0
        public bool AlterEvent(int key, string start, string end, string address, string comment, int hearse)
        {
            Events E = new Events(0, DateTime.Now, DateTime.Now, "", "", status.Deleted, null);

            foreach (Events i in Eventslist)
            {
                if (i.Key == key)
                {
                    E = i;
                }
            }
            if (E.Key == 0)
            {
                throw new IndexOutOfRangeException("key not found");
            }
            if (hearse == 0)
            {
                E.Hearse = null;
            }
            else
            {
                E.Hearse = HearseRepo.GetHearse(hearse);
            }
            if (!(start == null))
            {
                DateTime ostart;
                if (DateTime.TryParse(start, out ostart))
                {
                    bool free = true;

                    foreach (Events e in Eventslist)
                    {
                        if (e.Hearse == E.Hearse && !(ostart < e.Start || ostart > e.End))
                        {
                            free = false;
                        }
                    }
                    if (free)
                    {
                        E.Start = ostart;
                    }
                    else
                    {
                        free = true;
                    }
                }
                if (!(E.Start == ostart))
                {
                    return(false);
                }
            }
            if (!(end == null))
            {
                DateTime oend;
                if (DateTime.TryParse(end, out oend))
                {
                    bool free = true;

                    foreach (Events e in Eventslist)
                    {
                        if (e.Hearse == E.Hearse && !(oend > e.Start || oend < e.End))
                        {
                            free = false;
                        }
                    }
                    if (free)
                    {
                        E.Start = oend;
                    }
                    else
                    {
                        free = true;
                    }
                }
                if (E.End == oend)
                {
                    return(true);
                }
            }
            if (!(address == null))
            {
                E.Address = address;
            }
            if (!(comment == null))
            {
                E.Comment = comment;
            }
            if (!(E.Status == status.NewlyMade))
            {
                E.Status = status.Changed;
            }
            return(true);
        }