예제 #1
0
        private void UpdatelbOrders(int orderId)
        {
            double orderTotal = 0;

            lbOrders.Items.Clear();

            Order order = orderData.GetOrderById(orderId);


            Dictionary <string, int>    nameCount = new Dictionary <string, int>();
            Dictionary <string, double> namePrice = new Dictionary <string, double>();

            List <Item> list = itemData.GetItemsByOrderId(orderId);

            // Check if are there any orders
            if (list.Count > 0)
            {
                Customer customer = customerData.GetCustomerById(order.CustomerId);
                lbOrders.Items.Add("");
                lbOrders.Items.Add(" The Mario and Luigi Pizza shop");
                lbOrders.Items.Add("");
                lbOrders.Items.Add(" " + String.Concat(Enumerable.Repeat("-", 65)));


                // getting all the items
                foreach (var item in list)
                {
                    if (item.Pizza != null)
                    {
                        // only for pizzas
                        string pizzaName = item.Pizza.GetPizzaName().Trim();
                        if (nameCount.ContainsKey(pizzaName))
                        {
                            nameCount[pizzaName]++;
                        }
                        else
                        {
                            nameCount.Add(pizzaName, 1);
                            namePrice.Add(pizzaName, item.Pizza.GetPizzaPrice());
                        }
                    }


                    if (item.Drink != null)
                    {
                        // only for drinks
                        if (nameCount.ContainsKey(item.Drink))
                        {
                            nameCount[item.Drink]++;
                        }
                        else
                        {
                            nameCount.Add(item.Drink, 1);
                            namePrice.Add(item.Drink, Drink.GetPriceByName(item.Drink));
                        }
                    }
                }

                // printing
                foreach (KeyValuePair <string, int> entry in nameCount)
                {
                    lbOrders.Items.Add(""); // new line
                    lbOrders.Items.Add(" " + entry.Key);

                    double productTotal = entry.Value * namePrice[entry.Key];

                    lbOrders.Items.Add(string.Format(" {0, -35}{1, 20}", $"    { entry.Value} x € {namePrice[entry.Key]}", $"€ { productTotal}"));

                    orderTotal += productTotal;
                }


                lbOrders.Items.Add("");

                lbOrders.Items.Add(" " + String.Concat(Enumerable.Repeat("-", 65)));
                lbOrders.Items.Add(String.Format(" {0, -35}{1,20}", "TOTAL", $"€ {orderTotal.ToString()}"));
                lbOrders.Items.Add(" " + String.Concat(Enumerable.Repeat("-", 65)));

                lbOrders.Items.Add("");
                lbOrders.Items.Add($" ORDERED BY  {customer.Name} ON {order.Timestamp.ToString("MM/dd/yyyy H:mm")}");

                order.TotalPrice = orderTotal;
                orderData.Update(order);
            }
        }