-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoApp.cs
118 lines (104 loc) · 3.13 KB
/
ToDoApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ToDoApp
{
public class TodoApp
{
string fileLocation = "todo-items.txt";
List<TodoItem> items = new List<TodoItem>();
readonly string helpOutput = @"Options
Add [item] Add a item to the todo application
Do #[number] Complete a given item
Print Print all todo items
Help Show all possible options
Exit Exit the command line application";
public TodoApp()
{
LoadItems();
}
public void UseTestEnvironment()
{
fileLocation = "todo-items-test.txt";
items.Clear();
}
void LoadItems()
{
// Check if file exists and read the content if it exists
if (File.Exists(fileLocation))
{
string[] lines = File.ReadAllLines(fileLocation);
foreach (string line in lines)
{
string text = line.Substring(1).Split(' ')[1];
int number = int.Parse(line.Substring(1).Split(' ')[0]);
TodoItem newItem = new TodoItem(text, number);
items.Add(newItem);
}
}
}
void SaveItems()
{
List<string> allItems = new List<string>();
foreach (TodoItem item in items)
{
allItems.Add(item.ToString());
}
File.WriteAllLines(fileLocation, allItems);
}
public void Add(string text)
{
int newNumber = 1;
if (items.Count > 0)
{
// Set the new number to the number of the last item + 1
newNumber = items.ElementAt(items.Count - 1).Number + 1;
}
TodoItem newItem = new TodoItem(text, newNumber);
items.Add(newItem);
Console.WriteLine(newItem);
SaveItems();
}
public void Do(int number)
{
bool found = false;
foreach (TodoItem item in items)
{
if (item.Number == number)
{
Console.WriteLine("Completed " + item);
found = true;
// Remove from list
items.Remove(item);
// Save new list
SaveItems();
break;
}
}
if (!found)
{
Console.WriteLine("Could not find a item with the specified number");
}
}
public void Print()
{
if (items.Count == 0)
{
Console.WriteLine("There is no items in list");
}
else
{
// Print all items in the todo list
foreach (TodoItem item in items)
{
Console.WriteLine(item);
}
}
}
public void Help()
{
Console.WriteLine(helpOutput);
}
}
}