Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HabitTracker.DzemalKurtic/HabitTracker.DzemalKurtic.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="HabitTracker.DzemalKurtic/HabitTracker.DzemalKurtic.csproj" />
</Solution>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.11" />
</ItemGroup>

</Project>
260 changes: 260 additions & 0 deletions HabitTracker.DzemalKurtic/HabitTracker.DzemalKurtic/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
using Microsoft.Data.Sqlite;
using System.Globalization;

namespace HabitTracker.DzemalKurtic
{
internal class Program
{
static string connectionString = @"Data Source=habit-Tracker.db";
static void Main(string[] args)
{
using (var connection = new SqliteConnection(connectionString))
{

connection.Open();
var tableCmd = connection.CreateCommand();

tableCmd.CommandText =
@"CREATE TABLE IF NOT EXISTS drinking_water (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Date TEXT,
Quantity INTEGER
)";

tableCmd.ExecuteNonQuery();

connection.Close();

}

GetUserInput();
}

static void GetUserInput()
{
Console.Clear();
bool closeApp = false;
while (closeApp == false)
{
Console.WriteLine("\n\nMAIN MENU");
Console.WriteLine("\nWhat would you like to do?");
Console.WriteLine("\nType 0 to Close Application.");
Console.WriteLine("Type 1 to View All Records.");
Console.WriteLine("Type 2 to Insert Record.");
Console.WriteLine("Type 3 to Delete Record.");
Console.WriteLine("Type 4 to Update Record.");
Console.WriteLine("------------------------------------------\n");

string command = Console.ReadLine();

switch (command)
{
case "0":
Console.WriteLine("\nGoodbye!\n");
closeApp = true;
Environment.Exit(0);
break;
case "1":
GetAllRecords();
break;
case "2":
Insert();
break;
case "3":
Delete();
break;
case "4":
Update();
break;
default:
Console.WriteLine("\nInvalid Command. Please type a number from 0 to 4.\n");
break;
}
}

}

private static void GetAllRecords()
{
Console.Clear();
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var tableCmd = connection.CreateCommand();
tableCmd.CommandText =
$"SELECT * FROM drinking_water ";

List<DrinkingWater> tableData = new();

SqliteDataReader reader = tableCmd.ExecuteReader();

if (reader.HasRows)
{
while (reader.Read())
{
tableData.Add(
new DrinkingWater
{
Id = reader.GetInt32(0),
Date = DateTime.ParseExact(reader.GetString(1), "dd-MM-yy", new CultureInfo("en-US")),
Quantity = reader.GetInt32(2)
}); ;
}
}
else
{
Console.WriteLine("No rows found");
}

connection.Close();

Console.WriteLine("------------------------------------------\n");
foreach (var dw in tableData)
{
Console.WriteLine($"{dw.Id} - {dw.Date.ToString("dd-MMM-yyyy")} - Quantity: {dw.Quantity}");
}
Console.WriteLine("------------------------------------------\n");
}
}

private static void Insert()
{
string date = GetDateInput();

int quantity = GetNumberInput("\n\nPlease insert number of glasses or other measure of your choice (no decimals allowed)\n\n");

using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var tableCmd = connection.CreateCommand();

tableCmd.CommandText =
"INSERT INTO drinking_water(date, quantity) VALUES(@date, @quantity)";

tableCmd.Parameters.AddWithValue("@date", date);
tableCmd.Parameters.AddWithValue("@quantity", quantity);

tableCmd.ExecuteNonQuery();

connection.Close();
}
}

private static void Delete()
{
Console.Clear();
GetAllRecords();

var recordId = GetNumberInput("\n\nPlease type the Id of the record you want to delete or type 0 to go back to Main Menu\n\n");

using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var tableCmd = connection.CreateCommand();

tableCmd.CommandText = "DELETE from drinking_water WHERE Id = @recordId";

tableCmd.Parameters.AddWithValue("@recordId", recordId);

int rowCount = tableCmd.ExecuteNonQuery();

if (rowCount == 0)
{
Console.WriteLine($"\n\nRecord with Id {recordId} doesn't exist. \n\n");
Delete();
}

}

Console.WriteLine($"\n\nRecord with Id {recordId} was deleted. \n\n");

GetUserInput();
}

internal static void Update()
{
GetAllRecords();

var recordId = GetNumberInput("\n\nPlease type Id of the record would like to update. Type 0 to return to main manu.\n\n");

using (var connection = new SqliteConnection(connectionString))
{
connection.Open();

var checkCmd = connection.CreateCommand();
checkCmd.CommandText = "SELECT EXISTS(SELECT 1 FROM drinking_water WHERE Id = @recordId)";

checkCmd.Parameters.AddWithValue("@recordId", recordId);

int checkQuery = Convert.ToInt32(checkCmd.ExecuteScalar());

if (checkQuery == 0)
{
Console.WriteLine($"\n\nRecord with Id {recordId} doesn't exist.\n\n");
connection.Close();
Update();
}

string date = GetDateInput();

int quantity = GetNumberInput("\n\nPlease insert number of glasses or other measure of your choice (no decimals allowed)\n\n");

var tableCmd = connection.CreateCommand();
tableCmd.CommandText = "UPDATE drinking_water SET date = @date, quantity = @quantity WHERE Id = @recordId";

tableCmd.Parameters.AddWithValue("@date", date);
tableCmd.Parameters.AddWithValue("@quantity", quantity);
tableCmd.Parameters.AddWithValue("@recordId", recordId);

tableCmd.ExecuteNonQuery();

connection.Close();
}
}

internal static string GetDateInput()
{
Console.WriteLine("\n\nPlease insert the date: (Format: dd-mm-yy). Type 0 to return to main manu. If you want to add today's date type 1.\n\n");

string dateInput = Console.ReadLine();

if (dateInput == "0") GetUserInput();

if (dateInput == "1") return DateTime.Now.Date.ToString("dd-MM-yy");

while (!DateTime.TryParseExact(dateInput, "dd-MM-yy", new CultureInfo("en-US"), DateTimeStyles.None, out _))
{
Console.WriteLine("\n\nInvalid date. (Format: dd-mm-yy). Type 0 to return to main manu or try again:\n\n");
dateInput = Console.ReadLine();
}

return dateInput;
}

internal static int GetNumberInput(string message)
{
Console.WriteLine(message);

string numberInput = Console.ReadLine();

if (numberInput == "0") GetUserInput();

while (!Int32.TryParse(numberInput, out _) || Convert.ToInt32(numberInput) < 0)
{
Console.WriteLine("\n\nInvalid number. Try again.\n\n");
numberInput = Console.ReadLine();
}

int finalInput = Convert.ToInt32(numberInput);

return finalInput;
}
}

public class DrinkingWater
{
public int Id { get; set; }
public DateTime Date { get; set; }
public int Quantity { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"HabitTracker.DzemalKurtic": {
"commandName": "Project",
"workingDirectory": "C:\\Users\\User\\Desktop\\CSharpAcademy\\CodeReviews.Console.HabitTracker\\HabitTracker.DzemalKurtic\\HabitTracker.DzemalKurtic"
}
}
}
69 changes: 69 additions & 0 deletions HabitTracker.DzemalKurtic/HabitTracker.DzemalKurtic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
**Project requirements:**



* Logging occurrence of a habit (by quantity)
* Users need to be bale to input the date of the habit
* App should use a real database
* Users should be able to insert, delete, update and view their habit
* All input errors should be handled
* Only ADO.NET should be used



**How the App works?**



When a user starts the app, he sees a menu with options:



0 - close the app

1 - view all records

2 - insert a record

3 - delete a record

4 - update a record



&#x20;

Clicking 1 will show him all the habits that are recorded in the database.



Clicking 2 will open another screen where he will be able to enter a date in dd-mm-yy format.

Clicking 1 will enter today's date.

After that he will be able to enter the quantity.



Clicking 3 will bring up a new screen where a use will be able to delete the habits that are already recorded.

You must use Id of the record that exists in the database.



Clicking 4 will bring up a new screen where a user will be able to update an existing record.

You must use Id of the record that exists in the database.

After choosing an Id user will be able to enter a new date for the habit.



**What was hard:**



Working with Sqlite was a new experience for me since I never worked without an ORM.

Update functionality was the hardest one.