Advertisement
Guest User

Untitled

a guest
May 14th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Task_1._4._1
  8. {
  9.     class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             // Ничего не вывело. Я так думаю, при присвоении переменной testUser пустой ссылки, коллектор собирает мусор не сразу.
  14.             User testUser = new User("Andy", "Prok", "lolok");
  15.             Console.WriteLine(testUser.ToString());
  16.             testUser = null;
  17.             GC.Collect();
  18.  
  19.             UserDB Users = new UserDB();
  20.             User currentUser = null;
  21.             string help_commands = "\n{param} - optional param, [param] - not optional param\nadduser {first name} {second name} {password}\nlogin {first name} {second name} {password}\nlist -- to see all users\naddnote {note} [book id] -- to add note in exist book or creating new book with the first note\nshowallnotes -- to look all notes in books\nbooks -- id of all books are exist";
  22.             string firstName, secondName, password,fullName;
  23.             string cmd = "";
  24.             while(cmd != "exit")
  25.             {
  26.                 Console.Write("BookNotes>> ");
  27.                 string[] data;
  28.                 try
  29.                 {
  30.                     cmd = Console.ReadLine();
  31.                     data = cmd.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  32.                     cmd = data[0];
  33.                 }
  34.                 catch(IndexOutOfRangeException ex)
  35.                 {
  36.                     Console.WriteLine("Вы ввели пустую команду.");
  37.                     continue;
  38.                 }
  39.  
  40.                 if (cmd != "")
  41.                 {
  42.                     cmd = data[0];
  43.                     switch (cmd)
  44.                     {
  45.                         case "adduser":
  46.                             try
  47.                             {
  48.                                 firstName = data[1];
  49.                                 secondName = data[2];
  50.                                 password = data[3];
  51.                                 User user = new User(firstName, secondName, password);
  52.                                 Users.AddUser(user);
  53.                             }
  54.                             catch(IndexOutOfRangeException ex)
  55.                             {
  56.                                 Console.WriteLine("Вы неправильно ввели параметры. Введите команду help для справки.");
  57.                             }
  58.                             break;
  59.  
  60.                         case "login":
  61.                             try
  62.                             {
  63.                                 firstName = data[1];
  64.                                 secondName = data[2];
  65.                                 password = data[3];
  66.                                 fullName = firstName + " " + secondName;
  67.                                 if (Users.Login(fullName, password))
  68.                                 {
  69.                                     if(currentUser != null)
  70.                                     {
  71.                                         currentUser.IsLogin = false;
  72.                                     }
  73.                                     currentUser = Users.GetUser(fullName, password);
  74.                                 }
  75.                             }
  76.                             catch (IndexOutOfRangeException ex)
  77.                             {
  78.                                 Console.WriteLine("Вы неправильно ввели параметры. Введите команду help для справки.");
  79.                             }
  80.                             break;
  81.  
  82.                         case "list":
  83.                             Users.List();
  84.                             break;
  85.  
  86.                         case "addnote":
  87.                             if (currentUser != null)
  88.                             {
  89.                                 Note note = new Note();
  90.                                 int ret = 0;
  91.  
  92.                                 if (int.TryParse(data[data.Length - 1], out ret))
  93.                                 {
  94.                                     note.UserNote = note.ParseCmdToNote(data,true);
  95.                                     int id = ret;
  96.                                     currentUser.Books.AddNote(note, id);
  97.                                 }
  98.                                 else
  99.                                 {
  100.                                     note.UserNote = note.ParseCmdToNote(data,false);
  101.                                     currentUser.Books.AddNote(note);
  102.                                 }
  103.                             }
  104.                             else
  105.                             {
  106.                                 Console.WriteLine("Вы еще не вошли в систему.");
  107.                             }
  108.                             break;
  109.  
  110.                         case "showallnotes":
  111.                             currentUser.Books.ShowAllNotes();
  112.                             break;
  113.  
  114.                         case "books":
  115.                             currentUser.Books.AllBooks();
  116.                             break;
  117.  
  118.                         case "help":
  119.                             Console.WriteLine(help_commands);
  120.                             break;
  121.  
  122.                         default:
  123.                             Console.WriteLine("Введите одну из команд, описанных в help. Для этого напишите help в командной строке.");
  124.                             break;
  125.                     }
  126.                 }
  127.                 else
  128.                 {
  129.                     Console.WriteLine("Введите какую-нибудь команду");
  130.                 }
  131.             }
  132.         }
  133.     }
  134. }
  135.  
  136.  
  137.  
  138. using System;
  139. using System.Collections.Generic;
  140. using System.Linq;
  141. using System.Text;
  142. using System.Threading.Tasks;
  143.  
  144. namespace Task_1._4._1
  145. {
  146.     class UserDB
  147.     {
  148.         public User[] UserList;
  149.  
  150.         public UserDB()
  151.         {
  152.             UserList = new User[0];
  153.         }
  154.  
  155.         public void AddUser(User value)
  156.         {
  157.             if (!Contains(value.fullName,value.Password))
  158.             {
  159.                 User[] newArr = new User[UserList.Length + 1];
  160.                 for (int i = 0; i < newArr.Length - 1; i++)
  161.                 {
  162.                     newArr[i] = UserList[i];
  163.                 }
  164.  
  165.                 newArr[newArr.Length - 1] = value;
  166.                 UserList = newArr;
  167.                 newArr = null;
  168.                 GC.Collect();
  169.  
  170.                 Console.WriteLine("Пользователь успешно добавлен в базу данных");
  171.             }
  172.             else
  173.             {
  174.                 Console.WriteLine("Данный пользователь уже в базе данных");
  175.             }
  176.         }
  177.  
  178.         public bool Login(string fullName, string pass)
  179.         {
  180.             if(Contains(fullName,pass))
  181.             {
  182.                 User user = GetUser(fullName,pass);
  183.                 if(!user.IsLogin & user.Password == pass)
  184.                 {
  185.                     user.IsLogin = true;
  186.                     Console.WriteLine("Вы вошли в систему");
  187.                 }
  188.                 else
  189.                 {
  190.                     Console.WriteLine("Такой юзер уже залогинен");
  191.                 }
  192.  
  193.                 return true;
  194.             }
  195.             else
  196.             {
  197.                 Console.WriteLine("Такого пользователя не существует.");
  198.                 return false;
  199.             }
  200.         }
  201.  
  202.         private bool Contains(string fullName, string pass)
  203.         {
  204.             bool userIs = false;
  205.             for (int i = 0; i < UserList.Length; i++)
  206.             {
  207.                 if (UserList[i].fullName == fullName &&
  208.                    UserList[i].Password == pass)
  209.                 {
  210.                     userIs = true;
  211.                 }
  212.             }
  213.  
  214.             return userIs;
  215.         }
  216.  
  217.         public User GetUser(string fullName, string pass)
  218.         {
  219.             for (int i = 0; i < UserList.Length; i++)
  220.             {
  221.                 if (UserList[i].fullName == fullName &&
  222.                    UserList[i].Password == pass)
  223.                 {
  224.                     return UserList[i];
  225.                 }
  226.             }
  227.  
  228.             return null;
  229.         }
  230.  
  231.         public void List()
  232.         {
  233.             for(int i = 0; i < UserList.Length; i++)
  234.             {
  235.                 Console.WriteLine("{0} - {1}", i + 1, UserList[i].ToString());
  236.             }
  237.         }      
  238.     }
  239. }
  240.  
  241.  
  242. using System;
  243. using System.Collections.Generic;
  244. using System.Linq;
  245. using System.Text;
  246. using System.Threading.Tasks;
  247.  
  248. namespace Task_1._4._1
  249. {
  250.     class User
  251.     {
  252.             public string firstName;
  253.             public string secondName;
  254.             public string fullName;
  255.             private string _password;
  256.             private bool _isLogin;
  257.             public Book Books;
  258.  
  259.             public User(string fName, string sName, string pass)
  260.             {
  261.                 this.firstName = fName;
  262.                 this.secondName = sName;
  263.                 this.fullName = fName + " " + sName;
  264.                 this._password = pass;
  265.                 this._isLogin = false;
  266.                 Books = new Book();
  267.             }      
  268.  
  269.             public bool IsLogin
  270.             {
  271.                 get { return _isLogin;}
  272.                 set { _isLogin = value;}
  273.             }
  274.  
  275.             public string Password
  276.             {
  277.                 get
  278.                 {
  279.                     return _password;
  280.                 }
  281.             }
  282.  
  283.             public override string ToString()
  284.             {
  285.                 return firstName + " " + secondName;
  286.             }
  287.  
  288.             ~User()
  289.             {
  290.                 Console.WriteLine("Юзер {0} удален.", fullName);
  291.             }
  292.              
  293.     }
  294. }
  295.  
  296.  
  297.  
  298. using System;
  299. using System.Collections.Generic;
  300. using System.Linq;
  301. using System.Text;
  302. using System.Threading.Tasks;
  303.  
  304. namespace Task_1._4._1
  305. {
  306.     class Note
  307.     {
  308.         private string _userNote = "";
  309.  
  310.         public Note()
  311.         {
  312.  
  313.         }
  314.  
  315.         public Note(string note)
  316.         {
  317.             this._userNote = note;
  318.         }
  319.  
  320.         public string ParseCmdToNote(string[] cmd, bool isID)
  321.         {
  322.             StringBuilder res = new StringBuilder();
  323.             int ret = 0;
  324.             if (isID)
  325.             {
  326.                 for (int i = 1; i < cmd.Length - 1; i++)
  327.                 {
  328.                     res.Append(cmd[i] + " ");
  329.                 }
  330.             }
  331.             else
  332.             {
  333.                 for (int i = 1; i < cmd.Length; i++)
  334.                 {
  335.                     res.Append(cmd[i] + " ");
  336.                 }
  337.             }
  338.             return res.ToString();
  339.         }
  340.  
  341.         public string UserNote
  342.         {
  343.             get;
  344.             set;
  345.         }
  346.     }
  347. }
  348.  
  349.  
  350. using System;
  351. using System.Collections.Generic;
  352. using System.Linq;
  353. using System.Text;
  354. using System.Threading.Tasks;
  355.  
  356. namespace Task_1._4._1
  357. {
  358.     class Book
  359.     {
  360.         public Note[] Books;
  361.  
  362.         public Book()
  363.         {
  364.             Books = new Note[0];
  365.         }
  366.  
  367.         public void AddNote(Note value)
  368.         {
  369.             Note[] newArr = new Note[Books.Length + 1];
  370.             for (int i = 0; i < newArr.Length - 1; i++)
  371.             {
  372.                 newArr[i] = Books[i];
  373.             }
  374.  
  375.             newArr[newArr.Length - 1] = value;
  376.             Books = newArr;
  377.             newArr = null;
  378.             GC.Collect();
  379.         }
  380.  
  381.         public void AddNote(Note value, int ind)
  382.         {
  383.             if (ind < Books.Length)
  384.             {
  385.                 Books[ind].UserNote += "\n\n" + value.UserNote;
  386.             }
  387.             else
  388.             {
  389.                 Console.WriteLine("Error: ID такой книги не существует. Пожалуйста добавьте новую книгу, начав с новой записки или укажите верный ID. Посмотреть валидные ID можно командой books.");
  390.             }
  391.         }
  392.  
  393.         public void ShowAllNotes()
  394.         {
  395.             if (Books.Length != 0)
  396.             {
  397.                 int i = 0;
  398.                 foreach (var n in this.Books)
  399.                 {
  400.                     Console.WriteLine("{0}: {1}", i, n.UserNote);
  401.                     i++;
  402.                 }
  403.             }
  404.             else
  405.             {
  406.                 Console.WriteLine("Еще не создано ни одной книги");
  407.             }
  408.         }
  409.  
  410.         public void AllBooks()
  411.         {
  412.             if (Books.Length != 0)
  413.             {
  414.                 for (int i = 0; i < Books.Length; i++)
  415.                 {
  416.                     Console.Write(i + " ");
  417.                 }
  418.  
  419.                 Console.WriteLine();
  420.             }
  421.             else
  422.             {
  423.                 Console.WriteLine("Еще не создано ни одной книги");
  424.             }
  425.         }
  426.     }
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement