//When a final match is found, populate a list with the names of the //stores and use this method to add the correct information about the final match private void addFinalMatch(List<string> storeNamesInMatch) { matchesfound = true; FinalMatch theMatch = new FinalMatch(); List<Store> storesInMatch = new List<Store>(); foreach (string storename in storeNamesInMatch) { theMatch.AddStore(storename); storesInMatch.Add(new Store(storename, StoreDictionary[storename])); } for (int i = 0; i < WantedItemList.Count; i++) { Item item = WantedItemList[i]; storesInMatch = storesInMatch.OrderBy(s => s.getPrice(item.extid)).ThenByDescending((s => s.getQty(item.extid))).ToList(); int totalwantedqty = item.qty; int storeindex = 0; while (totalwantedqty > 0) { Store currentStore = storesInMatch[storeindex]; int storeQty = currentStore.getQty(item.extid); int actualQtyFromStore = (storeQty >= totalwantedqty) ? totalwantedqty : storeQty; totalwantedqty -= actualQtyFromStore; theMatch.GetDetails(currentStore.getName()).AddItem(item.extid, actualQtyFromStore, currentStore.getPrice(item.extid)); storeindex++; } } // Check for a solution that doesn't use one of the stores. Reject it since it would have been // found for a smaller value of k. foreach(string storename in theMatch.GetStoreNames()) { if (theMatch.GetDetails(storename).totalStorePrice == 0) { return; } } MatchAdd(theMatch); }
private void MatchAdd(FinalMatch thismatch) { lock (calcLock) { matchesfoundcount++; matches.Add(thismatch); if (matches.Count > settings.nummatches) { matches = matches.OrderBy(i => i.totalprice).Take(settings.nummatches).ToList(); } } }