Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace FundamentalsFinalExam
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Town> towns = new List<Town>();
- while (true)
- {
- string input = Console.ReadLine();
- if (input == "Sail")
- {
- break;
- }
- string[] data = input.Split("||").ToArray();
- string name = data[0];
- int population = int.Parse(data[1]);
- int gold = int.Parse(data[2]);
- Town currentTown = new Town(name, population, gold);
- if (!towns.Any(x => x.Name == name))
- {
- towns.Add(currentTown);
- }
- else
- {
- Town temp = towns.FirstOrDefault(x => x.Name == name);
- temp.Gold += gold;
- temp.People += population;
- }
- }
- while (true)
- {
- string input = Console.ReadLine();
- if (input == "End")
- {
- break;
- }
- string[] data = input.Split("=>").ToArray();
- string command = data[0];
- if (command == "Plunder")
- {
- string name = data[1];
- int population = int.Parse(data[2]);
- int gold = int.Parse(data[3]);
- Town currentTown = towns.FirstOrDefault(x => x.Name == name);
- currentTown.People -= population;
- currentTown.Gold -= gold;
- Console.WriteLine($"{name} plundered! {gold} gold stolen, {population} citizens killed.");
- if (currentTown.People <= 0 || currentTown.Gold <= 0)
- {
- Console.WriteLine($"{name} has been wiped off the map!");
- towns.Remove(currentTown);
- }
- }
- else if (command == "Prosper")
- {
- string name = data[1];
- int gold = int.Parse(data[2]);
- Town currentTown = towns.FirstOrDefault(x => x.Name == name);
- if (gold < 0)
- {
- Console.WriteLine($"Gold added cannot be a negative number!");
- }
- else
- {
- currentTown.Gold += gold;
- Console.WriteLine($"{gold} gold added to the city treasury. {name} now has {currentTown.Gold} gold.");
- }
- }
- }
- if (towns.Count > 0)
- {
- Console.WriteLine($"Ahoy, Captain! There are {towns.Count} wealthy settlements to go to:");
- foreach (var town in towns.OrderByDescending(x => x.Gold).ThenBy(x => x.Name))
- {
- Console.WriteLine($"{town.Name} -> Population: {town.People} citizens, Gold: {town.Gold} kg");
- }
- }
- else
- {
- Console.WriteLine($"Ahoy, Captain! All targets have been plundered and destroyed!");
- }
- }
- class Town
- {
- public Town(string name, int people, int gold)
- {
- Name = name;
- People = people;
- Gold = gold;
- }
- public string Name { get; set; }
- public int People { get; set; }
- public int Gold { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement