Advertisement
avrevic

Untitled

Nov 21st, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.13 KB | None | 0 0
  1. using avrevic_zadaca_2.Builder;
  2. using avrevic_zadaca_2.CompositePattern;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace avrevic_zadaca_2
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             List<Define> dataDefinitions = new List<Define>();
  16.             List<Structure> structures = new List<Structure>();
  17.             List<Action> actions = new List<Action>();
  18.             List<int> listaRoditelja = new List<int>();
  19.  
  20.             string[] readLines;
  21.  
  22.             if (args.Length < 1)
  23.             {
  24.                 printExitMsg("Niste upisali sve parametre potrebne za rad programa!");
  25.                 return;
  26.             }
  27.  
  28.             //Pokusaj procitati datoteku
  29.             try
  30.             {
  31.                 //Procitaj sve linije iz datoteke u polje
  32.                 readLines = System.IO.File.ReadAllLines(args[0]);
  33.                 //Kreci se liniju po liniju i provjeri kojeg tipa je procitani objekt
  34.                 //(1- definiranje, 2- struktura, 3- akcija)
  35.  
  36.                 DirectorData directorData = new DirectorData();
  37.                 Console.WriteLine("Podaci iz datoteke\n===========================================\n");
  38.                 foreach (string line in readLines)
  39.                 {
  40.                     string[] zapis = line.Split('\t');
  41.  
  42.                     switch (zapis[0])
  43.                     {
  44.                         case "0":
  45.                             DataBuilder dataDefinition = new DefineBuilder(zapis, dataDefinitions);
  46.                             directorData.Construct(dataDefinition);
  47.                             Define defineData = (Define)dataDefinition.getData();
  48.                             if (defineData.errorMessage != "")
  49.                             {
  50.                                 Console.WriteLine(" " + line + " -----> " + defineData.errorMessage);
  51.                             }
  52.                             else
  53.                             {
  54.                                 dataDefinitions.Add(defineData);
  55.                                 Console.WriteLine(" " + line);
  56.                             }
  57.                             break;
  58.                         case "1":
  59.                             DataBuilder dataStructure = new StructureBuilder(zapis, structures, dataDefinitions);
  60.                             directorData.Construct(dataStructure);
  61.                             Structure structureData = (Structure)dataStructure.getData();
  62.                             if (structureData.errorMessage != "")
  63.                             {
  64.                                 Console.WriteLine(" " + line + " -----> " + structureData.errorMessage);
  65.                             }
  66.                             else
  67.                             {
  68.                                 structures.Add(structureData);
  69.                                 Console.WriteLine(" " + line);
  70.                             }
  71.                             break;
  72.                         case "2":
  73.                             DataBuilder dataAction = new ActionBuilder(zapis, actions, dataDefinitions);
  74.                             directorData.Construct(dataAction);
  75.                             Action actionData = (Action)dataAction.getData();
  76.                             if (actionData.errorMessage != "")
  77.                             {
  78.                                 Console.WriteLine(" " + line + " -----> " + actionData.errorMessage);
  79.                             }
  80.                             else
  81.                             {
  82.                                 actions.Add(actionData);
  83.                                 Console.WriteLine(" " + line);
  84.                             }
  85.                             break;
  86.                     }
  87.                 }
  88.             }
  89.             catch (Exception ex)
  90.             {
  91.                 printExitMsg("Nije uspješno pročitana datoteka!\n------\n" + ex.Message + "\n------\n");
  92.                 return;
  93.             }
  94.  
  95.             Console.WriteLine("===========================================\n\n");
  96.  
  97.             Composite fakeRoot = new Composite(null);
  98.  
  99.             foreach (Structure s in structures)
  100.             {
  101.                 Data data = null;
  102.                 Data parent = null;
  103.  
  104.                 //Pronalazenje prikladnih struktura za objekte
  105.                 foreach (Define dataDefinition in dataDefinitions)
  106.                 {
  107.                     if (dataDefinition.code == s.code)
  108.                     {
  109.                         data = dataDefinition;
  110.                     }
  111.                     if (dataDefinition.code == s.parent)
  112.                     {
  113.                         parent = dataDefinition;
  114.                     }
  115.                 }
  116.  
  117.                 //Pronalazenje roditelja
  118.                 Component parentComponent = fakeRoot.findComponent(parent);
  119.                 if (parentComponent == null)
  120.                 {
  121.                     parentComponent = new Composite(parent);
  122.                     fakeRoot.addComponent(parentComponent);
  123.                 }
  124.  
  125.                 //Dodavanje komponente roditelju
  126.                 if (parentComponent.findComponent(data) == null)
  127.                 {
  128.                     Composite newChild = new Composite(data);
  129.                     newChild.parent = parentComponent;
  130.                     parentComponent.addComponent(newChild);
  131.                 }
  132.             }
  133.  
  134.             //Pronalazenje i postavljanje samo jednog ishodisnog cvora
  135.             int maxIndex = 0;
  136.             int maxCount = 0;
  137.             int counter = 0;
  138.             foreach (Component child in fakeRoot.children)
  139.             {
  140.                 int numComponents = child.numComponents(0);
  141.                 if (numComponents > maxCount)
  142.                 {
  143.                     maxIndex = counter;
  144.                     maxCount = numComponents;
  145.                 }
  146.                 counter++;
  147.                 //Console.WriteLine(child.data.code + " -> " + child.numComponents(0));
  148.             }
  149.  
  150.             List<Component> allTopChildren = new List<Component>();
  151.             foreach (Component child in fakeRoot.children)
  152.             {
  153.                 allTopChildren.Add(child);
  154.             }
  155.             Component bigBoss = allTopChildren[maxIndex];
  156.             fakeRoot.children.Clear();
  157.             fakeRoot.children.Add(bigBoss);
  158.             allTopChildren.RemoveAt(maxIndex);
  159.  
  160.             for (int i = 0; i < allTopChildren.Count; i++)
  161.             {
  162.                 allTopChildren[i].parent = bigBoss;
  163.                 fakeRoot.children[0].children.Add(allTopChildren[i]);
  164.             }
  165.  
  166.             Console.WriteLine("Pregled stanja\n===========================================");
  167.             fakeRoot.display(1);
  168.             Console.WriteLine("===========================================\n\n");
  169.             Console.WriteLine("\n\nPress any key to continue...");
  170.             Console.ReadKey();
  171.             return;
  172.         }
  173.  
  174.         static void printExitMsg(string msg)
  175.         {
  176.             Console.WriteLine(msg);
  177.             Console.WriteLine("\n\nPress any key to continue...");
  178.             Console.ReadKey();
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement