Beispiel #1
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());
            }
        }
Beispiel #2
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");
            }
        }