public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendLine("<Gold> $" + Gold); sb.AppendLine("##Gold - " + Gold); var gemsTotal = Gems.Sum(g => g.Quantity); if (gemsTotal > 0) { sb.AppendLine("<Gem> $" + gemsTotal); foreach (var gem in Gems.OrderByDescending(g => g.Name).ThenBy(g => g.Quantity)) { sb.AppendLine($"##{gem.Name} - {gem.Quantity}"); } var cashTotal = Cash.Sum(c => c.Quantity); if (cashTotal > 0) { sb.AppendLine("<Cash> $" + cashTotal); foreach (var cash in Cash.OrderByDescending(g => g.Currency).ThenBy(g => g.Quantity)) { sb.AppendLine($"##{cash.Currency} - {cash.Quantity}"); } } } return(sb.ToString()); }
public void AddGem(string name, long quantity) { if (IsEnoughSpace(quantity) && Gold >= Gems.Sum(g => g.Quantity) + quantity) { Gem gem = Gems.FirstOrDefault(g => g.Name == name); if (gem == null) { gem = new Gem(name); Gems.Add(gem); } gem.Quantity += quantity; } }
public void AddCash(string currency, long quantity) { if (IsEnoughSpace(quantity) && Gems.Sum(g => g.Quantity) >= Cash.Sum(c => c.Quantity) + quantity) { Cash cash = this.Cash.FirstOrDefault(c => c.Currency == currency); if (cash == null) { cash = new Cash(currency); this.Cash.Add(cash); } cash.Quantity += quantity; } }
private bool IsEnoughSpace(long quantity) => Capacity >= quantity + Gold + Gems.Sum(g => g.Quantity) + Cash.Sum(c => c.Quantity);
public long GetGemsAmount() { return(Gems.Sum(g => g.Value)); }