Created
June 28, 2018 15:54
-
-
Save kondr1/0d630dcdcff979d9082be06b3d30ca5c to your computer and use it in GitHub Desktop.
Final
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.IO; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
static TODOList list = null; | |
static void Main(string[] args) | |
{ | |
Menu menu = new Menu(() => | |
{ | |
if (list != null) | |
{ | |
Console.WriteLine("=> Work with: " + list.Path); | |
} | |
return ""; | |
}); | |
menu.Add("newtodo", "create new todo file", () => | |
{ | |
Console.WriteLine("Name:"); | |
list = TODOList.Create(Console.ReadLine()); | |
return "Success"; | |
}); | |
menu.Add("addentity", "create new todo in todo list", () => | |
{ | |
OpenList().Add(NewEnityDialog()); | |
return "Success"; | |
}); | |
menu.Add("showtodo", "print todo by id", () => | |
{ | |
try | |
{ | |
Console.WriteLine(EntityById(OpenList()).ToString()); | |
return "Success"; | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
return "Fail"; | |
} | |
}); | |
menu.Add("edittodo", "edit todo by id", () => | |
{ | |
try | |
{ | |
TODOList l = OpenList(); | |
TODOEntity e = EntityById(l); | |
Console.WriteLine(e.ToString()); | |
TODOEntity newEntity = NewEnityDialog(); | |
newEntity.ID = e.ID; | |
l.Update(newEntity); | |
l.Save(); | |
return "Success"; | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
return "Fail"; | |
} | |
}); | |
menu.Add("deletetodo", "remove todo by id from list", () => | |
{ | |
try | |
{ | |
TODOList l = OpenList(); | |
TODOEntity e = EntityById(l); | |
Console.WriteLine(e.ToString()); | |
l.Remove(e); | |
l.Save(); | |
return "Success"; | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
return "Fail"; | |
} | |
}); | |
menu.Add("showtasks", "show all task", () => | |
{ | |
try | |
{ | |
OpenList().Print(); | |
return "Success"; | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
return "Fail"; | |
} | |
}); | |
menu.Start(); | |
} | |
static public TODOList OpenList() | |
{ | |
if (list == null) | |
{ | |
Console.WriteLine("File name:"); | |
list = TODOList.Open(Console.ReadLine()); | |
return list; | |
} | |
return list; | |
} | |
static public TODOEntity EntityById(TODOList list) | |
{ | |
Console.WriteLine("ID:"); | |
int.TryParse(Console.ReadLine(), out int id); | |
if (id == 0) | |
{ | |
throw new Exception("ID must a number"); | |
} | |
return list.ReadById(id); | |
} | |
static public TODOEntity NewEnityDialog() | |
{ | |
Console.WriteLine("Name:"); | |
string name = Console.ReadLine(); | |
Console.WriteLine("Status:"); | |
string status = Console.ReadLine(); | |
Console.WriteLine("Description:"); | |
string desc = Console.ReadLine(); | |
return new TODOEntity(0, name, status, desc); | |
} | |
} | |
} | |
class TODOList | |
{ | |
public string Path; | |
public List<TODOEntity> List = new List<TODOEntity>(); | |
public TODOList(string path) | |
{ | |
Path = path; | |
} | |
public static TODOList Create(string path) | |
{ | |
if (!File.Exists(path)) | |
{ | |
File.Create(path).Close(); | |
return new TODOList(path); | |
} | |
throw new Exception("File exist"); | |
} | |
public static TODOList Open(string path) | |
{ | |
if (!File.Exists(path)) | |
{ | |
throw new Exception("File not exist"); | |
} | |
TODOList list = new TODOList(path); | |
list.Read(); | |
return list; | |
} | |
public void Add(string Name, string Status, string Description) | |
{ | |
int ID = List.Count == 0 ? 1 : List.Last().ID + 1; | |
List.Add(new TODOEntity(ID, Name, Status, Description)); | |
} | |
public void Add(TODOEntity entity) | |
{ | |
Add(entity.Name, entity.Status, entity.Description); | |
} | |
public void Read() | |
{ | |
using (StreamReader sr = new StreamReader(Path)) | |
{ | |
while (!sr.EndOfStream) | |
{ | |
List.Add(TODOEntity.FromString(sr.ReadLine())); | |
} | |
} | |
} | |
public void Save() | |
{ | |
using (StreamWriter sw = new StreamWriter(Path, false)) | |
{ | |
foreach (TODOEntity entity in List) | |
{ | |
sw.WriteLine(entity.ToString()); | |
} | |
} | |
} | |
public TODOEntity ReadById(int id) | |
{ | |
return List.Where((x) => x.ID == id).Single(); | |
} | |
public void Print() | |
{ | |
foreach(TODOEntity e in List) | |
{ | |
Console.WriteLine(e); | |
} | |
} | |
public void Remove(TODOEntity e) | |
{ | |
List.Remove(e); | |
Save(); | |
} | |
public void Update(TODOEntity e) | |
{ | |
TODOEntity u = List.Where(x => x.ID == e.ID).Single(); | |
u.Name = e.Name; | |
u.Status = e.Status; | |
u.Description = e.Description; | |
Save(); | |
} | |
} | |
class TODOEntity | |
{ | |
public int ID; | |
public string Status; | |
public string Name; | |
public string Description; | |
private const char sep = '|'; | |
public TODOEntity(int id, string name, string status, string desc) | |
{ | |
ID = id; | |
Name = name; | |
Status = status; | |
Description = desc; | |
} | |
public override string ToString() | |
{ | |
return ID.ToString() + sep + Name + sep + Status + sep + Description; | |
} | |
public static TODOEntity FromString(string str) | |
{ | |
string[] vs = str.Split(sep); | |
return new TODOEntity(int.Parse(vs[0]), vs[1], vs[2], vs[3]); | |
} | |
} | |
delegate string Call(); | |
class Menu | |
{ | |
List<Element> Elements = new List<Element>(); | |
public string ExitCommand = "exit"; | |
public Call Callback; | |
public Menu(Call cb) | |
{ | |
Callback = cb; | |
} | |
public void Start() | |
{ | |
Console.WriteLine("------------------"); | |
Console.WriteLine("List of available commands:"); | |
if (Callback != null) | |
{ | |
Callback(); | |
} | |
foreach (Element e in Elements) | |
{ | |
Console.WriteLine(e); | |
} | |
Console.WriteLine("Write '"+ExitCommand+"' for close the program."); | |
string cmd = Console.ReadLine(); | |
if (cmd == ExitCommand) | |
{ | |
Environment.Exit(0); | |
} | |
Console.WriteLine(Exec(cmd)); | |
Start(); | |
} | |
public void Add(string name, string desc, Call call) | |
{ | |
Elements.Add(new Element(name, desc, call)); | |
} | |
public string Exec(string param) | |
{ | |
foreach (Element el in Elements) | |
{ | |
if (el.Name == param) | |
{ | |
return el.Callback(); | |
} | |
} | |
return ""; | |
} | |
} | |
class Element | |
{ | |
public string Description; | |
public string Name; | |
public Call Callback; | |
public Element(string name, string desc, Call call) | |
{ | |
Description = desc; | |
Name = name; | |
Callback = call; | |
} | |
public override string ToString() | |
{ | |
return Name + " — " + Description; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment