//private int canSend(BeerBar otherBar) //{ // var transportCost = transportationCost(otherBar); // var canSupply = Supply - transportCost; // var demand = Math.Abs(otherBar.Supply); // return otherBar.Supply < 0 && demand < canSupply ? demand : canSupply; //} public void SendSupply(BeerBar otherBar, bool includeCost) { var sendLeft = otherBar.Position < Position; var transportCost = includeCost ? transportationCost(otherBar) : 0; var inDemand = otherBar.Supply < 0; var demand = inDemand ? -otherBar.Supply : 0; var canSupply = Supply - transportCost; var toSend = sendLeft ? Math.Min(demand, canSupply) : canSupply; var actualSupplyAndTransportCost = toSend + transportCost; otherBar.Supply += toSend; otherBar.Beers += toSend; Supply -= actualSupplyAndTransportCost; Beers -= actualSupplyAndTransportCost; }
int transportationCost(BeerBar otherBar) { return(2 * Math.Abs(Position - otherBar.Position)); }
//public void SetSupply(int demand) //{ // Supply = Beers - demand; //} public bool CanSupply(BeerBar otherBar, bool includeCost) { var cost = includeCost ? transportationCost(otherBar) : 0; return(Supply - cost > 0); }