public String readFromFile(String fileName) { try { StreamReader sr = new StreamReader(fileName); String line; String[] tokens; Repository.Stack <Student> newStack = new Repository.Stack <Student>(); while ((line = sr.ReadLine()) != null) { tokens = line.Split(new String[] { " " }, StringSplitOptions.None); //System.Console.Out.WriteLine(tokens[0] + "\n" + tokens[1] + "\n" + tokens[2]); if ("Student".Equals(tokens[0])) { newStack.push(new Student(Convert.ToInt32(tokens[1]), tokens[2], Convert.ToInt32(tokens[3]))); } else if ("UndergraduateStudent".Equals(tokens[0])) { newStack.push(new UndergraduateStudent(Convert.ToInt32(tokens[1]), tokens[2], Convert.ToInt32(tokens[3]), Convert.ToInt32(tokens[4]))); } else if ("GraduateStudent".Equals(tokens[0])) { newStack.push(new GraduateStudent(Convert.ToInt32(tokens[1]), tokens[2], Convert.ToInt32(tokens[3]), tokens[4], Convert.ToInt32(tokens[5]), Convert.ToInt32(tokens[6]))); } else if ("PhDStudent".Equals(tokens[0])) { newStack.push(new PhDStudent(Convert.ToInt32(tokens[1]), tokens[2], Convert.ToInt32(tokens[3]), tokens[4], tokens[5], Convert.ToInt32(tokens[6]))); } else { throw new MyException("Error reading from file!"); } } this.repo.replaceContent(newStack); } catch (MyException ex) { return(ex.getMessage()); } catch (IOException) { return("Error reading from file!"); } catch (FormatException) { return("Invalid format in file!"); } return("Students read from file successfully!"); }
public static void moveElements <W, T>(Repository.Stack <W> source, Repository.Stack <T> destination) where W : T { Repository.Stack <T> temp = new Repository.Stack <T>(); try { while (!source.isEmpty()) { temp.push(source.pop()); } while (!temp.isEmpty()) { destination.push(temp.pop()); } } catch (MyException e) { System.Console.WriteLine(e.getMessage()); } }