Example #1
0
        public void Generate(int TotalNumEntries)
        {
            //Generating random numbers
            Random random = new Random();
            //First ticket are never under 500
            int RandomTicketID = 500;

            string fileName_SerialNumbers = "TicketDATA/SerialNumbers.txt";

            //Checks directory excists
            Directory.CreateDirectory(Path.GetDirectoryName(fileName_SerialNumbers));

            //Findes file with vaild ID
            var SerialNumbersFile = new StreamWriter(new FileStream(fileName_SerialNumbers, FileMode.Create), Encoding.UTF8);

            SerialNumbersFile.WriteLine("EntryNumber \t TicketID");

            for (int i = 0; i < TotalNumEntries; i++)
            {
                //Adds randomnumber to last number
                RandomTicketID += random.Next(11, 22);
                //writes the ID to stream
                SerialNumbersFile.WriteLine(i + "\t\t" + RandomTicketID);
                //Create New object with input
                SubmissionDATA p = new SubmissionDATA(i, RandomTicketID);
                //saves the object file
                p.SaveToFile();
            }
            //Close stream
            SerialNumbersFile.Close();
            Console.Out.WriteLine("There have been generated new tickets .");
        }
Example #2
0
        //Gets object from a file.
        public void LoadFromFile()
        {
            try
            {
                SubmissionDATA SubmissionFromFile = new SubmissionDATA();

                //Opens a file stream for entry number
                using (FileStream strm = new FileStream("TicketDATA/Entry" + EntryNumber + ".dat", FileMode.OpenOrCreate))
                {
                    //loads the object from the file.
                    IFormatter fmt = new BinaryFormatter();
                    SubmissionFromFile = fmt.Deserialize(strm) as SubmissionDATA;

                    //Overwrites information from the file to obejct
                    FirstName = SubmissionFromFile.FirstName;
                    LastName  = SubmissionFromFile.LastName;
                    Phone     = SubmissionFromFile.Phone;
                    Email     = SubmissionFromFile.Email;
                    AreTaken  = SubmissionFromFile.AreTaken;
                    TicketID  = SubmissionFromFile.TicketID;
                }
            }
            //if the file doesn't excist a new version will be created.
            catch (System.Runtime.Serialization.SerializationException)
            {
                if (TicketID == 0)
                {
                    Random random = new Random();
                    TicketID = random.Next(1, 2000);
                }
                SaveToFile();
                Console.Error.WriteLine("ERROR!!! File not fund: Entry{0].dat, new version has been created", EntryNumber);
            }
        }