Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace _1._4._1Try2
- {
- class Program
- {
- static void Main(string[] args)
- {
- UserRepository repository = new UserRepository();
- AuthorizeProvider authorize = new AuthorizeProvider(repository);
- repository.Add(new User());
- repository.Add(new User());
- repository.Add(new User());
- authorize.AuthorizeDialog();
- if (authorize.IsAuthorize)
- {
- }
- else
- {
- }
- ///////
- User user = new User();
- var Exit = true;
- do
- {
- TextCommands();
- Console.WriteLine("\nВведите команду: ");
- bool userAutorize = false;
- string сommand = Console.ReadLine();
- switch (сommand)
- {
- case "Y":
- Console.WriteLine("Вы нажали букву Y");
- break;
- case "1":
- Console.WriteLine("Добавить пользователя:");
- Console.WriteLine("Введите Логин & Пароль нового пользователя.");
- Console.Write("Login:");
- var login = Console.ReadLine();
- Console.Write("Password:");
- var password = Console.ReadLine();
- //User newUser = new User(iden, login, password);
- //newUser.AddUser(newUser);
- user.AddUser(new User(login, password));
- //Console.WriteLine("\nUsers:");
- //user.DisplayUsers();
- break;
- case "2":
- Console.WriteLine("Авторизация");
- Console.WriteLine("Введите Логин");
- var loginOn = Console.ReadLine();
- Console.WriteLine("Введите пароль");
- var passwordOn = Console.ReadLine();
- userAutorize = user.Autorization(loginOn, passwordOn);
- break;
- case "3":
- user.DisplayUsersCommand();
- break;
- case "0":
- Exit = false;
- break;
- default:
- Console.WriteLine("Вы ввели неизвестную команду или команда была введена некорректно.");
- break;
- }
- //Exit = (сommand == "0") ? false : true;
- } while (Exit == true);
- Console.WriteLine("\nВыход.");
- //Console.ReadLine();
- }
- static void TextCommands()
- {
- Console.WriteLine("\nОсновные команды консоли:");
- Console.WriteLine("1 - Добавить пользователя.");
- Console.WriteLine("2 - Авторизация.");
- Console.WriteLine("3 - Вывести пользователей.");
- Console.WriteLine("0 - Выход.");
- }
- }
- class UserRepository
- {
- private List<User> _users;
- public UserRepository()
- {
- _users = new List<User>();
- }
- public void Add(User user)
- {
- _users.Add(user);
- }
- public User GetFromLogin(string login)
- {
- return _users.Find((x) => x.Name == login);
- }
- }
- class AuthorizeProvider
- {
- private UserRepository _repository;
- private User _authorized;
- public bool IsAuthorize
- {
- get
- {
- return _authorized != null;
- }
- }
- public AuthorizeProvider(UserRepository repository)
- {
- _repository = repository;
- }
- public User GetUser()
- {
- return _authorized;
- }
- public void AuthorizeDialog()
- {
- Console.WriteLine("Авторизация");
- Console.WriteLine("Введите Логин");
- var loginOn = Console.ReadLine();
- Console.WriteLine("Введите пароль");
- var passwordOn = Console.ReadLine();
- var user = _repository.GetFromLogin(loginOn);
- if (user != null)
- {
- if(user.Pass == passwordOn)
- {
- _authorized = user;
- Console.WriteLine("Вы авторизовались!");
- }
- else
- {
- Console.WriteLine("Пароль не верный!");
- }
- }
- else
- {
- Console.WriteLine("Пользователь с таким логином отсутствуе!");
- }
- }
- }
- class User
- {
- public static int Count = 0;
- private int Id { get; set; }
- public string Name { get; set; }
- public string Pass { get; set; }
- private List<User> _users = new List<User>();
- public bool Autorization(string login, string password)
- {
- Console.WriteLine("Авторизация: " + login);
- foreach (User i in _users)
- {
- if (i.Name.Contains(login) && i.Pass.Contains(password))
- {
- Console.WriteLine("Содержит имя и пароль, осталось это вернуть");
- return true;
- }
- else
- {
- Console.WriteLine("Логин или пароль введен неверно");
- }
- }
- return false;
- }
- public void AddUser(User u)
- {
- _users.Add(u);
- }
- public void DisplayUsers()
- {
- foreach (User i in _users)
- {
- i.DisplayUser();
- }
- }
- public void DisplayUsersCommand()
- {
- //Сделать если лист пустой, то выводится текст, что лист пустой иначе выполняется команда
- Console.WriteLine("Пользователи:");
- foreach (User i in _users)
- {
- Console.WriteLine("Id: " + i.Id + "; Name: " + i.Name);
- }
- }
- ////Конструктор
- public User()
- { }
- public User(string name, string pass)
- {
- Count++;
- Id = Count;
- Name = name;
- Pass = pass;
- Console.WriteLine("Пользователь создан.");
- //DisplayUser();
- }
- public void DisplayUser()
- {
- Console.WriteLine("Id: {0}, Login: {1}, Password: {2}.", Id, Name, Pass);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement