public void CorrectlyCreatesFilesOnStartup() { ShopUtils.CreateFiles(); Assert.AreEqual(true, File.Exists(ShopUtils.GetFilePath("Cart.json"))); Assert.AreEqual(true, File.Exists(ShopUtils.GetFilePath("Products.json"))); Assert.AreEqual(true, File.Exists(ShopUtils.GetFilePath("Coupons.json"))); }
private void UpdateCouponListBox() { editCouponListBox.Items.Clear(); foreach (Coupon c in couponList) { int percent = (int)((1 - c.Discount) * 100); editCouponListBox.Items.Add($"{c.Code} ({percent}%)"); } couponList.Serialize(ShopUtils.GetFilePath("Coupons.json")); }
private void UpdateProductListBox() { // every time changes are made to products, update the listbox in order to display the changes editProductListBox.Items.Clear(); foreach (Product product in productList) { editProductListBox.Items.Add(product.Title + " (" + product.Price + ") kr"); } //serialize the changes in the productList in order to keep the changes productList.Serialize(ShopUtils.GetFilePath("Products.json")); }
private void AddProductClick(object sender, RoutedEventArgs e) { try { // throws an exception if the image doesn't exist if (!File.Exists("Images/" + pathBox.Text)) { throw new FileNotFoundException(); } // instantiate a new product and set it's properties using textbox.text if (decimal.Parse(priceBox.Text) > 0) { Product product = new Product { Title = nameBox.Text, Description = descriptionBox.Text, Price = decimal.Parse(priceBox.Text), ProductImage = pathBox.Text, Category = categoryBox.Text }; productList.Add(product); } else { MessageBox.Show("Priset måste vara högre än noll"); return; } nameBox.Clear(); descriptionBox.Clear(); priceBox.Clear(); pathBox.Clear(); categoryBox.Clear(); productList.Serialize(ShopUtils.GetFilePath("Products.json")); MessageBox.Show("Produkt tillagd"); } // throw exception for input other than integers and decimals in the priceBox catch (FormatException) { MessageBox.Show("Felaktig inmatning för pris"); } catch (FileNotFoundException) { MessageBox.Show("Bilden kunde inte hittas"); } }
public void InvalidJsonFormatting() { ShopUtils.CreateFiles(); List <Product> products = new List <Product>() { new Product(), new Product(), new Product() }; string path = ShopUtils.GetFilePath("TESTCART.json"); Action action = delegate { File.WriteAllText(path, "This is not json"); products = ShopUtils.DeserializeProducts(path); }; Assert.ThrowsException <JsonException>(action); //If the exception is thrown, the product count should still remain 3 Assert.AreEqual(3, products.Count); }
private void Start() { System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; ShopUtils.CreateFiles(); productList = ShopUtils.DeserializeProducts(ShopUtils.GetFilePath("Products.json")); couponList = Coupon.DeserializeCoupons(); #region Custom brushes // declare a brushconverter to convert a hex color code string to a Brush color BrushConverter brushConverter = new System.Windows.Media.BrushConverter(); Brush backgroundBrush = (Brush)brushConverter.ConvertFromString("#2F3136"); listBoxBrush = (Brush)brushConverter.ConvertFromString("#36393F"); textBoxBrush = (Brush)brushConverter.ConvertFromString("#40444B"); #endregion // Window options Title = "Sortimenthanteraren"; Width = 1100; Height = 600; WindowStartupLocation = WindowStartupLocation.CenterScreen; // Changes the Window icon Uri iconUri = new Uri("Images/Ica.png", UriKind.RelativeOrAbsolute); this.Icon = BitmapFrame.Create(iconUri); // Scrolling ScrollViewer root = new ScrollViewer(); root.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; Content = root; // Main grid Grid mainGrid = new Grid(); root.Content = mainGrid; mainGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); mainGrid.RowDefinitions.Add(new RowDefinition()); // first column contains a stackpanel with buttons which determines what grid to create, the second column contains a class field grid "buttonClickGrid" which is the parent grid for the eventhandlers nested grids mainGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); mainGrid.ColumnDefinitions.Add(new ColumnDefinition()); mainGrid.Background = backgroundBrush; // Window heading, always visible TextBlock headingTextBlock = ShopUtils.CreateTextBlock("Sortimenthanteraren", 18, TextAlignment.Center); mainGrid.Children.Add(headingTextBlock); Grid.SetRow(headingTextBlock, 0); Grid.SetColumnSpan(headingTextBlock, 2); StackPanel buttonPanel = new StackPanel { VerticalAlignment = VerticalAlignment.Top }; mainGrid.Children.Add(buttonPanel); Grid.SetColumn(buttonPanel, 0); Grid.SetRow(buttonPanel, 1); // using a empty class field grid to be able to add nested grids from the eventhandlers buttonClickGrid = new Grid(); mainGrid.Children.Add(buttonClickGrid); Grid.SetRow(buttonClickGrid, 1); Grid.SetColumn(buttonClickGrid, 1); // each button declares a new grid which is added as buttonClickGrid's only child to display a menu to add/edit/remove coupons and products Button editProductButton = ShopUtils.CreateButton("Ändra produkter"); editProductButton.Padding = new Thickness(10); buttonPanel.Children.Add(editProductButton); editProductButton.Click += CreateEditProductGrid; Button addProductButton = ShopUtils.CreateButton("Lägg till produkt"); addProductButton.Padding = new Thickness(10); buttonPanel.Children.Add(addProductButton); addProductButton.Click += CreateAddProductGrid; Button EditCouponsButton = ShopUtils.CreateButton("Lägg till/Ändra kuponger"); EditCouponsButton.Padding = new Thickness(10); buttonPanel.Children.Add(EditCouponsButton); EditCouponsButton.Click += CreateEditCouponsGrid; }
public void SaveCartTest() { ShopUtils.CreateFiles(); List <Product> preSaveCart = new List <Product>() { new Product() { Title = "Äpple", Category = "Mat", Count = 14, Price = 4.99M, Description = "Rött äpple", ProductImage = "" }, new Product() { Title = "Banan", Category = "Mat", Count = 4, Price = 9.99M, Description = "Guleböj", ProductImage = "" }, new Product() { Title = "Päron", Category = "Mat", Count = 4541354, Price = 0.99M, Description = "Lila Päron", ProductImage = "" } }; preSaveCart.Serialize(ShopUtils.GetFilePath("TESTCART.json")); List <Product> deserializedCart = ShopUtils.DeserializeProducts(ShopUtils.GetFilePath("TESTCART.json")); bool areEqual = deserializedCart.Count == preSaveCart.Count; if (areEqual) { for (int i = 0; i < preSaveCart.Count; i++) { foreach (PropertyInfo p in preSaveCart[i].GetType().GetProperties()) { if (p.GetValue(preSaveCart[i]).ToString() != p.GetValue(deserializedCart[i]).ToString()) { areEqual = false; //If they are not equal we should break out of the loop break; } } //If they are not equal, we break out of the loop and the test will fail. if (!areEqual) { break; } } } Assert.AreEqual(true, areEqual); }