Module 13 Snippets
------------------
 


Demo 1
------
private static string directory = @"D:\data\BethanysPieShopHRM\";
private static string fileName = "employees.txt";

internal static void CheckForExistingEmployeeFile()
{
 Console.ForegroundColor = ConsoleColor.Yellow;
 string path = $"{directory}{fileName}";

	bool existingFileFound = File.Exists(path);
}
		


if (existingFileFound)
{
	Console.WriteLine("An existing file with Employee data is found.");
}
else
{
	//Create the airectory already
	if (!Directory.Exists(path))
		Directory.CreateDirectory(directory);
	Console.WriteLine("Directory is ready for saving files.");
}
Console.ForegroundColor = ConsoleColor.Blue;


Demo 2
------

internal static void SaveEmployees(Employee[] employees, int employeeCount)
{
	//First, we are going to convert employee data to a string
	//Alternatively, we could again use JSON.NET and store them as JSON

	string path = $"{directory}{fileName}";
	StringBuilder sb = new StringBuilder();

	foreach (var employee in employees)
	{
		string type = GetEmployeeType(employee);

		sb.Append($"firstName:{employee.FirstName};");
		sb.Append($"lastName:{employee.LastName};");
		sb.Append($"email:{employee.Email};");
		sb.Append($"birthDay:{employee.BirthDay.ToShortDateString()};");
		sb.Append($"hourlyRate:{employee.HourlyRate};");
		sb.Append($"type:{type};");
		sb.Append(Environment.NewLine);
	}

	File.WriteAllText(path, sb.ToString());

	Console.ForegroundColor = ConsoleColor.Green;
	Console.WriteLine("Saved employees successfully");
	Console.ResetColor();


}

   private static string GetEmployeeType(Employee employee)
{
	if (employee is Manager)
		return "2";
	else if (employee is StoreManager)
		return "3";
	else if (employee is JuniorResearcher)
		return "5";
	else if (employee is Researcher)
		return "4";
	else if (employee is Employee)
		return "1";
	return "0";
}
		
		
		
string path = $"{directory}{fileName}";
if (File.Exists(path))
{
	employees.Clear();

	//now read the file
	string[] employeesAsString = File.ReadAllLines(path);
	for (int i = 0; i < employeesAsString.Length; i++)
	{
		string[] employeeSplits = employeesAsString[i].Split(';');
		string firstName = employeeSplits[0].Substring(employeeSplits[0].IndexOf(':') + 1);
		string lastName = employeeSplits[1].Substring(employeeSplits[1].IndexOf(':') + 1);
		string email = employeeSplits[2].Substring(employeeSplits[2].IndexOf(':') + 1);
		DateTime birthDay = DateTime.Parse(employeeSplits[3].Substring(employeeSplits[3].IndexOf(':') + 1));
		double hourlyRate = double.Parse(employeeSplits[4].Substring(employeeSplits[4].IndexOf(':') + 1));
		string employeeType = employeeSplits[5].Substring(employeeSplits[5].IndexOf(':') + 1);

		Employee employee = null;

		switch (employeeType)
		{
			case "1":
				employee = new Employee(firstName, lastName, email, birthDay, hourlyRate);
				break;
			case "2":
				employee = new Manager(firstName, lastName, email, birthDay, hourlyRate);
				break;
			case "3":
				employee = new StoreManager(firstName, lastName, email, birthDay, hourlyRate);
				break;
			case "4":
				employee = new Researcher(firstName, lastName, email, birthDay, hourlyRate);
				break;
			case "5":
				employee = new JuniorResearcher(firstName, lastName, email, birthDay, hourlyRate);
				break;
		}


		employees.Add(employee);

	}
	 Console.ForegroundColor = ConsoleColor.Green;
	Console.WriteLine($"Loaded {employees.Count} employees!\n\n");
	Console.ResetColor();
}
