Exemplo n.º 1
0
        public static Person[] Extract(String input)
        {
            //Mr. John Smith, born on 2001/06/15
            List <String> people = MyParser.GetEntries(input, @"(Mr|Mrs)\D+, born on \d{4}/((0\d)|(1[012]))/(([012]\d)|3[01])");

            Person[] result = new Person[people.Count];
            int      i      = 0;

            foreach (String s in people)
            {
                Gender   Gender = Gender.m;
                String   FirstName;
                String   LastName;
                DateTime DateOfBirth;
                String[] parts = s.Split(' ');
                if (parts[0].Equals("Mrs."))
                {
                    Gender = Gender.f;
                }
                FirstName   = parts[1];
                LastName    = parts[2].Substring(0, parts[2].Length - 1);
                DateOfBirth = DateTime.ParseExact(parts[5], "yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture);
                Person res = new Person(Gender, FirstName, LastName, DateOfBirth);
                result[i++] = res;
            }
            return(result);
        }
Exemplo n.º 2
0
        //examples:
        // Mr. John Smith, born on 2001/06/15 (class Person)
        // Company Microsoft (IT, USA)          (class Company)
        // FC Dynamo (Kyiv, Ukraine, est. 1927)   (class FootballClub)
        //
        // to add other class you need to implement IComparable and to write an Extract method which parses input string into array of objects of that class
        // you can use  List<String> GetEntries(String input, String regexStr) to apply regex on input string and then parse each entry into object

        // main() here just to locate it easier
        public static void Main()
        {
            String s = " 123 abc Mr. John Smith, born on 2001/06/15 abc123 Company Microsoft (IT, USA) gdfgdf Mrs. Jane Smith, born on 1999/11/03 32 asd" +
                       "abc 123 FC Dynamo (Kyiv, Ukraine, est. 1927) asdad FC Dynamo (Kyiv, Ukraine, est. 1927)";

            Extract[]          ex  = { Person.Extract, FootballClub.Extract, Company.Extract };
            List <IComparable> res = MyParser.ParseAll(s, ex);

            Console.WriteLine("input string:");
            Console.WriteLine(s);
            Console.WriteLine();

            foreach (IComparable t in res)
            {
                Console.WriteLine(t.ToString());
            }
            Console.WriteLine();
            //removing duplicates is implemented by sorting collection by type, splitting it, removing duplicates from each type, merging back
            List <IComparable> newres = RemoveDuplicates(res);

            Console.WriteLine("removed duplicates:");
            foreach (IComparable t in newres)
            {
                Console.WriteLine(t.ToString());
            }
        }
Exemplo n.º 3
0
        public static FootballClub[] Extract(String input)
        {
            List <String> clubs = MyParser.GetEntries(input, @"FC\D+\((\D+\d{4})\)");

            FootballClub[] result = new FootballClub[clubs.Count];
            int            i      = 0;

            foreach (String s in clubs)
            {
                String   name;
                String   country;
                String   city;
                DateTime established;
                String[] parts = s.Split(' ');

                name    = parts[1].Substring(0, parts[1].Length);
                city    = parts[2].Substring(1, parts[2].Length - 2);
                country = parts[3].Substring(0, parts[3].Length - 1);

                established = DateTime.ParseExact(parts[5].Substring(0, 4), "yyyy", System.Globalization.CultureInfo.InvariantCulture);
                FootballClub res = new FootballClub(name, country, city, established);
                result[i++] = res;
            }
            return(result);
        }
Exemplo n.º 4
0
        // main methods: MyParser.SaveToXml(XmlWriteable), puts object into its directory in xml
        // PersonFactory/FootballClubFactory/CompanyFactory.GetFromXml(path), get objects from xml,
        //implement generic interface MyXmlReader (only writeable objects can be read from xml)
        //xmlwriteable objects need to provide xml representation and file name
        public static void Main()
        {
            String s = " 123 abc Mr. John Smith, born on 2001/06/15 abc123 Company Microsoft (IT, USA) gdfgdf Mrs. Jane Smith, born on 1999/11/03 32 asd" +
                       "abc 123 FC Dynamo (Kyiv, Ukraine, est. 1927) asdad FC Dynamo (Kyiv, Ukraine, est. 1927)";

            Extract[]          ex  = { Person.Extract, FootballClub.Extract, Company.Extract };
            List <IComparable> res = MyParser.ParseAll(s, ex);

            foreach (IComparable t in res)
            {
                Console.WriteLine(t.ToString());
            }


            Console.WriteLine();
            SaveToXml((Person)res[0]);
            SaveToXml((FootballClub)res[2]);
            SaveToXml((Company)res[4]);
            Console.WriteLine("Saved [0],[2],[4] to xml (dirs Company, FootballClub, Person)");
            PersonFactory       pf  = new PersonFactory();
            FootballClubFactory fcf = new FootballClubFactory();
            CompanyFactory      cf  = new CompanyFactory();

            try
            {
                Person       nsmith = pf.GetFromXml("Person/Smith.xml");
                FootballClub ndyn   = fcf.GetFromXml("FootballClub/Dynamo.xml");
                Company      ncomp  = cf.GetFromXml("Company/Microsoft.xml");
                Console.WriteLine("Objects from xml:");
                Console.WriteLine(nsmith.ToString());
                Console.WriteLine(ndyn.ToString());
                Console.WriteLine(ncomp.ToString());
            }
            catch (ObjNotFoundInXmlException e)
            {
                Console.WriteLine("Obj wasn't found");
            }
            //
            Console.WriteLine("Trying to generate and handle exception(wrong filepath):");
            try
            {
                Company ncomp = cf.GetFromXml("Company/Microsoft2.xml");
            }
            catch (ObjNotFoundInXmlException e)
            {
                Console.WriteLine("Exception: Obj wasn't found");
            }
        }
Exemplo n.º 5
0
        public static Company[] Extract(String input)
        {
            //Company Microsoft (IT, USA)
            List <String> companies = MyParser.GetEntries(input, @"Company \D+ \((\D+,\D+)\)");

            Company[] result = new Company[companies.Count];
            int       i      = 0;

            foreach (String s in companies)
            {
                String   Name;
                String   Segment;
                String   Location;
                String[] parts = s.Split(' ');
                Name     = parts[1];
                Segment  = parts[2].Substring(1, parts[2].Length - 2);
                Location = parts[3].Substring(0, parts[3].Length - 1);
                Company res = new Company(Name, Segment, Location);
                result[i++] = res;
            }
            return(result);
        }