-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
249 lines (199 loc) · 8.34 KB
/
Program.cs
File metadata and controls
249 lines (199 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Globalization;
using Microsoft.Data.Sqlite;
namespace HabitTracker
{
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)
{
Console.WriteLine("\n\nMAIN MENU");
Console.WriteLine("\nWhat would you like to do?");
Console.WriteLine("\nType 0 to Close The 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");
Console.Write("Your action:\t");
string? command = Console.ReadLine();
switch (command)
{
case "0":
Console.WriteLine("\nGoodbye!\n");
closeApp = true;
Environment.Exit(0);
break;
case "1":
GetAllRecord();
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 GetAllRecord()
{
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"), DateTimeStyles.None),
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-MM-yyyy")} - {dw.Quantity} glasses");
}
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.ExecuteNonQuery();
connection.Close();
}
}
private static void Delete()
{
Console.Clear();
GetAllRecord();
var recordId = GetNumberInput("\n\nPlease type the Id of the record you want to delete.\nType 0 to return 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}'";
int rowCount = tableCmd.ExecuteNonQuery();
if (rowCount == 0)
{
Console.WriteLine($"\n\nRecord with Id {recordId} doesnt't exist.\n\n");
Delete();
}
connection.Close();
}
Console.WriteLine($"\n\nRecord with Id {recordId} was deleted.\n\n");
}
internal static void Update()
{
Console.Clear();
GetAllRecord();
var recordId = GetNumberInput("\n\nPlease type the Id of the record you want to update.\nType 0 to return to Main Menu.\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})";
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.ExecuteNonQuery();
connection.Close();
}
}
internal static string GetDateInput()
{
Console.WriteLine("\n\nPlease insert the date: (Format: dd-mm-yy). Type 0 to return to main menu.\n\n");
string dateInput = Console.ReadLine();
if (dateInput == "0")
GetUserInput();
while(!DateTime.TryParseExact(dateInput, "dd-MM-yy", new CultureInfo("en-US"), DateTimeStyles.None, out _))
{
Console.WriteLine("\n\nInvalid date. Format: (dd-mm-yy). Try again or type 0 to return to Main Menu.\n\n");
dateInput = Console.ReadLine();
if (dateInput == "0")
GetUserInput();
}
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 || Convert.ToInt32(numberInput) > 4)
{
Console.WriteLine("\n\nInvalid number. Try again or type 0 to return to Main Menu.\n\n");
numberInput = Console.ReadLine();
if (numberInput == "0")
GetUserInput();
}
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; }
}
}