11,578 questions
This will give a breakdown of profits for traders of particular items:
List<Trade> trades = new() {
new Trade { ItemId = "Item 1", Sender = "Person 1", Receiver = "Person 2", Price = 100 },
new Trade { ItemId = "Item 1", Sender = "Person 2", Receiver = "Person 3", Price = 110 },
new Trade { ItemId = "Item 1", Sender = "Person 3", Receiver = "Person 2", Price = 120 },
new Trade { ItemId = "Item 2", Sender = "Person 4", Receiver = "Person 2", Price = 500 }
};
var traderDetails = trades
.GroupBy(t => new { t.Receiver, t.ItemId })
.Select(t => {
var totalBuyPrice = t.Sum(_ => _.Price);
var totalSellPrice = trades.Where(_ => _.Sender == t.Key.Receiver && _.ItemId == t.Key.ItemId).Sum(_ => _.Price);
return new {
Trader = t.Key.Receiver,
t.Key.ItemId,
TotalBuyPrice = totalBuyPrice,
TotalSellPrice = totalSellPrice,
Profit = totalSellPrice - totalBuyPrice,
Profitable = totalSellPrice - totalBuyPrice > 0
};
});
foreach (var details in traderDetails) {
if (details.Profitable) {
ForegroundColor = ConsoleColor.Green;
} else {
ForegroundColor = ConsoleColor.Red;
}
WriteLine(details.Trader + " bought " + details.ItemId + " for a total of " + details.TotalBuyPrice + " and sold for a total of " + details.TotalSellPrice + " Profit: " + details.Profit);
}
Or if you the most profitable trader overall:
var mostProfitable = traderDetails.GroupBy(t => t.Trader)
.OrderByDescending(t => t.Sum(_ => _.Profit)).Take(1).Select(_ => _.Key).FirstOrDefault();
WriteLine();
WriteLine("Most profitable: " + mostProfitable);