[ create a new paste ] login | about

Link: http://codepad.org/Kynck94U    [ raw code | fork ]

8Observer8 - C, pasted on Feb 18:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace StudentsToBynaryFile
{
    class Program
    {
        static void Main(string[] args)
        {
            // Список студентов
            List<Student> students = new List<Student>();

            // Команда пользователя
            string command;

            // Имя, фамилия
            string firstName, secondName;

            // Курс
            int course;

            // Факультет
            string faculty;

            // Цикл работает пока пользователь не введёт команду: stop
            do
            {
                // Предлагаем пользователю ввести комманду
                Console.Write("\nEnter a command: ");
                command = Console.ReadLine();

                // Проверяем какую команду ввёл пользователь и выполняем её
                switch (command)
                {
                    case "add":
                        // Имя
                        Console.Write("Enter a firstName: ");
                        firstName = Console.ReadLine();

                        // Фамилия
                        Console.Write("Enter a secondName: ");
                        secondName = Console.ReadLine();

                        // Курс
                        Console.Write("Enter a course: ");
                        course = int.Parse(Console.ReadLine());
                        //course = Convert.ToInt32(Console.ReadLine());

                        // Факультет
                        Console.Write("Enter a faculty: ");
                        faculty = Console.ReadLine();

                        Student student = new Student();
                        student.info = new FIO();
                        student.info.firstName = firstName;
                        student.info.secondName = secondName;
                        student.course = course;
                        student.faculty = faculty;

                        students.Add(student);

                        break;

                    case "show":
                        for (int i = 0; i < students.Count; ++i)
                        {
                            Console.WriteLine("First Name: " + students.ElementAt(i).info.firstName);
                            Console.WriteLine("Second Name: " + students.ElementAt(i).info.secondName);
                            Console.WriteLine("Course: " + students.ElementAt(i).course);
                            Console.WriteLine("Faculty: " + students.ElementAt(i).faculty);
                        }
                        break;

                    case "save":
                        SerializableObject obj = new SerializableObject();
                        obj.Students = students;

                        MySerializer serializer = new MySerializer();
                        serializer.SerializeObject("output.txt", obj);
                        break;

                    case "load":
                        SerializableObject obj2 = new SerializableObject();
                        MySerializer serializer2 = new MySerializer();

                        obj2 = serializer2.DeserializeObject("output.txt");
                        students = obj2.Students;
                        break;

                    case "help":
                        Console.WriteLine("add");
                        Console.WriteLine("show");
                        Console.WriteLine("save");
                        Console.WriteLine("load");
                        Console.WriteLine("stop");
                        break;

                    case "stop":
                        break;

                    default:
                        Console.WriteLine("Error: incorrect command. Please, type: help");
                        break;
                }
            } while (command != "stop");
        }
    }

    [Serializable]
    public class SerializableObject : ISerializable
    {
        private List<Student> students;

        public List<Student> Students
        {
            get { return this.students; }
            set { this.students = value; }
        }

        public SerializableObject() { }

        public SerializableObject(SerializationInfo sInfo, StreamingContext contextArg)
        {
            this.students = (List<Student>)sInfo.GetValue("Students", typeof(List<Student>));
        }

        public void GetObjectData(SerializationInfo sInfo, StreamingContext contextArg)
        {
            sInfo.AddValue("Students", this.students);
        }
    }

    //information about student
    [Serializable]
    public class Student : ISerializable
    {
        public Student() { }

        public Student(SerializationInfo sInfo, StreamingContext contextArg)
        {
            this.course = (int)sInfo.GetValue("Course", typeof(int));
            this.faculty = (string)sInfo.GetValue("Faculty", typeof(string));
            this.info = (FIO)sInfo.GetValue("FIO", typeof(FIO));
        }

        public void GetObjectData(SerializationInfo sInfo, StreamingContext contextArg)
        {
            sInfo.AddValue("Course", this.course);
            sInfo.AddValue("Faculty", this.faculty);
            sInfo.AddValue("FIO", this.info);
        }

        public int course;
        public string faculty;
        public FIO info;
    }

    //personal information
    [Serializable]
    public class FIO : ISerializable
    {
        public FIO() { }

        public FIO(SerializationInfo sInfo, StreamingContext contextArg)
        {
            this.firstName = (string)sInfo.GetValue("FirstName", typeof(string));
            this.secondName = (string)sInfo.GetValue("SecondName", typeof(string));
        }

        public void GetObjectData(SerializationInfo sInfo, StreamingContext contextArg)
        {
            sInfo.AddValue("FirstName", this.firstName);
            sInfo.AddValue("SecondName", this.secondName);
        }

        public string firstName;
        public string secondName;
    }

    public class MySerializer
    {
        public MySerializer() { }

        public void SerializeObject(string fileName, SerializableObject objToSerialize)
        {
            FileStream fstream = File.Open(fileName, FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(fstream, objToSerialize);
            fstream.Close();
        }

        public SerializableObject DeserializeObject(string fileName)
        {
            SerializableObject objToSerialize = null;
            FileStream fstream = File.Open(fileName, FileMode.Open);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            objToSerialize = (SerializableObject)binaryFormatter.Deserialize(fstream);
            fstream.Close();
            return objToSerialize;
        }
    }    
}


Create a new paste based on this one


Comments: