public void Run() { GetBuyers(); string name = Console.ReadLine(); while (name != "End") { bool isCitizen = this.citizenBuyers.Any(x => x.Name == name); bool isRebel = this.rebelBuyers.Any(x => x.Name == name); if (isCitizen) { Citizen citizen = this.citizenBuyers.First(x => x.Name == name); int index = this.citizenBuyers.IndexOf(citizen); this.citizenBuyers[index].BuyFood(); } else if (isRebel) { Rebel rebel = this.rebelBuyers.First(x => x.Name == name); int index = this.rebelBuyers.IndexOf(rebel); this.rebelBuyers[index].BuyFood(); } name = Console.ReadLine(); } int totalFood = this.citizenBuyers.Sum(f => f.Food) + this.rebelBuyers.Sum(f => f.Food); Console.WriteLine(totalFood); }
private static void GetCitizensAndRebels(int numberofPeople, List <IBuyer> people) { for (int i = 0; i < numberofPeople; i++) { var personArgs = Console.ReadLine().Split(' '); switch (personArgs.Length) { case 4: var name = personArgs[0]; var age = int.Parse(personArgs[1]); var id = personArgs[2]; var birthdate = personArgs[3]; var citizen = new Citizen(name, age, id, birthdate); people.Add(citizen); break; case 3: var rebelName = personArgs[0]; var rebelAge = int.Parse(personArgs[1]); var group = personArgs[2]; var rebel = new Rebel(rebelName, rebelAge, @group); people.Add(rebel); break; } } }
private void CreateRebel(string[] buyerInfo) { string name = buyerInfo[0]; int age = int.Parse(buyerInfo[1]); string group = buyerInfo[2]; Rebel rebel = new Rebel(name, age, group); this.rebelBuyers.Add(rebel); }
public static void Main() { int numberOfPeople = int.Parse(Console.ReadLine()); Dictionary <string, IBuyer> people = new Dictionary <string, IBuyer>(); for (int i = 0; i < numberOfPeople; i++) { string[] peopleInfo = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (peopleInfo.Length == 4) { string name = peopleInfo[0]; int age = int.Parse(peopleInfo[1]); string id = peopleInfo[2]; string birthday = peopleInfo[3]; Citizen citizen = new Citizen(name, age, id, birthday); people.Add(name, citizen); } else if (peopleInfo.Length == 3) { string name = peopleInfo[0]; int age = int.Parse(peopleInfo[1]); string group = peopleInfo[2]; Rebel rebel = new Rebel(name, age, group); people.Add(name, rebel); } } string buyerName = Console.ReadLine().Trim(); while (buyerName != "End") { try { people[buyerName].BuyFood(); } catch { } buyerName = Console.ReadLine().Trim(); } int totalFood = 0; foreach (var kvp in people) { totalFood += kvp.Value.Food; } Console.WriteLine(totalFood); }
public static void Main() { int n = int.Parse(Console.ReadLine()); List <Human> people = new List <Human>(); for (int i = 0; i < n; i++) { string[] humanDetails = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (humanDetails.Length == 4) { string name = humanDetails[0]; int age = int.Parse(humanDetails[1]); string id = humanDetails[2]; string birthDate = humanDetails[3]; Human currentPerson = new Person(name, age, id, birthDate); people.Add(currentPerson); } else { string name = humanDetails[0]; int age = int.Parse(humanDetails[1]); string group = humanDetails[2]; Human currentRebel = new Rebel(name, age, group); people.Add(currentRebel); } } string nameCommand = Console.ReadLine(); while (nameCommand != "End") { people.FirstOrDefault(x => x.Name == nameCommand)?.BuyFood(); //var searchHuman = people.FirstOrDefault(x => x.Name == nameCommand); //if (searchHuman != null) //{ // searchHuman.BuyFood(); //foreach (var person in people) //{ // if (person.Equals(searchHuman)) // { // person.BuyFood(); // } //} //} nameCommand = Console.ReadLine(); } var sumOfFood = people.Sum(x => x.Food); Console.WriteLine(sumOfFood); }
public static void Main() { int n = int.Parse(Console.ReadLine()); List <IBuyer> buyers = new List <IBuyer>(); for (int i = 0; i < n; i++) { string[] inputArgs = Console.ReadLine().Split(); if (inputArgs.Length == 4) { string name = inputArgs[0]; int age = int.Parse(inputArgs[1]); string id = inputArgs[2]; string birthday = inputArgs[3]; Human human = new Human(name, age, id, birthday); buyers.Add(human); } else if (inputArgs.Length == 3) { string name = inputArgs[0]; int age = int.Parse(inputArgs[1]); string group = inputArgs[2]; Rebel rebel = new Rebel(name, age, group); buyers.Add(rebel); } } string command = Console.ReadLine(); while (command != "End") { string currentName = command; var buyer = buyers.Where(x => x.Name == currentName).FirstOrDefault(); if (buyer != null) { buyer.BuyFood(); } command = Console.ReadLine(); } int food = buyers.Where(x => x.Food > 0).Sum(x => x.Food); Console.WriteLine(food); }
static void Main(string[] args) { int count = int.Parse(Console.ReadLine()); List <Existable> citizens = new List <Existable>(); for (int i = 0; i < count; i++) { string[] tokens = Console.ReadLine() .Split(); string name = tokens[0]; int age = int.Parse(tokens[1]); if (tokens.Length == 4) { string id = tokens[2]; DateTime birthDate = DateTime.ParseExact(tokens[3], "dd/MM/yyyy", CultureInfo.InvariantCulture); var citizen = new Citizen(name, age, id, birthDate); citizens.Add(citizen); } else { string group = tokens[2]; var rebel = new Rebel(name, age, group); citizens.Add(rebel); } } string input = Console.ReadLine(); int sum = 0; while (input != "End") { var citizen = citizens.FirstOrDefault(x => x.Name == input); if (citizen != null) { sum += citizen.BuyFood(); } input = Console.ReadLine(); } Console.WriteLine(sum); }
public static void Main() { int n = int.Parse(Console.ReadLine()); var buyers = new Dictionary <string, IBuyer>(); for (int i = 0; i < n; i++) { var tokens = Console.ReadLine().Split(); if (tokens.Length > 3) { var name = tokens[0]; var age = int.Parse(tokens[1]); var id = tokens[2]; var birthdate = tokens[3]; Citizen citizen = new Citizen(name, age, id, birthdate); buyers[name] = citizen; } else { var name = tokens[0]; var age = int.Parse(tokens[1]); var group = tokens[2]; Rebel rebel = new Rebel(name, age, group); buyers[name] = rebel; } } var nameCmd = Console.ReadLine(); while (nameCmd != "End") { if (buyers.ContainsKey(nameCmd)) { buyers[nameCmd].BuyFood(); } nameCmd = Console.ReadLine(); } Console.WriteLine(buyers.Values.Sum(b => b.Food)); }
static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); List <Human> humans = new List <Human>(); for (int i = 0; i < n; i++) { string[] citizenInfo = Console.ReadLine().Split(); string name = citizenInfo[0]; int age = int.Parse(citizenInfo[1]); int citizenLenght = citizenInfo.Length; switch (citizenLenght) { case 4: string id = citizenInfo[2]; string birthdate = citizenInfo[3]; Citizen citizen = new Citizen(name, age, id, birthdate); humans.Add(citizen); break; case 3: string group = citizenInfo[2]; Rebel rebel = new Rebel(name, age, group); humans.Add(rebel); break; } } string buyerName = Console.ReadLine(); while (buyerName != "End") { humans.Find(h => h.Name == buyerName)?.BuyFood(); buyerName = Console.ReadLine(); } int foodQuantity = 0; humans.ForEach(x => foodQuantity += x.Food); Console.WriteLine(foodQuantity); }
public static void Main() { string input; List <IBuyer> buyers = new List <IBuyer>(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { var buyerInfo = Console.ReadLine().Split(); IBuyer buyer = null; if (buyerInfo.Length == 4) { buyer = new Citizen(buyerInfo[0], int.Parse(buyerInfo[1]), buyerInfo[2], buyerInfo[3]); } else { buyer = new Rebel(buyerInfo[0], int.Parse(buyerInfo[1]), buyerInfo[2]); } buyer.BuyFood(); buyers.Add(buyer); } int totalFood = 0; while ((input = Console.ReadLine()) != "End") { IBuyer currentBuyer = buyers.FirstOrDefault(b => b.Name == input); if (buyers.Contains(currentBuyer)) { totalFood += currentBuyer.Food; } } Console.WriteLine(totalFood); }
private static void ParseInput(List <IBuyer> buyers) { int number = int.Parse(Console.ReadLine()); for (int i = 0; i < number; i++) { string[] inputParts = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (inputParts.Length == 4) { IBuyer human = new Human(inputParts[0], int.Parse(inputParts[1]), inputParts[2], inputParts[3]); buyers.Add(human); } else if (inputParts.Length == 3) { IBuyer rebel = new Rebel(inputParts[0], int.Parse(inputParts[1]), inputParts[2]); buyers.Add(rebel); } } }
public static void Main() { var n = int.Parse(Console.ReadLine()); var input = Console.ReadLine(); var buyers = new List <Buyer>(); for (int i = 0; i < n; i++) { var tokens = input.Split(); Buyer buyer; var name = tokens[0]; var age = int.Parse(tokens[1]); if (tokens.Length == 4) { buyer = new Citizen(name, age, tokens[2], tokens[3]); } else { buyer = new Rebel(name, age, tokens[2]); } buyers.Add(buyer); input = Console.ReadLine(); } while (input != "End") { var buyer = buyers.FirstOrDefault(b => b.Name == input); buyer?.BuyFood(); input = Console.ReadLine(); } Console.WriteLine(buyers.Sum(b => b.Food)); }
static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var listOfBuy = new List <IBuy>(); for (int i = 0; i < n; i++) { var buyerArgs = Console.ReadLine().Split(); IBuy currentBuyer; if (buyerArgs.Length == 3) { currentBuyer = new Rebel(buyerArgs[0], int.Parse(buyerArgs[1]), buyerArgs[2]); } else { currentBuyer = new Citizen(buyerArgs[0], int.Parse(buyerArgs[1]), buyerArgs[2], buyerArgs[3]); } listOfBuy.Add(currentBuyer); } string buyerName; while ((buyerName = Console.ReadLine()) != "End") { foreach (var buyer in listOfBuy.Where(b => b.Name == buyerName)) { buyer.BuyFood(); } } var totalAmountOfFood = listOfBuy.Sum(b => b.Food); Console.WriteLine(totalAmountOfFood); }
static void Main(string[] args) { List <IBuyer> buyers = new List <IBuyer>(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { string[] line = Console.ReadLine().Split(); if (line.Length == 4) { string name = line[0]; int age = int.Parse(line[1]); string id = line[2]; DateTime birthdate = DateTime.ParseExact(line[3], "dd/MM/yyyy", CultureInfo.InvariantCulture); Citizen citizen = new Citizen(name, age, id, birthdate); buyers.Add(citizen); } else { string name = line[0]; int age = int.Parse(line[1]); string group = line[2]; Rebel rebel = new Rebel(name, age, group); buyers.Add(rebel); } } while (true) { string line = Console.ReadLine(); if (line == "End") { break; } string name = line; var filterBuyers = buyers.Where(x => x.Name == name) .ToList(); foreach (var buyer in filterBuyers) { buyer.BuyFood(); } } int total = 0; foreach (var buyer in buyers) { total += buyer.Food; } Console.WriteLine(total); }
static void Main() { int peopleCount = int.Parse(Console.ReadLine()); //var members = new Dictionary<string, IBuyer>(); var citizens = new Dictionary <string, Citizen>(); var rebels = new Dictionary <string, Rebel>(); for (int cnt = 1; cnt <= peopleCount; cnt++) { string input = Console.ReadLine(); var tokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 4) { string name = tokens[0]; int age = int.Parse(tokens[1]); string id = tokens[2]; string birthdate = tokens[3]; Citizen citizen = new Citizen(name, age, id, birthdate); if (citizens.ContainsKey(name) == false) { citizens.Add(name, citizen); } } else if (tokens.Length == 3) { string name = tokens[0]; int age = int.Parse(tokens[1]); string group = tokens[2]; Rebel rebel = new Rebel(name, age, group); if (rebels.ContainsKey(name) == false) { rebels.Add(name, rebel); } } } string command = Console.ReadLine(); int foodPurchased = 0; while (command != "End") { string nameToFind = command.Trim(); if (citizens.ContainsKey(nameToFind)) { var member = citizens[nameToFind]; int oldValue = member.Food; int newValue = -1; citizens[nameToFind].BuyFood(); newValue = member.Food; foodPurchased += newValue - oldValue; } else if (rebels.ContainsKey(nameToFind)) { var member = rebels[nameToFind]; int oldValue = member.Food; int newValue = -1; rebels[nameToFind].BuyFood(); newValue = member.Food; foodPurchased += newValue - oldValue; } command = Console.ReadLine(); } Console.WriteLine(foodPurchased); }