I'm making a simple console-program. This program is supposed to give the user a console-menu that will give you three choices: To add an employee to a list, remove an employee from the list, and then to exit. I am trying to create a list that will add different users to the list after they have been initialized by user input. After that I want to print the list to console, but the list wont hold on to any input. If anyone has a clue whats wrong, please tell me! Also, I am fairly new to coding, so extensive explanations are greatly appreciated. Thank you!
using Employees;
namespace Inheritance
{
public class Program
{
static void Main(string[] args)
{
bool keepRunning = true;
while (keepRunning)
{
List<Employee> EmployeesList = new List<Employee>();
void AddToList(Employee employee)
{
EmployeesList.Add(employee);
}
Console.WriteLine("TIFFANYS & CO - PRIVATE PROGRAM TM\nPress the number of the action you want to pursue, and press [ENTER].\n1) Add an Employee\n2) Delete an Employee\n3) Exit");
var menuChoice = Int32.Parse(Console.ReadLine());
switch (menuChoice)
{
case 0:
foreach (Employee item in EmployeesList)
{
Console.WriteLine(item.firstName " " item.lastName " " item.salary);
}
break;
case 1:
Console.WriteLine("a) Add a Manager\nb) Add an Engineer\nc) Add a Researcher");
var menuChoice1 = Console.ReadLine();
switch (menuChoice1)
{
case "a":
Console.WriteLine("Please type in the first name of the manager and press [ENTER]:");
string managerFirstName = Console.ReadLine();
Console.WriteLine("Please type in the last name of the manager and press [ENTER]:");
string managerLastName = Console.ReadLine();
Console.WriteLine("Please type in the salary of the manager and press [ENTER]:");
int managerSalary = Int32.Parse(Console.ReadLine());
Console.WriteLine("Please type in how many meetings the manager will attend weekly and press [ENTER]:");
int managerMeetings = Int32.Parse(Console.ReadLine());
Console.WriteLine("Please type in how many vacation-weeks the manager has annually and press [ENTER]:");
int managerVacationWeeks = Int32.Parse(Console.ReadLine());
Manager manager = new(managerFirstName, managerLastName, managerSalary, managerMeetings, managerVacationWeeks);
EmployeesList.Add(manager);
foreach (var employee in EmployeesList)
{
Console.WriteLine(employee);
}
break;
case "b":
Console.WriteLine("Please type in the first name of the engineer and press [ENTER]:");
string engineerFirstName = Console.ReadLine();
Console.WriteLine("Please type in the last name of the engineer and press [ENTER]:");
string engineerLastName = Console.ReadLine();
Console.WriteLine("Please type in the salary of the engineer and press [ENTER]:");
int engineerSalary = Int32.Parse(Console.ReadLine());
//Console.WriteLine("Is the engineer able to use C#? If yes, type [yes] and press [ENTER]. If no, type [no] and press [ENTER]:");
//string cSkill = Console.ReadLine();
//if (cSkill == "yes")
//{
// engineer.cSharpSkill = true;
//}
//else
//{
// engineer.cSharpSkill = false;
//}
Console.WriteLine("How many years of experience does the engineer have? Type the number of years and press [ENTER]:");
int engineerExperience = Int32.Parse(Console.ReadLine());
Console.WriteLine("What kind of engineering is the engineers field? Please write their field and press [ENTER]:");
string engineerField = Console.ReadLine();
Engineer engineer = new(engineerFirstName, engineerLastName, engineerSalary, engineerExperience, engineerField);
EmployeesList.Add(engineer);
break;
case "c":
Console.WriteLine("Please type in the first name of the researcher and press [ENTER]:");
string researcherFirstName = Console.ReadLine();
Console.WriteLine("Please type in the last name of the researcher and press [ENTER]:");
string researcherLastName = Console.ReadLine();
Console.WriteLine("Please type in the salary of the researcher and press [ENTER]:");
int researcherSalary = Int32.Parse(Console.ReadLine());
Console.WriteLine("What university did the researcher get their Doctorate? Please type in the name of the university and press [ENTER]:");
string researcherUniversity = Console.ReadLine();
Console.WriteLine("What was the thesis statement of the researchers doctorate? Please type in the thesis question and press [ENTER]:");
string researcherThesis = Console.ReadLine();
Researcher researcher = new(researcherFirstName, researcherLastName, researcherSalary, researcherUniversity, researcherThesis);
EmployeesList.Add(researcher);
break;
}
break;
case 2:
Console.WriteLine("What is the last name of the engineer to be deleted?");
string toDeleteLastName = Console.ReadLine();
Employee employeeToBeDeleted = EmployeesList.Find(e => e.lastName == toDeleteLastName);
if (employeeToBeDeleted != null)
EmployeesList.Remove(employeeToBeDeleted);
else
Console.WriteLine($"Could not find {employeeToBeDeleted}");
break;
case 3:
keepRunning = false;
break;
}
}
}
}
}
CodePudding user response:
There is a library installed via NuGet,
CodePudding user response:
You have the loop setup this way:
while (keepRunning)
{
List<Employee> EmployeesList = new List<Employee>();
So every time you loop you delete the list of employees and get a new one. Try swapping the order:
List<Employee> EmployeesList = new List<Employee>();
while (keepRunning)
{
This way you start your program with a fresh employee list and use that same list while your program is running.