Ejemplo n.º 1
0
        static void Main()
        {
            bool   flag = true;
            char   givOption;
            string fileName;

            do
            {
                Console.WriteLine("Enter file name:");
                fileName = Console.ReadLine();
            }while (!Validation.ValidateFileName(fileName));

            Type to_work = typeof(Event);

            Collection <Event> allObjects = new Collection <Event>(fileName);

            PropertyInfo[] object_properties = to_work.GetProperties();
            while (flag)
            {
                Menu();
                givOption = Convert.ToChar(Console.ReadLine());
                switch (givOption)
                {
                case 'Q': flag = false; break;

                case 'A':

                    object toAdd = Activator.CreateInstance(to_work);
                    try
                    {
                        foreach (PropertyInfo x in object_properties)
                        {
                            to_work.GetProperty(x.Name).SetValue(toAdd, EnterValues(x));
                        }
                        if (allObjects.IsPresent((int)to_work.GetProperty("ID", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(toAdd)))
                        {
                            Console.WriteLine("Object with such ID already exists");
                        }
                        else
                        {
                            allObjects.Add(toAdd);
                            Console.WriteLine("Object successfully added to collection");
                            allObjects.Overwrite();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        if (e.InnerException != null)
                        {
                            Console.WriteLine(e.InnerException.Message);
                        }
                    }
                    break;

                case 'D':
                    Console.Write(allObjects.PrintAll());
                    Console.WriteLine("Select the object to delete:");
                    string to_delete = Console.ReadLine();
                    Console.WriteLine(allObjects.DeleteOne(to_delete));
                    allObjects.Overwrite(); break;

                case 'E':
                    Console.Write(allObjects.PrintAll());
                    Console.WriteLine("Select the object to edit:");
                    string to_edit      = Console.ReadLine();
                    object objectToEdit = allObjects.FindByID(to_edit);
                    if (objectToEdit != null)
                    {
                        Console.Write(objectToEdit);
                        Console.WriteLine("Select the property to edit:");
                        string       choice         = Console.ReadLine();
                        PropertyInfo chosenProperty = object_properties.SingleOrDefault(r => r.Name.Equals(choice));
                        if (chosenProperty != null)
                        {
                            try
                            {
                                var new_data = EnterValues(chosenProperty);
                                to_work.GetProperty(choice).SetValue(objectToEdit, new_data);
                                Console.Write($"Success\n{objectToEdit}");
                                allObjects.Overwrite();
                            }
                            catch (Exception e)
                            {
                                if (e.InnerException != null)
                                {
                                    Console.WriteLine(e.InnerException.Message);
                                }
                                else
                                {
                                    Console.WriteLine(e.Message);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("No such property");
                        }
                    }
                    else
                    {
                        Console.WriteLine("No such item");
                    }
                    break;

                case 'S':
                    Console.WriteLine("Select the property to sort by:");
                    string parameter = Console.ReadLine();
                    if (object_properties.SingleOrDefault(r => r.Name.Equals(parameter)) != null)
                    {
                        if (allObjects.Sorting(parameter))
                        {
                            Console.WriteLine("Success");
                            Console.Write(allObjects.PrintAll());
                        }
                        else
                        {
                            Console.WriteLine("Nothing to sort");
                        }
                    }
                    else
                    {
                        Console.WriteLine("No such property");
                    }
                    break;

                case 'F':
                    Console.WriteLine("Enter what to find:");
                    string to_find = Console.ReadLine();
                    Console.Write(allObjects.FindObjects(to_find));
                    break;

                case 'P': Console.Write(allObjects.PrintAll()); break;

                default: Console.WriteLine("No such option"); break;
                }
            }
            Console.WriteLine("The end");
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static void Main()
        {
            bool   flag = true;
            char   givOption;
            string fileName;

            do
            {
                Console.WriteLine("Enter file name:");
                fileName = Console.ReadLine();
            }while (!Validation.ValidateFileName(fileName));
            Collection allEvents = new Collection(fileName);

            string[] event_properties = new string[6]
            {
                "Id", "Title", "Duration", "Price", "DateTime", "RestName"
            };
            while (flag)
            {
                try
                {
                    Menu();
                    givOption = Convert.ToChar(Console.ReadLine());
                    switch (givOption)
                    {
                    case 'Q': flag = false; break;

                    case 'A':
                        Dictionary <string, string> dictToCreate = new Dictionary <string, string> {
                        };

                        foreach (string x in event_properties)
                        {
                            string new_data = EnterValues(x);
                            dictToCreate.Add(x, new_data);
                        }

                        Event toAdd = new Event(dictToCreate);
                        if (toAdd.Correct())
                        {
                            if (allEvents.IsPresent(toAdd.Id))
                            {
                                Console.WriteLine("Event with such ID already exists");
                            }
                            else
                            {
                                allEvents.Add(toAdd);
                                Console.WriteLine("Event successfully added to collection");
                                allEvents.Overwrite();
                            }
                        }
                        else
                        {
                            Console.WriteLine("There were mistakes in your input:");
                            toAdd.PrintErrors();
                        }
                        break;

                    case 'D':
                        Console.Write(allEvents.PrintAll());
                        Console.WriteLine("Select the Event to delete:");
                        string to_delete = Console.ReadLine();
                        Console.WriteLine(allEvents.DeleteOne(to_delete));
                        allEvents.Overwrite(); break;

                    case 'E':
                        Console.Write(allEvents.PrintAll());
                        Console.WriteLine("Select the Event to edit:");
                        string to_edit     = Console.ReadLine();
                        Event  eventToEdit = allEvents.FindByID(to_edit);
                        if (eventToEdit != null)
                        {
                            Console.Write(eventToEdit);
                            Console.WriteLine("Select the property to edit:");
                            string choice = Console.ReadLine();
                            if (event_properties.SingleOrDefault(r => r.Equals(choice)) != null)
                            {
                                string new_data = EnterValues(choice);
                                typeof(Event).GetProperty(choice).SetValue(eventToEdit, new_data);
                                if (eventToEdit.Correct())
                                {
                                    Console.Write($"Success\n{eventToEdit}");
                                    allEvents.Overwrite();
                                }
                                else
                                {
                                    Console.WriteLine($"Wrong Input: {eventToEdit.GetErrors()}");
                                    eventToEdit.ClearErrors();
                                }
                            }
                            else
                            {
                                Console.WriteLine("No such property");
                            }
                        }
                        else
                        {
                            Console.WriteLine("No such item");
                        }
                        break;

                    case 'S':
                        Console.WriteLine("Select the property to sort by:");
                        string parameter = Console.ReadLine();
                        if (event_properties.SingleOrDefault(r => r.Equals(parameter)) != null)
                        {
                            allEvents.Sorting(parameter);
                            Console.WriteLine("Success");
                            Console.Write(allEvents.PrintAll());
                        }
                        else
                        {
                            Console.WriteLine("No such property");
                        }
                        break;

                    case 'F':
                        Console.WriteLine("Enter what to find:");
                        string to_find = Console.ReadLine();
                        Console.Write(allEvents.FindEvents(to_find));
                        break;

                    case 'P': Console.Write(allEvents.PrintAll()); break;

                    default: Console.WriteLine("No such option"); break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    Console.WriteLine("Mistakes were made");
                    Console.ReadKey();
                }
            }
            Console.WriteLine("The end");
            Console.ReadKey();
        }