Beispiel #1
0
        private List <Award> ReadAwardsIdFromConsole()
        {
            int          awardId = -1;
            List <Award> awards  = new List <Award>();

            string[] awardsIdString = Console.ReadLine().Split(' ');

            while (awardsIdString.Length == 0 || awardsIdString == null)
            {
                Console.WriteLine("Invalid input format! Please enter again: ");
                awardsIdString = Console.ReadLine().Split(' ');
            }

            foreach (var item in awardsIdString)
            {
                if (Int32.TryParse(item, out awardId))
                {
                    try
                    {
                        var award = _awardLogic.GetById(awardId);
                        awards.Add(award);
                    }
                    catch (ArgumentOutOfRangeException ex)
                    {
                        Console.WriteLine($"{Environment.NewLine}Id = {awardId}: {ex.Message}!");
                    }
                }
            }

            return(awards);
        }
Beispiel #2
0
 private static void ShowAwardById(IAwardLogic awardLogic)
 {
     Console.Write("Enter award ID: ");
     try
     {
         int id = int.Parse(Console.ReadLine());
         Console.WriteLine(awardLogic.GetById(id));
     }
     catch
     {
         Console.WriteLine($"ERROR. Wrong ID!{Environment.NewLine}");
     }
 }
Beispiel #3
0
        public IEnumerable <Award> GetAwardsFor(int id, IAwardLogic awards)
        {
            var awardList = new List <Award>();
            var awardIds  = GetAwardIdsFor(id);

            if (awardIds == null)
            {
                return(awardList);
            }
            foreach (var i in awardIds)
            {
                awardList.Add(awards.GetById(i));
            }
            return(awardList);
        }
Beispiel #4
0
        private static void ShowUsersWithAwards()
        {
            var users = _userLogic.GetAll();

            Console.WriteLine("Users with awards: ");
            foreach (var user in users)
            {
                Console.WriteLine(user);
                Console.WriteLine("Awards: ");
                foreach (var awardId in user.Awards)
                {
                    Console.WriteLine(_awardLogic.GetById(awardId));
                }
                Console.WriteLine();
            }
        }
Beispiel #5
0
        private static void ShowDetails(IUserLogic userLogic, IAwardLogic awardLogic, IUserAwardLogic userAwardLogic)
        {
            Console.WriteLine();
            int id = ReadInt("Enter the Id of user: "******"Detailed information about selected user:"******"User Id: {0}", selectedUser.Id);
                Console.WriteLine("User Name: {0}", selectedUser.Name);
                Console.WriteLine("User date of birth: {0:D}", selectedUser.DateOfBirth);
                Console.WriteLine("User age: {0}", selectedUser.Age);
                Console.WriteLine();

                if (userAwardLogic.GetAll().Where(x => x.UserId == selectedUser.Id).Count() == 0)
                {
                    Console.WriteLine("User \"{0}\" has not any awards.", selectedUser.Name);
                }
                else
                {
                    Console.WriteLine("The awards list of \"{0}\":", selectedUser.Name);
                    foreach (var item in userAwardLogic.GetAll())
                    {
                        if (item.UserId == selectedUser.Id)
                        {
                            Console.WriteLine(awardLogic.GetById(item.AwardId).Title);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("It's not possible to show any information " +
                                  "about awards of selected user because {0}. One of the reasons is " +
                                  "that no one of users never get any award. Or may be somebody removed file with " +
                                  "information about it.", ex.Message);
            }

            Console.WriteLine();
            Init();
        }
Beispiel #6
0
        private static void ShowUsers(IUserLogic userLogic, IAwardLogic awardLogic)
        {
            Console.WriteLine("Id|Name|Age");
            foreach (var user in userLogic.GetAll())
            {
                Console.WriteLine(user);
                Console.Write($"User's awards:");
                Dictionary <int, List <int> > awardId_UsersIDs = awardLogic.GetDictOfAwardsAndUsers();
                foreach (var item in awardId_UsersIDs)
                {
                    if (item.Value.Contains(user.Id))
                    {
                        Console.Write(awardLogic.GetById(item.Key));
                    }
                }

                Console.WriteLine();
            }
        }
Beispiel #7
0
        private static void UserMode()
        {
            while (true)
            {
                Menu();
                if (!int.TryParse(Console.ReadLine(), out int mode))
                {
                    Console.WriteLine("Incorrect input. Try again.");
                    continue;
                }

                switch (mode)
                {
                case 1:
                {
                    User user = CreateUser();
                    _userLogic.Add(user);
                    Console.WriteLine($"User added. User  {user.Id} : {user.Name}");
                    Console.WriteLine($"Press any key to continue");
                    Console.ReadLine();
                    break;
                }

                case 2:
                {
                    int  id   = GetId();
                    User user = _userLogic.GetById(id);
                    ShowUser(user);
                    Console.WriteLine($"Press any key to continue");
                    Console.ReadLine();
                    break;
                }

                case 3:
                {
                    ShowAllUsers();
                    Console.WriteLine($"Press any key to continue");
                    Console.ReadLine();
                    break;
                }

                case 4:
                {
                    int id = GetId();
                    if (_userLogic.RemoveById(id))
                    {
                        Console.WriteLine($"User with ID = {id} is removed.");
                    }
                    else
                    {
                        Console.WriteLine("Wrong id.");
                    }
                    Console.WriteLine($"Press any key to continue");
                    Console.ReadLine();
                    break;
                }

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

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

                    if (_userLogic.AddAward(user.Id, award.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.WriteLine($"Press any key to continue");
                    Console.ReadLine();
                    break;
                }

                case 6:
                {
                    ShowAllUsers();
                    Console.WriteLine("Select User to remove Award");
                    User user = _userLogic.GetById(GetId());
                    if (user == null)
                    {
                        Console.WriteLine("Wrong id.");
                        break;
                    }

                    ShowAllAwards();
                    Console.WriteLine("Select Award to remove");
                    Award award = _awardLogic.GetById(GetId());
                    if (award == null)
                    {
                        Console.WriteLine("Wrong id.");
                        break;
                    }

                    if (_userLogic.RemoveAward(user.Id, award.Id))
                    {
                        Console.WriteLine($"The Award \"{award.Title}\" has been remove to the User \"{user.Name}\".");
                    }
                    else
                    {
                        Console.WriteLine($"Can't remove the Award \"{award.Title}\" to the User \"{user.Name}\".");
                    }
                    Console.WriteLine($"Press any key to continue");
                    Console.ReadLine();
                    break;
                }

                case 7:
                {
                    AwardMode();
                    break;
                }

                case 0:
                {
                    break;
                }

                default:
                {
                    Console.WriteLine("Wrong. Try again.");
                    Console.ReadLine();
                    break;
                }
                }
                if (mode == 0)
                {
                    break;
                }
                Console.Clear();
            }
        }
Beispiel #8
0
 public static Award GetAwardById(int id)
 {
     return(_awardLogic.GetById(id));
 }
Beispiel #9
0
        public static void UserMode()
        {
            while (true)
            {
                ConsoleDisplay.UserMenuDisplay();
                if (!int.TryParse(Console.ReadLine(), out int input))
                {
                    Console.WriteLine("Incorrect input. Try again!");
                    continue;
                }
                switch (input)
                {
                case 1:
                {
                    User user = CreateUser();
                    _userLogic.Add(user);
                    Console.WriteLine("User added with ID - {0}", user.Id);
                    Console.ReadLine();
                    break;
                }

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

                case 3:
                {
                    ShowAllUsers();
                    Console.ReadLine();
                    break;
                }

                case 4:
                {
                    int id = GetId();
                    if (_userLogic.RemoveById(id))
                    {
                        Console.WriteLine("User with ID - {0} is deleted.", id);
                    }
                    else
                    {
                        Console.WriteLine("User with this ID does not exist.");
                    }
                    Console.ReadLine();
                    break;
                }

                case 5:
                {
                    ShowAllUsers();
                    Console.WriteLine("Select user for rewarding.");
                    User user = _userLogic.GetById(GetId());
                    if (user == null)
                    {
                        Console.WriteLine("User with this ID does not exist.");
                        break;
                    }
                    Console.WriteLine("List of awards:");
                    ShowAllAwards();
                    Console.WriteLine("Select award for rewarding.");
                    Award award = _awardLogic.GetById(GetId());
                    if (award == null)
                    {
                        Console.WriteLine("Award with this ID does not exist.");
                        break;
                    }
                    if (_userLogic.GiveAward(user.Id, award))
                    {
                        Console.WriteLine("The award was successfully presented.");
                    }
                    else
                    {
                        Console.WriteLine("An error occurred during the presentation of award.");
                    }
                    Console.ReadLine();
                    break;
                }

                case 6:
                {
                    ShowAllUsers();
                    Console.WriteLine("Select user to take award from.");
                    User user = _userLogic.GetById(GetId());
                    if (user == null)
                    {
                        Console.WriteLine("User with this ID does not exist.");
                        break;
                    }
                    ShowAllAwards();
                    Console.WriteLine("Select award to take from user.");
                    Award award = _awardLogic.GetById(GetId());
                    if (award == null)
                    {
                        Console.WriteLine("Award with this ID does not exist.");
                        break;
                    }
                    if (_userLogic.TakeAwayAward(user.Id, award.Id))
                    {
                        _awardLogic.RemoveUserFromAward(awardId: award.Id, userId: user.Id);
                        Console.WriteLine("The award {0} has been taken from user {1}.", award.Title, user.Name);
                    }
                    else
                    {
                        Console.WriteLine("Can't take award.");
                    }
                    Console.ReadLine();
                    break;
                }

                case 0:
                    break;

                default:
                    break;
                }
                if (input == 0)
                {
                    break;
                }
                Console.Clear();
            }
        }
Beispiel #10
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();
            }
        }
Beispiel #11
0
        private static bool ProcessInput()
        {
            string command = ReadInput();

            string[] commandArgs = command.Split(separator,
                                                 StringSplitOptions.RemoveEmptyEntries);

            switch (commandArgs[0].ToLower())
            {
            case "add":
                switch (commandArgs[1].ToLower())
                {
                case "user":
                    userLogic.Add(new User()
                    {
                        Name        = ReadInput("Name"),
                        DateOfBirth = DateTime.Parse(ReadInput("Date of birth"))
                    });
                    break;

                case "award":
                    awardLogic.Add(new Award()
                    {
                        Title = ReadInput("Title")
                    });
                    break;

                default:
                    WriteLine("Usage: add <user/award>");
                    break;
                }
                break;

            case "remove":
                switch (commandArgs[1].ToLower())
                {
                case "user":
                    userLogic.Remove(int.Parse(commandArgs[2]));
                    break;

                case "award":
                    awardLogic.Remove(int.Parse(commandArgs[2]));
                    break;

                default:
                    WriteLine("Usage: remove <user/award>");
                    break;
                }
                break;

            case "list":
                switch (commandArgs[1].ToLower())
                {
                case "users":
                    foreach (var entry in userLogic.GetAll())
                    {
                        WriteLine(entry.ToString());
                        foreach (var award in userLogic.GetAwardsFor(entry.Id, awardLogic))
                        {
                            Write(" - ");
                            WriteLine(award.ToString());
                        }
                    }
                    break;

                case "awards":
                    foreach (var entry in awardLogic.GetAll())
                    {
                        WriteLine(entry.ToString());
                    }
                    break;

                default:
                    WriteLine("Usage: list <users/awards>");
                    break;
                }
                break;

            case "award":
                switch (commandArgs[1].ToLower())
                {
                case "assign":
                    userLogic.AddAward(int.Parse(commandArgs[2]),
                                       awardLogic.GetById(int.Parse(commandArgs[3])));
                    break;

                case "revoke":
                    userLogic.RemoveAward(int.Parse(commandArgs[2]),
                                          awardLogic.GetById(int.Parse(commandArgs[3])));
                    break;

                default:
                    WriteLine("Usage: award <assign/revoke> (userId) (awardId)");
                    break;
                }
                break;

            case "exit":
                return(false);
            }
            return(true);
        }
Beispiel #12
0
        public static void Run(IUserLogic userLogic, IAwardLogic awardLogic)
        {
            for (; ;)
            {
                Console.Clear();
                Console.WriteLine($"Use commands:\nMode - change data mode\nGetAllUsers - show all users\nUser - show user with current id\nDeleteUser - delete user with current id\nAddUser - add new user");
                Console.WriteLine($"GetAllAwards - show all Awards\nAward - show user with current id\nDeleteAward -delete user with current id\nAddAward - add new user\nExit - quit from application");
                string choose = Console.ReadLine();
                if (choose == "Exit")
                {
                    return;
                }
                switch (choose)
                {
                case "Mode":
                    Console.WriteLine($"Current mode {DataMode.GetLogic("DataMode")} will changed");
                    DataMode.SwitchLogic("DataMode");
                    Console.WriteLine($"New mode {DataMode.GetLogic("DataMode")}. This mode will work after restart of application");
                    Console.ReadKey();
                    break;

                case "GetAllUsers":
                    foreach (var item in userLogic.GetAll())
                    {
                        Console.WriteLine(item);
                    }
                    Console.ReadKey();
                    break;

                case "GetAllAwards":
                    foreach (var item in awardLogic.GetAll())
                    {
                        Console.WriteLine(item);
                    }
                    Console.ReadKey();
                    break;

                case "User":
                    Console.WriteLine("Enter user id");
                    uint.TryParse(Console.ReadLine(), out _userId);
                    Console.WriteLine(userLogic.GetById(_userId));
                    if (userLogic.GetUserAwards(_userId).Any())
                    {
                        Console.WriteLine("Awards:");
                        foreach (uint item in userLogic.GetUserAwards(_userId))
                        {
                            Console.WriteLine(awardLogic.GetById(item));
                        }
                    }
                    Console.ReadKey();
                    break;

                case "Award":
                    Console.WriteLine("Enter award id");
                    uint.TryParse(Console.ReadLine(), out _awardId);
                    Console.WriteLine(awardLogic.GetById(_awardId));
                    Console.ReadKey();
                    break;

                case "DeleteUser":
                    Console.WriteLine("Enter user id");
                    uint.TryParse(Console.ReadLine(), out _userId);
                    if (userLogic.DeleteById(_userId))
                    {
                        Console.WriteLine($"User with id = {_userId} deleted successfully!");
                    }
                    else
                    {
                        Console.WriteLine($"Can't delete user with id = {_userId}!");
                    }
                    Console.ReadKey();
                    break;

                case "DeleteAward":
                    Console.WriteLine("Enter award id");
                    uint.TryParse(Console.ReadLine(), out _awardId);
                    if (awardLogic.DeleteById(_awardId))
                    {
                        Console.WriteLine($"Award with id = {_awardId} deleted successfully!");
                    }
                    else
                    {
                        Console.WriteLine($"Can't delete award with id = {_awardId}!");
                    }
                    Console.ReadKey();
                    break;

                case "AddUser":
                    Console.WriteLine("Enter new user name");
                    string _newUserName = Console.ReadLine();
                    Console.WriteLine("Enter new user birthday");
                    DateTime.TryParse(Console.ReadLine(), out DateTime _userBirthday);
                    if (userLogic.Add(new User()
                    {
                        Name = _newUserName, DateOfBirth = _userBirthday
                    }))
                    {
                        Console.WriteLine("User added successfully!");
                    }
                    else
                    {
                        Console.WriteLine("Can't create new user!");
                    }
                    Console.ReadKey();
                    break;

                case "AddAward":
                    Console.WriteLine("Enter new award name");
                    string _newAwardName = Console.ReadLine();
                    if (awardLogic.Add(new Award()
                    {
                        Name = _newAwardName
                    }))
                    {
                        Console.WriteLine("Award added successfully!");
                    }
                    else
                    {
                        Console.WriteLine("Can't create new award!");
                    }
                    Console.ReadKey();
                    break;

                default:
                    break;
                }
            }
        }