Example #1
0
 public void deserializeDataFromFile(string fileName)
 {
     if (File.Exists(fileName))
     {
         Stream               fileStream   = File.OpenRead(fileName);
         BinaryFormatter      deserializer = new BinaryFormatter();
         Repository.Stack <T> stack        = (Repository.Stack <T>)deserializer.Deserialize(fileStream);
         this.elements = stack;
         fileStream.Close();
     }
 }
Example #2
0
        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!");
        }
Example #3
0
        public static int noOfElementsGreaterThan <T>(Repository.Stack <T> stack, T elem) where T : Model.Comparable <T>
        {
            int result = 0;

            Repository.Stack <T> temp = stack.copy();

            while (!temp.isEmpty())
            {
                Model.Comparable <T> comparableElem = temp.pop();
                if (comparableElem.isGreaterThan(elem))
                {
                    result++;
                }
            }

            return(result);
        }
Example #4
0
 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());
     }
 }
Example #5
0
        public static String elementsFromStack <T>(Repository.Stack <T> stack)
        {
            Repository.Stack <T> copy = stack.copy();
            String result             = "\n";

            while (!copy.isEmpty())
            {
                try
                {
                    T element = copy.pop();
                    result += element.ToString();
                    result += "\n";
                }
                catch (MyException ex)
                {
                    return(ex.getMessage());
                }
            }

            return(result);
        }