620 lines
18 KiB
C#
620 lines
18 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
|
|
class StarProtect
|
|
{
|
|
|
|
// Box helpers
|
|
const string TL = "\u250c";
|
|
const string TR = "\u2510";
|
|
const string BL = "\u2514";
|
|
const string BR = "\u2518";
|
|
const string H = "\u2500";
|
|
const string V = "\u2502";
|
|
|
|
//ANSI colors
|
|
const string dimGray = "\u001b[38;2;90;90;90m";
|
|
const string boldCyan = "\u001b[1;36m";
|
|
const string reset = "\u001b[m";
|
|
const string red = "\u001b[31m";
|
|
const string cyan = "\u001b[36m";
|
|
|
|
// stores
|
|
const string fp_uw = "underwriters.json";
|
|
const string fp_data = "uwdata.json";
|
|
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
Console.Clear();
|
|
Login();
|
|
|
|
}
|
|
|
|
public static void Login()
|
|
{
|
|
|
|
PrintHeading("[ Star Protect Vehicle ]", boldCyan);
|
|
string home = $"1. Admin Login\n2. Underwriter Login\n3. Exit";
|
|
DrawBox(home, boldCyan);
|
|
ResetColor();
|
|
Console.Write("> ");
|
|
string inp = Console.ReadLine() ?? string.Empty;
|
|
int choice = handleChoice(inp, 3, Login);
|
|
if (choice == -1)
|
|
{
|
|
return;
|
|
}
|
|
switch (choice)
|
|
{
|
|
case 1:
|
|
AdminLogin();
|
|
break;
|
|
case 2:
|
|
UnderwriterLogin();
|
|
break;
|
|
case 3:
|
|
Environment.Exit(0);
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
// Admin Page
|
|
|
|
public static void AdminLogin()
|
|
{
|
|
Console.Clear();
|
|
PrintHeading("[ Admin Login ]", boldCyan);
|
|
string username, password;
|
|
|
|
void auth()
|
|
{
|
|
Prompt("enter username (admin):");
|
|
username = Console.ReadLine();
|
|
Prompt("enter password (admin123): ");
|
|
password = Console.ReadLine();
|
|
}
|
|
|
|
auth();
|
|
int retries = 3;
|
|
|
|
while (retries != 0)
|
|
{
|
|
if (username != "admin" || password != "admin123")
|
|
{
|
|
Warn($"Invalid username/password. ({retries--} retries left)");
|
|
auth();
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
if (retries == 0)
|
|
{
|
|
Console.Clear();
|
|
Login();
|
|
return;
|
|
}
|
|
AdminHome();
|
|
|
|
}
|
|
|
|
// Admin Home Page
|
|
|
|
public static void AdminHome()
|
|
{
|
|
Console.Clear();
|
|
PrintHeading("[ Star Protect Admin Panel ]", boldCyan);
|
|
string menu = "1. Underwriter registration\n" +
|
|
"2. Search underwriter\n" +
|
|
"3. Update underwriter password\n" +
|
|
"4. Delete underwriter\n" +
|
|
"5. View all underwriters\n" +
|
|
"6. View all vehicles registered by underwriter\n" +
|
|
"7. Return to main login menu\n" +
|
|
"8. Exit";
|
|
DrawBox(menu, boldCyan);
|
|
Prompt(">");
|
|
string inp = Console.ReadLine();
|
|
int choice = handleChoice(inp, 8, AdminHome);
|
|
if (choice == -1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
switch (choice)
|
|
{
|
|
case 1:
|
|
RegisterUnderwriter();
|
|
break;
|
|
case 2:
|
|
SearchUnderwriter();
|
|
break;
|
|
case 3:
|
|
UpdateUwPassword();
|
|
break;
|
|
case 4:
|
|
DelUnderwriter();
|
|
break;
|
|
case 5:
|
|
ViewAllUw();
|
|
break;
|
|
case 6:
|
|
ViewVehicles();
|
|
break;
|
|
case 7:
|
|
Console.Clear();
|
|
Login();
|
|
return;
|
|
case 8:
|
|
Environment.Exit(0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Admin Functions
|
|
public static void RegisterUnderwriter()
|
|
{
|
|
Console.Clear();
|
|
|
|
// check for uwdata.json
|
|
// we store underwriter index here for auto-incrementing the ID
|
|
// temporary measure before we have a functional DB
|
|
// IF file exists, parse the json data and increment the index
|
|
// ELSE create new file and set index to 1
|
|
|
|
UwData data;
|
|
if (File.Exists(fp_data))
|
|
{
|
|
string jsonString = File.ReadAllText(fp_data);
|
|
data = JsonSerializer.Deserialize<UwData>(jsonString);
|
|
data.numUw += 1;
|
|
}
|
|
else
|
|
{
|
|
data = new UwData { numUw = 1 };
|
|
}
|
|
|
|
// check for underwriters.json
|
|
// List of all underwriters
|
|
// Will enable searching
|
|
List<Underwriter> uwList;
|
|
uwList = File.Exists(fp_uw) ? JsonSerializer.Deserialize<List<Underwriter>>(File.ReadAllText(fp_uw)) :
|
|
new List<Underwriter>();
|
|
var uw = new Underwriter { ID = 100 + data.numUw };
|
|
DrawBox($"Registring New Underwriter (ID: {uw.ID})", boldCyan);
|
|
Prompt("Enter underwriter name: ");
|
|
uw.name = Console.ReadLine();
|
|
Prompt("Enter DOB (YYYY-MM-DD): ");
|
|
uw.DOB = GetDate();
|
|
Prompt("Enter date of joining: ");
|
|
uw.joinDate = GetDate();
|
|
Prompt("Enter default password for underwriter: ");
|
|
uw.defaultPassword = GetPass();
|
|
|
|
// var options = new JsonSerializerOptions { WriteIndented = true};
|
|
// string jsonstring = JsonSerializer.Serialize(uw, options);
|
|
string uwdata = $"Underwriter ID: {uw.ID}\n" +
|
|
$"Underwriter Name: {uw.name}\n" +
|
|
$"Underwriter DOB: {uw.DOB}\n" +
|
|
$"Underwriter DOJ: {uw.joinDate}\n" +
|
|
$"Underwriter Pass: {uw.defaultPassword}\n";
|
|
DrawBox(uwdata, boldCyan);
|
|
Prompt("Is the entered data correct? (y/n)");
|
|
string yOrN = GetYorN();
|
|
if (yOrN == "n")
|
|
{
|
|
RegisterUnderwriter();
|
|
return;
|
|
}
|
|
|
|
|
|
// updating the Json
|
|
uwList.Add(uw);
|
|
string newJsonString = JsonSerializer.Serialize(data);
|
|
File.WriteAllText(fp_data, newJsonString);
|
|
newJsonString = JsonSerializer.Serialize(uwList);
|
|
File.WriteAllText(fp_uw, newJsonString);
|
|
Prompt($"Registered underwriter with ID: {uw.ID}.\npress any key to continue...");
|
|
Console.ReadKey();
|
|
Console.Clear();
|
|
AdminHome();
|
|
}
|
|
|
|
// menu for searching underwriters
|
|
public static void SearchUnderwriter()
|
|
{
|
|
Console.Clear();
|
|
if (!File.Exists(fp_uw))
|
|
{
|
|
Warn("no underwriters exist, please add some before searching. Returning...");
|
|
Thread.Sleep(1500);
|
|
AdminHome();
|
|
return;
|
|
}
|
|
List<Underwriter> uwList = JsonSerializer.Deserialize<List<Underwriter>>(File.ReadAllText(fp_uw));
|
|
PrintHeading("[ Underwriter Search ]", boldCyan);
|
|
DrawBox("1. View all underwriters\n2. Search underwriter by id\n3. Return to previous menu", cyan);
|
|
Prompt(">");
|
|
string inp = Console.ReadLine();
|
|
int choice = handleChoice(inp, 3, SearchUnderwriter);
|
|
switch (choice)
|
|
{
|
|
case 1:
|
|
ViewAllUw();
|
|
break;
|
|
case 2:
|
|
SearchUnderwriterByID();
|
|
break;
|
|
case 3:
|
|
AdminHome();
|
|
return;
|
|
}
|
|
}
|
|
|
|
public static void SearchUnderwriterByID()
|
|
{
|
|
Console.Clear();
|
|
PrintHeading("[ Underwriter search by ID ]", boldCyan);
|
|
Prompt("ID >");
|
|
string inp = Console.ReadLine();
|
|
int uwid = getID(inp);
|
|
List<Underwriter> uwList = GetUw();
|
|
int uwIndex = FindUwIndex(uwList, uwid);
|
|
if (uwIndex == -1)
|
|
{
|
|
Console.Clear();
|
|
PrintHeading("[ Underwriter search by ID ]", boldCyan);
|
|
Warn($"Underwriter with ID {uwid} does not exist.");
|
|
DrawBox("1. Re-enter ID.\n2. Return to previous menu.", boldCyan);
|
|
Prompt(">");
|
|
inp = Console.ReadLine();
|
|
int choice = handleChoice(inp, 2, SearchUnderwriterByID);
|
|
switch (choice)
|
|
{
|
|
case 1:
|
|
SearchUnderwriterByID();
|
|
return;
|
|
case 2:
|
|
SearchUnderwriter();
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Underwriter uw = uwList[uwIndex];
|
|
DisplayUw(uw);
|
|
Prompt("press any key to return...");
|
|
Console.ReadKey();
|
|
SearchUnderwriter();
|
|
}
|
|
}
|
|
|
|
|
|
public static void UpdateUwPassword()
|
|
{
|
|
Console.Clear();
|
|
PrintHeading("[ Update Underwriter Password]", boldCyan);
|
|
Prompt("ID: ");
|
|
List<Underwriter> uwList = GetUw();
|
|
string inp = Console.ReadLine();
|
|
int id = getID(inp);
|
|
int index = FindUwIndex(uwList, id);
|
|
if (index == -1)
|
|
{
|
|
Warn($"Underwriter with ID {id} does not exist.");
|
|
DrawBox("1. Re-enter underwriter id\n2. Go back to previous menu", boldCyan);
|
|
inp = Console.ReadLine();
|
|
int choice = handleChoice(inp, 2, UpdateUwPassword);
|
|
switch (choice)
|
|
{
|
|
case 1:
|
|
UpdateUwPassword();
|
|
return;
|
|
case 2:
|
|
AdminHome();
|
|
return;
|
|
}
|
|
}
|
|
Underwriter uw = uwList[index];
|
|
Prompt($"Found underwriter {uw.ID} ({uw.name})\nEnter new password:\n");
|
|
string pass = GetPass();
|
|
uw.defaultPassword = pass;
|
|
uwList[index] = uw;
|
|
Prompt("updated password\n");
|
|
UpdateUw(uwList);
|
|
DisplayUw(uw);
|
|
Prompt("Press any key to return...");
|
|
Console.ReadKey();
|
|
AdminHome();
|
|
|
|
}
|
|
public static void DelUnderwriter()
|
|
{
|
|
Console.Clear();
|
|
PrintHeading("[ Delete Underwriter ]", boldCyan);
|
|
Prompt("ID: ");
|
|
List<Underwriter> uwList = GetUw();
|
|
string inp = Console.ReadLine();
|
|
int id = getID(inp);
|
|
int index = FindUwIndex(uwList, id);
|
|
if (index == -1)
|
|
{
|
|
Warn($"Underwriter with ID {id} does not exist.");
|
|
DrawBox("1. Re-enter underwriter id\n2. Go back to previous menu", boldCyan);
|
|
inp = Console.ReadLine();
|
|
int choice = handleChoice(inp, 2, DelUnderwriter);
|
|
switch (choice)
|
|
{
|
|
case 1:
|
|
DelUnderwriter();
|
|
return;
|
|
case 2:
|
|
AdminHome();
|
|
return;
|
|
}
|
|
}
|
|
Underwriter uw = uwList[index];
|
|
DisplayUw(uw);
|
|
Prompt("confirm deletion?\n1. Yes\n2. No\n>");
|
|
inp = Console.ReadLine();
|
|
int ch = handleChoice(inp, 2, DelUnderwriter);
|
|
if (ch == 2)
|
|
{
|
|
AdminHome();
|
|
return;
|
|
}
|
|
uwList.RemoveAt(index);
|
|
UpdateUw(uwList);
|
|
Prompt($"deleted underwriter {uw.ID}\n");
|
|
Prompt("Press any key to return...");
|
|
Console.ReadKey();
|
|
AdminHome();
|
|
}
|
|
public static void ViewAllUw()
|
|
{
|
|
Console.Clear();
|
|
PrintHeading("[ All Underwriters ]", boldCyan);
|
|
List<Underwriter> uwList = GetUw();
|
|
if (uwList.Count == 0)
|
|
{
|
|
Warn("No underwriters in db, please insert some...");
|
|
Thread.Sleep(1500);
|
|
AdminHome();
|
|
return;
|
|
}
|
|
string uwIdString = "";
|
|
foreach (Underwriter u in uwList)
|
|
{
|
|
uwIdString += $"{u.ID}\n";
|
|
}
|
|
DrawBox(uwIdString, boldCyan);
|
|
Prompt("Press any key to return...");
|
|
Console.ReadKey();
|
|
AdminHome();
|
|
}
|
|
|
|
public static void ViewVehicles() { }
|
|
|
|
|
|
// Underwriter Page
|
|
|
|
public static void UnderwriterLogin()
|
|
{
|
|
|
|
}
|
|
|
|
// set a color for console text
|
|
public static void SetColor(ConsoleColor color)
|
|
{
|
|
Console.ForegroundColor = color;
|
|
}
|
|
|
|
// reset console text color
|
|
public static void ResetColor()
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
}
|
|
|
|
|
|
// function to draw a box around the text
|
|
public static void DrawBox(string text, string colorCode)
|
|
{
|
|
string reset = "\u001b[m";
|
|
|
|
string[] lines = text.Split("\n");
|
|
int width = lines.Max(l => l.Length);
|
|
|
|
string topborder = TL + Repeat(H, width + 2) + TR;
|
|
string bottomborder = BL + Repeat(H, width + 2) + BR;
|
|
Console.WriteLine($"{colorCode}{topborder}{reset}");
|
|
foreach (string line in lines)
|
|
{
|
|
string paddedLine = line.PadRight(width);
|
|
Console.WriteLine($"{colorCode}{V}{reset} {paddedLine} {colorCode}{V}{reset}");
|
|
|
|
}
|
|
Console.WriteLine($"{colorCode}{bottomborder}{colorCode}");
|
|
|
|
}
|
|
|
|
// numOpts: number of options in menu
|
|
// func: the function to call if input validation fails
|
|
// we will recursively call the function till a valid input it registered
|
|
|
|
public static int handleChoice(string inp, int numOpts, Action func)
|
|
{
|
|
if (!int.TryParse(inp, out int choice))
|
|
{
|
|
Warn("Input must be a number.");
|
|
Thread.Sleep(2000);
|
|
Console.Clear();
|
|
func();
|
|
return -1;
|
|
}
|
|
if ((choice < 1) || (choice > numOpts))
|
|
{
|
|
Warn("Invalid choice entered.");
|
|
Thread.Sleep(2000);
|
|
Console.Clear();
|
|
func();
|
|
return -1;
|
|
}
|
|
return choice;
|
|
}
|
|
|
|
// helper func to validate ID
|
|
public static int getID(string inp)
|
|
{
|
|
int uwid;
|
|
while (!int.TryParse(inp, out uwid))
|
|
{
|
|
Warn("input is not a valid ID. only positive numeric values are allowed.");
|
|
Prompt("ID >");
|
|
inp = Console.ReadLine();
|
|
}
|
|
return uwid;
|
|
}
|
|
|
|
// Helper Function to get a valid password
|
|
public static string GetPass()
|
|
{
|
|
string pass = Console.ReadLine();
|
|
while (!CheckPass(pass))
|
|
{
|
|
Warn("password must be alphanumeric with at least one special character, please retry.");
|
|
Prompt(">");
|
|
pass = Console.ReadLine();
|
|
}
|
|
return pass;
|
|
}
|
|
|
|
// function to check if password is valid
|
|
public static bool CheckPass(string pass)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(pass)) return false;
|
|
bool hasLetter = false;
|
|
bool hasDigit = false;
|
|
bool hasSpecial = false;
|
|
|
|
foreach (char ch in pass)
|
|
{
|
|
if (char.IsLetter(ch)) hasLetter = true;
|
|
else if (char.IsDigit(ch)) hasDigit = true;
|
|
else hasSpecial = true;
|
|
}
|
|
return hasLetter && hasDigit && hasSpecial;
|
|
}
|
|
|
|
|
|
// Helper Function to get a valid date
|
|
public static DateOnly GetDate()
|
|
{
|
|
DateOnly date;
|
|
string inp = Console.ReadLine();
|
|
while (!DateOnly.TryParse(inp, out date))
|
|
{
|
|
Warn("Invalid date entered, please retry");
|
|
Prompt(">");
|
|
inp = Console.ReadLine();
|
|
}
|
|
return date;
|
|
}
|
|
|
|
// Helper func to get yes or no
|
|
public static string GetYorN()
|
|
{
|
|
string inp = Console.ReadLine();
|
|
inp = inp.ToLower();
|
|
while (inp != "y" && inp != "n")
|
|
{
|
|
Warn("invalid response.");
|
|
Prompt(">");
|
|
inp = Console.ReadLine();
|
|
inp = inp.ToLower();
|
|
}
|
|
return inp;
|
|
}
|
|
|
|
// function to display underwriter
|
|
public static void DisplayUw(Underwriter uw)
|
|
{
|
|
string uwdata = $"Underwriter ID: {uw.ID}\n" +
|
|
$"Underwriter Name: {uw.name}\n" +
|
|
$"Underwriter DOB: {uw.DOB}\n" +
|
|
$"Underwriter DOJ: {uw.joinDate}\n" +
|
|
$"Underwriter Pass: {uw.defaultPassword}\n";
|
|
DrawBox(uwdata, boldCyan);
|
|
|
|
}
|
|
|
|
// get underwriters
|
|
public static List<Underwriter> GetUw()
|
|
{
|
|
List<Underwriter> uwList;
|
|
uwList = File.Exists(fp_uw) ? JsonSerializer.Deserialize<List<Underwriter>>(File.ReadAllText(fp_uw)) :
|
|
new List<Underwriter>();
|
|
return uwList;
|
|
}
|
|
|
|
// update underwriters
|
|
public static void UpdateUw(List<Underwriter> uwList)
|
|
{
|
|
string jsonString = JsonSerializer.Serialize(uwList);
|
|
File.WriteAllText(fp_uw, jsonString);
|
|
}
|
|
|
|
public static void DelUw(int id)
|
|
{
|
|
List<Underwriter> uwList = GetUw();
|
|
int index = FindUwIndex(uwList, id);
|
|
uwList.RemoveAt(index);
|
|
UpdateUw(uwList);
|
|
}
|
|
|
|
public static int FindUwIndex(List<Underwriter> uwList, int id)
|
|
{
|
|
for (int i = 0; i < uwList.Count; i++)
|
|
{
|
|
if (uwList[i].ID == id)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Helper functions
|
|
|
|
public static void Warn(string text) => Console.WriteLine($"{red}{text}{reset}");
|
|
|
|
public static void Prompt(string text) => Console.Write($"{cyan}{text}{reset}");
|
|
|
|
public static void PrintHeading(string text, string colorCode) => Console.WriteLine($"{colorCode}{text}{reset}");
|
|
|
|
public static string Repeat(string c, int count) => new StringBuilder().Insert(0, c, count).ToString();
|
|
}
|
|
|
|
|
|
// Underwriter class for JSON operation
|
|
|
|
public class Underwriter
|
|
{
|
|
public int ID { get; set; }
|
|
public string name { get; set; }
|
|
public DateOnly DOB { get; set; }
|
|
public DateOnly joinDate { get; set; }
|
|
public string defaultPassword { get; set; }
|
|
}
|
|
|
|
// store number of underwriters who exist, for auto ID generation
|
|
public class UwData
|
|
{
|
|
public int numUw { get; set; }
|
|
} |