public ShoppingCart GetUserShoppingCart(User user) { ShoppingCart shoppingCart = new ShoppingCart(-1); string[] lines = File.ReadAllLines(user.FileName); if(lines != null) { foreach(string line in lines) { string[] itemDetails = line.Split(','); Item item = new Item(); int amount = int.Parse(itemDetails[0]); item.Id = long.Parse(itemDetails[1]); item.Name = itemDetails[2]; item.Price = itemDetails[3]; item.Quantity = itemDetails[4]; item.UnitOfMeasure = itemDetails[5]; item.Code = itemDetails[6]; item.Type = itemDetails[7]; item.StoreId = long.Parse(itemDetails[8]); shoppingCart.AddItem(item, amount); } return shoppingCart; } return null; }
public ShoppingCart CompareShoppingCart(long storeId, ShoppingCart oldShoppingCart) { ShoppingCart newShoppingCart = new ShoppingCart(storeId); List<Item> items = GetItemsByStoreId(storeId); foreach (var item in oldShoppingCart.Items) { if (item.Key.Type == "1") { var tempList = items.Where(newItem => newItem.Code == item.Key.Code).ToList(); if ((tempList != null) && (tempList.Count == 1)) { newShoppingCart.AddItem(tempList[0], item.Value); } } else { var tempList = items.Where(newItem => newItem.Name == item.Key.Name).ToList(); if ((tempList != null) && (tempList.Count == 1)) { newShoppingCart.AddItem(tempList[0], item.Value); } } } return newShoppingCart; }
private void PopulateShoppingCart(ShoppingCart shoppingCart, ListBox listBox, Label label) { foreach (var item in shoppingCart.Items) { listBox.Items.Add(item); } shoppingCart.CalculateSum(); label.Text = oldShoppingCart.Sum.ToString(); }
public ComparerForm(ShoppingCart oldShoppingCart, List<Store> stores) { InitializeComponent(); this.oldShoppingCart = oldShoppingCart; this.stores = stores; manager = new PriceCompareManager(); PopulateNewShoppingCart(oldShoppingCart, listViewOldShoppingCart, labelOldShoppingCart); PopulateStores(); }
private void listViewStores_SelectedIndexChanged(object sender, EventArgs e) { if (listViewStores.SelectedItems.Count == 0) { return; } ListViewItem item = listViewStores.SelectedItems[0]; newShoppingCart = manager.CompareShoppingCart(long.Parse(item.Text), oldShoppingCart); PopulateNewShoppingCart(newShoppingCart, listViewNewShoppingCart, labelNewShopingCart); }
private void PopulateNewShoppingCart(ShoppingCart ShoppingCart, ListView listView, Label labelSum) { listViewNewShoppingCart.Items.Clear(); foreach (var element in ShoppingCart.Items) { ListViewItem listViewItem = new ListViewItem(element.Key.Name); listViewItem.SubItems.Add(element.Key.Price); listViewItem.SubItems.Add(element.Value.ToString()); listView.Items.Add(listViewItem); } ShoppingCart.CalculateSum(); labelSum.Text = ShoppingCart.Sum.ToString(); ShowMessageAboutShoppingCart(ShoppingCart); }
private void ShowMessageAboutShoppingCart(ShoppingCart newShoppingCart) { List<Item> items = newShoppingCart.Items.Keys.Where(item => item.Type != "1").ToList(); if(items.Count > 0) { StringBuilder sb = new StringBuilder(); sb.AppendLine("In this shopping cart There is sum of internal items of the store."); sb.AppendLine("Because of this the comparison may be incorrect."); sb.AppendLine("The internal items:"); foreach (var item in items) { sb.AppendLine(item.Name); } MessageBox.Show(sb.ToString()); } }
public void SaveShoppingCartToTheUser(User user, ShoppingCart shoppingCart) { StreamWriter file = new StreamWriter(user.FileName); StringBuilder sb = new StringBuilder(); foreach (var element in shoppingCart.Items) { sb.Append(element.Value + ","); sb.Append(element.Key.Id + ","); sb.Append(element.Key.Name + ","); sb.Append(element.Key.Price + ","); sb.Append(element.Key.Quantity + ","); sb.Append(element.Key.UnitOfMeasure + ","); sb.Append(element.Key.Code + ","); sb.Append(element.Key.Type + ","); sb.Append(element.Key.StoreId); sb.Append("\n"); } file.Write(sb.ToString()); file.Close(); }