C#
First time here! Feedback very welcome! Still reasonably new to C#/OOP - don't feel fully confident in it yet.
class ToDo
{
private List<string> ToDoList;
private string message;
public ToDo(){
ToDoList = new List<string> ();
}
public string Message {
get { return this.message; }
set { this.message = value; }
}
public string AddItem(string item) {
ToDoList.Add (item);
return item;
}
public string RemoveItem(string rItem) {
for (int i = 0; i < ToDoList.Count; i++) {
if (rItem == ToDoList [i]) {
ToDoList.RemoveAt (i);
Console.WriteLine ();
Console.WriteLine ("Success.");
} else {
Console.WriteLine ();
Console.WriteLine ("Failed - No record found.");
}
}
return rItem;
}
public void ShowList(){
foreach (var listElement in ToDoList){
Console.WriteLine (listElement);
}
}
public static void Main (string[] args) {
ToDo toDo = new ToDo ();
bool loopFlag = false;
while (loopFlag == false) {
Console.WriteLine ();
Console.WriteLine ("Main Menu - Pick a Number: \n ");
Console.WriteLine ("1. Add Item");
Console.WriteLine ("2. Remove Item");
Console.WriteLine ("3. List All Items");
Console.WriteLine ("4. Exit \n ");
try
{
string userStringResponse = Console.ReadLine ();
int userIntResponse = Convert.ToInt32 (userStringResponse);
switch (userIntResponse) {
case 1:
Console.WriteLine ();
Console.WriteLine ("You chose Add Item - Please type the item to add: \n ");
string uAddResp = Console.ReadLine ();
if (uAddResp == "") {
Console.WriteLine("Please Enter Valid Data - Field cannot be blank.");
} else {
toDo.AddItem (uAddResp.ToUpper());
}
break;
case 2:
Console.WriteLine ();
Console.WriteLine ("You chose Remove Item - Please type the item to Remove: \n ");
string uRemResp = Console.ReadLine ();
toDo.RemoveItem (uRemResp.ToUpper());
break;
case 3:
Console.WriteLine ();
if (toDo.ToDoList.Count == 0) {
Console.WriteLine ("No record found - List empty.");
} else {
Console.WriteLine ("Current List: \n\n");
toDo.ShowList ();
}
break;
case 4:
Console.WriteLine ();
Console.WriteLine ("You chose Exit - Press any key to exit program.");
Console.ReadLine ();
loopFlag = true;
break;
}
}
catch (FormatException fEx) {
toDo.Message = fEx.ToString();
Console.WriteLine ("Error: Incorrect Format (Please type a single number from the Main Menu) \n\n"
+ toDo.Message);
}
catch (Exception gEx) {
toDo.Message = gEx.ToString();
Console.WriteLine ("Error: " +
"\n\n" + toDo.Message);
}
}
}
}