Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _1._4._1Try2
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             UserRepository repository = new UserRepository();
  15.             AuthorizeProvider authorize = new AuthorizeProvider(repository);
  16.             repository.Add(new User());
  17.             repository.Add(new User());
  18.             repository.Add(new User());
  19.  
  20.             authorize.AuthorizeDialog();
  21.             if (authorize.IsAuthorize)
  22.             {
  23.  
  24.             }
  25.             else
  26.             {
  27.  
  28.             }
  29.  
  30.             ///////
  31.             User user = new User();
  32.             var Exit = true;
  33.             do
  34.             {
  35.                 TextCommands();
  36.                 Console.WriteLine("\nВведите команду: ");
  37.                 bool userAutorize = false;
  38.                 string сommand = Console.ReadLine();
  39.                 switch (сommand)
  40.                 {
  41.                     case "Y":
  42.                         Console.WriteLine("Вы нажали букву Y");
  43.                         break;
  44.  
  45.                     case "1":
  46.                         Console.WriteLine("Добавить пользователя:");
  47.                         Console.WriteLine("Введите Логин & Пароль нового пользователя.");
  48.                         Console.Write("Login:");
  49.                         var login = Console.ReadLine();
  50.                         Console.Write("Password:");
  51.                         var password = Console.ReadLine();
  52.                         //User newUser = new User(iden, login, password);
  53.                         //newUser.AddUser(newUser);
  54.                         user.AddUser(new User(login, password));
  55.                         //Console.WriteLine("\nUsers:");
  56.                         //user.DisplayUsers();
  57.                         break;
  58.                     case "2":
  59.                         Console.WriteLine("Авторизация");
  60.                         Console.WriteLine("Введите Логин");
  61.                         var loginOn = Console.ReadLine();
  62.                         Console.WriteLine("Введите пароль");
  63.                         var passwordOn = Console.ReadLine();
  64.  
  65.                         userAutorize = user.Autorization(loginOn, passwordOn);
  66.  
  67.                         break;
  68.                     case "3":
  69.                         user.DisplayUsersCommand();
  70.                         break;
  71.                     case "0":
  72.                         Exit = false;
  73.                         break;
  74.                     default:
  75.                         Console.WriteLine("Вы ввели неизвестную команду или команда была введена некорректно.");
  76.                         break;
  77.                 }
  78.  
  79.                 //Exit = (сommand == "0") ? false : true;
  80.             } while (Exit == true);
  81.  
  82.             Console.WriteLine("\nВыход.");
  83.             //Console.ReadLine();
  84.         }
  85.  
  86.         static void TextCommands()
  87.         {
  88.             Console.WriteLine("\nОсновные команды консоли:");
  89.  
  90.             Console.WriteLine("1 - Добавить пользователя.");
  91.             Console.WriteLine("2 - Авторизация.");
  92.             Console.WriteLine("3 - Вывести пользователей.");
  93.             Console.WriteLine("0 - Выход.");
  94.  
  95.         }
  96.     }
  97.  
  98.     class UserRepository
  99.     {
  100.         private List<User> _users;
  101.  
  102.         public UserRepository()
  103.         {
  104.             _users = new List<User>();
  105.         }
  106.  
  107.         public void Add(User user)
  108.         {
  109.             _users.Add(user);
  110.         }
  111.  
  112.         public User GetFromLogin(string login)
  113.         {
  114.             return _users.Find((x) => x.Name == login);
  115.         }
  116.     }
  117.  
  118.     class AuthorizeProvider
  119.     {
  120.         private UserRepository _repository;
  121.         private User _authorized;
  122.         public bool IsAuthorize
  123.         {
  124.             get
  125.             {
  126.                 return _authorized != null;
  127.             }
  128.         }
  129.  
  130.         public AuthorizeProvider(UserRepository repository)
  131.         {
  132.             _repository = repository;
  133.         }
  134.  
  135.         public User GetUser()
  136.         {
  137.             return _authorized;
  138.         }
  139.  
  140.         public void AuthorizeDialog()
  141.         {
  142.             Console.WriteLine("Авторизация");
  143.             Console.WriteLine("Введите Логин");
  144.             var loginOn = Console.ReadLine();
  145.             Console.WriteLine("Введите пароль");
  146.             var passwordOn = Console.ReadLine();
  147.  
  148.             var user = _repository.GetFromLogin(loginOn);
  149.             if (user != null)
  150.             {
  151.                 if(user.Pass == passwordOn)
  152.                 {
  153.                     _authorized = user;
  154.                     Console.WriteLine("Вы авторизовались!");
  155.                 }
  156.                 else
  157.                 {
  158.                     Console.WriteLine("Пароль не верный!");
  159.                 }
  160.             }
  161.             else
  162.             {
  163.                 Console.WriteLine("Пользователь с таким логином отсутствуе!");
  164.             }
  165.         }
  166.     }
  167.    
  168.     class User
  169.     {
  170.         public static int Count = 0;
  171.  
  172.         private int Id { get; set; }
  173.         public string Name { get; set; }
  174.         public string Pass { get; set; }
  175.         private List<User> _users = new List<User>();
  176.  
  177.         public bool Autorization(string login, string password)
  178.         {
  179.             Console.WriteLine("Авторизация: " + login);
  180.             foreach (User i in _users)
  181.             {
  182.                 if (i.Name.Contains(login) && i.Pass.Contains(password))
  183.                 {
  184.                     Console.WriteLine("Содержит имя и пароль, осталось это вернуть");
  185.                     return true;
  186.                 }
  187.                 else
  188.                 {
  189.                     Console.WriteLine("Логин или пароль введен неверно");
  190.                 }
  191.             }
  192.  
  193.             return false;
  194.         }
  195.         public void AddUser(User u)
  196.         {
  197.             _users.Add(u);
  198.         }
  199.         public void DisplayUsers()
  200.         {
  201.             foreach (User i in _users)
  202.             {
  203.                 i.DisplayUser();
  204.             }
  205.         }
  206.         public void DisplayUsersCommand()
  207.         {
  208.             //Сделать если лист пустой, то выводится текст, что лист пустой иначе выполняется команда
  209.             Console.WriteLine("Пользователи:");
  210.             foreach (User i in _users)
  211.             {
  212.                 Console.WriteLine("Id: " + i.Id + "; Name: " + i.Name);
  213.             }
  214.         }
  215.  
  216.         ////Конструктор
  217.         public User()
  218.         { }
  219.  
  220.         public User(string name, string pass)
  221.         {
  222.             Count++;
  223.             Id = Count;
  224.             Name = name;
  225.             Pass = pass;
  226.             Console.WriteLine("Пользователь создан.");
  227.             //DisplayUser();
  228.  
  229.  
  230.         }
  231.         public void DisplayUser()
  232.         {
  233.             Console.WriteLine("Id: {0}, Login: {1}, Password: {2}.", Id, Name, Pass);
  234.         }
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement