public override string ToString() { StringBuilder result = new StringBuilder(); result.AppendLine(this.Name.ToString()); result.AppendLine("Company:"); if (this.Company != null) { result.AppendLine(this.Company.ToString()); } result.AppendLine("Car:"); if (this.Car != null) { result.AppendLine(this.Car.ToString()); } result.AppendLine("Pokemon:"); if (Pokemons.Any()) { result.AppendLine(string.Join(Environment.NewLine, Pokemons)); } result.AppendLine("Parents:"); if (Parents.Any()) { result.AppendLine(string.Join(Environment.NewLine, Parents)); } result.AppendLine("Children:"); if (Children.Any()) { result.AppendLine(string.Join(Environment.NewLine, Children)); } return(result.ToString().TrimEnd()); }
public override string ToString() { string output = $"{Name}\n"; output += $"Company:\n"; if (Company?.ToString() != null) { output += $"{Company?.ToString()}\n"; } output += $"Car:\n"; if (Car?.ToString() != null) { output += $"{Car?.ToString()}\n"; } output += $"Pokemon:\n"; Pokemons.ForEach(p => output += p.ToString() + "\n"); output += $"Parents:\n"; Parents.ForEach(p => output += p.ToString() + "\n"); output += $"Children:\n"; Children.ForEach(c => output += c.ToString() + "\n"); return(output.Trim()); }
static void Main(string[] args) { string command = Console.ReadLine(); Dictionary <string, Person> dictionary = new Dictionary <string, Person>(); while (command != "End") { string[] info = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string name = info[0]; if (!dictionary.ContainsKey(name)) { dictionary.Add(name, new Person(name)); } if (info.Length == 5) { string companyName = info[2]; string department = info[3]; decimal salary = decimal.Parse(info[4]); Company company = new Company(companyName, department, salary); dictionary[name].Company = company; } else if (info.Length == 4) { if (command.Contains("pokemon")) { string pokemonName = info[2]; string pokemonType = info[3]; Pokemon pokemon = new Pokemon(pokemonName, pokemonType); dictionary[name].Pokemons.Add(pokemon); } else if (command.Contains("parents")) { string parentName = info[2]; DateTime parentBirthday = DateTime.ParseExact(info[3], "dd/MM/yyyy", CultureInfo.InvariantCulture); Parents parent = new Parents(parentName, parentBirthday); dictionary[name].Parents.Add(parent); } else if (command.Contains("children")) { string childName = info[2]; DateTime childBirthday = DateTime.ParseExact(info[3], "dd/MM/yyyy", CultureInfo.InvariantCulture); Children child = new Children(childName, childBirthday); dictionary[name].Children.Add(child); } else if (command.Contains("car")) { string carModel = info[2]; int carSpeed = int.Parse(info[3]); Car car = new Car(carModel, carSpeed); dictionary[name].Car = car; } } command = Console.ReadLine(); } string person = Console.ReadLine(); foreach (var item in dictionary.Where(a => a.Key == person)) { Console.WriteLine(item.Key); Console.WriteLine("Company:"); if (item.Value.Company != null) { Console.WriteLine($"{item.Value.Company.CompanyName} {item.Value.Company.Department} {item.Value.Company.Salary:f2}"); } Console.WriteLine("Car:"); if (item.Value.Car != null) { Console.WriteLine($"{item.Value.Car.Model} {item.Value.Car.Speed}"); } Console.WriteLine("Pokemon:"); if (item.Value.Pokemons.Count > 0) { foreach (var pokemon in item.Value.Pokemons) { Console.WriteLine($"{pokemon.Name} {pokemon.Type}"); } } Console.WriteLine("Parents:"); if (item.Value.Parents.Count > 0) { foreach (var parent in item.Value.Parents) { Console.WriteLine($"{parent.Name} {parent.Birthday.Day:d2}/{parent.Birthday.Month:d2}/{parent.Birthday.Year}"); } } Console.WriteLine("Children:"); if (item.Value.Children.Count > 0) { foreach (var child in item.Value.Children) { Console.WriteLine($"{child.Name} {child.Birthday.Day:d2}/{child.Birthday.Month:d2}/{child.Birthday.Year}"); } } } }
static void Main(string[] args) { string input = Console.ReadLine(); var people = new Dictionary <string, Person>(); while (input != "End") { string[] arr = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); string personName = arr[0]; string @object = arr[1].ToLower(); string name = ""; string birthday = ""; switch (@object) { case "company": name = arr[2]; string department = arr[3]; decimal salary = decimal.Parse(arr[4]); Company company = new Company(name, department, salary); Person.Contains(personName, people); people[personName].Company = new KeyValuePair <string, Company>("Company:", company); break; case "pokemon": name = arr[2]; string type = arr[3]; Pokemon pokemon = new Pokemon(name, type); Person.Contains(personName, people); people[personName].Pokemons.Value.Add(pokemon); break; case "parents": name = arr[2]; birthday = arr[3]; Parents parents = new Parents(name, birthday); Person.Contains(personName, people); people[personName].Parents.Value.Add(parents); break; case "children": name = arr[2]; birthday = arr[3]; Children children = new Children(name, birthday); Person.Contains(personName, people); people[personName].Children.Value.Add(children); break; case "car": string model = arr[2]; string speed = arr[3]; Car car = new Car(model, speed); Person.Contains(personName, people); people[personName].Car = new KeyValuePair <string, Car>("Car:", car); break; } input = Console.ReadLine(); } string printPerson = Console.ReadLine(); Console.WriteLine(printPerson); Console.WriteLine(people[printPerson].ToString()); }
public static void Main(string[] args) { List <People> peoples = new List <People>(); string input; while ((input = Console.ReadLine()) != "End") { string[] arr = input.Split().ToArray(); string name = arr[0]; if (peoples.All(x => x.Name != name)) { peoples.Add(new People(name)); } if (arr[1] == "company") { string nameCompany = arr[2]; string departmnet = arr[3]; double salary = double.Parse(arr[4]); Company company = new Company(nameCompany, departmnet, salary); People person = peoples.FirstOrDefault(x => x.Name == name); person.Company = company; } else if (arr[1] == "car") { string model = arr[2]; double speed = double.Parse(arr[3]); Car car = new Car(model, speed); People person = peoples.FirstOrDefault(x => x.Name == name); person.Car = car; } else if (arr[1] == "children") { string nameChildren = arr[2]; string birthDay = arr[3]; Children children = new Children(nameChildren, birthDay); People person = peoples.FirstOrDefault(x => x.Name == name); person.Childrens.Add(children); } else if (arr[1] == "parents") { string nameParent = arr[2]; string birthDay = arr[3]; Parents parent = new Parents(nameParent, birthDay); People person = peoples.FirstOrDefault(x => x.Name == name); person.Parents.Add(parent); } else if (arr[1] == "pokemon") { string namePokemon = arr[2]; string type = arr[3]; Pokemon pokemon = new Pokemon(namePokemon, type); People person = peoples.FirstOrDefault(x => x.Name == name); person.Pokemons.Add(pokemon); } } string findPerson = Console.ReadLine(); People pers = peoples.First(x => x.Name == findPerson); Console.WriteLine(pers.Name); Console.WriteLine("Company:"); if (pers.Company != null) { Console.WriteLine(pers.Company); } Console.WriteLine("Car:"); if (pers.Car != null) { Console.WriteLine(pers.Car); } Console.WriteLine("Pokemon:"); if (pers.Pokemons != null) { pers.Pokemons.ForEach(p => Console.WriteLine(p)); } Console.WriteLine("Parents:"); if (pers.Parents != null) { pers.Parents.ForEach(p => Console.WriteLine(p)); } Console.WriteLine("Children:"); if (pers.Childrens != null) { pers.Childrens.ForEach(c => Console.WriteLine(c)); } }
public override string ToString() { string output = string.Empty; output = string.Concat(output, Name); output = string.Concat(output, Environment.NewLine); output = string.Concat(output, "Company:"); output = string.Concat(output, Environment.NewLine); if (Company != null) { output = string.Concat(output, Company.ToString()); output = string.Concat(output, Environment.NewLine); } output = string.Concat(output, "Car:"); output = string.Concat(output, Environment.NewLine); if (Car != null) { output = string.Concat(output, Car.ToString()); output = string.Concat(output, Environment.NewLine); } output = string.Concat(output, "Pokemon:"); output = string.Concat(output, Environment.NewLine); if (Pokemons.Any()) { foreach (var pokemon in Pokemons) { output = string.Concat(output, pokemon.ToString()); output = string.Concat(output, Environment.NewLine); } } output = string.Concat(output, "Parents:"); output = string.Concat(output, Environment.NewLine); if (Parents.Any()) { foreach (var parent in Parents) { output = string.Concat(output, parent.ToString()); output = string.Concat(output, Environment.NewLine); } } output = string.Concat(output, "Children:"); output = string.Concat(output, Environment.NewLine); if (Children.Any()) { foreach (var child in Children) { output = string.Concat(output, child.ToString()); output = string.Concat(output, Environment.NewLine); } } return(output); }
static void Main(string[] args) { Dictionary <string, Person> people = new Dictionary <string, Person>(); string input = Console.ReadLine(); while (input != "End") { string[] inputTokens = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (!people.ContainsKey(inputTokens[0])) { people[inputTokens[0]] = new Person(inputTokens[0]); } if (inputTokens[1] == "company") { string personName = inputTokens[0]; string companyName = inputTokens[2]; string companyDepartment = inputTokens[3]; decimal salary = decimal.Parse(inputTokens[4]); Company company = new Company(companyName, companyDepartment, salary); people[personName].Company = company; } else if (inputTokens[1] == "pokemon") { string pokemonName = inputTokens[2]; string pokemonType = inputTokens[3]; Pokemon pokemon = new Pokemon(pokemonName, pokemonType); people[inputTokens[0]].Pokemons.Add(pokemon); } else if (inputTokens[1] == "parents") { string parentsName = inputTokens[2]; string birthDay = inputTokens[3]; Parents parent = new Parents() { ParentName = parentsName, ParentBirthday = birthDay }; people[inputTokens[0]].Parents.Add(parent); } else if (inputTokens[1] == "children") { string childName = inputTokens[2]; string childBirthday = inputTokens[3]; Children child = new Children() { ChildrenName = childName, ChildrenBirthDay = childBirthday }; people[inputTokens[0]].Children.Add(child); } else if (inputTokens[1] == "car") { string carModel = inputTokens[2]; string carSpeed = inputTokens[3]; Car car = new Car() { CarModel = carModel, CarSpeed = carSpeed }; people[inputTokens[0]].Car = car; } input = Console.ReadLine(); } string nameToSearch = Console.ReadLine(); foreach (var item in people) { if (item.Key == nameToSearch) { Console.WriteLine(item.Key); Console.WriteLine("Company:"); if (item.Value.Company != null) { Console.WriteLine($"{item.Value.Company.CompanyName} {item.Value.Company.Department} {item.Value.Company.Salary:F2}"); } Console.WriteLine("Car:"); if (item.Value.Car != null) { Console.WriteLine($"{item.Value.Car.CarModel} {item.Value.Car.CarSpeed}"); } Console.WriteLine("Pokemon:"); if (item.Value.Pokemons.Count > 0) { foreach (var pokemon in item.Value.Pokemons) { Console.WriteLine($"{pokemon.PokemonName} {pokemon.PokemonType}"); } } Console.WriteLine("Parents:"); if (item.Value.Parents.Count > 0) { foreach (var parent in item.Value.Parents) { Console.WriteLine($"{parent.ParentName} {parent.ParentBirthday}"); } } Console.WriteLine("Children:"); if (item.Value.Children.Count > 0) { foreach (var children in item.Value.Children) { Console.WriteLine($"{children.ChildrenName} {children.ChildrenBirthDay}"); } } } } }
static void Main(string[] args) { string input = Console.ReadLine(); Dictionary <string, Person> persons = new Dictionary <string, Person>(); while (input != "End") { string[] inputTokens = input.Split(' '); string personName = inputTokens[0]; if (!persons.ContainsKey(personName)) { Person newPerson = new Person(personName); persons.Add(personName, newPerson); } string cmd = inputTokens[1]; switch (cmd) { case "company": string companyName = inputTokens[2]; string department = inputTokens[3]; decimal salary = decimal.Parse(inputTokens[4]); Company company = new Company(companyName, department, salary); persons[personName].Company = company; break; case "car": string carName = inputTokens[2]; int speed = int.Parse(inputTokens[3]); Car car = new Car(carName, speed); persons[personName].Car = car; break; case "pokemon": string pokemonName = inputTokens[2]; string type = inputTokens[3]; Pokemon pokemon = new Pokemon(pokemonName, type); persons[personName].AddPokemon(pokemon); break; case "parents": string parentName = inputTokens[2]; string parentBirthday = inputTokens[3]; Parents parent = new Parents(parentName, parentBirthday); persons[personName].AddParent(parent); break; case "children": string childrenName = inputTokens[2]; string childrenBirthday = inputTokens[3]; Children children = new Children(childrenName, childrenBirthday); persons[personName].AddChildren(children); break; default: break; } input = Console.ReadLine(); } string name = Console.ReadLine(); Console.WriteLine ( persons[name] .ToString() .Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine) .TrimEnd() ); }