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; while((line = streamReader.ReadLine()) != null) { processLine(line, employees, counter++); } return true; } catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine(e.StackTrace); } finally { // If an instance of the streamreader was made, we want to close it. if(streamReader != null) { streamReader.Close(); } } return false; }
static void Main(string[] args) { // creating some examples of employees Employee employee1 = new Employee("paul", "bath", 452.01m); Employee employee2 = new Employee("mike", "jones", 281.330m); //for use in the changeAn__ parts //creast a simple in that will be se foor the value Vs. reference int mynumber = 5; //Write the value of the int before the method, // call the method, // print after call Console.WriteLine(mynumber); changeAnInt(mynumber); Console.WriteLine(mynumber); //Write the value of the employee before the method, // call the method, // print after call // not used anymore? Console.WriteLine( employee.GetFullName()); Console.WriteLine(employee1.ToString()); changeAnObject(employee1); Console.WriteLine(employee1.ToString()); //Showing how to use an arrary with objects Employee[] employees = new Employee[10]; //Instantiate some employees employees[0] = new Employee("wellp", "wellp", 3353m); employees[1] = new Employee("wellp", "wellp", 335m); employees[2] = new Employee("wellp", "wellp", 3778m); employees[3] = new Employee("wellp", "wellp", 3533m); employees[4] = new Employee("wellp", "wellp", 3353m); //A for each loop that will loop the through each element of the employeees array foreach (Employee employee in employees) { //Chekc to make sure that the current object is not null // we know that the firt 5 have values be cause we assigned them 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()); } } }
private void processLine(string line, Employee[] employees, int index) { // Split line into parts for each comma. string[] parts = line.Split(','); string firstName = parts[0]; string lastName = parts[1]; decimal salary = decimal.Parse(parts[2]); // Add a new employee to the array. Use parts parsed out. employees[index] = new Employee(firstName, lastName, salary); }
private void processLine(String line, Employee[] employees, Int32 index) { //Split the line into pars, and assign the parts to a string array String[] parts = line.Split(','); String firstName = parts[0]; String lastName = parts[1]; Decimal salary = decimal.Parse(parts[2]); //Now we just need to add a new wmployee to the aray 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. i tis reading a line from // the file. It is assigning that info to the 'line' variable, and // lastly it is makin gsur ethat 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 ex) { // Spit out the errros that occured. Console.WriteLine(ex.ToString()); Console.WriteLine(ex.StackTrace); return false; } 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(); } } }
private void processLine(string line, Employee[] employees, int index) { //split the line into parts, and assign the party 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 need to add a new employee to the array and us 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); }
//this method takes in an 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) { // create a couple instances of the Employee class Employee employee1 = new Employee("Dave", "Barnes", 537.00m); Employee employee2 = new Employee("Joe", "Somebody", 125.50m); // create a simple int that will be used for value vs reference int myNumber = 5; // write the value of the int before the method, call the method, print after call. Console.WriteLine(myNumber); ChangeAnInt(myNumber); Console.WriteLine(myNumber); // write the value of the employee before the method, call the method, print after call. Console.WriteLine(employee1.ToString()); ChangeAnObject(employee1); Console.WriteLine(employee1.ToString()); //Console.WriteLine(employee.GetFullName()); //Console.WriteLine(employee.ToString()); // showing how to use an array with objects Employee[] employees = new Employee[10]; /* // instanciate some employees into the array employees[0] = new Employee("James", "Kirk", 453.00m); employees[1] = new Employee("Jean-Luc", "Picard", 290.00m); employees[2] = new Employee("Benjamin", "Sisko", 587.00m); employees[3] = new Employee("Kathryn", "Janeway", 194.00m); employees[4] = new Employee("Johnathan", "Archer", 394.00m); */ // Let's use the new CSVProcessor we made! CSVProcessor csvProcessor = new CSVProcessor(); // Call the ImportCSV method passing the path and the employees array // over so they can be used. csvProcessor.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 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 employees. 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 provided 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 class is 2) while (choice != 2) { // If the choice is 1, which in this case has to be, but if there // were more menus options it might not be so obvious. if (choice == 1) { // Create an 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 reprompt the user for some input choice = ui.GetUserInput(); } }
static void Main(string[] args) { Employee employee0 = new Employee("Chad", "Cunningham", 600.00m); Employee employee1 = new Employee("Joe", "Somebody", 5.00m); int myNumber = 5; Console.WriteLine(myNumber); ChangeAnInt(myNumber); Console.WriteLine(myNumber); Console.WriteLine(employee0.ToString()); ChangeAnObject(employee0); Console.WriteLine(employee0.ToString()); //Console.WriteLine(employee0.GetFullName()); //Console.WriteLine(employee0.ToString()); // >Array of objects. Employee[] employeeArray = new Employee[10]; // >Instanciate some employees into the array. employeeArray[0] = new Employee("Darren", "Froberg", 765.00m); employeeArray[1] = new Employee("Stan", "Marsh", 532.00m); employeeArray[2] = new Employee("Kyle", "Broflowskie", 223.00m); employeeArray[3] = new Employee("Eric", "Cartman", 142.00m); employeeArray[4] = new Employee("Kenny", "McCormick", 2.00m); // >Display everything in the array. foreach (Employee employee in employeeArray) { if (employee != null) { Console.WriteLine(employee.ToString()); } } UserInterface ui = new UserInterface(); int choice = ui.GetUserInput(); while (choice != 2) { if (choice == 1) { string allOutput = ""; foreach (Employee employee in employeeArray) { if (employee != null) { allOutput += employee.ToString() + Environment.NewLine; } } ui.PrintAllOutput(allOutput); } choice = ui.GetUserInput(); } }
static void Main(string[] args) { //Create a couple of instances of the Employee class Employee employee1 = new Employee("Dave", "Barnes", 537.00m); Employee employee2 = new Employee("Joe", "Somebody", 125.50m); //Creat simple int that will be used for value vs reference int myNumber = 5; //Write the value of the int before the method, call the method, print after call. Console.WriteLine(myNumber); changeAnInt(myNumber); Console.WriteLine(myNumber); //Write the value of the employee before the method, call the method, print after call. Console.WriteLine(employee1.ToString()); changeAnObject(employee1); Console.WriteLine(employee1.ToString()); //Console.WriteLine(employee.GetFullName()); //Console.WriteLine(employee.ToString()); //Showing how to use an array with objects Employee[] employees = new Employee[10]; //Instanciate some employees into the array employees[0] = new Employee("James", "Kirk", 453.00m); employees[1] = new Employee("Jean-Luc", "Picard", 290.00m); employees[2] = new Employee("Benjamin", "Sisko", 587.00m); employees[3] = new Employee("Kathryn", "Janeway", 194.00m); employees[4] = new Employee("Johnathan", "Archer", 394.00m); //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(); //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(); //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 an employee class, which is passed by reference // and then changes a property of it static void changeAnObject(Employee employee) { employee.FirstName = "Olivia"; }
static void Main(string[] args) { Employee employee1 = new Employee("Alysia", "Zimmerman", 435.00m); Employee employee2 = new Employee("Jesse", "Smith", 120.00m); int myNumber = 5; //Write the Value of the int before the method, call the method, print after call/ Console.WriteLine(myNumber); changeAnInt(myNumber); Console.WriteLine(myNumber); Console.WriteLine(employee1); changeAnObject(employee1); Console.WriteLine(employee1.ToString()); // Console.WriteLine(employee.GetFullName()); // Console.WriteLine(employee.ToString()); //Showing how to use an array with objects Employee[] employees = new Employee[10]; //Instanciates some employees into array //employees[0] = new Employee("James", "Bailey", 543.00m); //employees[1] = new Employee("Justin", "Aaron", 344.00m); //employees[2] = new Employee("Ashley", "Nick", 546.00m); //employees[3] = new Employee("Meagan", "Combe", 364.00m); //employees[4] = new Employee("Rene", "Base", 583.00m); //employees[5] = new Employee("Tyler", "Anderson", 344.00m); //employees[6] = new Employee("Bret", "Band", 573.00m); //employees[7] = new Employee("Kyle", "Andrew", 224.00m); //employees[8] = new Employee("Kelsey", "Drew", 563.00m); // employees[9] = new Employee("Morgan", "Amber", 347.00m); //Lets use the new CSVProcessor we made CSVProcessor csvProcessor = new CSVProcessor(); //Call the importCSV method passing the path and the employees array // over so they can be used csvProcessor.ImportCSV("../data/employees.csv", employees); //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. if (employee != null) { Console.WriteLine(employee.ToString()); } } //We are creating a new UserInterface class and it's okay // that the UserInterface calss 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(); // 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, that is what we do. Here we are calling // the GetUserInterface method on the calss 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 has to be true if (choice == 1) { //Create an empty string 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) { //Concatenate the employee changed to a string plus a new line allOutput += employee.ToString() + Environment.NewLine; } } ui.PrintAllOutput(allOutput); } //Now that "work" that we wanted to do is done, // we need to re-prompt the user for input choice = ui.GetUserInput(); } }
//Method takes Employee class passed by reference, changes property static void changeAnObject(Employee employee) { employee.FirstName = "Lucifer"; }
static void Main(string[] args) { // Create new instances of the Employee class Employee employee1 = new Employee("Bryan", "Greener", 99999.99m); Employee employee2 = new Employee("Foxy", "Nigel", 13.37m); /* // Create simple int that wi be used for value vs reference int myNumber = 5; // Write value of int before method, call method, print Console.WriteLine(myNumber); changeAnInt(myNumber); Console.WriteLine(myNumber); // Change value of employee before method, call method, print Console.WriteLine(employee.ToString()); changeAnObject(employee); Console.WriteLine(employee.ToString()); */ //Console.WriteLine(employee.GetFullName()); //Console.WriteLine(employee.ToString()); //Console.ReadLine(); // use array with objects Employee[] employees = new Employee[10]; // Instanciate employees into array //employees[0] = new Employee("James", "Cameron", 100.00m); //employees[1] = new Employee("James", "Booooo", 101.00m); //employees[2] = new Employee("Geordy", "Laforge", 102.00m); //employees[3] = new Employee("Simon", "Lane", 103.00m); //employees[4] = new Employee("Lewis", "Brindley", 104.00m); //employees[5] = new Employee("Scrub", "Lord", 420.00m); SR sr = new SR(); // Call the ImportCSV method passing the path and the employees array. sr.ImportCSV("../data/employees.csv", employees); foreach (Employee employee in employees) { if (employee != null) { Console.WriteLine(employee.ToString()); } } // Create new instance of UserInterface class. Uses default constructor. Therefore we don't have to pass parameters. UserInterface ui = new UserInterface(); // The line below is not a valid statement because we are tryingto make an instance of a static class; it won't work. //StaticUI stui = new StaticUI(); // Return valid integer. (Line 27 of UserInterface) int choice = ui.GetUserInput(); // Below calls the static class of StaticUI and directly assigns GetUserInput to choice value //choice = StaticUI.GetUserInput(); // While choice is not EXIT while (choice != 2) { // If choice is LIST // If there were more menu options, this wouldn't be so obvious if (choice == 1) { // Create an empty string to concat to string allOutput = ""; foreach (Employee employee in employees) { // If no null in spot of array if (employee != null) { // Concat the employee changes to a string and drop line allOutput += employee.ToString() + Environment.NewLine; } } ui.PrintAllOutput(allOutput); } // Break While loop choice = ui.GetUserInput(); } }
static void Main(string[] args) { Employee employee1 = new Employee("Joshua", "Sziede", 123.45m); Employee employee2 = new Employee("First", "Last", 321.00m); int myNumber = 5; Console.WriteLine(myNumber); changeAnInt(myNumber); Console.WriteLine(myNumber); Console.WriteLine(employee1.ToString()); changeAnObject(employee1); Console.WriteLine(employee1.ToString()); //Console.WriteLine(employee.GetFullName()); //Console.WriteLine(employee.ToString()); //showing how to use an array with objects Employee[] employees = new Employee[10]; //instanciate some employees into the array employees[0] = new Employee("James", "Kirk", 453.00m); employees[1] = new Employee("Jean-Luc", "Picard", 290.00m); employees[2] = new Employee("Benjamin", "Sisko", 587.00m); employees[3] = new Employee("Kathryn", "Janeway", 194.00m); employees[4] = new Employee("Johnathan", "Archer", 394.00m); foreach (Employee employee in employees) { if (employee != null) { Console.WriteLine(employee.ToString()); } } //we are creating a new userinterface class, and it is ok //that the userinterface class does not have a defined constructor UserInterface ui = new UserInterface(); //call the getuserrinput method of the ui class //it will return a valid integer that represents the choice they want to do int choice = ui.GetUserInput(); ///userinterface //while the user's choice is not the exit option from the menu while(choice != 2) { //only other option besides two is one, but just in case there were more options added if (choice == 1) { //create an 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) // | { // | allOutput += employee.ToString() + Environment.NewLine; // <- } } ui.PrintAllOuput(allOutput); } //not that the work that we wanted to do is done, //we need to reprompt the user choice = ui.GetUserInput(); } }
//Method thake in an Employee class, which is passed by reference //and the changes a property of it. static void ChangeAnObject(Employee employee) { employee.FristName = "Jason"; }