From 48ec3d9c5835bc2ff8e9af65af466bbd35ebe437 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Wed, 11 Feb 2026 22:47:10 -0500 Subject: [PATCH 01/13] got inital sqlite db set up --- CodeReviews.Console.HabitTracker.sln | 24 +++++++++ HabitTracker.natholder/HabitTracker.db | Bin 0 -> 12288 bytes .../HabitTracker.natholder.csproj | 14 ++++++ HabitTracker.natholder/Program.cs | 47 ++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 CodeReviews.Console.HabitTracker.sln create mode 100644 HabitTracker.natholder/HabitTracker.db create mode 100644 HabitTracker.natholder/HabitTracker.natholder.csproj create mode 100644 HabitTracker.natholder/Program.cs diff --git a/CodeReviews.Console.HabitTracker.sln b/CodeReviews.Console.HabitTracker.sln new file mode 100644 index 00000000..fab62861 --- /dev/null +++ b/CodeReviews.Console.HabitTracker.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HabitTracker.natholder", "HabitTracker.natholder\HabitTracker.natholder.csproj", "{AA4088A3-C32F-4B29-48E0-339A2F9967C6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AA4088A3-C32F-4B29-48E0-339A2F9967C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AA4088A3-C32F-4B29-48E0-339A2F9967C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AA4088A3-C32F-4B29-48E0-339A2F9967C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AA4088A3-C32F-4B29-48E0-339A2F9967C6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D67F1646-947A-4847-A048-583B3565ABC1} + EndGlobalSection +EndGlobal diff --git a/HabitTracker.natholder/HabitTracker.db b/HabitTracker.natholder/HabitTracker.db new file mode 100644 index 0000000000000000000000000000000000000000..a806d9085f3c588064a363df479936cc2f4c1d97 GIT binary patch literal 12288 zcmeI#F-yZh6u|MjRun|3Tgl`bTUuQF0!Djiu!%LELlz~~2!XZ|6BjrA$eQ5?Em ztB6yY{vgXpn zGKquGj}y5ziTL;SCWu^lXd*joRE$#B%a7cL*&sB{S7Gk%RG-%<=-0iuYx)TU5I_I{ s1Q0*~0R#|0009IL*kS?t|1JKxTnzyP5I_I{1Q0*~0R#|00D&KYB?YWXrvLx| literal 0 HcmV?d00001 diff --git a/HabitTracker.natholder/HabitTracker.natholder.csproj b/HabitTracker.natholder/HabitTracker.natholder.csproj new file mode 100644 index 00000000..6ed0cec0 --- /dev/null +++ b/HabitTracker.natholder/HabitTracker.natholder.csproj @@ -0,0 +1,14 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs new file mode 100644 index 00000000..a5aba08a --- /dev/null +++ b/HabitTracker.natholder/Program.cs @@ -0,0 +1,47 @@ +using System; +using System.Diagnostics; +using Microsoft.Data.Sqlite; + +namespace HabitTracker +{ + class Program + { + + static void Main(string[] args) + { + string dbFileName = "HabitTracker.db"; + string connectionString = $"Data Source ={dbFileName}"; + + try + { + using (var connection = new SqliteConnection(connectionString)) + { + connection.Open(); + Console.WriteLine($"Database {dbFileName} created or opened successfully."); + CreateTable(connection); + } + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } + } + + + static void CreateTable(SqliteConnection connection) + { + string CreateTableQuery = @" + CREATE TABLE IF NOT EXISTS Habits ( + Id INTEGER PRIMARY KEY AUTOINCREMENT, + Date DATETIME, + Miles INT + )"; + + using (var command = new SqliteCommand(CreateTableQuery, connection)) + { + command.ExecuteNonQuery(); + Console.WriteLine("Table 'Habits' created or already exists."); + } + } + } +} \ No newline at end of file From fd79d694d38befe660ad9aef47759cdea5cbc239 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Mon, 16 Feb 2026 20:15:23 -0500 Subject: [PATCH 02/13] menu + GetMiles and GetDate functions --- HabitTracker.natholder/Program.cs | 109 ++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 7 deletions(-) diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs index a5aba08a..00a58a9b 100644 --- a/HabitTracker.natholder/Program.cs +++ b/HabitTracker.natholder/Program.cs @@ -1,5 +1,7 @@ using System; using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Security.Cryptography.X509Certificates; using Microsoft.Data.Sqlite; namespace HabitTracker @@ -11,20 +13,63 @@ static void Main(string[] args) { string dbFileName = "HabitTracker.db"; string connectionString = $"Data Source ={dbFileName}"; - + string menuText = @" + __ ____ __ ______ __ + / |/ (_) /__ _____ /_ __/________ ______/ /_____ _____ + / /|_/ / / / _ \/ ___/ / / / ___/ __ `/ ___/ //_/ _ \/ ___/ + / / / / / / __(__ ) / / / / / /_/ / /__/ ,< / __/ / +/_/ /_/_/_/\___/____/ /_/ /_/ \__,_/\___/_/|_|\___/_/"; + string? userInput; + bool running = true; + int number; + double miles = 0.0; + DateTime date = new DateTime(); try { - using (var connection = new SqliteConnection(connectionString)) - { - connection.Open(); - Console.WriteLine($"Database {dbFileName} created or opened successfully."); - CreateTable(connection); - } + using var connection = new SqliteConnection(connectionString); + connection.Open(); + Console.WriteLine($"Database {dbFileName} created or opened successfully."); + CreateTable(connection); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } + + while (running) + { + + Console.WriteLine(menuText); + ShowMenu(); + userInput = Console.ReadLine(); + if (int.TryParse(userInput, out number) && number > 0 && number <= 6) + { + switch (userInput) + { + case "1": + Console.WriteLine("view"); + break; + case "2": + miles = GetMiles(miles, userInput); + date = GetDate(date, userInput); + Console.WriteLine(date.Date); + break; + case "3": + Console.WriteLine("update"); + break; + case "4": + Console.WriteLine("delete"); + break; + default: + running = false; + break; + } + } + else + { + Console.WriteLine("Invalid input. Please try again."); + } + } } @@ -43,5 +88,55 @@ Miles INT Console.WriteLine("Table 'Habits' created or already exists."); } } + + static void ShowMenu() + { + Console.WriteLine("1. View"); + Console.WriteLine("2. Insert"); + Console.WriteLine("3. Update"); + Console.WriteLine("4. Delete"); + Console.WriteLine("5. Quit"); + } + + static int Insert() + { + + + string CreateTableQuery = @" + CREATE TABLE IF NOT EXISTS Habits ( + Id INTEGER PRIMARY KEY AUTOINCREMENT, + Date DATETIME, + Miles INT + )"; + + return 0; + } + + static double GetMiles(double miles, string? input) + { + Console.WriteLine("How many miles?"); + input = Console.ReadLine(); + return double.TryParse(input, out miles) == false ? -1 : miles; + } + + static DateTime GetDate(DateTime date, string? input) + { + + Console.WriteLine("What day was your run? (tyoe td for today's date)"); + input = Console.ReadLine(); + if (input == "td") + { + date = DateTime.Today; + return date; + } + else if (DateTime.TryParse(input, out date)) + { + return date; + } + else + { + return DateTime.MinValue; + } + } } } \ No newline at end of file From ff18be58490db87add266e72d7e279629e7924dd Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Mon, 16 Feb 2026 20:30:35 -0500 Subject: [PATCH 03/13] insert working + add .db files to gitignore --- .gitignore | 1 + HabitTracker.natholder/Program.cs | 109 +++++++++++++++--------------- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index 0b26c11d..e1293c09 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ *.user *.userosscache *.sln.docstates +*.db # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs index 00a58a9b..66bdb242 100644 --- a/HabitTracker.natholder/Program.cs +++ b/HabitTracker.natholder/Program.cs @@ -24,52 +24,55 @@ __ ____ __ ______ __ int number; double miles = 0.0; DateTime date = new DateTime(); + int rows = 0; + try { using var connection = new SqliteConnection(connectionString); connection.Open(); Console.WriteLine($"Database {dbFileName} created or opened successfully."); CreateTable(connection); - } - catch (Exception ex) - { - Console.WriteLine($"An error occurred: {ex.Message}"); - } - - while (running) - { - Console.WriteLine(menuText); - ShowMenu(); - userInput = Console.ReadLine(); - if (int.TryParse(userInput, out number) && number > 0 && number <= 6) + while (running) { - switch (userInput) + + Console.WriteLine(menuText); + ShowMenu(); + userInput = Console.ReadLine(); + if (int.TryParse(userInput, out number) && number > 0 && number <= 6) { - case "1": - Console.WriteLine("view"); - break; - case "2": - miles = GetMiles(miles, userInput); - date = GetDate(date, userInput); - Console.WriteLine(date.Date); - break; - case "3": - Console.WriteLine("update"); - break; - case "4": - Console.WriteLine("delete"); - break; - default: - running = false; - break; + switch (userInput) + { + case "1": + Console.WriteLine("view"); + break; + case "2": + miles = GetMiles(miles, userInput); + date = GetDate(date, userInput); + rows = Insert(connection, miles, date); + Console.WriteLine($"{rows} rows inserted!"); + break; + case "3": + Console.WriteLine("update"); + break; + case "4": + Console.WriteLine("delete"); + break; + default: + running = false; + break; + } + } + else + { + Console.WriteLine("Invalid input. Please try again."); } - } - else - { - Console.WriteLine("Invalid input. Please try again."); } } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } } @@ -82,11 +85,21 @@ CREATE TABLE IF NOT EXISTS Habits ( Miles INT )"; - using (var command = new SqliteCommand(CreateTableQuery, connection)) - { - command.ExecuteNonQuery(); - Console.WriteLine("Table 'Habits' created or already exists."); - } + using var command = new SqliteCommand(CreateTableQuery, connection); + command.ExecuteNonQuery(); + Console.WriteLine("Table 'Habits' created or already exists."); + } + + static int Insert(SqliteConnection connection, double miles, DateTime date) + { + string InsertRowQuery = @" + INSERT INTO Habits (Date, Miles) VALUES (@date, @miles)"; + using var command = new SqliteCommand(InsertRowQuery, connection); + command.Parameters.AddWithValue("@date", date); + command.Parameters.AddWithValue("@miles", miles); + int rowsAffected = command.ExecuteNonQuery(); + + return rowsAffected; } static void ShowMenu() @@ -98,20 +111,6 @@ static void ShowMenu() Console.WriteLine("5. Quit"); } - static int Insert() - { - - - string CreateTableQuery = @" - CREATE TABLE IF NOT EXISTS Habits ( - Id INTEGER PRIMARY KEY AUTOINCREMENT, - Date DATETIME, - Miles INT - )"; - - return 0; - } - static double GetMiles(double miles, string? input) { Console.WriteLine("How many miles?"); @@ -122,7 +121,7 @@ static double GetMiles(double miles, string? input) static DateTime GetDate(DateTime date, string? input) { - Console.WriteLine("What day was your run? (tyoe td for today's date)"); + Console.WriteLine("What day was your run? (type td for today's date)"); input = Console.ReadLine(); if (input == "td") { @@ -139,4 +138,4 @@ static DateTime GetDate(DateTime date, string? input) } } } -} \ No newline at end of file +} From 218297fd9aeb51fd77b81a8faf9bade92988cb10 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Mon, 16 Feb 2026 20:32:32 -0500 Subject: [PATCH 04/13] gitignore update --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e1293c09..15a48951 100644 --- a/.gitignore +++ b/.gitignore @@ -419,3 +419,4 @@ HabitTracker.MartinL-no/.DS_Store HabitTracker.MartinL-no/habitTracker.MartinL-no/.DS_Store /HabitTracker.Johay90/HabitTracker/habit_tracker.db /.idea/ +HabitTracker.natholder/HabitTracker.db From c82ba12f1f3e7481f66157b197efae7fd9b6ae8a Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Mon, 16 Feb 2026 20:33:56 -0500 Subject: [PATCH 05/13] remove db --- HabitTracker.natholder/HabitTracker.db | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 HabitTracker.natholder/HabitTracker.db diff --git a/HabitTracker.natholder/HabitTracker.db b/HabitTracker.natholder/HabitTracker.db deleted file mode 100644 index a806d9085f3c588064a363df479936cc2f4c1d97..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI#F-yZh6u|MjRun|3Tgl`bTUuQF0!Djiu!%LELlz~~2!XZ|6BjrA$eQ5?Em ztB6yY{vgXpn zGKquGj}y5ziTL;SCWu^lXd*joRE$#B%a7cL*&sB{S7Gk%RG-%<=-0iuYx)TU5I_I{ s1Q0*~0R#|0009IL*kS?t|1JKxTnzyP5I_I{1Q0*~0R#|00D&KYB?YWXrvLx| From 1d6357afe73496765c0cd2d53e8681cab4387889 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Mon, 16 Feb 2026 20:35:15 -0500 Subject: [PATCH 06/13] ignore update --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 15a48951..e1293c09 100644 --- a/.gitignore +++ b/.gitignore @@ -419,4 +419,3 @@ HabitTracker.MartinL-no/.DS_Store HabitTracker.MartinL-no/habitTracker.MartinL-no/.DS_Store /HabitTracker.Johay90/HabitTracker/habit_tracker.db /.idea/ -HabitTracker.natholder/HabitTracker.db From e1b8d1c78b0fe508b50ee782232a67097d241d74 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Mon, 16 Feb 2026 22:00:40 -0500 Subject: [PATCH 07/13] better input validation --- HabitTracker.natholder/Program.cs | 165 +++++++++++++++++------------- 1 file changed, 95 insertions(+), 70 deletions(-) diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs index 66bdb242..92db7139 100644 --- a/HabitTracker.natholder/Program.cs +++ b/HabitTracker.natholder/Program.cs @@ -8,74 +8,71 @@ namespace HabitTracker { class Program { - + static string dbFileName = "HabitTracker.db"; + static string connectionString = $"Data Source ={dbFileName}"; static void Main(string[] args) { - string dbFileName = "HabitTracker.db"; - string connectionString = $"Data Source ={dbFileName}"; + initDB(); + MenuLoop(); + } + + static void initDB() + { + try + { + using var connection = new SqliteConnection(connectionString); + connection.Open(); + Console.WriteLine($"Database {dbFileName} created or opened successfully."); + CreateTable(connection); + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred: {ex.Message}"); + } + } + static void MenuLoop() + { string menuText = @" __ ____ __ ______ __ / |/ (_) /__ _____ /_ __/________ ______/ /_____ _____ / /|_/ / / / _ \/ ___/ / / / ___/ __ `/ ___/ //_/ _ \/ ___/ / / / / / / __(__ ) / / / / / /_/ / /__/ ,< / __/ / /_/ /_/_/_/\___/____/ /_/ /_/ \__,_/\___/_/|_|\___/_/"; - string? userInput; bool running = true; + string? userInput; int number; - double miles = 0.0; - DateTime date = new DateTime(); - int rows = 0; - - try + Console.WriteLine(menuText); + while (running) { - using var connection = new SqliteConnection(connectionString); - connection.Open(); - Console.WriteLine($"Database {dbFileName} created or opened successfully."); - CreateTable(connection); - - while (running) + ShowMenu(); + userInput = Console.ReadLine(); + if (int.TryParse(userInput, out number) && number > 0 && number <= 6) { - - Console.WriteLine(menuText); - ShowMenu(); - userInput = Console.ReadLine(); - if (int.TryParse(userInput, out number) && number > 0 && number <= 6) - { - switch (userInput) - { - case "1": - Console.WriteLine("view"); - break; - case "2": - miles = GetMiles(miles, userInput); - date = GetDate(date, userInput); - rows = Insert(connection, miles, date); - Console.WriteLine($"{rows} rows inserted!"); - break; - case "3": - Console.WriteLine("update"); - break; - case "4": - Console.WriteLine("delete"); - break; - default: - running = false; - break; - } - } - else + switch (userInput) { - Console.WriteLine("Invalid input. Please try again."); + case "1": + Console.WriteLine("view"); + break; + case "2": + Insert(); + break; + case "3": + Console.WriteLine("update"); + break; + case "4": + Console.WriteLine("delete"); + break; + default: + running = false; + break; } } - } - catch (Exception ex) - { - Console.WriteLine($"An error occurred: {ex.Message}"); + else + { + Console.WriteLine("Invalid input. Please try again."); + } } } - - static void CreateTable(SqliteConnection connection) { string CreateTableQuery = @" @@ -90,18 +87,29 @@ Miles INT Console.WriteLine("Table 'Habits' created or already exists."); } - static int Insert(SqliteConnection connection, double miles, DateTime date) + static void Insert() { - string InsertRowQuery = @" - INSERT INTO Habits (Date, Miles) VALUES (@date, @miles)"; - using var command = new SqliteCommand(InsertRowQuery, connection); + DateTime date = GetDate(); + double miles = GetMiles(); + using var connection = new SqliteConnection(connectionString); + connection.Open(); + var insert = connection.CreateCommand(); + insert.CommandText = $"INSERT INTO Habits (Date, Miles) VALUES ('{date}', {miles})"; + insert.ExecuteNonQuery(); + connection.Close(); + } + + static int Delete(SqliteConnection connection, DateTime date) + { + string DeleteRowsQuery = @" + DELETE FROM Habits WHERE date = @date"; + using var command = new SqliteCommand(DeleteRowsQuery, connection); command.Parameters.AddWithValue("@date", date); - command.Parameters.AddWithValue("@miles", miles); int rowsAffected = command.ExecuteNonQuery(); - return rowsAffected; } + static void ShowMenu() { Console.WriteLine("1. View"); @@ -111,31 +119,48 @@ static void ShowMenu() Console.WriteLine("5. Quit"); } - static double GetMiles(double miles, string? input) + static double GetMiles() { - Console.WriteLine("How many miles?"); + double miles; + string? input; + Console.WriteLine("How many miles did you run?"); input = Console.ReadLine(); - return double.TryParse(input, out miles) == false ? -1 : miles; + if (double.TryParse(input, out miles)) + { + return miles; + } + else + { + Console.WriteLine("Please enter a valid number"); + return GetMiles(); + } } - static DateTime GetDate(DateTime date, string? input) + static DateTime GetDate() { - - Console.WriteLine("What day was your run? (type td for today's date)"); + string? input; + DateTime date; + Console.WriteLine("What day was your run? (type 'td' for today's date, or enter date as MM/DD/YYYY):"); input = Console.ReadLine(); - if (input == "td") + + if (string.IsNullOrWhiteSpace(input)) { - date = DateTime.Today; - return date; + Console.WriteLine("Please enter a valid date or 'td' for today."); + return GetDate(); // Recursive call for empty input } - else if (DateTime.TryParse(input, out date)) + + if (input.ToLower() == "td") { - return date; + return DateTime.Today; } - else + + if (DateTime.TryParse(input, out date)) { - return DateTime.MinValue; + return date; } + + Console.WriteLine("Invalid date format. Please try again (e.g., 02/16/2026) or type 'td' for today."); + return GetDate(); // Recursive call for invalid date } } } From 8bef48dc6200c25fb54756b08b0fa540fe327a45 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Mon, 16 Feb 2026 22:28:02 -0500 Subject: [PATCH 08/13] select command --- HabitTracker.natholder/Program.cs | 55 +++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs index 92db7139..d622d701 100644 --- a/HabitTracker.natholder/Program.cs +++ b/HabitTracker.natholder/Program.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel.Design; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; @@ -6,6 +7,13 @@ namespace HabitTracker { + + public class Runs + { + public int Id { get; set; } + public DateTime Date { get; set; } + public double Miles { get; set; } + } class Program { static string dbFileName = "HabitTracker.db"; @@ -51,7 +59,7 @@ __ ____ __ ______ __ switch (userInput) { case "1": - Console.WriteLine("view"); + SelectRecords(); break; case "2": Insert(); @@ -79,7 +87,7 @@ static void CreateTable(SqliteConnection connection) CREATE TABLE IF NOT EXISTS Habits ( Id INTEGER PRIMARY KEY AUTOINCREMENT, Date DATETIME, - Miles INT + Miles DOUBLE )"; using var command = new SqliteCommand(CreateTableQuery, connection); @@ -99,6 +107,47 @@ static void Insert() connection.Close(); } + static void SelectRecords() + { + Console.Clear(); + using var connection = new SqliteConnection(connectionString); + connection.Open(); + var select = connection.CreateCommand(); + select.CommandText = $"SELECT * FROM Habits"; + List tableData = []; + SqliteDataReader reader = select.ExecuteReader(); + if (reader.HasRows) + { + while (reader.Read()) + { + tableData.Add( + new Runs + { + Id = reader.GetInt32(0), + Date = reader.GetDateTime(1), + Miles = reader.GetDouble(2) + } + ); + } + } + else + { + Console.WriteLine("No rows found."); + } + connection.Close(); + + Console.WriteLine("---------------------------------------"); + Console.WriteLine(" Id | Date | Miles"); + Console.WriteLine("---------------------------------------"); + foreach (var row in tableData) + { + Console.WriteLine($"| {row.Id} | {row.Date.ToString("MM-dd-yyyy")} | {row.Miles} |"); + } + Console.WriteLine("---------------------------------------"); + + } + + static int Delete(SqliteConnection connection, DateTime date) { string DeleteRowsQuery = @" @@ -149,7 +198,7 @@ static DateTime GetDate() return GetDate(); // Recursive call for empty input } - if (input.ToLower() == "td") + if (input?.ToLower() == "td") { return DateTime.Today; } From c7bd36f329cbf10bfabf2ddb2dcd5be1a0e74cfd Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Mon, 16 Feb 2026 22:31:24 -0500 Subject: [PATCH 09/13] todos --- HabitTracker.natholder/Program.cs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs index d622d701..c168dbdd 100644 --- a/HabitTracker.natholder/Program.cs +++ b/HabitTracker.natholder/Program.cs @@ -68,7 +68,7 @@ __ ____ __ ______ __ Console.WriteLine("update"); break; case "4": - Console.WriteLine("delete"); + Delete(); break; default: running = false; @@ -147,15 +147,23 @@ static void SelectRecords() } + //TODO: finish delete method + static void Delete() + { + DateTime date = GetDate(); + double miles = GetMiles(); + using var connection = new SqliteConnection(connectionString); + connection.Open(); + var insert = connection.CreateCommand(); + insert.CommandText = $"DELETE FROM Habits WHERE Id = {id}"; + insert.ExecuteNonQuery(); + connection.Close(); + } - static int Delete(SqliteConnection connection, DateTime date) + //TODO: Start and finish update method + static void Update() { - string DeleteRowsQuery = @" - DELETE FROM Habits WHERE date = @date"; - using var command = new SqliteCommand(DeleteRowsQuery, connection); - command.Parameters.AddWithValue("@date", date); - int rowsAffected = command.ExecuteNonQuery(); - return rowsAffected; + } From 0539fdf740e73e93927fbabb71f2558d85513ccd Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Tue, 17 Feb 2026 20:44:04 -0500 Subject: [PATCH 10/13] delete done --- HabitTracker.natholder/Program.cs | 33 +++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs index c168dbdd..80bc1493 100644 --- a/HabitTracker.natholder/Program.cs +++ b/HabitTracker.natholder/Program.cs @@ -147,16 +147,24 @@ static void SelectRecords() } - //TODO: finish delete method static void Delete() { - DateTime date = GetDate(); - double miles = GetMiles(); + int id = GetId(); using var connection = new SqliteConnection(connectionString); connection.Open(); var insert = connection.CreateCommand(); insert.CommandText = $"DELETE FROM Habits WHERE Id = {id}"; - insert.ExecuteNonQuery(); + int rowsAffected = insert.ExecuteNonQuery(); + + if (rowsAffected > 0) + { + Console.WriteLine($"Successfully deleted record with ID: {id}"); + } + else + { + Console.WriteLine($"No record found with ID: {id}"); + } + connection.Close(); } @@ -193,6 +201,23 @@ static double GetMiles() } } + static int GetId() + { + int id; + string? input; + Console.WriteLine("Enter the id of the record you would like to delete."); + input = Console.ReadLine(); + if (int.TryParse(input, out id)) + { + return id; + } + else + { + Console.WriteLine("Please enter a valid number"); + return GetId(); + } + } + static DateTime GetDate() { string? input; From 6fa68b308efa59e7545b39418f87efa5998aa580 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Tue, 17 Feb 2026 20:52:09 -0500 Subject: [PATCH 11/13] delete done --- HabitTracker.natholder/Program.cs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs index 80bc1493..ee55da63 100644 --- a/HabitTracker.natholder/Program.cs +++ b/HabitTracker.natholder/Program.cs @@ -65,7 +65,7 @@ __ ____ __ ______ __ Insert(); break; case "3": - Console.WriteLine("update"); + Update(); break; case "4": Delete(); @@ -171,7 +171,34 @@ static void Delete() //TODO: Start and finish update method static void Update() { + Console.Clear(); + SelectRecords(); + + Console.WriteLine("What is the id of the record you want to update?"); + int id = GetId(); + Console.WriteLine("Enter new values for this record:"); + DateTime newDate = GetDate(); + double newMiles = GetMiles(); + + using var connection = new SqliteConnection(connectionString); + connection.Open(); + var update = connection.CreateCommand(); + update.CommandText = $"UPDATE Habits SET Date = @date, Miles = @miles WHERE Id = {id}"; + update.Parameters.AddWithValue("@date", newDate); + update.Parameters.AddWithValue("@miles", newMiles); + int rowsAffected = update.ExecuteNonQuery(); + + if (rowsAffected > 0) + { + Console.WriteLine($"Successfully updated record with ID: {id}"); + } + else + { + Console.WriteLine($"No record found with ID: {id}"); + } + + connection.Close(); } From 8e0046b8210e51d86ac3336100f7c5cb135f3303 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Tue, 17 Feb 2026 21:00:12 -0500 Subject: [PATCH 12/13] readme --- HabitTracker.natholder/readme.md | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 HabitTracker.natholder/readme.md diff --git a/HabitTracker.natholder/readme.md b/HabitTracker.natholder/readme.md new file mode 100644 index 00000000..e01a100c --- /dev/null +++ b/HabitTracker.natholder/readme.md @@ -0,0 +1,50 @@ +# HabitTracker Console App + +A console application to log occurrences of miles (runs). The app stores entries in a SQLite database using ADO.NET and supports inserting, viewing, updating and deleting records. + +--- + +## Quick overview + +- Record format: `Date` (when the habit occurred) and `Miles` (quantity). +- Database: `HabitTracker.db` is created automatically when the app runs. +- Storage access: ADO.NET only (no EF / Dapper). Parameterized SQL is used to prevent injection. + +--- + +## How to run + +1. Open a terminal in the project folder `HabitTracker.natholder`. +2. Build & run: + + ```bash + dotnet run + ``` + +3. The app will create `HabitTracker.db` (if missing) and the `Habits` table. +4. Use the menu to Insert, View, Update, or Delete records. + +--- + +## Features + +- Creates a sqlite db and habit table on first run or if they have been deleted. +- Insert records with validated values.. +- View (select) all records +- Update an existing record +- Delete a record +- input validation and error handling + +--- + +## Thought process / retrospective + +I think that once i got started and made a little bit of progress, that the rest of the project came a lot easier. I had some trouble implementing the view command and had to use the video tutorial. I feel like I am learning though, and am continuting to improve. + +--- + +## Potential improvements (next steps) + +- Add unit tests for parsing/validation logic. + +--- From 2bbc47c5d8c14597bebe00149c8b20c750d0ade7 Mon Sep 17 00:00:00 2001 From: Nat Holder Date: Tue, 17 Feb 2026 21:13:08 -0500 Subject: [PATCH 13/13] qa fixes --- HabitTracker.natholder/Program.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/HabitTracker.natholder/Program.cs b/HabitTracker.natholder/Program.cs index ee55da63..d4d9bd15 100644 --- a/HabitTracker.natholder/Program.cs +++ b/HabitTracker.natholder/Program.cs @@ -1,9 +1,4 @@ -using System; -using System.ComponentModel.Design; -using System.Diagnostics; -using System.Runtime.CompilerServices; -using System.Security.Cryptography.X509Certificates; -using Microsoft.Data.Sqlite; +using Microsoft.Data.Sqlite; namespace HabitTracker { @@ -20,11 +15,11 @@ class Program static string connectionString = $"Data Source ={dbFileName}"; static void Main(string[] args) { - initDB(); + InitDB(); MenuLoop(); } - static void initDB() + static void InitDB() { try { @@ -168,7 +163,6 @@ static void Delete() connection.Close(); } - //TODO: Start and finish update method static void Update() { Console.Clear(); @@ -180,7 +174,7 @@ static void Update() Console.WriteLine("Enter new values for this record:"); DateTime newDate = GetDate(); double newMiles = GetMiles(); - + using var connection = new SqliteConnection(connectionString); connection.Open(); var update = connection.CreateCommand(); @@ -188,7 +182,7 @@ static void Update() update.Parameters.AddWithValue("@date", newDate); update.Parameters.AddWithValue("@miles", newMiles); int rowsAffected = update.ExecuteNonQuery(); - + if (rowsAffected > 0) { Console.WriteLine($"Successfully updated record with ID: {id}"); @@ -197,7 +191,7 @@ static void Update() { Console.WriteLine($"No record found with ID: {id}"); } - + connection.Close(); }