public void SaveRegistrator(Registrator registrator)
        {
            FileStream   fileStream = new FileStream(fileloc, FileMode.OpenOrCreate);
            StreamWriter writer     = new StreamWriter(fileStream);

            writer.Write(registrator.StructuredData);
            writer.Flush();

            writer.Close();
        }
        public Registrator[] GetRegistrators()
        {
            int numberOfRegs = NumberOfRegistrators();

            Registrator[] registrators = new Registrator[numberOfRegs];

            for (int i = 0; i < numberOfRegs; i++)
            {
                registrators[i] = GetRegistratorRecord(i);
            }

            return(registrators);
        }
Ejemplo n.º 3
0
        public Program()
//        {
//            database = new RegistratorDatabase(@"./database.txt");
//
//            Registrator registrator = database.GetRegistratorRecord(0);
//            Console.WriteLine(registrator.RegNumber);
//            Console.WriteLine(registrator.Name);
//            Console.WriteLine(registrator.Email);
//
//        }
//        public void sProgram()
        {
            database = new RegistratorDatabase(@"./database.txt");

            string   regnumber     = AskUserString("Reg Num");
            DateTime exceededAbove = AskUserDateTime("Enter time your vehicle entered above 50mph");
            DateTime exceededBelow = AskUserDateTime("Enter time your vehicle entered below 50mph"); // 2miles

            TimeSpan span = exceededBelow - exceededAbove;

            // s = d/t
            double speedMPH = 2D / span.TotalHours;

            Console.WriteLine("{0} / {1} = {2}", 2D, span.TotalHours, speedMPH);

            if (speedMPH > 50)
            {
                Registrator[] registrators = database.GetRegistrators();

                Registrator targetRegistrator = null;

                foreach (Registrator registrator in registrators)
                {
                    Console.WriteLine(registrator.RegNumber);
                    Console.WriteLine(registrator.Name);
                    Console.WriteLine(registrator.Email);
                    Console.WriteLine('\n');

                    if (string.Equals(registrator.RegNumber, regnumber, StringComparison.OrdinalIgnoreCase))
                    {
                        targetRegistrator = registrator;
                        break;
                    }
                }

                if (targetRegistrator != null)
                {
                    Directory.CreateDirectory(@"./emails/" + targetRegistrator.Email);
                    int id = 1;

                    while (File.Exists(@"./emails/" + targetRegistrator.Email + "/" + id + ".txt"))
                    {
                        id++;
                    }

                    StreamWriter fileStream = File.CreateText(@"./emails/" + targetRegistrator.Email + "/" + id + ".txt");

                    fileStream.Write(String.Format("Hello {0},\n" +
                                                   "You were {1:0.00}MPH above the speed limit!", targetRegistrator.Name, speedMPH - 50));

                    fileStream.Flush();
                    fileStream.Close();
                }
                else
                {
                    Console.WriteLine("Registration Number does not exist in our database.");
                }
            }
            else
            {
                Console.WriteLine("You're fine");
            }
        }