Ejemplo n.º 1
0
 /// <summary>
 /// Calculate how much is saved for a Grocery Item
 /// </summary>
 /// <param name="numPurchased"></param>
 /// <param name="gli"></param>
 /// <param name="compoundSavings"></param>
 /// <returns></returns>
 public decimal CalculateAmountSaved(int numPurchased, GroceryListItem gli, bool compoundSavings)
 {
     return gli.CalculateSavings(numPurchased, compoundSavings);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Calculate initial price for Grocery Item
 /// </summary>
 /// <param name="itemName"></param>
 /// <param name="numItemsPurchased"></param>
 /// <param name="gli"></param>
 /// <returns></returns>
 public decimal CalculatePrice(string itemName, int numItemsPurchased, GroceryListItem gli)
 {
     return numItemsPurchased * gli.Price;
 }
Ejemplo n.º 3
0
        public Dictionary<string, GroceryListItem> ReadRules(string fileName)
        {
            var itemList = new Dictionary<string, GroceryListItem>();
            var rootFolder = AppDomain.CurrentDomain.BaseDirectory;

            if (!rootFolder.EndsWith("\\")) rootFolder += "\\";

            var filePath = string.Format("{0}AppData\\{1}", rootFolder, fileName);

            WriteDebug("FilePath: " + filePath);

            if (!File.Exists(filePath))
            {
                WriteDebug("File not found: " + filePath);
                return null;
            }

            var lines = File.ReadLines(filePath);
            foreach (var line in lines)
            {
                //skip lines commented out
                if (line.StartsWith("%") || string.IsNullOrWhiteSpace(line))
                    continue;

                var rules = line.Split(';');

                if (rules.Length <= 0) continue;

                //first word in line of rules must be item name
                var rule = rules[0];
                var words = rule.Split(' ');
                var currentItem = words[0];
                var dollars = Regex.Match(rule, @"\d*\.?\d+").Value;
                decimal price;

                //check to see if the price is in a string
                if (string.IsNullOrWhiteSpace(dollars))
                {
                    long value;

                    //couldn't get a value for the price
                    if (!rule.TryParseLong(out value))
                    {
                        WriteDebug("No dollar amount was found for " + currentItem);
                        continue;
                    }

                    price = Convert.ToDecimal(value);
                }
                else if (!decimal.TryParse(dollars, out price))
                {
                    WriteDebug(string.Format("There was an error parsing {0} to decimal!", dollars));
                    continue;
                }

                if (rule.Contains("cents") && price > 0)
                    price = price/100;

                var gli = new GroceryListItem(currentItem, price);

                //get all pricing rules for list item
                for (var i = 1; i < rules.Length; i++)
                {
                    var pricingRule = GetPricingRule(rules[i]);

                    if (pricingRule == null) continue;

                    gli.PricingRules.Add(pricingRule);
                }

                //store item in dictionary with lower case singular name
                var listNameForItem = currentItem.Singularize();
                itemList[listNameForItem.ToLower()] = gli;
            }

            return itemList;
        }