private President[] LoadPresidentArray() { // Path to CSV file string filePath = "..\\..\\..\\Presidents.csv"; FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream); // Gets number of rows in CSV file int rowCount = getRowCount(filePath); // Creates President object array President[] presidents = new President[rowCount]; for (int rowCounter = 0; rowCounter < rowCount; rowCounter++) { presidents[rowCounter] = new President(); } // Gets data values from CSV for (int rowCounter = 0; rowCounter < rowCount; ++rowCounter) { string readRow = streamReader.ReadLine(); string[] values = readRow.Split(','); // Get only necessary columns presidents[rowCounter].Number = values[0]; presidents[rowCounter].First = values[1]; presidents[rowCounter].Last = values[2]; presidents[rowCounter].Education = values[19]; presidents[rowCounter].Occupation = values[20]; presidents[rowCounter].CauseOfDeath = values[34]; presidents[rowCounter].Party = values[3]; presidents[rowCounter].Religion = values[18]; } // Return the array containing data from CSV return(presidents); }
}//End GetCount private void writeButton_Click(object sender, EventArgs e) { const int QUAN_PROPERTIES = 38; //38 columns in the file string filePath = "C:\\Users\\selelia\\Downloads\\Project2\\Project2\\Presidents.csv"; string strRead; string delim = ","; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader readerClass = new StreamReader(fs); int count = 0; count = GetCount(filePath); //return number of lines including header //Create a President object array - one instance per row President[] presidents = new President[count]; for (int i = 0; i < count; ++i) { presidents[i] = new President(); } //Get the records string[] recordParts = new string[QUAN_PROPERTIES]; strRead = readerClass.ReadLine(); for (int counter = 1; counter < count; ++counter) { strRead = readerClass.ReadLine(); recordParts = strRead.Split(','); presidents[counter].FirstName = recordParts[1]; presidents[counter].LastName = recordParts[2]; presidents[counter].Party = recordParts[3]; presidents[counter].StartYear = recordParts[10]; presidents[counter].EndYear = recordParts[11]; presidents[counter].DateBorn = recordParts[12]; presidents[counter].DateDied = recordParts[13]; presidents[counter].CauseOfDeath = recordParts[34]; } readerClass.Close(); fs.Close(); int x = presListBoxValue.SelectedIndex + 1; string outputString = $"{presidents[x].FirstName} {presidents[x].LastName}'s"; /*int listInfoSelected = ffCatListBoxValue.SelectedItems.Count; * string[] infoOptions = new string[listInfoSelected]; * int infoOptionsCount = 0; * * foreach (var infoOption in ffCatListBoxValue.SelectedItems) * { * infoOptions[infoOptionsCount] = infoOption.ToString(); * infoOptionsCount++; * }*/ bool[] isChecked = new bool[ffCatListBoxValue.Items.Count]; int itemCount = 0; //will keep track of each item in the list box foreach (var item in ffCatListBoxValue.Items) //going to check each item in listbox to see if it is selected { if (ffCatListBoxValue.GetSelected(itemCount)) { isChecked[itemCount] = true; //that item was selected } else { isChecked[itemCount] = false; } ++itemCount; } string writeString = ""; for (int y = 0; y <= presListBoxValue.Items.Count; y++) //****** prezComboBox.Items.Count is the quantity of presidents { if (y == x) { if (isChecked[0]) { outputString += $"\n Party: {presidents[x].Party}"; writeString += $"{presidents[x].FirstName} {delim} {presidents[x].LastName} {delim} {presidents[x].Party} {delim}"; } if (isChecked[1]) // ***********changed all else ifs to just ifs - allows for more than one { // ************** also added \r\n at the each of each line, to make the string multiline outputString += $"\n Start year: {presidents[x].StartYear}"; writeString += $"{presidents[x].FirstName} {delim} {presidents[x].LastName} {delim} {presidents[x].Party} {delim}"; } if (isChecked[2]) { outputString += $"\n End year: {presidents[x].EndYear}"; writeString += $"{presidents[x].FirstName} {delim} {presidents[x].LastName} {delim} {presidents[x].EndYear} {delim}"; } if (isChecked[3]) { outputString += $"\n Date born: {presidents[x].DateBorn}"; writeString += $"{presidents[x].FirstName} {delim} {presidents[x].LastName} {delim} {presidents[x].DateBorn} {delim}"; } if (isChecked[4]) { outputString += $"\n Date died: {presidents[x].DateDied}"; writeString += $"{presidents[x].FirstName} {delim} {presidents[x].LastName} {delim} {presidents[x].DateDied} {delim}"; } if (isChecked[5]) { outputString += $"\n Cause of death: {presidents[x].CauseOfDeath}"; writeString += $"{presidents[x].FirstName} {delim} {presidents[x].LastName} {delim} {presidents[x].CauseOfDeath} {delim}"; } } } string fileWritePath = "C:\\Users\\selelia\\output1.csv"; FileStream outFile2 = new FileStream(fileWritePath, FileMode.Append, FileAccess.Write); StreamWriter writerCSV = new StreamWriter(outFile2); writerCSV.WriteLine(writeString); writerCSV.Close(); outFile2.Close(); outputString += "\n selected info written to C:\\Users\\selelia\\output1.csv"; MessageBox.Show(outputString); }//end writeButton_Click
}//End previewPresButton_Click private void writePresButton_Click(object sender, EventArgs e) { const int QUAN_PROPERTIES = 38; //38 columns in the file string filePath = "C:\\Users\\selelia\\Downloads\\Project2\\Project2\\Presidents.csv"; string strRead; string delim = ","; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader readerClass = new StreamReader(fs); int count = 0; count = GetCount(filePath); //return number of lines including header //create a President object array. one instance per row President[] presidents = new President[count]; for (int i = 0; i < count; ++i) { presidents[i] = new President(); } //getting the records string[] recordParts = new string[QUAN_PROPERTIES]; strRead = readerClass.ReadLine(); for (int counter = 1; counter < count; ++counter) { strRead = readerClass.ReadLine(); recordParts = strRead.Split(','); presidents[counter].FirstName = recordParts[1]; presidents[counter].LastName = recordParts[2]; presidents[counter].Party = recordParts[3]; presidents[counter].Religion = recordParts[18]; presidents[counter].Occupation = recordParts[20]; presidents[counter].StateBorn = recordParts[14]; } readerClass.Close(); fs.Close(); int listCalValSelected = ffSubCatValue.SelectedItems.Count; string[] valOptions = new string[listCalValSelected]; int valOptionsCount = 0; foreach (var valOption in ffSubCatValue.SelectedItems) { valOptions[valOptionsCount] = valOption.ToString(); valOptionsCount++; } string presidentsOutput = ""; string writeOutput2 = ""; for (int y = 0; y < valOptions.Length; y++) { if (valOptions[y] == "Democratic") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Party == "Democratic") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Party}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Party} {delim}\n"; } } } else if (valOptions[y] == "Republican") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Party == "Republican") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Party}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Party} {delim}\n"; } } } else if (valOptions[y] == "Federalist") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Party == "Federalist") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Party}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Party} {delim}\n"; } } } else if (valOptions[y] == "Whig") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Party == "Whig") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Party}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Party} {delim}\n"; } } } else if (valOptions[y] == "Dem-Rep") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Party == "Dem-Rep") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Party}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Party} {delim}\n"; } } } else if (valOptions[y] == "No Designation") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Party == "no designation") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Party}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Party} {delim}\n"; } } } else if (valOptions[y] == "Methodist") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Religion == "Methodist") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Religion}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Religion} {delim}\n"; } } } else if (valOptions[y] == "Baptist") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Religion == "Baptist") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Religion}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Religion} {delim}\n"; } } } else if (valOptions[y] == "Episcopalian") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Religion == "Episcopalian") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Religion}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Religion} {delim}\n"; } } } else if (valOptions[y] == "Presbyterian") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Religion == "Presbyterian") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Religion}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Religion} {delim}\n"; } } } else if (valOptions[y] == "Unitarian") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Religion == "Unitarian") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Religion}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Religion} {delim}\n"; } } } else if (valOptions[y] == "No affiliation") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Religion == "no affiliation" || presidents[z].Religion == "no formal affiliation") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Religion}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Religion} {delim}\n"; } } } else if (valOptions[y] == "Lawyer") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Occupation == "Lawyer") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName}: {presidents[z].Occupation}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Occupation} {delim}\n"; } } } else if (valOptions[y] == "Soldier") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Occupation == "Soldier") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName}: {presidents[z].Occupation}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Occupation} {delim}\n"; } } } else if (valOptions[y] == "Farmer") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Occupation == "Farmer") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Occupation}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Occupation} {delim}\n"; } } } else if (valOptions[y] == "Engineer") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Occupation == "Engineer") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Occupation}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Occupation} {delim}\n"; } } } else if (valOptions[y] == "Teacher") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Occupation == "Teacher") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Occupation}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Occupation} {delim}\n"; } } } else if (valOptions[y] == "Actor") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].Occupation == "Actor") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].Occupation}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].Occupation} {delim}\n"; } } } else if (valOptions[y] == "Virginia") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].StateBorn == "Virginia") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].StateBorn}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].StateBorn} {delim}\n"; } } } else if (valOptions[y] == "Ohio") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].StateBorn == "Ohio") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].StateBorn}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].StateBorn} {delim}\n"; } } } else if (valOptions[y] == "New York") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].StateBorn == "New York") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].StateBorn}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].StateBorn} {delim}\n"; } } } else if (valOptions[y] == "Massachusetts") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].StateBorn == "Massachusetts") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].StateBorn}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].StateBorn} {delim}\n"; } } } else if (valOptions[y] == "California") { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].StateBorn == "California") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].StateBorn}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].StateBorn} {delim}\n"; } } } else { for (int z = 2; z < presidents.Length; z++) { if (presidents[z].StateBorn == "Texas") { presidentsOutput += $"{presidents[z].FirstName} {presidents[z].LastName} {presidents[z].StateBorn}\n"; writeOutput2 += $"{presidents[z].FirstName} {delim} {presidents[z].LastName} {delim} {presidents[z].StateBorn} {delim}\n"; } } } } string fileWritePath = "C:\\Users\\selelia\\President_Output.csv"; FileStream outFile3 = new FileStream(fileWritePath, FileMode.Append, FileAccess.Write); StreamWriter writerCSV = new StreamWriter(outFile3); writerCSV.WriteLine(writeOutput2); writerCSV.Close(); outFile3.Close(); presidentsOutput += "\n selected info written to C:\\Users\\selelia\\President_Output.csv"; MessageBox.Show(presidentsOutput); } //End writePresButton_Click
}//end GetCount method private void buttonWriteInfo_Click(object sender, EventArgs e) { const int QUAN_PROPERTIES = 38; //38 columns in the file string filePath = "C:\\Users\\stoerit\\Presidents.csv"; string strRead; string delim = ","; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader readerClass = new StreamReader(fs); int count = 0; count = GetCount(filePath); //return number of lines including header //create a President object array. one instance per row President[] presidents = new President[count]; for (int i = 0; i < count; ++i) { presidents[i] = new President(); } //getting the records string[] recordParts = new string[QUAN_PROPERTIES]; for (int counter = 1; counter < count; ++counter) { strRead = readerClass.ReadLine(); recordParts = strRead.Split(','); presidents[counter].FirstName = recordParts[1]; presidents[counter].LastName = recordParts[2]; presidents[counter].Party = recordParts[3]; presidents[counter].StartYear = recordParts[10]; presidents[counter].EndYear = recordParts[11]; presidents[counter].DateBorn = recordParts[12]; presidents[counter].DateDied = recordParts[13]; presidents[counter].CauseOfDeath = recordParts[34]; } readerClass.Close(); fs.Close(); int x = listBoxPresidents.SelectedIndex + 2; string outputString = $"{presidents[x].FirstName} {presidents[x].LastName}'s"; int listInfoSelected = listBoxInfoOptions.SelectedItems.Count; string[] infoOptions = new string[listInfoSelected]; int infoOptionsCount = 0; foreach (var infoOption in listBoxInfoOptions.SelectedItems) { infoOptions[infoOptionsCount] = infoOption.ToString(); infoOptionsCount++; } string writeString = ""; for (int y = 0; y < infoOptions.Length; y++) { if (infoOptions[y] == "Party") { outputString += $"\n Party: {presidents[x].Party}"; writeString += $"{presidents[x].Party} {delim}"; } else if (infoOptions[y] == "Start year") { outputString += $"\n Start year: {presidents[x].StartYear}"; writeString += $"{presidents[x].StartYear} {delim}"; } else if (infoOptions[y] == "End year") { outputString += $"\n End year: {presidents[x].EndYear}"; writeString += $"{presidents[x].EndYear} {delim}"; } else if (infoOptions[y] == "Date born") { outputString += $"\n Date born: {presidents[x].DateBorn}"; writeString += $"{presidents[x].DateBorn} {delim}"; } else if (infoOptions[y] == "Date died") { outputString += $"\n Date died: {presidents[x].DateDied}"; writeString += $"{presidents[x].DateDied} {delim}"; } else { outputString += $"\n Cause of death: {presidents[x].CauseOfDeath}"; writeString += $"{presidents[x].CauseOfDeath} {delim}"; } } string fileWritePath = "C:\\Users\\stoerit\\PresidentsInfoOutput.csv"; FileStream outFile2 = new FileStream(fileWritePath, FileMode.Append, FileAccess.Write); StreamWriter writerCSV = new StreamWriter(outFile2); writerCSV.WriteLine(writeString); writerCSV.Close(); outFile2.Close(); outputString += "\n selected info written to C:\\Users\\stoerit\\PresidentsInfoOutput.csv"; MessageBox.Show(outputString); }//end buttonWriteInfo_Click