Advertisement
Guest User

Untitled

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