// utility public override string ToString() { StringBuilder sb = new StringBuilder(); FinalizedCoffees?.ToList().ForEach(c => sb.AppendLine(c.ToString())); sb.AppendLine(); sb.AppendLine($"The total cost of your order is ${GetTotalPrice()}"); return(sb.ToString()); }
// Current Working Coffee management methods public void FinalizeWorkingCoffee() { if (CurrentWorkingCoffee == null) { // Not really the end of the world. Just going to log an error. logger.WriteError("Tried to add null working coffee to order."); return; } FinalizedCoffees = FinalizedCoffees.Append(CurrentWorkingCoffee); CurrentWorkingCoffee = null; }
// Transaction-based methods public string CompleteTransaction() { // This validation calls for some refactoring decimal change = MoneyAdded - GetTotalPrice(); if (!FinalizedCoffees.ToList().Any()) { throw new TransactionCompletedWithoutOrdersException("A transaction cannot be completed because there are currently no coffees ordered."); } if (change < 0) { throw new InvalidMoneyException($"You have not provided sufficient money to cover the cost of the order. Please add {change}."); } // Not sure why but couldn't get FinalizedCoffees.ToList().Clear() to work // Do want to get rid of this initialization here FinalizedCoffees = new List <ICoffee>(); return($"Thank you for your patronage. Your change is: {change}"); }
private decimal GetTotalPrice() { return(FinalizedCoffees.Select(c => c.GetPrice()).Sum()); }