Advertisement
Guest User

Untitled

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