Advertisement
Guest User

Untitled

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