0

I am using a constructor Employee(string id, string fullname, string roleid) in the business logic to add elements in the list Employees. The business logic I used is:

namespace ClassLibrary2
{

    public class Employe
    {
        public List<Employee> Employees { get; set; } = new List<Employee>();

        public void Add(Employee employee)
        {
            Employees.Add(employee);
        }

        public List<Employee> ViewAll()
        {
            return Employees;
        }}}

The above business logic is executed in the below console application :

namespace EmployeeApp
{

static class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("PPM Project");
            Console.WriteLine("1. Add employee");
            Console.WriteLine("2. View Employee");
            Console.WriteLine("3. Save");

            var userInput = Console.ReadLine();
            var business1 = new Employe();

            while (true)
            {


                switch (userInput)
                {
                    case "1":
                                       
                            Console.WriteLine("adding employee full name:");
                            var FName = Console.ReadLine();

                            Console.WriteLine("adding 4digit Employee Id:");
                            var EmpId = Console.ReadLine();

                            var newEmployee = new Employee(EmpId, FName);
                            business1.Add(newEmployee);
                            Console.WriteLine($"An employee with EmployeeId {newEmployee.EmpId} was added. \n");
                            break;

                    case "2":
                        var allemployee = business1.ViewAll();
                        Console.WriteLine("\nList of Employees: \n");
                        foreach (var employee in allemployee)
                        {
                            Console.WriteLine($"Employee: {employee.EmpId}\t {employee.FName}.");
                        }
                        break;
            
            case "3":
            //how to save user input for further use
            break;
        }
                Console.WriteLine("Select option");
                userInput = Console.ReadLine();
            }}}}

I want to save the app state so that when I restart the program I can retrieve the saved input. I tried saving the data in .txt file; using the How to permanently save input given by user in c# console application? but because I am providing user input in Lists in console rather than giving input in application, I am having trouble following the previous thread.

  • 1
    Try to have a look in here: https://www.codeproject.com/Questions/5287193/Save-a-list-to-a-text-file-with-Csharp or here https://stackoverflow.com/questions/44365257/write-list-of-objects-to-text-file or here https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file#write-selected-strings-from-an-array-to-a-file – thesystem Feb 24 '22 at 07:17

0 Answers0