public static void ListAll(Dictionary<int, StudentData> newDict) { string temp = ""; int count = 0; List<String> templist = new List<string>(); Dictionary<String, StudentData> tempDict = new Dictionary<string, StudentData>(); foreach (KeyValuePair<int, StudentData> e in newDict) { if (tempDict.ContainsKey(e.Value._LastName)) { temp = e.Value._LastName + count.ToString(); tempDict.Add(temp, e.Value); templist.Add(temp); } else { tempDict.Add(e.Value._LastName, e.Value); templist.Add(e.Value._LastName); } } templist.Sort(); Console.WriteLine("\n"); foreach (String i in templist) { Console.WriteLine(tempDict[i]); } Console.WriteLine(""); }
public static void SearchByID(Dictionary<int, StudentData> newDict) { int SearchID; bool success; Console.WriteLine("\n"); do { Console.Write("What ID would you like to search for?: "); success = int.TryParse(Console.ReadLine(), out SearchID); } while (success == false || SearchID > 999999 || SearchID <100000); if (newDict.ContainsKey(SearchID)) { Console.WriteLine(newDict[SearchID]); } else { Console.WriteLine("There is no student with that ID"); } }
static void Main(string[] args) { ConsoleKey input; //Stores user menu inputs String LineRead; //Holds StreamReader line reads String[] temp = null; //temp array to hold StreamReader buffer int ID = 0; //holds student ID numbers read in from file StreamReader newStreamReader; //Reads stream from students file StreamReader marksReader; //Reads stream from marks file Dictionary<int, StudentData> myDictionary = new Dictionary<int, StudentData>();//Student Dictionary try { //Opens stream to Students.txt newStreamReader = new StreamReader("Students.txt"); try { //Reads each line in while the next line in not empty while ((LineRead = newStreamReader.ReadLine()) != null) { //Stores and splits the string that is read in temp = LineRead.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); //Parses the ID number from the string array int.TryParse(temp[2], out ID); //Creates and stores a student value StudentData newStudent = new StudentData(temp[0], temp[1], ID, new List<Marks>()); //Stores the student value in the dictionary myDictionary.Add(ID, newStudent); } } catch (Exception e) { } finally { //Closes the stream to Students.txt newStreamReader.Close(); } } catch (Exception e) { } try { //Opens a stream to Marks.txt marksReader = new StreamReader("Marks.txt"); try { //Reads each line in from Marks.txt while the next line s not null while ((LineRead = marksReader.ReadLine()) != null) { double Value, OutOf, Weight; //Variables to store data read in from the file temp = LineRead.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //splits the values in the array and stores them double.TryParse(temp[1], out Value); //Parses mark value to a double double.TryParse(temp[2], out OutOf); //Parses value mark is out of to a double double.TryParse(temp[3], out Weight); //parses mark weight to a double int.TryParse(temp[0], out ID); //Parses Student ID to an int //Creates a stores the values as a new mark structure Marks newMark = new Marks(Value, OutOf, Weight, ID); //Stores marks in a list in dictionray if (myDictionary.ContainsKey(newMark._ID)) myDictionary[newMark._ID]._Markslist.Add(newMark); } } catch (Exception e) { } finally { //closes stream to Marks.txt marksReader.Close(); } } catch (Exception e) { } //foreach (Marks i in secondList) //{ //if(myDictionary.ContainsKey(i._ID)) // myDictionary[i._ID]._Markslist.Add(i); //} do { //Displays the menu for the database Console.WriteLine("{0} Student Records found", myDictionary.Count); Console.WriteLine(""); Console.WriteLine("A - Add new student"); Console.WriteLine("M - Add new mark"); Console.WriteLine("L - List all records (Alphabetical Order)"); Console.WriteLine("S - Search by student ID"); Console.WriteLine("N - Search by last name"); Console.WriteLine("Q - Quit"); Console.WriteLine("X - Quit and save to disk"); Console.WriteLine(""); Console.Write("Your choice? "); //Reads user menu commands input = Console.ReadKey(true).Key; //Switch to control the menu switch (input) { case ConsoleKey.A: AddStudent(myDictionary); break; case ConsoleKey.M: Addmark(myDictionary); break; case ConsoleKey.L: ListAll(myDictionary); break; case ConsoleKey.S: SearchByID(myDictionary); break; case ConsoleKey.N: SearchByLastName(myDictionary); break; case ConsoleKey.Q: input = QuitConfirm(input); break; case ConsoleKey.X: SaveAndQuit(myDictionary); break; } } //Loops menu while user input is not Q while (input != ConsoleKey.Q && input != ConsoleKey.X); }
//******************************************************************************************** //Method: public static void ListAll(Dictionary<int, StudentData> newDict) //Purpose: Lists all student marks //Parameters: Dictionary<int, StudentData> newDict //Returns: nothing //********************************************************************************************* public static void ListAll(Dictionary<int, StudentData> newDict) { string temp = ""; //Varaible to store a string int count = 0; //Variable to count List<String> templist = new List<string>(); Dictionary<String, StudentData> tempDict = new Dictionary<string, StudentData>(); foreach (KeyValuePair<int, StudentData> e in newDict) { if (tempDict.ContainsKey(e.Value._LastName)) { temp = e.Value._LastName + count.ToString(); tempDict.Add(temp, e.Value); templist.Add(temp); } else { tempDict.Add(e.Value._LastName, e.Value); templist.Add(e.Value._LastName); } } templist.Sort(); Console.WriteLine("\n"); foreach (String i in templist) { Console.WriteLine(tempDict[i]); foreach (Marks l in tempDict[i]._Markslist) { Console.WriteLine("Mark: {0}, OutOf: {1}, Weight {2}", l._Value, l._OutOf, l._Weight); } } Console.WriteLine(""); }