Ejemplo n.º 1
0
        //Newtonsoft.Json.JsonConvert.SerializeObject(student);

        static void Main(string[] args)
        {
            CsvFileReaderWriter reader = new CsvFileReaderWriter();

            Console.WriteLine(GetDirectory(URL));

            string         directories = GetDirectory(URL);
            List <string>  studentdir  = directories.Split("\r\n", StringSplitOptions.None).ToList();
            List <student> students    = new List <student>();

            foreach (var studentdirs in studentdir)
            {
                string[] studentProps = studentdirs.Split(" ", StringSplitOptions.None);

                if (studentProps.Length >= 1 && !String.IsNullOrEmpty(studentProps[0]))
                {
                    student student = new student();
                    student.StudentId = studentProps[0];
                    student.FirstName = studentProps[1];
                    student.LastName  = studentProps[2];

                    string remoteDownloadFilePath  = $"/{studentdirs}/info.csv";
                    string remoteDownloadFilePath2 = $"/{studentdirs}/myimage.jpg";
                    //Path to a valid folder and the new file to be saved
                    string localDownloadFileDestination  = $@"C:\Users\Meet Patel\Desktop\Assignment3\Ass3\Assignment3\Content\Data\{student.FirstName}.csv";
                    string localDownloadFileDestination2 = $@"C:\Users\Meet Patel\Desktop\Assignment3\Ass3\Assignment3\Content\Images\{student.FirstName}.jpg";

                    if (student.StudentId == "200467632")
                    {
                        var downcsv = DownloadFile(FTP.BaseUrl + remoteDownloadFilePath, localDownloadFileDestination);
                        var downimg = DownloadFile(FTP.BaseUrl + remoteDownloadFilePath2, localDownloadFileDestination2);
                    }

                    var fields = reader.GetEntities(reader.ParseString(localDownloadFileDestination));

                    foreach (var i in fields)
                    {
                        Console.WriteLine(i);
                    }

                    //List<string[]> entities = reader.GetEntities(localDownloadFileDestination);


                    students.Add(student);
                    Console.WriteLine($"{student.LastName} {student.FirstName} {student.LastName} {student.ImageData} {student.MyRecord}");
                }
            }

            // LIST AGGREGATES ====================================================

            Console.WriteLine($"The list contain {students.Count()} students");

            var studentWith2004 = students.Where(x => x.StudentId.StartsWith("2004"));

            Console.WriteLine($"there are {studentWith2004.Count()} that start with 2004");

            int MaxAge = students.Max(temp => temp.Age);

            Console.WriteLine($"The highest Age in the list is {MaxAge}");

            int MinAge = students.Min(temp => temp.Age);

            Console.WriteLine($"The lowest Age in the list is {MinAge}");

            double AverageAge = students.Average(temp => temp.Age);

            Console.WriteLine($"The average age is => {AverageAge.ToString("0")}");

            student me = students.Find(x => x.StudentId == "200467632");

            me.MyRecord = true;

            //=====================================================================

            List <string> studentsCSV = new List <string>();

            foreach (var student in students)
            {
                Console.WriteLine($"{student.LastName} {student.FirstName} {student.LastName} {student.ImageData} {student.MyRecord}");
                Console.WriteLine(student);
                Console.WriteLine(student.ToCSV());

                studentsCSV.Add(student.ToCSV());
            }

            using (StreamWriter sw = new StreamWriter(@"C:\Users\Meet Patel\Desktop\Assignment3\Ass3\Assignment3\Content\Data\students.csv"))
            {
                sw.WriteLine("StudentId, FirstName, LastName, ImageData, MyRecord");
                foreach (var studentCSV in studentsCSV)
                {
                    sw.WriteLine(studentCSV);
                }
            }

            string json = JsonConvert.SerializeObject(students);

            using (StreamWriter sw = new StreamWriter(@"C:\Users\Meet Patel\Desktop\Assignment3\Ass3\Assignment3\Content\Data\students.json"))
            {
                sw.WriteLine(json);
            }


            XmlSerializer serializer = new XmlSerializer(typeof(List <student>));

            using (Stream fs = new FileStream(@"C:\Users\Meet Patel\Desktop\Assignment3\Ass3\Assignment3\Content\Data\students.xml", FileMode.Create))
            {
                XmlWriter writer = new XmlTextWriter(fs, Encoding.Unicode);
                serializer.Serialize(writer, students);
            }
            string localcsv   = @"C:\Users\Meet Patel\Desktop\Assignment3\Ass3\Assignment3\Content\Data\students.csv";
            string remotecsv  = "/200467632%20Meet%20Patel/students.csv";
            string localjson  = @"C:\Users\Meet Patel\Desktop\Assignment3\Ass3\Assignment3\Content\Data\students.json";
            string remotejson = "//200467632%20Meet%20Patel/students.json";
            string localxml   = @"C:\Users\Meet Patel\Desktop\Assignment3\Ass3\Assignment3\Content\Data\students.xml";
            string remotexml  = "/200467632%20Meet%20Patel/students.xml";

            Console.WriteLine(UploadFile(localcsv, URL + remotecsv));
            Console.WriteLine(UploadFile(localjson, URL + remotejson));
            Console.WriteLine(UploadFile(localxml, URL + remotexml));
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //Question 1
            List <string> directories = FTP.GetDirectory(Constants.FTP.BaseUrl);

            foreach (var entry in directories)
            {
                Console.WriteLine(entry);
            }

            Console.WriteLine("\n");
            //Question 2.1
            if (FTP.FileExists(Constants.FTP.MyUrl + "/" + Constants.Student.InfoCSVFileName))
            {
                Console.WriteLine($"{Constants.Student.InfoCSVFileName} file exists.");
            }
            else
            {
                Console.WriteLine($"{Constants.Student.InfoCSVFileName} file does not exist.");
            }
            if (FTP.FileExists(Constants.FTP.MyUrl + "/" + Constants.Student.MyImageFileName))
            {
                Console.WriteLine($"{Constants.Student.MyImageFileName} file exists.");
            }
            else
            {
                Console.WriteLine($"{Constants.Student.MyImageFileName} file does not exist.");
            }

            //Question 2.2
            List <string> fileList = FTP.GetFilesinDirectory(Constants.FTP.MyUrl);

            foreach (var file in fileList)
            {
                Console.WriteLine(file);
            }

            //Question2.3
            var    myCsvBytes    = FTP.DownloadFileBytes(Constants.FTP.MyUrl + "/info.csv");
            string infoMyCsvData = Encoding.UTF8.GetString(myCsvBytes, 0, myCsvBytes.Length);

            string[] myLines = infoMyCsvData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < myLines[0].Split(",", StringSplitOptions.RemoveEmptyEntries).Count(); i++)
            {
                Console.WriteLine($"{myLines[0].Split(",", StringSplitOptions.RemoveEmptyEntries)[i]}: {myLines[1].Split(",", StringSplitOptions.RemoveEmptyEntries)[i]}\n");
            }

            //Question 2.4

            //Question 4
            List <Student> students = new List <Student>();

            foreach (var directory in directories)
            {
                Student student = new Student();
                if (directory == "200471940 Karish Thangarajah")
                {
                    student.MyRecord = true;
                }

                if (FTP.FileExists(Constants.FTP.BaseUrl + "/" + directory + "/info.csv"))
                {
                    var    CSVFileBytes = FTP.DownloadFileBytes(Constants.FTP.BaseUrl + "/" + directory + "/info.csv");
                    string infoCsvData  = Encoding.UTF8.GetString(CSVFileBytes, 0, CSVFileBytes.Length);

                    string[] lines = infoCsvData.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
                    if (lines[1] != "" || lines[1] != null || infoCsvData != "")
                    {
                        student.FromCSV(lines[1]);
                        students.Add(student);
                    }
                    else
                    {
                        Console.WriteLine("Missing content");
                    }
                }
                else
                {
                    Console.WriteLine("    info.csv does not exist.");
                }

                if (FTP.FileExists(Constants.FTP.BaseUrl + "/" + directory + "/myimage.jpg"))
                {
                    var    ImageFileBytes = FTP.DownloadFileBytes(Constants.FTP.BaseUrl + "/" + directory + "/myimage.jpg");
                    Image  image          = Converter.ByteArrayToImage(ImageFileBytes);
                    string imagedata      = Converter.ImageToBase64(image, ImageFormat.Jpeg);
                    student.ImageData = imagedata;
                }
                else
                {
                    Console.WriteLine("    info.csv does not exist.");
                }



                Console.WriteLine(student.ToString());
                Console.WriteLine(student.ToCSV());
                Console.WriteLine("\n");
            }

            // Question 5.1
            //   a)
            Console.WriteLine(students.Count());
            //   b)
            List <Student> filtered = new List <Student>();

            foreach (var student in students)
            {
                if (student.StudentId.StartsWith("2004"))
                {
                    filtered.Add(student);
                }
            }
            Console.WriteLine(filtered.Count());
            //   c)
            List <Student> filtered2 = new List <Student>();

            foreach (var student in students)
            {
                if (student.DateOfBirthDT.ToShortDateString().Contains("1997"))
                {
                    filtered2.Add(student);
                }
            }

            Console.WriteLine(filtered2.Count());

            Student me = students.Find(x => x.MyRecord == true);

            Console.WriteLine(me.ToString());

            Console.WriteLine($"The average age of the class is { students.Average(x => x.Age)}.");
            Console.WriteLine($"The oldest age of the class is { students.Max(x => x.Age)}.");
            Console.WriteLine($"The youngest age of the class is { students.Min(x => x.Age)}.");

            //csv upload
            CsvFileReaderWriter reader = new CsvFileReaderWriter();
            List <string>       ForCSV = new List <string> {
                "StudentId,FirstName,LastName,DateOfBirth,ImageData"
            };

            foreach (var student in students)
            {
                ForCSV.Add(student.ToCSV());
            }
            string ForCSVStr = String.Join("\r\n", ForCSV);

            List <string[]> fields = reader.GetEntities(reader.ParseString(ForCSVStr));

            reader.WriteFile(@"C:\Users\karis\OneDrive\Desktop\BDAT1001\Assignment3\students.csv", fields);

            Console.WriteLine(FTP.UploadFile(@"C:\Users\karis\OneDrive\Desktop\BDAT1001\Assignment3\students.csv", Constants.FTP.MyUrl + "/students.csv"));

            //JSON upload
            string stuJSON = JsonConvert.SerializeObject(students);

            System.IO.File.WriteAllText(@"C:\Users\karis\OneDrive\Desktop\BDAT1001\Assignment3\students.json", stuJSON);

            Console.WriteLine(FTP.UploadFile(@"C:\Users\karis\OneDrive\Desktop\BDAT1001\Assignment3\students.json", Constants.FTP.MyUrl + "/students.json"));

            //XML upload
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(students.GetType());
            TextWriter writer = new StreamWriter(@"C:\Users\karis\OneDrive\Desktop\BDAT1001\Assignment3\students.xml");

            x.Serialize(writer, students);

            Console.WriteLine(FTP.UploadFile(@"C:\Users\karis\OneDrive\Desktop\BDAT1001\Assignment3\students.xml", Constants.FTP.MyUrl + "/students.xml"));
        }
Ejemplo n.º 3
0
        //Newtonsoft.Json.JsonConvert.SerializeObject(student);

        static void Main(string[] args)
        {
            CsvFileReaderWriter reader = new CsvFileReaderWriter();

            Console.WriteLine(GetDirectory(url));

            string         directories        = GetDirectory(url);
            List <string>  Studentdirectories = directories.Split("\r\n", StringSplitOptions.None).ToList();
            List <student> students           = new List <student>();

            foreach (var Studentdir in Studentdirectories)
            {
                string[] Student_Props = Studentdir.Split(" ", StringSplitOptions.None);

                if (Student_Props.Length >= 1 && !String.IsNullOrEmpty(Student_Props[0]))
                {
                    student student = new student();
                    student.StudentId = Student_Props[0];
                    student.FirstName = Student_Props[1];
                    student.LastName  = Student_Props[2];

                    string remoteDownloadFilePath  = $"/{Studentdir}/info.csv";
                    string remoteDownloadFilePath2 = $"/{Studentdir}/myimage.jpg";
                    //Path to a valid folder and the new file to be saved
                    string localDownloadFileDestination  = $@"C:\Users\falak\Desktop\Assignment3 (1)\Assignment3\Content\Data\{student.FirstName}.csv";
                    string localDownloadFileDestination2 = $@"C:\Users\falak\Desktop\Assignment3 (1)\Assignment3\Content\Images\{student.FirstName}.jpg";
                    var    downcsv = DownloadFile(FTP.BaseUrl + remoteDownloadFilePath, localDownloadFileDestination);
                    var    downimg = DownloadFile(FTP.BaseUrl + remoteDownloadFilePath2, localDownloadFileDestination2);

                    var fields = reader.GetEntities(reader.ParseString(localDownloadFileDestination));

                    List <string[]> entities = reader.GetEntities(localDownloadFileDestination);


                    students.Add(student);
                    Console.WriteLine($"{student.LastName} {student.FirstName} {student.LastName} {student.ImageData} {student.MyRecord}");
                }
            }

            Console.WriteLine($"The list contain {students.Count()} students");

            var Student2004 = students.Where(x => x.StudentId.StartsWith("2004"));

            Console.WriteLine($"there are {Student2004.Count()} that start with 2004");

            int Highestage = students.Max(temp => temp.Age);

            Console.WriteLine($"The highest Age in the list is {Highestage}");

            int Lowestage = students.Min(temp => temp.Age);

            Console.WriteLine($"The lowest Age in the list is {Lowestage}");

            double Avg_age = students.Average(temp => temp.Age);

            Console.WriteLine($"The average age is => {Avg_age.ToString("0")}");

            student me = students.Find(x => x.StudentId == "200452357");

            me.MyRecord = true;


            List <string> studentsCSV = new List <string>();

            foreach (var student in students)
            {
                Console.WriteLine($"{student.LastName} {student.FirstName} {student.LastName} {student.ImageData} {student.MyRecord}");
                Console.WriteLine(student);
                Console.WriteLine(student.ToCSV());

                studentsCSV.Add(student.ToCSV());
            }

            using (StreamWriter sw = new StreamWriter(@"C:\Users\falak\Desktop\Assignment3 (1)\Assignment3\Content\Data\students.csv"))
            {
                sw.WriteLine("StudentId, FirstName, LastName");
                foreach (var studentCSV in studentsCSV)
                {
                    sw.WriteLine(studentCSV);
                }
            }

            string json = JsonConvert.SerializeObject(students);

            using (StreamWriter sw = new StreamWriter(@"C:\Users\falak\Desktop\Assignment3 (1)\Assignment3\Content\Data\students.json"))
            {
                sw.WriteLine(json);
            }


            XmlSerializer serializer = new XmlSerializer(typeof(List <student>));

            using (Stream fs = new FileStream(@"C:\Users\falak\Desktop\Assignment3 (1)\Assignment3\Content\Data\students.xml", FileMode.Create))
            {
                XmlWriter writer = new XmlTextWriter(fs, Encoding.Unicode);
                serializer.Serialize(writer, students);
            }
            string uploadcsv = @"C:\Users\falak\Desktop\Assignment3 (1)\Assignment3\Content\Data\students.csv";

            string csvpath    = "/200452357%20Falak%20Ghoda/students.csv";
            string uploadjson = @"C:\Users\falak\Desktop\Assignment3 (1)\Assignment3\Content\Data\students.json";
            string jsonpath   = "//200452357%20Falak%20Ghoda//students.json";
            string uploadxml  = @"C:\Users\falak\Desktop\Assignment3 (1)\Assignment3\Content\Data\students.xml";
            string xmlpath    = "/200452357%20Falak%20Ghoda//students.xml";

            Console.WriteLine(UploadFile(uploadcsv, url + csvpath));
            Console.WriteLine(UploadFile(uploadjson, url + jsonpath));
            Console.WriteLine(UploadFile(uploadxml, url + xmlpath));
        }