-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.cs
120 lines (111 loc) · 4.33 KB
/
program.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
119
120
using System;
using System.Collections.Generic;
using System.IO;
class ToDoList {
private List<string> list;
private string filepath = "data.txt";
public ToDoList() {
list = ReadFile(); //empty list if no file or empty file, else it reads previous state from txt file
}
public void Add(string todo) {
list.Add(todo);
WriteFile();
Console.WriteLine("#"+(list.Count)+" "+todo);
}
public int getListCount() {
return list.Count;
}
public void Do(int number) {
if (number > 0 && number <= list.Count && list[number-1] != "") {
Console.WriteLine("Completed #"+number+" "+list[number-1]);
list[number-1] = "";
WriteFile();
}
else {
Console.WriteLine("#"+number+" is not on the list");
}
}
private List<string> ReadFile() {
string line;
List<string> newList = new List<string>();
try {
StreamReader sr = new StreamReader(filepath);
line = sr.ReadLine();
while (line != null) {
newList.Add(line);
line = sr.ReadLine();
}
sr.Close();
}
catch(Exception e) {
Console.WriteLine("Exception: " + e.Message+". Will not load any data");
}
return newList;
}
private void WriteFile() {
string text = "";
foreach (string todo in list) {
text += todo +"\n";
}
System.IO.File.WriteAllText(filepath, text);
}
public void Print() {
for (int i = 0; i < list.Count; i++) {
if (list[i] != "") {
Console.WriteLine("#"+(i+1)+" "+list[i]);
}
}
}
}
public class ExecuteApp {
public static void Main(string[] args) { // try to read state from previous run saved in a file
ToDoList todolist = new ToDoList();
string input;
while (true) {
input = Console.ReadLine();
if (input.Length < 4) {
Console.WriteLine("Command must be at least 4 char long");
}
else if(input.ToLower() == "exit") {
break;
}
else if (input.Substring(0,3).ToLower() == "add" && input.Length >4) {
string added = input.Substring(4);
if (added[0] != '"' || added[added.Length-1] != '"' || added.Length<3) { // if it doesnt have quote end and quote start. In the assignment it showed
//Console.WriteLine("Put quotation marks around what you want to add"); // "" around the strings added, but seeing as # was used weirdly too, I decided to allow both
todolist.Add(added); // So if theres quote marks, then they are removed, if there arent then i just add what it says
} else {
added = added.Substring(0,added.Length-1); //remove quote end
added = added.Substring(1,added.Length-1); //remov quote start
todolist.Add(added);
}
}
else if (input.Substring(0,2).ToLower() == "do") {
string number = input.Substring(3);
if (input[3] == '#') { // Assignment was confusing with how # is supposed to be used. task says that # is the number, but example shows usage of Do #1, so i added both
if (input.Length>4) {
number = input.Substring(4);
}
}
int number2;
if(int.TryParse(number, out number2)) {
todolist.Do(number2);
}
else {
Console.WriteLine("Not valid input for Do. Example: 'Do #2'");
}
}
else if (input.Length >=5) {
if (input.Substring(0,5).ToLower() == "print") {
todolist.Print();
}
else {
Console.WriteLine(input+" is not a valid command");
}
}
else {
Console.WriteLine(input+" is not a valid command");
}
}
}
}