Advertisement
Guest User

Untitled

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