Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task_1._4._1
- {
- class Program
- {
- public static void Main(string[] args)
- {
- UserDB Users = new UserDB();
- User currentUser = null;
- 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";
- string firstName, secondName, password,fullName;
- string cmd = "";
- while(cmd != "exit")
- {
- Console.Write("BookNotes>> ");
- cmd = Console.ReadLine();
- string[] data = cmd.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- if(data[0] != "")
- {
- cmd = data[0];
- switch (cmd)
- {
- case "adduser":
- try
- {
- firstName = data[1];
- secondName = data[2];
- password = data[3];
- User user = new User(firstName, secondName, password);
- Users.AddUser(user);
- }
- catch(IndexOutOfRangeException ex)
- {
- Console.WriteLine("Вы неправильно ввели параметры. Введите команду help для справки.");
- }
- break;
- case "login":
- try
- {
- firstName = data[1];
- secondName = data[2];
- password = data[3];
- fullName = firstName + " " + secondName;
- if (Users.Login(fullName, password))
- {
- if(currentUser != null)
- {
- currentUser.IsLogin = false;
- }
- currentUser = Users.GetUser(fullName, password);
- }
- }
- catch (IndexOutOfRangeException ex)
- {
- Console.WriteLine("Вы неправильно ввели параметры. Введите команду help для справки.");
- }
- break;
- case "list":
- Users.List();
- break;
- case "addnote":
- if (currentUser != null)
- {
- Note note = new Note();
- int ret = 0;
- if (int.TryParse(data[data.Length - 1], out ret))
- {
- note.UserNote = note.ParseCmdToNote(data,true);
- int id = ret;
- currentUser.Books.AddNote(note, id);
- }
- else
- {
- note.UserNote = note.ParseCmdToNote(data,false);
- currentUser.Books.AddNote(note);
- }
- }
- else
- {
- Console.WriteLine("Вы еще не вошли в систему.");
- }
- break;
- case "showallnotes":
- currentUser.Books.ShowAllNotes();
- break;
- case "books":
- currentUser.Books.AllBooks();
- break;
- case "help":
- Console.WriteLine(help_commands);
- break;
- default:
- Console.WriteLine("Введите одну из команд, описанных в help. Для этого напишите help в командной строке.");
- break;
- }
- }
- else
- {
- Console.WriteLine("Введите какую-нибудь команду");
- }
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task_1._4._1
- {
- class UserDB
- {
- public User[] UserList;
- public UserDB()
- {
- UserList = new User[0];
- }
- public void AddUser(User value)
- {
- if (!Contains(value.fullName,value.Password))
- {
- User[] newArr = new User[UserList.Length + 1];
- for (int i = 0; i < newArr.Length - 1; i++)
- {
- newArr[i] = UserList[i];
- }
- newArr[newArr.Length - 1] = value;
- UserList = newArr;
- newArr = null;
- GC.Collect();
- Console.WriteLine("Пользователь успешно добавлен в базу данных");
- }
- else
- {
- Console.WriteLine("Данный пользователь уже в базе данных");
- }
- }
- public bool Login(string fullName, string pass)
- {
- if(Contains(fullName,pass))
- {
- User user = GetUser(fullName,pass);
- if(!user.IsLogin & user.Password == pass)
- {
- user.IsLogin = true;
- Console.WriteLine("Вы вошли в систему");
- }
- else
- {
- Console.WriteLine("Такой юзер уже залогинен");
- }
- return true;
- }
- else
- {
- Console.WriteLine("Такого пользователя не существует.");
- return false;
- }
- }
- private bool Contains(string fullName, string pass)
- {
- bool userIs = false;
- for (int i = 0; i < UserList.Length; i++)
- {
- if (UserList[i].fullName == fullName &&
- UserList[i].Password == pass)
- {
- userIs = true;
- }
- }
- return userIs;
- }
- public User GetUser(string fullName, string pass)
- {
- for (int i = 0; i < UserList.Length; i++)
- {
- if (UserList[i].fullName == fullName &&
- UserList[i].Password == pass)
- {
- return UserList[i];
- }
- }
- return null;
- }
- public void List()
- {
- for(int i = 0; i < UserList.Length; i++)
- {
- Console.WriteLine("{0} - {1}", i + 1, UserList[i].ToString());
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task_1._4._1
- {
- class User
- {
- public string firstName;
- public string secondName;
- public string fullName;
- private string _password;
- private bool _isLogin;
- public Book Books;
- public User(string fName, string sName, string pass)
- {
- this.firstName = fName;
- this.secondName = sName;
- this.fullName = fName + " " + sName;
- this._password = pass;
- this._isLogin = false;
- Books = new Book();
- }
- public bool IsLogin
- {
- get { return _isLogin;}
- set { _isLogin = value;}
- }
- public string Password
- {
- get
- {
- return _password;
- }
- }
- public override string ToString()
- {
- return firstName + " " + secondName;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task_1._4._1
- {
- class Note
- {
- private string _userNote = "";
- public Note()
- {
- }
- public Note(string note)
- {
- this._userNote = note;
- }
- public string ParseCmdToNote(string[] cmd, bool isID)
- {
- StringBuilder res = new StringBuilder();
- int ret = 0;
- if (isID)
- {
- for (int i = 1; i < cmd.Length - 1; i++)
- {
- res.Append(cmd[i] + " ");
- }
- }
- else
- {
- for (int i = 1; i < cmd.Length; i++)
- {
- res.Append(cmd[i] + " ");
- }
- }
- return res.ToString();
- }
- public string UserNote
- {
- get;
- set;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task_1._4._1
- {
- class Book
- {
- public Note[] Books;
- public Book()
- {
- Books = new Note[0];
- }
- public void AddNote(Note value)
- {
- Note[] newArr = new Note[Books.Length + 1];
- for (int i = 0; i < newArr.Length - 1; i++)
- {
- newArr[i] = Books[i];
- }
- newArr[newArr.Length - 1] = value;
- Books = newArr;
- newArr = null;
- GC.Collect();
- }
- public void AddNote(Note value, int ind)
- {
- if (ind < Books.Length)
- {
- Books[ind].UserNote += "\n\n" + value.UserNote;
- }
- else
- {
- Console.WriteLine("Error: ID такой книги не существует. Пожалуйста добавьте новую книгу, начав с новой записки или укажите верный ID. Посмотреть валидные ID можно командой books.");
- }
- }
- public void ShowAllNotes()
- {
- if (Books.Length != 0)
- {
- int i = 0;
- foreach (var n in this.Books)
- {
- Console.WriteLine("{0}: {1}", i, n.UserNote);
- i++;
- }
- }
- else
- {
- Console.WriteLine("Еще не создано ни одной книги");
- }
- }
- public void AllBooks()
- {
- if (Books.Length != 0)
- {
- for (int i = 0; i < Books.Length; i++)
- {
- Console.Write(i + " ");
- }
- Console.WriteLine();
- }
- else
- {
- Console.WriteLine("Еще не создано ни одной книги");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement