private void processLine(string line, Employee[] employees, int index) { //Split the line into parts, and assign the parts to a string array string[] parts = line.Split(','); //Create some local variables and assign the various parts to them. string firstName = parts[0]; string lastName = parts[1]; decimal salary = decimal.Parse(parts[2]); //Now we just need to add a new employee to the array and use the parts //we parsed out. If you had a collection class, I would hope that it has //a method that you made called 'add' that would then do the work of //adding a new employee to the collection. employees[index] = new Employee(firstName, lastName, salary); }
public bool ImportCSV(string pathToCSVFile, Employee[] employees) { StreamReader streamReader = null; try { //declare a string to represent a line we read string line; //Create a new instance of the StreamReader class streamReader = new StreamReader(pathToCSVFile); //Create a counter to know what index to place the new object int counter = 0; //This line is doing a bunch of stuff. It is reading a line from //the file. It is assigning that info to the 'line' variable, and //lastly it is making sure that what it just read was not null. //if it is null, we are done reading the file and we can exit the //loop. while((line = streamReader.ReadLine()) != null) { processLine(line, employees, counter++); } return true; } catch (Exception e) { //Spit out the errors that occured Console.WriteLine(e.ToString()); Console.WriteLine(e.StackTrace); } finally { //If an instance of the streamreader was made, we want to ensure //that we close it. The finally block is a perfect spot to put it //since it will get called regardless of whether or not the try //succeeded if (streamReader != null) { streamReader.Close(); } } return false; }
static void Main(string[] args) { SalaryEmployee salaryEmployee1 = new SalaryEmployee("Billy", "Bob", 40000.34m); HourlyEmployee hourlyEmployee1 = new HourlyEmployee("Rick", "James", 49.99m); //Creat simple int that will be used for value vs reference //Write the value of the int before the method, call the method, print after call. //Write the value of the employee before the method, call the method, print after call. Console.WriteLine(salaryEmployee1.ToString()); Console.WriteLine(hourlyEmployee1.ToString()); //Console.WriteLine(employee.GetFullName()); //Console.WriteLine(employee.ToString()); //Showing how to use an array with objects IEmployee[] employees = new Employee[10]; //Instanciate some employees into the array employees[0] = new HourlyEmployee("James", "Kirk", 14.05m); employees[1] = new SalaryEmployee("Jean-Luc", "Picard", 55273.00m); employees[2] = new HourlyEmployee("Benjamin", "Sisko", 14.59m); employees[3] = new SalaryEmployee("Kathryn", "Janeway", 67123); employees[4] = new SalaryEmployee("Johnathan", "Archer", 12232); employees[5] = new HourlyEmployee("James", "Kirk",12.05m); employees[6] = new SalaryEmployee("Jean-Luc", "Picard", 55123.00m); employees[7] = new HourlyEmployee("Benjamin", "Sisko", 14.56m); employees[8] = new SalaryEmployee("Kathryn", "Janeway",67000); employees[9] = new SalaryEmployee("Johnathan", "Archer", 12000); //Lets use the new CSVProcessor we made! //CSVProcessor csvProcesor = new CSVProcessor(); //Call the ImportCSV method passing the path, and the employees array //over so they can be used. //csvProcesor.ImportCSV("../data/employees.csv", employees); //A for each loop that will loop through each element of the employees array foreach (Employee employee in employees) { //Check to make sure that the current object is not null. //we know that the first 5 have values because we assigned them right above //but the last 5 are null, so we better put in a check. if (employee != null) { //output the information of the employee Console.WriteLine(employee.ToString()); } } //We are creating a new UserInterface class, and it's okay //that the UserInterface class does not have a defined //constructor. It will have a default one provide to us that //we can take advantage of by just not passing in any parameters UserInterface ui = new UserInterface(); //This is not a valid statement. Because we are trying to make //an instance of a static class, it won't work. //StaticUserInterface stui = new StaticUserInterface(); //Call the GetUserInput method of the UI class. It will return //a valid integer that represents the choice they want to do. int choice = ui.GetUserInput(); //To use a static class to execute methods we simply call the //method on the class name, (or type). Since it is not possible //to use the 'new' keyword and make an instance, the only way //we can access the class is through the class name, so that //is what we do. Here we are calling the GetUserInterface method //on the class name to get it to run. //choice = StaticUserInterface.GetUserInput(); //While the choice is not the exit choice (which in this case is 2) while (choice != 2) { //If the choice is 1, which in this case it has to be, but if there //were more menu options it might not be so obvious. if (choice == 1) { //Create a empty string to concat to. string allOutput = ""; //For each Employee in the employees array. foreach (Employee employee in employees) { //So long as the spot in the array is not null if (employee != null) { //Concat the employee changed to a string plus a new line //to the allOutput string. allOutput += employee.ToString() + Environment.NewLine; } } //Now that the large string containing what I would like to output //is created, I can output it to the screen using the //PrintAllOutput method of the UI class. ui.PrintAllOutput(allOutput); } //Now that the "Work" that we wanted to do is done, //We need to re-prompt the user for some input choice = ui.GetUserInput(); } }
static void Main(string[] args) { //Showing how to use an array with objects IEmployee[] employees = new Employee[10]; //Instanciate some employees into the array employees[0] = new HourlyEmployee("James", "Kirk", 14.05m); employees[1] = new SalaryEmployee("Jean-Luc", "Picard", 55273.00m); employees[2] = new HourlyEmployee("Benjamin", "Sisko", 14.59m); employees[3] = new SalaryEmployee("Kathryn", "Janeway", 67123); employees[4] = new SalaryEmployee("Johnathan", "Archer", 12232); employees[5] = new HourlyEmployee("James", "Kirk", 12.05m); employees[6] = new SalaryEmployee("Jean-Luc", "Picard", 55123.00m); employees[7] = new HourlyEmployee("Benjamin", "Sisko", 14.56m); employees[8] = new SalaryEmployee("Kathryn", "Janeway", 67000); employees[9] = new SalaryEmployee("Johnathan", "Archer", 12000); //We are creating a new UserInterface class, and it's okay //that the UserInterface class does not have a defined //constructor. It will have a default one provide to us that //we can take advantage of by just not passing in any parameters UserInterface ui = new UserInterface(); //This is not a valid statement. Because we are trying to make //an instance of a static class, it won't work. //StaticUserInterface stui = new StaticUserInterface(); //Call the GetUserInput method of the UI class. It will return //a valid integer that represents the choice they want to do. int choice = ui.GetUserInput(); //To use a static class to execute methods we simply call the //method on the class name, (or type). Since it is not possible //to use the 'new' keyword and make an instance, the only way //we can access the class is through the class name, so that //is what we do. Here we are calling the GetUserInterface method //on the class name to get it to run. //choice = StaticUserInterface.GetUserInput(); //While the choice is not the exit choice (which in this case is 2) while (choice != 2) { //If the choice is 1, which in this case it has to be, but if there //were more menu options it might not be so obvious. if (choice == 1) { //Create a empty string to concat to. string allOutput = ""; //For each Employee in the employees array. foreach (Employee employee in employees) { //So long as the spot in the array is not null if (employee != null) { //Concat the employee changed to a string plus a new line //to the allOutput string. allOutput += employee.ToString() + Environment.NewLine; } } //Now that the large string containing what I would like to output //is created, I can output it to the screen using the //PrintAllOutput method of the UI class. ui.PrintAllOutput(allOutput); } //Now that the "Work" that we wanted to do is done, //We need to re-prompt the user for some input choice = ui.GetUserInput(); } }
//This method takes in a Employee class, which is passed by reference //and then changes a property of it. static void changeAnObject(Employee employee) { employee.FirstName = "Thor"; }
static void Main(string[] args) { //Showing how to use an array with objects Employee[] employees = new Employee[10]; //IEmployee[] employees = new Employee[10]; //Instanciate some employees into the array employees[0] = new HourlyEmployee("James", "Kirk", 14.05m); employees[1] = new SalaryEmployee("Jean-Luc", "Picard", 5527.00m); employees[2] = new HourlyEmployee("Benjamin", "Sisko", 14.56m); employees[3] = new SalaryEmployee("Kathryn", "Janeway", 67123m); employees[4] = new SalaryEmployee("Johnathan", "Archer", 12232m); employees[5] = new HourlyEmployee("James", "Kirk",12.5m); employees[6] = new SalaryEmployee("Jean-Luc", "Picard", 55123.00m); employees[7] = new HourlyEmployee("Benjamin", "Sisko", 14.56m); employees[8] = new SalaryEmployee("Kathryn", "Janeway", 67000m); employees[9] = new SalaryEmployee("Johnathan", "Archer", 12000m); //We are creating a new UserInterface class, and it's okay //that the UserInterface class does not have a defined //constructor. It will have a default one provide to us that //we can take advantage of by just not passing in any parameters UserInterface ui = new UserInterface(); //This is not a valid statement. Because we are trying to make //an instance of a static class, it won't work. //StaticUserInterface stui = new StaticUserInterface(); //Call the GetUserInput method of the UI class. It will return //a valid integer that represents the choice they want to do. int choice = ui.GetUserInput(); //To use a static class to execute methods we simply call the //method on the class name, (or type). Since it is not possible //to use the 'new' keyword and make an instance, the only way //we can access the class is through the class name, so that //is what we do. Here we are calling the GetUserInterface method //on the class name to get it to run. //choice = StaticUserInterface.GetUserInput(); //While the choice is not the exit choice (which in this case is 2) while (choice != 2) { //If the choice is 1, which in this case it has to be, but if there //were more menu options it might not be so obvious. if (choice == 1) { //Create a empty string to concat to. string allOutput = ""; //For each Employee in the employees array. foreach (Employee employee in employees) { //So long as the spot in the array is not null if (employee != null) { //Concat the employee changed to a string plus a new line //to the allOutput string. allOutput += employee.ToString() + Environment.NewLine; } } //Now that the large string containing what I would like to output //is created, I can output it to the screen using the //PrintAllOutput method of the UI class. ui.PrintAllOutput(allOutput); } //Now that the "Work" that we wanted to do is done, //We need to re-prompt the user for some input choice = ui.GetUserInput(); } }