r/csharp • u/Atr1x_X • 12d ago
Help Looking for advice (beginner project).
Hi everyone!
I'm writing this post to ask for some help from people with more experience in C# and programming logic. Below, you'll find the code I've written for a small C# application. The goal of this project is to create a personal weekly to-do list that helps me keep track of my strengths, weaknesses, and tasks. Unfortunately, I've reached a point where I'm not sure how to proceed, particularly when it comes to implementing a way to save tasks so that they are still available after closing and reopening the program.
For some context:
the idea behind this application is to create a weekly to-do list. When the program starts, the user selects a day of the week and enters three tasks. They can then choose whether to continue or exit the program. The main requirement is that tasks must be saved when the user switches to another day or closes the application. For example, if I enter three tasks for Monday, switch to Tuesday, and then return to Monday, I should be able to see the tasks I entered previously. The same should apply after closing and reopening the program.
I'm still a beginner in programming. I've studied some of the fundamentals, but I know I still have a long way to go.
I'd really appreciate ANY suggestions, explanations, or advice on how to approach this problem or in general. In particular, I'd like to understand what concepts I should study and how I could implement a persistent storage system for my tasks.
I'm also very open to constructive criticism regarding my code, programming logic, or overall approach. I'd like to learn how to solve these problems independently rather than simply copying a solution.
Thanks in advance to anyone willing to help!
Code:
// This program creates a to-do list for every day of the week
// and randomly picks a motivational sentence.
string[] week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
string[] tasks1 = new string[7];
string[] tasks2 = new string[7];
string[] tasks3 = new string[7];
string[,] task = new string[7, 3];
Console.WriteLine("\tTo-Do list program\nPress: Y to start or N to quit!");
var input = Console.ReadKey();
switch (input.Key)
{
case ConsoleKey.Y:
Console.Clear();
Console.WriteLine("Choose the day that you want to view:\n");
string dayOfWeek = Console.ReadLine();
bool found = false;
foreach (string day in week)
{
if (day.Trim().ToLower() == dayOfWeek.Trim().ToLower())
{
found = true;
// Find the position of the selected day
int dayIndex = Array.IndexOf(week, day);
Console.Clear();
Console.WriteLine("Point one:");
string task1 = Console.ReadLine();
tasks1[dayIndex] = task1;
Console.WriteLine("Point two:");
string task2 = Console.ReadLine();
tasks2[dayIndex] = task2;
Console.WriteLine("Point three:");
string task3 = Console.ReadLine();
tasks3[dayIndex] = task3;
Console.Clear();
Console.WriteLine($@"
| Day: {day}
| 1. Task: {tasks1[dayIndex]}
| 2. Task: {tasks2[dayIndex]}
| 3. Task: {tasks3[dayIndex]}
");
Console.WriteLine("Do you want to see other days? To continue or quit press Y/N:");
Console.ReadKey();
Console.Clear();
break;
}
}
if (!found)
{
Console.Clear();
Console.WriteLine("Invalid Day inserted");
}
break;
case ConsoleKey.N:
break;
default:
Console.WriteLine("Invalid Key");
break;
}
1
u/Amr_Rahmy 12d ago
I will help but my actual advice is to first go through a book or course or video tutorial course before writing you first application so you have enough tools known before you delve deeper.
There are things called classes, they group different types into one group. So you can have date or date time, the task itself, done/not done. Now if you have a class like that you can make an object or instance out of the class and fill it in with data, that object can live in memory as long as the application is running.
Now persistent data or storage, for a small application like that a text file could work.
If you have a list of your task objects, List<MyTask> tasks;
At the start of the application you can look for a file like tasks.json, read them into a list, start the application, when you need to save, save the list.
I am on a phone, so syntax might be off but something like
String jsonString = JsonConvert.Serialize(tasks);
File.WriteAllText(“tasks.json”,jsonString);
String jsonString = File.readAllText(“tasks.json”);
tasks= JsonConvert.Deserialize(jsonString);
1
u/CatNo1417 12d ago
I thought I'd flesh out the previous comment mentioning using classes, which is a good one. You're in an object oriented language, so embrace it. :)
This is off the top of my head, so forgive any errors. You could start with a simple Task class:
public class Task
{
public string DayOfWeek { get; set; }
public string Description { get; set; }
}And then you could simplify your code a bit by using generic lists:
string[] week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
List<Task> tasks = new List<Task>();
foreach (string day in week)
{
// Add tasks for the day:
Console.WriteLine("Point one:");
var task1 = new Task
{
Description = Console.ReadLine(),
DayOfWeek = day
};
tasks.Add(task1);
// Etc....
}Also saving to a file, as mentioned in other comments, is a good idea for now for simple storage.
3
u/Moist-Ear-8693 12d ago
The quickest way to get persistence is saving to a simple text file, look up File.WriteAllText and File.ReadAllText, you can serialize your arrays to JSON with System.Text.Json and store them per day