From 349f5e0ff93a59bb8648f81752595ca4379ba4be Mon Sep 17 00:00:00 2001 From: Tony Bark Date: Sun, 16 Mar 2025 17:19:54 -0400 Subject: [PATCH] Simple date selection Thanks to switching to JSON, I can finally add date selection. --- Program.cs | 46 +++++++++++++++++++++++++++++++++++++++++++-- PublishTimes.csproj | 2 +- Schedule.cs | 3 +++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/Program.cs b/Program.cs index c5dd133..7c58bda 100644 --- a/Program.cs +++ b/Program.cs @@ -116,8 +116,8 @@ string SelectTopics(List topics) userChoices.Add($"{numOfTopics} {title}"); } - var selection = string.Join(", ", userChoices.ToArray()); - Console.WriteLine($"{Environment.NewLine}Select a Number{Environment.NewLine}{selection}"); + var topicSelect = string.Join(", ", userChoices.ToArray()); + Console.WriteLine($"{Environment.NewLine}Choose a Topic{Environment.NewLine}{topicSelect}"); var input = Console.ReadLine(); // Attempt to parse a number. @@ -129,6 +129,45 @@ string SelectTopics(List topics) return topicChoice; } +/// +/// Prompts the user to select a date (either today or tomorrow) and returns the selected date as a formatted string. +/// +/// A string representing the selected date in a short date format. +string SelectDate() +{ + var dtChoices = new[] { "Today", "Tomorrow" }; + var dtDict = new Dictionary(); + var dtSelection = new List(); + var dtChoice = 0; + var dtNum = 0; + + foreach (var days in dtChoices) + { + dtNum++; + var day = days.Trim(); + dtDict.Add(dtNum, day); + dtSelection.Add($"{dtNum} {day}"); + } + + var topicSelect = string.Join(", ", dtSelection.ToArray()); + Console.WriteLine($"{Environment.NewLine}Choose a Date{Environment.NewLine}{topicSelect}"); + var input = Console.ReadLine(); + + // Attempt to parse a number. + if (int.TryParse(input, out dtNum) == true) + dtChoice = dtNum; + + // Any choice above 2 tomorrow + if (dtChoice >= 2) + { + var dt = DateTime.Now.AddDays(1); + return dt.ToString("d"); + } + + return DateTime.Today.ToString("d"); +} + /// /// Allows the user to choose a new topic from a given list or default to placeholder if no selection is made. /// @@ -212,6 +251,8 @@ void ExportSchedule(List storeTimes) topics = config.Topics.ToList(); chosenTopic = NewTopic(topics); + var date = SelectDate(); + // Write to file. var jsonContent = File.ReadAllText(filePath); var jsonList = string.IsNullOrWhiteSpace(jsonContent) ? new List() @@ -221,6 +262,7 @@ void ExportSchedule(List storeTimes) jsonList.Add(new Schedule() { Topic = chosenTopic.Trim(), + Date = date, Times = times, }); diff --git a/PublishTimes.csproj b/PublishTimes.csproj index ef9295c..b412cef 100644 --- a/PublishTimes.csproj +++ b/PublishTimes.csproj @@ -4,7 +4,7 @@ Exe net9.0 enable - 0.2.101 + 0.2.105 enable diff --git a/Schedule.cs b/Schedule.cs index 7b7ed6f..c430bc9 100644 --- a/Schedule.cs +++ b/Schedule.cs @@ -4,6 +4,9 @@ public class Schedule [JsonPropertyName("topic")] public string Topic { get; set; } = ""; + [JsonPropertyName("date")] + public string Date { get; set; } = ""; + [JsonPropertyName("times")] public IList Times { get; set; } = new List(); }