コード例 #1
0
        public override string Solve(Bomb bomb)
        {
            var totalAmount = 0.0;

            foreach (var item in ItemsBought)
            {
                switch (Day)
                {
                case "sunday":
                {
                    if (item.BuyPerPound)
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity, 2);
                    }
                    else if (item.Name.Contains('s'))
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity, 2) + 2.15;
                    }
                    break;
                }

                case "monday":
                {
                    if (ItemsBought.FindIndex(x => x == item) == 0 || ItemsBought.FindIndex(x => x == item) == 2 || ItemsBought.FindIndex(x => x == item) == 5)
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity * 0.85, 2);
                    }
                    else
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity, 2);
                    }
                    break;
                }

                case "tuesday":
                {
                    if (item.BuyPerPound)
                    {
                        totalAmount += totalAmount += Math.Round(item.OneUnitPrice * item.Quantity, 2);
                    }
                    totalAmount += Math.Round(item.OneUnitPrice * item.Quantity, 2) + InternalFunctions.DigitalRoot(item.OneUnitPrice);
                    break;
                }

                case "wednesday":
                {
                    var largestDigit  = item.OneUnitPrice.ToString().Replace(".", "").AsEnumerable().ToList().OrderBy(x => x).First();
                    var smallestDigit = item.OneUnitPrice.ToString().Replace(".", "").AsEnumerable().ToList().OrderBy(x => x).Last();
                    item.OneUnitPrice = double.Parse(item.OneUnitPrice.ToString().Replace(largestDigit, smallestDigit), CultureInfo.InvariantCulture);
                    totalAmount      += Math.Round(item.OneUnitPrice * item.Quantity, 2);
                    break;
                }

                case "thursday":
                {
                    if (ItemsBought.FindIndex(x => x == item) % 2 == 0)
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity * 0.5, 2);
                    }
                    else
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity, 2);
                    }
                    break;
                }

                case "friday":
                {
                    if (item.Category == "fruit")
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity * 1.25, 2);
                    }
                    else
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity, 2);
                    }
                    break;
                }

                case "saturday":
                {
                    if (item.Category == "sweet")
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity * 0.65, 2);
                    }
                    else
                    {
                        totalAmount += Math.Round(item.OneUnitPrice * item.Quantity, 2);
                    }
                    break;
                }

                default: break;
                }
            }
            Solved = true;

            if (totalAmount > ValuePaid)
            {
                var message = "The amount of the cart is ";
                var dollars = totalAmount.ToString().Split(',')[0];
                message += dollars == "1" ? dollars + " dollar" : dollars + " dollars";
                var cents = totalAmount.ToString().Split(',')[1];
                if (cents == "0")
                {
                    return(message + ".");
                }
                message += " and ";
                message += cents == "01" ? "1 cent." : (int.Parse(cents) + " cents.");
                return(message);
            }
            else
            {
                totalAmount = Math.Round(ValuePaid - totalAmount, 2);
                var message = "Return ";
                var dollars = totalAmount.ToString().Split(',')[0];
                message += dollars == "1" ? dollars + " dollar" : dollars + " dollars";
                var cents = totalAmount.ToString().Split(',')[1];
                if (cents == "00")
                {
                    return(message + ".");
                }
                message += " and ";
                message += cents == "01" ? "1 cent." : (int.Parse(cents) + " cents.");
                return(message);
            }
        }