Exemplo n.º 1
0
        private static void UserMode()
        {
            while (true)
            {
                ShowUserMenu();
                if (!int.TryParse(Console.ReadLine(), out int selectUser))
                {
                    Console.WriteLine("Incorrect input. Try again.");
                    continue;
                }

                switch (selectUser)
                {
                // add new user
                case 1:
                {
                    User user = CreateUser();
                    _userLogic.Add(user);
                    Console.WriteLine($"User added. User's ID is {user.Id}");
                    Console.ReadLine();
                    break;
                }

                // get user by id
                case 2:
                {
                    int  id   = GetId();
                    User user = _userLogic.GetById(id);
                    ShowUser(user);
                    Console.ReadLine();
                    break;
                }

                // show all users
                case 3:
                {
                    ShowAllUsers();
                    Console.ReadLine();
                    break;
                }

                // remove user by id
                case 4:
                {
                    int id = GetId();
                    if (_userLogic.RemoveById(id))
                    {
                        Console.WriteLine($"User with ID = {id} is removed.");
                    }
                    else
                    {
                        Console.WriteLine(NO_USER_ID);
                    }
                    Console.ReadLine();
                    break;
                }

                // give award to user
                case 5:
                {
                    ShowAllUsers();
                    Console.WriteLine("Select User to give Award to.");
                    User user = _userLogic.GetById(GetId());
                    if (user == null)
                    {
                        Console.WriteLine(NO_USER_ID);
                        break;
                    }

                    ShowAllAwards();
                    Console.WriteLine("Select Award to give to the User.");
                    Award award = _awardLogic.GetById(GetId());
                    if (award == null)
                    {
                        Console.WriteLine(NO_AWARD_ID);
                        break;
                    }

                    if (_userLogic.GiveAward(user.Id, award.Id))
                    {
                        _awardLogic.AddUserToAward(awardId: award.Id, userId: user.Id);
                        Console.WriteLine($"The Award \"{award.Title}\" has been given to the User \"{user.Name}\".");
                    }
                    else
                    {
                        Console.WriteLine($"Can't give the Award \"{award.Title}\" to the User \"{user.Name}\".");
                    }
                    Console.ReadLine();
                    break;
                }

                // take award from user
                case 6:
                {
                    ShowAllUsers();
                    Console.WriteLine("Select User to take Award from.");
                    User user = _userLogic.GetById(GetId());
                    if (user == null)
                    {
                        Console.WriteLine(NO_USER_ID);
                        break;
                    }

                    ShowAllAwards();
                    Console.WriteLine("Select Award to take from the User.");
                    Award award = _awardLogic.GetById(GetId());
                    if (award == null)
                    {
                        Console.WriteLine(NO_AWARD_ID);
                        break;
                    }

                    if (_userLogic.TakeAwayAward(user.Id, award.Id))
                    {
                        _awardLogic.RemoveUserFromAward(awardId: award.Id, userId: user.Id);
                        Console.WriteLine($"The Award \"{award.Title}\" has been taken from the User \"{user.Name}\".");
                    }
                    else
                    {
                        Console.WriteLine($"Can't take the Award \"{award.Title}\" from the User \"{user.Name}\".");
                    }
                    Console.ReadLine();
                    break;
                }

                // exit
                case 0:
                    break;

                default:
                {
                    Console.WriteLine("Wrong number. Try again.");
                    Console.ReadLine();
                    break;
                }
                }

                if (selectUser == 0)
                {
                    break;
                }
                Console.Clear();
            }
        }