From 1aed1b2dbbb519ece46c837161c5802edb740575 Mon Sep 17 00:00:00 2001 From: Subodh Singh Date: Thu, 30 Apr 2026 09:27:44 +0000 Subject: [PATCH] Upload files to "/" --- Program.cs | 1959 ++++++++++++++++++++++++++-------------------------- 1 file changed, 997 insertions(+), 962 deletions(-) diff --git a/Program.cs b/Program.cs index 24f5dcb..2003a59 100644 --- a/Program.cs +++ b/Program.cs @@ -1,963 +1,998 @@ -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"; - - const string fp_vehicle = "vehicles.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(jsonString); - data.numUw += 1; - } - else - { - data = new UwData { numUw = 1 }; - } - - // check for underwriters.json - // List of all underwriters - // Will enable searching - List uwList; - uwList = File.Exists(fp_uw) ? JsonSerializer.Deserialize>(File.ReadAllText(fp_uw)) : - new List(); - 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 uwList = JsonSerializer.Deserialize>(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 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 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 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 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() - { - Console.Clear(); - PrintHeading("[ View Vehicles Registered by Underwriter ]", boldCyan); - Prompt("Enter underwriter ID: "); - string inp = Console.ReadLine(); - int uwid = getID(inp); - List uwList = GetUw(); - int index = FindUwIndex(uwList, uwid); - if (index == -1) - { - Warn($"Underwriter with ID {uwid} does not exist."); - DrawBox("1. Re-enter underwriter id\n2. Go back to previous menu", boldCyan); - inp = Console.ReadLine(); - int choice = handleChoice(inp, 2, ViewVehicles); - switch (choice) - { - case 1: - ViewVehicles(); - return; - case 2: - AdminHome(); - return; - } - } - List vehicleList = GetVehicles(uwid); - if (vehicleList.Count == 0) - { - Warn($"No vehicles registered under underwriter {uwid}"); - Thread.Sleep(1500); - AdminHome(); - return; - } - foreach (VehicleInsurance v in vehicleList) - { - DisplayVehicle(v); - - } - - } - - - // Underwriter Page - - public static void UnderwriterLogin() - { - Console.Clear(); - PrintHeading("[ Underwriter Login ]", boldCyan); - Prompt("Enter underwriter ID: "); - string inp = Console.ReadLine(); - int uwid = getID(inp); - List uwList = GetUw(); - int index = FindUwIndex(uwList, uwid); - if (index == -1) - { - Warn($"Underwriter with ID {uwid} does not exist."); - DrawBox("1. Re-enter underwriter id\n2. Return to main menu", boldCyan); - inp = Console.ReadLine(); - int choice = handleChoice(inp, 2, UnderwriterLogin); - switch (choice) - { - case 1: - UnderwriterLogin(); - return; - case 2: - Console.Clear(); - Login(); - return; - } - } - Underwriter uw = uwList[index]; - Prompt("Enter password: "); - string pass = GetPass(); - if (pass != uw.defaultPassword) - { - Warn("Incorrect password."); - Thread.Sleep(1500); - UnderwriterLogin(); - return; - } - UnderwriterHome(uw); - } - - public static void UnderwriterHome(Underwriter uw) - { - Console.Clear(); - PrintHeading($"[ Welcome {uw.name} ]", boldCyan); - string menu = "1. Create new vehicle insurance\n" + - "2. Renew existing policy\n" + - "3. Change policy type\n" + - "4. View all registered vehicles\n" + - "5. Return to main login menu"; - DrawBox(menu, boldCyan); - Prompt(">"); - string inp = Console.ReadLine(); - int choice = handleChoice(inp, 5, () => UnderwriterHome(uw)); - if (choice == -1) - { - return; - } - switch (choice) - { - case 1: - CreateVehicleInsurance(uw); - break; - case 2: - RenewPolicy(uw); - break; - case 3: - ChangePolicyType(uw); - break; - case 4: - ViewRegisteredVehicles(uw); - break; - case 5: - Console.Clear(); - Login(); - return; - } - } - - public static void CreateVehicleInsurance(Underwriter uw) - { - Console.Clear(); - PrintHeading("[ Create New Vehicle Insurance ]", boldCyan); - var vehicle = new VehicleInsurance { policyNo = uw.ID }; - Prompt("Enter vehicle number: "); - vehicle.vehicleNo = Console.ReadLine(); - Prompt("Enter vehicle type: "); - vehicle.vehicleType = Console.ReadLine(); - Prompt("Enter customer name: "); - vehicle.customerName = Console.ReadLine(); - Prompt("Enter engine number: "); - vehicle.engineNo = Console.ReadLine(); - Prompt("Enter chasis number: "); - vehicle.chasisNo = Console.ReadLine(); - Prompt("Enter phone number: "); - vehicle.phoneNo = Console.ReadLine(); - Prompt("Enter premium amount: "); - string inp = Console.ReadLine(); - while (!decimal.TryParse(inp, out decimal premium)) - { - Warn("Invalid amount entered, please retry."); - Prompt(">"); - inp = Console.ReadLine(); - } - vehicle.premiumAmnt = decimal.Parse(inp); - Prompt("Enter insurance type (Full Insurance/Third Party): "); - string type = Console.ReadLine().ToLower(); - while (type != "full insurance" && type != "third party") - { - Warn("Invalid insurance type entered, please retry."); - Prompt(">"); - type = Console.ReadLine().ToLower(); - } - vehicle.type = type; - Prompt("Enter policy start date: "); - vehicle.fromDate = GetDate(); - Prompt("Enter policy end date: "); - vehicle.toDate = GetDate(); - - AddVehicle(vehicle); - DrawBox("Vehicle insurance created successfully!", boldCyan); - Prompt("Press any key to return..."); - Console.ReadKey(); - UnderwriterHome(uw); - } - - - public static void RenewPolicy(Underwriter uw) - { - Console.Clear(); - PrintHeading("[ Renew Vehicle Insurance ]", boldCyan); - Prompt("Enter policy number: "); - string inp = Console.ReadLine(); - int policyNo = getID(inp); - List vehicleList = GetVehicles(uw.ID); - VehicleInsurance vehicle = vehicleList.FirstOrDefault(v => v.policyNo == policyNo); - if (vehicle == null) - { - Warn($"No vehicle insurance found with policy number {policyNo}"); - Thread.Sleep(1500); - UnderwriterHome(uw); - return; - } - DisplayVehicle(vehicle); - Prompt("Press R to confirm renewal, any other key to cancel."); - string choice = Console.ReadLine().ToLower(); - if (choice != "r") - { - UnderwriterHome(uw); - return; - } - Prompt("Enter updated premium amount: "); - inp = Console.ReadLine(); - while (!decimal.TryParse(inp, out decimal premium)) - { - Warn("Invalid amount entered, please retry."); - Prompt(">"); - inp = Console.ReadLine(); - } - VehicleInsurance newVehicle = new VehicleInsurance - { - policyNo = uw.ID, - vehicleNo = vehicle.vehicleNo, - vehicleType = vehicle.vehicleType, - customerName = vehicle.customerName, - engineNo = vehicle.engineNo, - chasisNo = vehicle.chasisNo, - phoneNo = vehicle.phoneNo, - type = vehicle.type, - premiumAmnt = decimal.Parse(inp) - }; - if (DateOnly.FromDateTime(DateTime.Now) > vehicle.toDate) - { - newVehicle.fromDate = DateOnly.FromDateTime(DateTime.Now); - newVehicle.toDate = newVehicle.fromDate.AddDays(365); - } - else - { - newVehicle.fromDate = vehicle.toDate.AddDays(1); - newVehicle.toDate = newVehicle.fromDate.AddDays(365); - } - AddVehicle(newVehicle); - DrawBox("Vehicle insurance renewed successfully!", boldCyan); - Prompt("Press any key to return..."); - Console.ReadKey(); - UnderwriterHome(uw); - } - - public static void ChangePolicyType(Underwriter uw) - { - Console.Clear(); - PrintHeading("[ Change Policy Type ]", boldCyan); - Prompt("Enter policy number: "); - string inp = Console.ReadLine(); - int policyNo = getID(inp); - List vehicleList = GetVehicles(uw.ID); - VehicleInsurance vehicle = vehicleList.FirstOrDefault(v => v.policyNo == policyNo); - if (vehicle == null) - { - Warn($"No vehicle insurance found with policy number {policyNo}"); - Thread.Sleep(1500); - UnderwriterHome(uw); - return; - } - DisplayVehicle(vehicle); - Prompt("Enter new policy type (Full Insurance/Third Party): "); - string type = Console.ReadLine().ToLower(); - while (type != "full insurance" && type != "third party") - { - Warn("Invalid insurance type entered, please retry."); - Prompt(">"); - type = Console.ReadLine().ToLower(); - } - vehicle.type = type; - UpdateVehicleInsurance(vehicleList); - DrawBox("Policy type updated successfully!", boldCyan); - Prompt("Press any key to return..."); - Console.ReadKey(); - UnderwriterHome(uw); - } - - public static void ViewRegisteredVehicles(Underwriter uw) - { - Console.Clear(); - PrintHeading("[ View Registered Vehicles ]", boldCyan); - List vehicleList = GetVehicles(uw.ID); - if (vehicleList.Count == 0) - { - Warn("No vehicles registered under your name."); - Thread.Sleep(1500); - UnderwriterHome(uw); - return; - } - foreach (VehicleInsurance v in vehicleList) - { - DisplayVehicle(v); - } - Prompt("Press any key to return..."); - Console.ReadKey(); - UnderwriterHome(uw); - } - - // 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 vehicle Insurance for underwriter - public static List GetVehicles(int uwid) - { - List vehicleList; - if (File.Exists(fp_vehicle)) - { - vehicleList = JsonSerializer.Deserialize>(File.ReadAllText(fp_vehicle)); - return vehicleList.Where(v => v.policyNo == uwid).ToList(); - } - else - { - return new List(); - } - } - - // get underwriters - public static List GetUw() - { - List uwList; - uwList = File.Exists(fp_uw) ? JsonSerializer.Deserialize>(File.ReadAllText(fp_uw)) : - new List(); - return uwList; - } - - // update underwriters - public static void UpdateUw(List uwList) - { - string jsonString = JsonSerializer.Serialize(uwList); - File.WriteAllText(fp_uw, jsonString); - } - - public static void DelUw(int id) - { - List uwList = GetUw(); - int index = FindUwIndex(uwList, id); - uwList.RemoveAt(index); - UpdateUw(uwList); - } - - public static int FindUwIndex(List uwList, int id) - { - for (int i = 0; i < uwList.Count; i++) - { - if (uwList[i].ID == id) - { - return i; - } - } - return -1; - } - - public static void UpdateVehicleInsurance(List vehicleList) - { - string jsonString = JsonSerializer.Serialize(vehicleList); - File.WriteAllText(fp_vehicle, jsonString); - } - - public static void AddVehicle(VehicleInsurance vehicle) - { - List vehicleList; - if (File.Exists(fp_vehicle)) - { - vehicleList = JsonSerializer.Deserialize>(File.ReadAllText(fp_vehicle)); - } - else - { - vehicleList = new List(); - } - vehicleList.Add(vehicle); - UpdateVehicleInsurance(vehicleList); - } - - public static void DisplayVehicle(VehicleInsurance vehicle) - { - string vehicleData = $"Policy No: {vehicle.policyNo}\n" + - $"Vehicle No: {vehicle.vehicleNo}\n" + - $"Vehicle Type: {vehicle.vehicleType}\n" + - $"Customer Name: {vehicle.customerName}\n" + - $"Engine No: {vehicle.engineNo}\n" + - $"Chasis No: {vehicle.chasisNo}\n" + - $"Phone No: {vehicle.phoneNo}\n" + - $"Premium Amount: {vehicle.premiumAmnt}\n" + - $"Insurance Type: {vehicle.type}\n" + - $"From Date: {vehicle.fromDate}\n" + - $"To Date: {vehicle.toDate}\n"; - DrawBox(vehicleData, boldCyan); - } - // 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; } -} - -public class VehicleInsurance -{ - public int policyNo { get; set; } - public string vehicleNo { get; set; } - public string vehicleType { get; set; } - public string customerName { get; set; } - public string engineNo { get; set; } - public string chasisNo { get; set; } - public string phoneNo { get; set; } - public decimal premiumAmnt { get; set; } - public string type { get; set; } - public DateOnly fromDate { get; set; } - public DateOnly toDate { get; set; } -} - -// store number of underwriters who exist, for auto ID generation -public class UwData -{ - public int numUw { get; set; } +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"; + + const string fp_vehicle = "vehicles.json"; + const string fp_pdata = "policydata.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(jsonString); + data.numUw += 1; + } + else + { + data = new UwData { numUw = 1 }; + } + + // check for underwriters.json + // List of all underwriters + // Will enable searching + List uwList; + uwList = File.Exists(fp_uw) ? JsonSerializer.Deserialize>(File.ReadAllText(fp_uw)) : + new List(); + 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 uwList = JsonSerializer.Deserialize>(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 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 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 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 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() + { + Console.Clear(); + PrintHeading("[ View Vehicles Registered by Underwriter ]", boldCyan); + Prompt("Enter underwriter ID: "); + string inp = Console.ReadLine(); + int uwid = getID(inp); + List uwList = GetUw(); + int index = FindUwIndex(uwList, uwid); + if (index == -1) + { + Warn($"Underwriter with ID {uwid} does not exist."); + DrawBox("1. Re-enter underwriter id\n2. Go back to previous menu", boldCyan); + inp = Console.ReadLine(); + int choice = handleChoice(inp, 2, ViewVehicles); + switch (choice) + { + case 1: + ViewVehicles(); + return; + case 2: + AdminHome(); + return; + } + } + List vehicleList = GetVehicles(uwid); + if (vehicleList.Count == 0) + { + Warn($"No vehicles registered under underwriter {uwid}"); + Thread.Sleep(1500); + AdminHome(); + return; + } + foreach (VehicleInsurance v in vehicleList) + { + DisplayVehicle(v); + } + Prompt("Press any key to return..."); + Console.ReadKey(); + AdminHome(); + + } + + + // Underwriter Page + + public static void UnderwriterLogin() + { + Console.Clear(); + PrintHeading("[ Underwriter Login ]", boldCyan); + Prompt("Enter underwriter ID: "); + string inp = Console.ReadLine(); + int uwid = getID(inp); + List uwList = GetUw(); + int index = FindUwIndex(uwList, uwid); + if (index == -1) + { + Warn($"Underwriter with ID {uwid} does not exist."); + DrawBox("1. Re-enter underwriter id\n2. Return to main menu", boldCyan); + inp = Console.ReadLine(); + int choice = handleChoice(inp, 2, UnderwriterLogin); + switch (choice) + { + case 1: + UnderwriterLogin(); + return; + case 2: + Console.Clear(); + Login(); + return; + } + } + Underwriter uw = uwList[index]; + Prompt("Enter password: "); + string pass = GetPass(); + if (pass != uw.defaultPassword) + { + Warn("Incorrect password."); + Thread.Sleep(1500); + UnderwriterLogin(); + return; + } + UnderwriterHome(uw); + } + + public static void UnderwriterHome(Underwriter uw) + { + Console.Clear(); + PrintHeading($"[ Welcome {uw.name} ]", boldCyan); + string menu = "1. Create new vehicle insurance\n" + + "2. Renew existing policy\n" + + "3. Change policy type\n" + + "4. View all registered vehicles\n" + + "5. Return to main login menu"; + DrawBox(menu, boldCyan); + Prompt(">"); + string inp = Console.ReadLine(); + int choice = handleChoice(inp, 5, () => UnderwriterHome(uw)); + if (choice == -1) + { + return; + } + switch (choice) + { + case 1: + CreateVehicleInsurance(uw); + break; + case 2: + RenewPolicy(uw); + break; + case 3: + ChangePolicyType(uw); + break; + case 4: + ViewRegisteredVehicles(uw); + break; + case 5: + Console.Clear(); + Login(); + return; + } + } + + public static void CreateVehicleInsurance(Underwriter uw) + { + Console.Clear(); + PrintHeading("[ Create New Vehicle Insurance ]", boldCyan); + IncrementPolicyData(); + PolicyData pd = GetPolicyData(); + var vehicle = new VehicleInsurance { policyNo = pd.numPol }; + vehicle.uwId = uw.ID; + DrawBox($"Policy #{vehicle.policyNo}", boldCyan); + ResetColor(); + Prompt("Enter vehicle number: "); + vehicle.vehicleNo = Console.ReadLine(); + Prompt("Enter vehicle type: "); + vehicle.vehicleType = Console.ReadLine(); + Prompt("Enter customer name: "); + vehicle.customerName = Console.ReadLine(); + Prompt("Enter engine number: "); + vehicle.engineNo = Console.ReadLine(); + Prompt("Enter chasis number: "); + vehicle.chasisNo = Console.ReadLine(); + Prompt("Enter phone number: "); + vehicle.phoneNo = Console.ReadLine(); + Prompt("Enter premium amount: "); + string inp = Console.ReadLine(); + while (!decimal.TryParse(inp, out decimal premium)) + { + Warn("Invalid amount entered, please retry."); + Prompt(">"); + inp = Console.ReadLine(); + } + vehicle.premiumAmnt = decimal.Parse(inp); + Prompt("Enter insurance type (Full Insurance/Third Party): "); + string type = Console.ReadLine().ToLower(); + while (type != "full insurance" && type != "third party") + { + Warn("Invalid insurance type entered, please retry."); + Prompt(">"); + type = Console.ReadLine().ToLower(); + } + vehicle.type = type; + Prompt("Enter policy start date: "); + vehicle.fromDate = GetDate(); + Prompt("Enter policy end date: "); + vehicle.toDate = GetDate(); + + AddVehicle(vehicle); + DrawBox("Vehicle insurance created successfully!", boldCyan); + Prompt("Press any key to return..."); + Console.ReadKey(); + UnderwriterHome(uw); + } + + + public static void RenewPolicy(Underwriter uw) + { + Console.Clear(); + PrintHeading("[ Renew Vehicle Insurance ]", boldCyan); + Prompt("Enter policy number: "); + string inp = Console.ReadLine(); + int policyNo = getID(inp); + List vehicleList = GetVehicles(uw.ID); + VehicleInsurance vehicle = vehicleList.FirstOrDefault(v => v.policyNo == policyNo); + if (vehicle == null) + { + Warn($"No vehicle insurance found with policy number {policyNo}"); + Thread.Sleep(1500); + UnderwriterHome(uw); + return; + } + DisplayVehicle(vehicle); + Prompt("Press R to confirm renewal, any other key to cancel."); + string choice = Console.ReadLine().ToLower(); + if (choice != "r") + { + UnderwriterHome(uw); + return; + } + Prompt("Enter updated premium amount: "); + inp = Console.ReadLine(); + while (!decimal.TryParse(inp, out decimal premium)) + { + Warn("Invalid amount entered, please retry."); + Prompt(">"); + inp = Console.ReadLine(); + } + VehicleInsurance newVehicle = new VehicleInsurance + { + policyNo = uw.ID, + vehicleNo = vehicle.vehicleNo, + vehicleType = vehicle.vehicleType, + customerName = vehicle.customerName, + engineNo = vehicle.engineNo, + chasisNo = vehicle.chasisNo, + phoneNo = vehicle.phoneNo, + type = vehicle.type, + premiumAmnt = decimal.Parse(inp) + }; + if (DateOnly.FromDateTime(DateTime.Now) > vehicle.toDate) + { + newVehicle.fromDate = DateOnly.FromDateTime(DateTime.Now); + newVehicle.toDate = newVehicle.fromDate.AddDays(365); + } + else + { + newVehicle.fromDate = vehicle.toDate.AddDays(1); + newVehicle.toDate = newVehicle.fromDate.AddDays(365); + } + AddVehicle(newVehicle); + DrawBox("Vehicle insurance renewed successfully!", boldCyan); + Prompt("Press any key to return..."); + Console.ReadKey(); + UnderwriterHome(uw); + } + + public static void ChangePolicyType(Underwriter uw) + { + Console.Clear(); + PrintHeading("[ Change Policy Type ]", boldCyan); + Prompt("Enter policy number: "); + string inp = Console.ReadLine(); + int policyNo = getID(inp); + List vehicleList = GetVehicles(uw.ID); + VehicleInsurance vehicle = vehicleList.FirstOrDefault(v => v.policyNo == policyNo); + if (vehicle == null) + { + Warn($"No vehicle insurance found with policy number {policyNo}"); + Thread.Sleep(1500); + UnderwriterHome(uw); + return; + } + DisplayVehicle(vehicle); + Prompt("Enter new policy type (Full Insurance/Third Party): "); + string type = Console.ReadLine().ToLower(); + while (type != "full insurance" && type != "third party") + { + Warn("Invalid insurance type entered, please retry."); + Prompt(">"); + type = Console.ReadLine().ToLower(); + } + vehicle.type = type; + UpdateVehicleInsurance(vehicleList); + DrawBox("Policy type updated successfully!", boldCyan); + Prompt("Press any key to return..."); + Console.ReadKey(); + UnderwriterHome(uw); + } + + public static void ViewRegisteredVehicles(Underwriter uw) + { + Console.Clear(); + PrintHeading("[ View Registered Vehicles ]", boldCyan); + List vehicleList = GetVehicles(uw.ID); + if (vehicleList.Count == 0) + { + Warn("No vehicles registered under your name."); + Thread.Sleep(1500); + UnderwriterHome(uw); + return; + } + foreach (VehicleInsurance v in vehicleList) + { + DisplayVehicle(v); + } + Prompt("Press any key to return..."); + Console.ReadKey(); + UnderwriterHome(uw); + } + + // 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 vehicle Insurance for underwriter + public static List GetVehicles(int uwid) + { + List vehicleList; + if (File.Exists(fp_vehicle)) + { + vehicleList = JsonSerializer.Deserialize>(File.ReadAllText(fp_vehicle)); + return vehicleList.Where(v => v.uwId == uwid).ToList(); + } + else + { + return new List(); + } + } + + // get underwriters + public static List GetUw() + { + List uwList; + uwList = File.Exists(fp_uw) ? JsonSerializer.Deserialize>(File.ReadAllText(fp_uw)) : + new List(); + return uwList; + } + + // update underwriters + public static void UpdateUw(List uwList) + { + string jsonString = JsonSerializer.Serialize(uwList); + File.WriteAllText(fp_uw, jsonString); + } + + public static void IncrementPolicyData() + { + PolicyData pd = GetPolicyData(); + pd.numPol += 1; + string jsonString = JsonSerializer.Serialize(pd); + File.WriteAllText(fp_pdata, jsonString); + } + + public static PolicyData GetPolicyData() + { + PolicyData pd; + if (File.Exists(fp_pdata)) + { + pd = JsonSerializer.Deserialize(File.ReadAllText(fp_pdata)); + return pd; + } + pd = new PolicyData { numPol = 0 }; + string jsonString = JsonSerializer.Serialize(pd); + File.WriteAllText(fp_pdata, jsonString); + return pd; + } + + public static void DelUw(int id) + { + List uwList = GetUw(); + int index = FindUwIndex(uwList, id); + uwList.RemoveAt(index); + UpdateUw(uwList); + } + + public static int FindUwIndex(List uwList, int id) + { + for (int i = 0; i < uwList.Count; i++) + { + if (uwList[i].ID == id) + { + return i; + } + } + return -1; + } + + public static void UpdateVehicleInsurance(List vehicleList) + { + string jsonString = JsonSerializer.Serialize(vehicleList); + File.WriteAllText(fp_vehicle, jsonString); + } + + public static void AddVehicle(VehicleInsurance vehicle) + { + List vehicleList; + if (File.Exists(fp_vehicle)) + { + vehicleList = JsonSerializer.Deserialize>(File.ReadAllText(fp_vehicle)); + } + else + { + vehicleList = new List(); + } + vehicleList.Add(vehicle); + UpdateVehicleInsurance(vehicleList); + } + + public static void DisplayVehicle(VehicleInsurance vehicle) + { + string vehicleData = $"Policy No: {vehicle.policyNo}\n" + + $"Vehicle No: {vehicle.vehicleNo}\n" + + $"Vehicle Type: {vehicle.vehicleType}\n" + + $"Customer Name: {vehicle.customerName}\n" + + $"Engine No: {vehicle.engineNo}\n" + + $"Chasis No: {vehicle.chasisNo}\n" + + $"Phone No: {vehicle.phoneNo}\n" + + $"Premium Amount: {vehicle.premiumAmnt}\n" + + $"Insurance Type: {vehicle.type}\n" + + $"From Date: {vehicle.fromDate}\n" + + $"To Date: {vehicle.toDate}\n"; + DrawBox(vehicleData, boldCyan); + } + // 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; } +} + +public class VehicleInsurance +{ + public int policyNo { get; set; } + public string vehicleNo { get; set; } + public string vehicleType { get; set; } + public string customerName { get; set; } + public string engineNo { get; set; } + public string chasisNo { get; set; } + public string phoneNo { get; set; } + public decimal premiumAmnt { get; set; } + public string type { get; set; } + public DateOnly fromDate { get; set; } + public DateOnly toDate { get; set; } + public int uwId { get; set; } +} + +// store number of underwriters who exist, for auto ID generation +public class UwData +{ + public int numUw { get; set; } +} + +public class PolicyData +{ + public int numPol { get; set; } } \ No newline at end of file