public string Print() { StringBuilder builder = new StringBuilder(); builder.AppendLine($"{"Category Name",15} {"Product Name",15} {"Quantity",10} {"Unit Price",12} {"Total Price",12}"); var categoryWithProducts = ProductsWithQuantity.GroupBy(p => p.Key.Category.Title).ToDictionary(e => e.Key, e => e.ToList()); foreach (var category in categoryWithProducts) { foreach (var product in category.Value) { builder.AppendLine($"{category.Key,15} {product.Key.Title,15} {product.Value,10} {product.Key.Price,12} {(product.Key.Price * product.Value),12}\t"); } } var totalAmount = GetTotalAmount(); var totalAmountAfterDiscount = GetTotalAmountAfterDiscounts(); var totalDeliveryCost = GetDeliveryCost(); builder.AppendLine($"\nTotal Amount: {Math.Round(totalAmount, 2)}"); builder.AppendLine($"Total Discount: {Math.Round(totalAmount - totalAmountAfterDiscount, 2)}"); builder.AppendLine($"Total Amount After Discounts: {Math.Round(totalAmountAfterDiscount, 2)}"); builder.AppendLine($"Delivery Cost: {Math.Round(totalDeliveryCost, 2)}"); builder.AppendLine($"Payable Amount: {Math.Round((double)totalAmountAfterDiscount + (double)totalDeliveryCost, 2)}"); return(builder.ToString()); }
public void AddItem(Product product, double amount) { this.CheckRule(new ValueMustBeGreaterThanZeroRule(amount, "Added Product Amount")); this.CheckRule(new ObjectNotNullRule(product, "Added Product")); if (ProductsWithQuantity.TryGetValue(product, out double productAmount)) { ProductsWithQuantity[product] = productAmount + amount; } else { ProductsWithQuantity.Add(product, amount); } }
public double GetTotalAmount() { return(ProductsWithQuantity.Sum(e => e.Key.Price * e.Value)); }
public double GetTotalQuantity() { return(ProductsWithQuantity.Sum(x => x.Value)); }
public int GetDeliveryCount() { return(ProductsWithQuantity.GroupBy(e => e.Key.Category.Title).Count()); }
private Dictionary <Product, double> GetProductsByCategory(Category category) { return(ProductsWithQuantity.Where(p => p.Key.Category == category).ToDictionary(p => p.Key, p => p.Value)); }