Here is a blunt but quick rewrite of your code using Json. I prefer this than using the CSV recommendation by
@jmcilhinney personally, but that's just me. There is nothing wrong with using a CSV file, but Json is better to work with. You don't have to use this example. I am just providing it as a working --work in progress-- alternative to your current approach, and you can easily modify it to your taste if you prefer.
Put a break point on line 12, and step through the code until line 18 where it terminates. You can find a debugging tutorial in my signature if you need it.
This :
var patient1 = WithPatient(0, "Tom", 33, Patient.GenderChoice.Male.ToString(), "666 Home Vile St.", "052895566", 0);
calls the
WithPatient
method which determines the course of action to take based on the last parameter passed to the method. Ie. If the value is
0
, it will call the constructor in the Patient class and creates a new Patient each time. We created two patients, and here is your second :
var patient2 = WithPatient(1, "Bob", 36, Patient.GenderChoice.Male.ToString(), "888 Home Vila St.", "052895567", 0);
and then we update the first patient we created. Set new credentials such as phone number and gender change.
Notice on the
WithPatient
method, we have an integer on the end of that method. If it's set to 0, it will create a new patient. If it's set to 1, it will update a patient with the new details passed to the method, and then with those details it creates a new Patient by calling the constructor of your Patient class. This takes place on line 24 :
Patient patient = new Patient(id, name, age, gender, address, phonenumber);
. Note line 47 requires you to remove the Patient you don't want any longer. Here is the complete code. See the additional notes below this code :
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace CTestConsole
{
internal static class Program
{
public static void Main(string[] args)
{
var patient1 = WithPatient(0, "Tom", 33, Patient.GenderChoice.Male.ToString(), "666 Home Vile St.", "052895566", 0);
var patient2 = WithPatient(1, "Bob", 36, Patient.GenderChoice.Male.ToString(), "888 Home Vila St.", "052895567", 0);
patient1 = WithPatient(0, "Tom", 33, Patient.GenderChoice.Female.ToString(), "999 Lala land", "086997513", 1);
}
public static Dictionary<int, Patient> Patients = new Dictionary<int, Patient>();
static readonly string PathToDBFile = AppDomain.CurrentDomain.BaseDirectory;
static readonly string FileName = "db.json";
public static Patient WithPatient(int id, string name, int age, string gender, string address, string phonenumber, int action)
{
Patient patient = new Patient(id, name, age, gender, address, phonenumber);
if (action == 0)
{
Patients.Add(patient.ID, patient);
File.WriteAllText(Path.Combine(PathToDBFile, FileName), JsonConvert.SerializeObject(Patients, Formatting.Indented));
return patient;
}
else if (action == 1)
{
string json_Data = File.ReadAllText(Path.Combine(PathToDBFile, FileName));
Patients.Clear();
Patients = JsonConvert.DeserializeObject<Dictionary<int, Patient>>(json_Data);
Patients.Values.Where(x => x.Name == name).Single().Name = name;
Patients.Values.Where(x => x.Name == name).Single().Age = age;
Patients.Values.Where(x => x.Name == name).Single().Gender = gender;
Patients.Values.Where(x => x.Name == name).Single().Address = address;
Patients.Values.Where(x => x.Name == name).Single().PhoneNumber = phonenumber;
File.WriteAllText(Path.Combine(PathToDBFile, FileName), JsonConvert.SerializeObject(Patients, Formatting.Indented));
return patient;
}
else if (action == 2)
{
}
return null;
}
}
public class Patient
{
public Patient(int iD, string name, int age, string gender, string address, string phoneNumber)
{
ID = iD;
Name = name;
Age = age;
Gender = gender;
Address = address;
PhoneNumber = phoneNumber;
}
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public enum GenderChoice
{
Male,
Female
};
public string Address { get; set; }
public string PhoneNumber { get; set; }
}
}
The reason why I used a Dictionary<int, Patient> here and not a List<Patient> is solely to make the Json data more readable and easier to work with. For example, this is what the above code creates :
{
"0": {
"ID": 0,
"Name": "Tom",
"Age": 33,
"Gender": "Female",
"Address": "999 Lala land",
"PhoneNumber": "086997513"
},
"1": {
"ID": 1,
"Name": "Bob",
"Age": 36,
"Gender": "Male",
"Address": "888 Home Vila St.",
"PhoneNumber": "052895567"
}
}
But had I used a list, you would have had a completely different structure. For example. If we used a list of Patient, the data would have looked something like this (May not be exact) :
{
{
"ID": 0,
"Name": "Tom",
"Age": 33,
"Gender": "Female",
"Address": "999 Lala land",
"PhoneNumber": "086997513"
},
{
"ID": 1,
"Name": "Bob",
"Age": 36,
"Gender": "Male",
"Address": "888 Home Vila St.",
"PhoneNumber": "052895567"
}
}
Noticing that the ID is removed from the opening tag :
This is actually important, because it allows us to later work with that ID later on when selecting data from the file, providing you decide to use this blunt example to get started. None the less, it does work, and will likely give you a faster head-start in your project, with only a few edits to make yourself.
Now regarding your updated code on p1/#11.
If I attempt to input a patient ID located on the second or third row it will not edit any of that data. Anyone know why this is?
Click the spoiler on my signature and press Ctrl+F type Debug, and read that link on how to debug your code. Debugging is a fundamental need to know trait all programmers should know how to do. And that tutorial explains how to troubleshoot your own code and demonstrates how to use the debugger properly. Remember, we are here to advise you and help you write better code. We are not here to debug your code for you. If there is something you don't know or understand in your own code, then we are happy to answer direct questions regarding why something functions differently than you anticipate.
Yes, nodes can be replaced in memory. But how do you get those nodes back into the file without overwriting the entire file?
There are a few ways. One is Regex, but likely overkill when you can also just use some Linq and replace a line the same way as our OP did on p1/#11 @ line 24. Given if the original poster uses Json, it will be easier to manipulate the data in a Json structure. Somehow I get the feeling this database isn't designed to be huge. So simply reading the file back to Json won't be a problem or a performance issue. However, I will dig into the Json docs to see if there is a better alternative way to manipulate lines using the Json lib instead.