Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using avrevic_zadaca_2.Builder;
- using avrevic_zadaca_2.CompositePattern;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace avrevic_zadaca_2
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Define> dataDefinitions = new List<Define>();
- List<Structure> structures = new List<Structure>();
- List<Action> actions = new List<Action>();
- List<int> listaRoditelja = new List<int>();
- string[] readLines;
- if (args.Length < 1)
- {
- printExitMsg("Niste upisali sve parametre potrebne za rad programa!");
- return;
- }
- //Pokusaj procitati datoteku
- try
- {
- //Procitaj sve linije iz datoteke u polje
- readLines = System.IO.File.ReadAllLines(args[0]);
- //Kreci se liniju po liniju i provjeri kojeg tipa je procitani objekt
- //(1- definiranje, 2- struktura, 3- akcija)
- DirectorData directorData = new DirectorData();
- Console.WriteLine("Podaci iz datoteke\n===========================================\n");
- foreach (string line in readLines)
- {
- string[] zapis = line.Split('\t');
- switch (zapis[0])
- {
- case "0":
- DataBuilder dataDefinition = new DefineBuilder(zapis, dataDefinitions);
- directorData.Construct(dataDefinition);
- Define defineData = (Define)dataDefinition.getData();
- if (defineData.errorMessage != "")
- {
- Console.WriteLine(" " + line + " -----> " + defineData.errorMessage);
- }
- else
- {
- dataDefinitions.Add(defineData);
- Console.WriteLine(" " + line);
- }
- break;
- case "1":
- DataBuilder dataStructure = new StructureBuilder(zapis, structures, dataDefinitions);
- directorData.Construct(dataStructure);
- Structure structureData = (Structure)dataStructure.getData();
- if (structureData.errorMessage != "")
- {
- Console.WriteLine(" " + line + " -----> " + structureData.errorMessage);
- }
- else
- {
- structures.Add(structureData);
- Console.WriteLine(" " + line);
- }
- break;
- case "2":
- DataBuilder dataAction = new ActionBuilder(zapis, actions, dataDefinitions);
- directorData.Construct(dataAction);
- Action actionData = (Action)dataAction.getData();
- if (actionData.errorMessage != "")
- {
- Console.WriteLine(" " + line + " -----> " + actionData.errorMessage);
- }
- else
- {
- actions.Add(actionData);
- Console.WriteLine(" " + line);
- }
- break;
- }
- }
- }
- catch (Exception ex)
- {
- printExitMsg("Nije uspješno pročitana datoteka!\n------\n" + ex.Message + "\n------\n");
- return;
- }
- Console.WriteLine("===========================================\n\n");
- Composite fakeRoot = new Composite(null);
- foreach (Structure s in structures)
- {
- Data data = null;
- Data parent = null;
- //Pronalazenje prikladnih struktura za objekte
- foreach (Define dataDefinition in dataDefinitions)
- {
- if (dataDefinition.code == s.code)
- {
- data = dataDefinition;
- }
- if (dataDefinition.code == s.parent)
- {
- parent = dataDefinition;
- }
- }
- //Pronalazenje roditelja
- Component parentComponent = fakeRoot.findComponent(parent);
- if (parentComponent == null)
- {
- parentComponent = new Composite(parent);
- fakeRoot.addComponent(parentComponent);
- }
- //Dodavanje komponente roditelju
- if (parentComponent.findComponent(data) == null)
- {
- Composite newChild = new Composite(data);
- newChild.parent = parentComponent;
- parentComponent.addComponent(newChild);
- }
- }
- //Pronalazenje i postavljanje samo jednog ishodisnog cvora
- int maxIndex = 0;
- int maxCount = 0;
- int counter = 0;
- foreach (Component child in fakeRoot.children)
- {
- int numComponents = child.numComponents(0);
- if (numComponents > maxCount)
- {
- maxIndex = counter;
- maxCount = numComponents;
- }
- counter++;
- //Console.WriteLine(child.data.code + " -> " + child.numComponents(0));
- }
- List<Component> allTopChildren = new List<Component>();
- foreach (Component child in fakeRoot.children)
- {
- allTopChildren.Add(child);
- }
- Component bigBoss = allTopChildren[maxIndex];
- fakeRoot.children.Clear();
- fakeRoot.children.Add(bigBoss);
- allTopChildren.RemoveAt(maxIndex);
- for (int i = 0; i < allTopChildren.Count; i++)
- {
- allTopChildren[i].parent = bigBoss;
- fakeRoot.children[0].children.Add(allTopChildren[i]);
- }
- Console.WriteLine("Pregled stanja\n===========================================");
- fakeRoot.display(1);
- Console.WriteLine("===========================================\n\n");
- Console.WriteLine("\n\nPress any key to continue...");
- Console.ReadKey();
- return;
- }
- static void printExitMsg(string msg)
- {
- Console.WriteLine(msg);
- Console.WriteLine("\n\nPress any key to continue...");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement