public void UpdateProduct(List <Shop> shops, AProduct product, List <UpdateQuery> queries) { for (int i = 0; i < shops.Count; i++) { UpdateProduct(shops[i], product, queries[i]); } }
private Dictionary <Shop, List <Product> > FindAllShopsWithProduct(List <AProduct> products) { ShopEqualityComparer comparer = new ShopEqualityComparer(); Dictionary <Shop, List <Product> > shops = new Dictionary <Shop, List <Product> >(comparer); for (int i = 0; i < products.Count; i++) { AProduct prod = products[i]; var pair_list = dao.GetProduct(prod.Type, prod.Name); foreach (Pair <Shop, Product> pair in pair_list) { Shop shop = pair.First; Product product = pair.Second; if (!shops.ContainsKey(shop)) { shops.Add(shop, new List <Product>()); } shops.TryGetValue(shop, out List <Product> list); list.Add(product); } } return(shops); }
public bool SetProductPrice(Shop shop, AProduct product, double price) { QueryType for_amount = QueryType.None; QueryType for_price = QueryType.Update; dao.UpdateProduct(shop, product, new UpdateQuery(for_amount, 0, for_price, price)); return(true); }
public bool SupplyShop(Shop shop, AProduct product, int amount) { QueryType for_amount = QueryType.ChangeBy; QueryType for_price = QueryType.None; dao.UpdateProduct(shop, product, new UpdateQuery(for_amount, amount, for_price, 0)); return(true); }
// обдумать возвращаемое значение public double BuyProduct(Shop shop, AProduct product, int amount) { List <AProduct> list = new List <AProduct>(); List <int> list2 = new List <int>(); list.Add(product); list2.Add(amount); return(BuyProduct(shop, list, list2)); }
public override void DoSelfDuty() { //从中介者拿到所需资源 ARes aRes = this.mediator.GetARes(); //生产 AProduct aProduct = ProcessA(aRes); //将产品交给中介者 this.mediator.OutputAProduct(aProduct); }
static void checkLab4() { ShopService logic = new ShopService(); logic.InitializeDAO("dao2.ini"); Shop shop1 = logic.AddShop("Пятерочка"); Shop shop2 = logic.AddShop("Заря"); Shop shop3 = logic.AddShop("Юнион"); AProduct prod1 = logic.AddProduct("Шоколадка", "Аленка"); AProduct prod2 = logic.AddProduct("Молоко", "Простоквашино"); AProduct prod3 = logic.AddProduct("Колбаса", "Докторская"); logic.SetProductPrice(shop2, prod2, 60); logic.SetProductPrice(shop3, prod2, 55); logic.SetProductPrice(shop1, prod1, 40); logic.SupplyShop(shop1, prod1, 20); logic.SupplyShop(shop3, prod2, 15); logic.SetProductPrice(shop3, prod1, 45); logic.SetProductPrice(shop1, prod2, 60); logic.SupplyShop(shop1, prod2, 10); logic.SupplyShop(shop3, prod1, 20); var pair = logic.FindCheapest(prod1); Console.WriteLine("{0}, {1}, {2}", pair.First.Name, pair.Second.Name, pair.Second.Price.ToString()); pair = logic.FindCheapest(prod2); Console.WriteLine("{0}, {1}, {2}", pair.First.Name, pair.Second.Name, pair.Second.Price.ToString()); List <AProduct> list = new List <AProduct>(); list.Add(prod1); list.Add(prod2); List <int> amounts = new List <int>(); amounts.Add(15); amounts.Add(5); check = true; logic.BuyProduct(shop1, list, amounts); list.RemoveAt(0); amounts.RemoveAt(0); Console.WriteLine("This {0}", logic.FindCheapestAmount(list, amounts).First.Name); logic.SupplyShop(shop1, prod2, 20); amounts[0] = 20; Console.WriteLine("This {0}", logic.FindCheapestAmount(list, amounts).First.Name); DataBase db = logic.GetData(); logic.InitializeDAO("dao1.ini", db); }
public void UpdateProduct(List <Shop> shops, AProduct product, List <UpdateQuery> queries) { List <AProduct> products = new List <AProduct>(); products.Add(product); List <List <UpdateQuery> > queries_for_shop = new List <List <UpdateQuery> >(); queries_for_shop.Add(queries); UpdateProduct(shops, products, queries_for_shop); }
public void UpdateProduct(Shop shop, AProduct product, UpdateQuery query) { List <AProduct> products = new List <AProduct>(); products.Add(product); List <UpdateQuery> queries = new List <UpdateQuery>(); queries.Add(query); UpdateProduct(shop, products, queries); }
public override void Add(AProduct obj) { try { if (obj is IPerishable perishable_obj && perishable_obj.IsOutOfDate()) { throw new OutOfDate($"Product out of date ! End date is {perishable_obj.EndDate}"); } else if (obj is IBreakable breackable_obj && breackable_obj.Breacked) { throw new DefectiveGoods($"Product is defective ! Breacked = {breackable_obj.Breacked}"); }
public bool DelProduct(AProduct product) { try { // можно быстрее через id\ тут вообще нужен еще и accountType для однозначности return(DoDelete(products_table, $"{products_table}.Name", $"'{product.Name}'")); } catch (Exception e) { Console.WriteLine(e); throw e; } }
private string MakeUpdatedProductLine(string product_line, List <Shop> shops, List <AProduct> products, List <List <UpdateQuery> > queries_for_every_shop) { // заменить на стрингбилдер // мб при нахождении магазина удалять найденный из список, чтобы упрощать дальнейший поиск string result = ""; for (int i = 0; i < products.Count; i++) { List <Shop> copy_shops = new List <Shop>(shops); AProduct product = products[i]; var splitted = product_line.Split(sect_separator); if (splitted[0] != product.Type + field_separator + product.Name) { continue; } result += splitted[0] + sect_separator; for (int j = 1; j < splitted.Length - 1; j++) { string shop_line = splitted[j]; var splitted_shop = shop_line.Split(field_separator); int shop_pos = FindShopWithShopLine(splitted_shop, copy_shops); if (shop_pos == -1) { result += shop_line + sect_separator; continue; } Shop shop = copy_shops[shop_pos]; UpdateQuery query = queries_for_every_shop[shop_pos][i]; result += MakeUpdatedShopLine(splitted_shop, query); copy_shops.RemoveAt(shop_pos); } if (copy_shops.Count != 0) { for (int j = 0; j < copy_shops.Count; j++) { string[] fake_splitshop = { copy_shops[j].Id.ToString(), 0.ToString(), 0.ToString() }; result += MakeUpdatedShopLine(fake_splitshop, queries_for_every_shop[j][i]); } } return(result); } return(product_line); }
public bool DelProduct(AProduct product) { try { File.WriteAllLines(shops_file, File.ReadAllLines(shops_file).Where( line => line.Split(sect_separator)[0] != product.Type + field_separator + product.Name + sect_separator)); return(true); } catch (Exception e) { Console.WriteLine(e); throw e; } }
public async Task <List <AProduct> > getOrderProducts(string id, byte[] error) { List <AProduct> products = new List <AProduct>(); IntPtr OrderProductPtr; await Task.Run(() => { int number_fields = Accounting_Wrapper.getOrderProducts(id, out OrderProductPtr, error, _ConnectionString); IntPtr current = OrderProductPtr; for (int i = 0; i < number_fields; ++i) { AProduct product = (AProduct)Marshal.PtrToStructure(current, typeof(AProduct)); current = (IntPtr)((long)current + Marshal.SizeOf(product)); products.Add(product); } Marshal.FreeCoTaskMem(OrderProductPtr); }); return(products); }
private int GetProductID(AProduct product) { string query = $"SELECT {products_table}.Id FROM {products_table} WHERE " + $"{products_table}.AccountType = '{product.Type}' AND {products_table}.Name = '{product.Name}'"; connection.Open(); SqlCommand command = new SqlCommand(query, connection); using (var reader = command.ExecuteReader()) { if (!reader.HasRows) { connection.Close(); return(-1); } reader.Read(); int id = reader.GetInt32(0); connection.Close(); return(id); } }
public void UpdateProduct(Shop shop, AProduct product, UpdateQuery query) { try { Shop curr_shop = GetShop(shop.Name); Product curr_prod = GetProduct(curr_shop, product.Type, product.Name); if (curr_prod == null) { int prod_id = GetProductID(product); string value = $"({shop.Id}, {prod_id}, 0, 0)"; DoInsert(shops_products_table, value); UpdateProduct(shop, product, query); } else { int amount = 0; double price = 0; if (query.for_amount != QueryType.None) { if (query.for_amount == QueryType.ChangeBy) { amount = curr_prod.Amount + query.amount; } else if (query.for_amount == QueryType.Update) { amount = query.amount; } } else { amount = curr_prod.Amount; } if (query.for_price != QueryType.None) { if (query.for_price == QueryType.ChangeBy) { price = curr_prod.Price + query.price; } else if (query.for_price == QueryType.Update) { price = query.price; } } else { price = curr_prod.Price; } string prod_info = $"{shops_products_table}.ShopId = {curr_shop.Id} AND {shops_products_table}.ProductId = {curr_prod.Id}"; string sql_query = $"UPDATE {shops_products_table} SET {shops_products_table}.Amount = {amount}, {shops_products_table}.Price = {price} " + $" WHERE {prod_info}"; connection.Open(); SqlCommand command = new SqlCommand(sql_query, connection); command.ExecuteNonQuery(); connection.Close(); } } catch (Exception e) { Console.WriteLine(e); throw; } }
public AProduct AddProduct(AProduct product) { return(AddProduct(product.Type, product.Name)); }
public Product GetProduct(Shop shop, AProduct product) { return(GetProduct(shop, product.Type, product.Name)); }
public Pair <Shop, Product> FindCheapest(AProduct product) { return(FindCheapest(product.Type, product.Name)); }
public List <Pair <Shop, Product> > GetProduct(AProduct product) { return(GetProduct(product.Type, product.Name)); }
public abstract void OutputAProduct(AProduct aProduct);
//A产出的产品放入缓存仓库 public override void OutputAProduct(AProduct aProduct) { aProductList.Add(aProduct); }