public void AddPrecious(IPrecious precious) { if (precious != null) { this.preciousType = precious.PreciousType; if (preciousType.ToLower() == "gold") { bool canAddGol = this.totalGoldQtty + precious.Quantity <= this.Capacity; if (canAddGol) { this.totalGoldQtty = AddCurrentPrrecious(precious, this.totalGoldQtty); } } else if (this.preciousType.ToLower().EndsWith("gem")) { bool canAddGems = this.totalGemsQtty + precious.Quantity <= this.totalGoldQtty; if (canAddGems) { this.totalGemsQtty = AddCurrentPrrecious(precious, this.totalGemsQtty); } } else if (this.preciousType.ToLower().Length == 3) { bool canAddCash = this.totalCashQtty + precious.Quantity <= this.totalGemsQtty; if (canAddCash) { this.totalCashQtty = AddCurrentPrrecious(precious, this.totalCashQtty); } } } }
private long AddCurrentPrrecious(IPrecious precious, long totalPreciousQtty) { var serachedPrecious = bagContent.FirstOrDefault(p => p.PreciousType.ToLower() == this.preciousType.ToLower()); if (serachedPrecious == null) { this.bagContent.Add(precious); } else { serachedPrecious.Quantity += precious.Quantity; } totalPreciousQtty += precious.Quantity; this.Capacity -= precious.Quantity; return(totalPreciousQtty); }
public void Run() { long bagCapacity = long.Parse(Console.ReadLine()); Bag bag = new Bag(bagCapacity); string[] caseArgs = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < caseArgs.Length; i += 2) { string preciousType = caseArgs[i]; long preciousQuantity = long.Parse(caseArgs[i + 1]); this.precious = this.factory.CreatePrecious(preciousType, preciousQuantity); bag.AddPrecious(precious); } Console.WriteLine(bag); }
public IPrecious CreatePrecious(string preciousType, long qtty) { string typePR = preciousType.ToLower(); if (typePR == "gold") { precious = new Gold(preciousType, qtty); } else if (typePR.EndsWith("gem")) { precious = new Gem(preciousType, qtty); } else if (typePR.Length == 3) { precious = new Cash(preciousType, qtty); } else { return(null); } return(precious); }