public static void CreateCouponFile() { List <Coupon> coupons = new List <Coupon>() { new Coupon("ABCDE12345", 0.7M), new Coupon("XYZ321", 0.3M), new Coupon("Matsmart50", 0.5M), new Coupon("STAMKUND", 0.15M) }; coupons.Serialize(ShopUtils.GetFilePath("Coupons.json")); }
public static List <Coupon> DeserializeCoupons() { List <Coupon> coupons = new List <Coupon>(); string path = ShopUtils.GetFilePath("Coupons.json"); using (StreamReader reader = new StreamReader(path)) { var json = reader.ReadToEnd(); coupons = JsonSerializer.Deserialize <List <Coupon> >(json); reader.Close(); } return(coupons); }
private void DisplaySelectedProduct(object sender, SelectionChangedEventArgs e) { if (productListBox.SelectedIndex != -1) { //change the heading, image, descriptionheading, description by referring to the selectedindex in the searchTermList productHeading.Text = searchTermList[productListBox.SelectedIndex].Title; // clear the imageGrid children to prevent images from stacking on top of each other imageGrid.Children.Clear(); currentImage = ShopUtils.CreateImage("Images/" + searchTermList[productListBox.SelectedIndex].ProductImage, true); imageGrid.Children.Add(currentImage); productDescriptionHeading.Text = "Produktbeskrivning"; productDescription.Text = searchTermList[productListBox.SelectedIndex].Description; } }
public static void CreateProductFile() { List <Product> products = new List <Product>() { new Product() { Title = "Äpple Royal Gala ca 150g", Description = "Äpple med söt frisk smak och vitt fruktkött. Prova i sallad, kakor, desserter eller ät det precis som det är. Passar som mellis och i utflyktskorgen.", Price = 4.36M, Category = "Frukt och grönt", ProductImage = "Apple.jpg" }, new Product() { Title = "Banan ca 180g", Description = "Smart mellanmål som blir sötare ju mognare den är. Annars är bananen också god att tillaga, t ex stekt i smör, sirap och kardemumma med glass till.", Price = 18.95M, Category = "Frukt och grönt", ProductImage = "Banan.jpg" }, new Product() { Title = "Ananas extra sweet ca 950g", Description = "Ursprungsland: Costa Rica", Price = 23.95M, Category = "Frukt och grönt", ProductImage = "Ananas.jpg", }, new Product() { Title = "Revben", Description = "ICA Revbensspjäll Tjocka är matiga spjäll som går att koka, grilla eller steka i ugn. Du kan marinera dem upp till ett dygn i kylskåp och sedan ugnssteka på 175 grader i cirka 1-1,5 tim tills innertemperaturen når cirka 85 grader. Du kan också koka dem först, sedan marinera över natten och slutligen ugnssteka dem. En snabbvariant är också att tillaga dem som de är i cirka 1 timme och sen pensla på glaze och in i ugnen igen i cirka 10 min för lite fin yta. Och det viktigaste: köttet kommer från utvalda svenska gårdar.", Price = 77.95M, Category = "Kött, fågel & fisk", ProductImage = "Revben.jpg", }, new Product() { Title = "Bacon", Description = "INGREDIENSER: Svenskt sidfläsk (96%), vatten, salt, antioxidationsmedel (E301), konserveringsmedel (E250), rökarom. Bacon är en rå produkt som ska tillagas före användning.", Price = 14.95M, Category = "Kött, fågel & fisk", ProductImage = "Bacon.jpg" }, new Product() { Title = "Herrgård mild 28% ca 667g", Description = "Energi (kcal) 360 kcal, Energi (kJ) 1493 kJ, Fett 28 g, Varav mättat fett 18 g, Kolhydrater 0 g, Varav socker 0 g, Protein 26 g, Salt 1.3 g, Vitamin B12 1.5 µg, Kalcium 774 mg", Price = 55.99M, Category = "Mejeri", ProductImage = "Cheese.jpg", }, new Product() { Title = "Cottage cheese Naturell 4% 500g Keso", Description = "pastöriserad MJÖLK, salt, modifierad stärkelse, VASSLEPERMEAT, syrningskultur, ystenzym.", Price = 24.99M, Category = "Mejeri", ProductImage = "Keso.jpg", }, new Product() { Title = "Mellanmjölk 1.5%", Description = "ICA Mellanmjölk Lite längre hållbarhet låter kanske för bra för att vara sann.", Price = 10.99M, Category = "Mejeri", ProductImage = "Milk.jpg" }, new Product() { Title = "Blandfärs 50/50 ca 1,6kg ICA", Description = "ICA Blandfärs är svenskt, fint malen 50/50-blandning av nöt- och fläskfärs som passar till allt ifrån köttbullar och järpar till köttfärssås och tacos. Fläsket ger färsen en lätt och rund smak och gör den enklare att forma om du till exempel vill göra köttbullar.", Price = 115.12M, Category = "Kött, fågel & fisk", ProductImage = "Mincedmeat.jpg" }, new Product() { Title = "Jäst för matbröd 50g Kronjäst", Description = "Färsk jäst som ger jäskraft till degar för matbröd. Rör ut jästen i fingervarm degvätska (ca 38 grader), eller kall degvätska (ca 10 grader) för kalljäsning.", Price = 3.50M, Category = "Skafferi", ProductImage = "Yeast.jpg" }, new Product() { Title = "Krossade Tomater 500g ICA", Description = "ICA Krossade tomater är krossade italienska solmogna tomater i tomatjuice och kanske den ultimata basvaran att ha i skafferiet för att snabbt få ihop en middag. God att bara krydda upp med lite vitlök och basilika till en pastasås eller låta bli smakbärare i en soppa.", Price = 11.50M, Category = "Skafferi", ProductImage = "Crushedtomatoes.jpg" } }; List <Product> cart = new List <Product>(); products.Serialize(ShopUtils.GetFilePath("Products.json")); cart.Serialize(ShopUtils.GetFilePath("Cart.json")); }
private void Start() { System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; ShopUtils.CreateFiles(); productList = ShopUtils.DeserializeProducts(ShopUtils.GetFilePath("Products.json")); shoppingCart = ShopUtils.DeserializeProducts(ShopUtils.GetFilePath("Cart.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"); Brush listBoxBrush = (Brush)brushConverter.ConvertFromString("#36393F"); Brush textBoxBrush = (Brush)brushConverter.ConvertFromString("#40444B"); Brush expanderBrush = (Brush)brushConverter.ConvertFromString("#202225"); #endregion // Set Window properties Title = "Butiken"; Width = 900; 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 definition Grid mainGrid = new Grid(); root.Content = mainGrid; mainGrid.RowDefinitions.Add(new RowDefinition()); // First column contains the shoppingcart and product assortment. mainGrid.ColumnDefinitions.Add(new ColumnDefinition()); // The second column displays the selected product and upon payment displays the receipt mainGrid.ColumnDefinitions.Add(new ColumnDefinition()); mainGrid.Background = backgroundBrush; #region grid definiton for the Expander in leftGrid Grid expanderCartGrid = new Grid(); expanderCartGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); expanderCartGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); expanderCartGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); expanderCartGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); expanderCartGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); expanderCartGrid.RowDefinitions.Add(new RowDefinition()); expanderCartGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); expanderCartGrid.ColumnDefinitions.Add(new ColumnDefinition()); expanderCartGrid.ColumnDefinitions.Add(new ColumnDefinition()); TextBlock cartTextBlock = ShopUtils.CreateTextBlock("Varukorg", 18, TextAlignment.Center); expanderCartGrid.Children.Add(cartTextBlock); Grid.SetRow(cartTextBlock, 0); Grid.SetColumn(cartTextBlock, 0); Grid.SetColumnSpan(cartTextBlock, 2); TextBlock discountTextBlock = ShopUtils.CreateTextBlock("Mata in rabattkod nedan", 12, TextAlignment.Center); expanderCartGrid.Children.Add(discountTextBlock); Grid.SetRow(discountTextBlock, 1); Grid.SetColumn(discountTextBlock, 0); Grid.SetColumnSpan(discountTextBlock, 1); // A combobox to display available coupons to the user couponComboBox = new ComboBox { HorizontalAlignment = HorizontalAlignment.Right, Height = 18, Margin = new Thickness(0, 5, 5, 5), BorderThickness = new Thickness(0), VerticalContentAlignment = VerticalAlignment.Top, Padding = new Thickness(5, 1, 5, 0), }; expanderCartGrid.Children.Add(couponComboBox); Grid.SetRow(couponComboBox, 1); Grid.SetColumn(couponComboBox, 1); // add a default index at 0, can be selected to clear the couponTextBox.Text couponComboBox.Items.Add("Dina rabattkoder"); couponComboBox.SelectedIndex = 0; couponComboBox.SelectionChanged += AddToCouponTextBox; // Adds all available coupons to the couponComboBox items from the couponList which recieves predetermined coupons from file foreach (var coupon in couponList) { couponComboBox.Items.Add(coupon.Code + " " + (100 - Math.Round(coupon.Discount * 100, 0)) + "%"); } // A textbox for the user to enter coupon codes couponTextBox = new TextBox { Margin = new Thickness(5), Background = textBoxBrush, Foreground = Brushes.White, BorderThickness = new Thickness(0), FontWeight = FontWeights.SemiBold }; expanderCartGrid.Children.Add(couponTextBox); Grid.SetRow(couponTextBox, 2); Grid.SetColumnSpan(couponTextBox, 1); ShopUtils.CreateButton("Använd rabattkod", expanderCartGrid, 2, 2, 1, ValidateCoupon); ShopUtils.CreateButton("Rensa varukorg", expanderCartGrid, row: 3, column: 0, columnspan: 1, ClearCartClick); ShopUtils.CreateButton("Spara varukorg", expanderCartGrid, 3, 1, 1, SaveCartClick); ShopUtils.CreateButton("Ta bort en vald produkt", expanderCartGrid, row: 4, column: 0, columnspan: 1, RemoveProductClick); ShopUtils.CreateButton("Ta bort varje vald produkt", expanderCartGrid, 4, 1, 1, RemoveAllSelectedProductsClick); // the cartListBox display all products in the shoppingCart list cartListBox = new ListBox { Margin = new Thickness(5), VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch, MaxHeight = 200, MinHeight = 200, Background = listBoxBrush, Foreground = Brushes.White, BorderThickness = new Thickness(0), FontWeight = FontWeights.SemiBold }; expanderCartGrid.Children.Add(cartListBox); Grid.SetRow(cartListBox, 5); Grid.SetColumnSpan(cartListBox, 2); sumTextBlock = ShopUtils.CreateTextBlock("Varukorgens summa: 0 kr", 12, TextAlignment.Left); expanderCartGrid.Children.Add(sumTextBlock); Grid.SetRow(sumTextBlock, 6); Grid.SetColumn(sumTextBlock, 0); Grid.SetColumnSpan(sumTextBlock, 1); ShopUtils.CreateButton("Till kassan", expanderCartGrid, 6, 1, 1, ShowReceipt); #endregion #region leftGrid definition Grid leftGrid = new Grid(); mainGrid.Children.Add(leftGrid); leftGrid.ColumnDefinitions.Add(new ColumnDefinition()); leftGrid.ColumnDefinitions.Add(new ColumnDefinition()); leftGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); leftGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); leftGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); leftGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); leftGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); leftGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); leftGrid.RowDefinitions.Add(new RowDefinition()); Grid.SetRow(leftGrid, 0); Grid.SetColumn(leftGrid, 0); // Expander definition, when expanded the expanderCartGrid will be displayed in the leftGrid cartExpander = new Expander { // sets the expanders content to the expanderCartGrid defined above Content = expanderCartGrid, Header = "Din varukorg 0 kr", FontWeight = FontWeights.SemiBold, Foreground = Brushes.White, Background = expanderBrush }; leftGrid.Children.Add(cartExpander); Grid.SetRow(cartExpander, 0); Grid.SetColumn(cartExpander, 0); // when expanded the cartExpander's columnspan increases to take up two columns and when collapsed shrinks to one column cartExpander.Collapsed += DecreaseCartColumnSpan; cartExpander.Expanded += IncreaseCartColumnSpan; // Update the cartListBox in the Expander to add items from the shoppingCart list UpdateCartListBox(); TextBlock products = ShopUtils.CreateTextBlock("Produkter", 18, TextAlignment.Center); leftGrid.Children.Add(products); Grid.SetRow(products, 1); Grid.SetColumn(products, 0); Grid.SetColumnSpan(products, 2); TextBlock searchHeading = ShopUtils.CreateTextBlock("Sök efter produkt", 12, TextAlignment.Center); leftGrid.Children.Add(searchHeading); Grid.SetRow(searchHeading, 2); Grid.SetColumn(searchHeading, 0); Grid.SetColumnSpan(searchHeading, 2); // A textbox definition where the user can input a search term searchBox = new TextBox { Margin = new Thickness(5), Background = textBoxBrush, Foreground = Brushes.White, BorderThickness = new Thickness(0), FontWeight = FontWeights.SemiBold }; leftGrid.Children.Add(searchBox); Grid.SetRow(searchBox, 3); Grid.SetColumnSpan(searchBox, 2); // when the searchbox text changes the ShowSearch event will run searchBox.TextChanged += ShowSearch; // A combobox which displays available categories categoryBox = new ComboBox { Margin = new Thickness(5), BorderThickness = new Thickness(0), FontWeight = FontWeights.SemiBold, Height = 22, }; leftGrid.Children.Add(categoryBox); Grid.SetRow(categoryBox, 4); Grid.SetColumn(categoryBox, 0); Grid.SetColumnSpan(categoryBox, 2); categoryBox.Items.Add("Välj kategori"); categoryBox.SelectedIndex = 0; categoryList = ShopUtils.GenerateCategories(productList); // adds categories to the categoryBox foreach (string category in categoryList) { categoryBox.Items.Add(category); } categoryBox.SelectionChanged += ShowCategory; ShopUtils.CreateButton("Lägg till vald produkt", leftGrid, row: 5, column: 0, columnspan: 2, AddProductToCart); // Listbox definition, used to display the product assortment from the searchTermList productListBox = new ListBox { Margin = new Thickness(5), VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment = HorizontalAlignment.Stretch, MaxHeight = 425, Background = listBoxBrush, Foreground = Brushes.White, BorderThickness = new Thickness(0), FontWeight = FontWeights.SemiBold }; leftGrid.Children.Add(productListBox); Grid.SetRow(productListBox, 6); Grid.SetColumn(productListBox, 0); Grid.SetColumnSpan(productListBox, 2); // set the searchTerm to empty string in order to add every product from the productList upon start UpdateProductListBox(""); // selecting an item in the listbox will display productinformation in the stackpanel to the right productListBox.SelectionChanged += DisplaySelectedProduct; #endregion #region rightStackPanel definition StackPanel rightStackPanel = new StackPanel(); mainGrid.Children.Add(rightStackPanel); Grid.SetRow(rightStackPanel, 0); Grid.SetColumn(rightStackPanel, 1); productHeading = ShopUtils.CreateTextBlock("Välj produkt", 18, TextAlignment.Center, rightStackPanel); productHeading.Margin = new Thickness(5, 30, 5, 32); imageGrid = new Grid(); rightStackPanel.Children.Add(imageGrid); // sets a startup image currentImage = ShopUtils.CreateImage("Images/Ica.png", false); imageGrid.Children.Add(currentImage); productDescriptionHeading = ShopUtils.CreateTextBlock("", 16, TextAlignment.Center, rightStackPanel); productDescription = ShopUtils.CreateTextBlock("", 12, TextAlignment.Center, rightStackPanel); productDescription.FontWeight = FontWeights.Thin; productDescription.Margin = new Thickness(30, 5, 30, 5); #endregion }
private void ShowReceipt(object sender, RoutedEventArgs e) { productHeading.Text = "Ditt kvitto"; productDescriptionHeading.Text = "Ditt köp har genomförts"; productDescription.Text = "Tack för att du handlar hos oss!"; // without clearing the imageGrid a picture would still be visible behind the datagrid imageGrid.Children.Clear(); decimal sum = 0; // DataGrid definition DataGrid receiptDataGrid = new DataGrid { Margin = new Thickness(5), MaxWidth = 450, MinHeight = 300, MaxHeight = 300, BorderThickness = new Thickness(0) }; // prevents the user from altering the receipt receiptDataGrid.IsReadOnly = true; // DataTable definition, which is the DataGrid's itemsource DataTable receiptTable = new DataTable(); // add columns for the receipt datatable receiptTable.Columns.Add(new DataColumn("Produkt", typeof(string))); receiptTable.Columns.Add(new DataColumn("Antal", typeof(int))); receiptTable.Columns.Add(new DataColumn("aPris", typeof(decimal))); receiptTable.Columns.Add(new DataColumn("Rabatt", typeof(decimal))); receiptTable.Columns.Add(new DataColumn("Summa", typeof(decimal))); //if a discount code has been validated the loop will add a product to a new row for each product in the shoppingCart list displaying the total discount for each product and summarize the total discount and price if (hasDiscount) { foreach (var product in shoppingCart) { decimal discount = product.Price - (product.Price * currentCoupon.Discount); receiptTable.Rows.Add(new object[] { product.Title, product.Count, product.Price, Math.Round(-discount * product.Count, 2), Math.Round(product.Price * product.Count * currentCoupon.Discount, 2) }); sum += product.Price * product.Count; } // adds a bottom row containing total discount and total price with discount receiptTable.Rows.Add(new object[] { "Totalt", null, null, Math.Round(-(sum - (sum * currentCoupon.Discount)), 2), Math.Round((sum * currentCoupon.Discount), 2) }); } //if no discount has been validated, the else statement set the discount values to null and present the products by adding a new row foreach product in the shoppingCart list else { foreach (var product in shoppingCart) { receiptTable.Rows.Add(new object[] { product.Title, product.Count, product.Price, null, Math.Round(product.Price * product.Count, 2) }); sum += product.Price * product.Count; } // adds a bottom row containing the total price receiptTable.Rows.Add(new object[] { "Totalt", null, null, null, Math.Round(sum, 2) }); } // sets the datagrids content to the datatable containing the products receiptDataGrid.ItemsSource = receiptTable.DefaultView; // displays the datagrid in the imageGrid in the StackPanel imageGrid.Children.Add(receiptDataGrid); Grid.SetRow(receiptDataGrid, 0); Grid.SetColumn(receiptDataGrid, 0); // reset the shopping cart shoppingCart.Clear(); string path = ShopUtils.GetFilePath("Cart.json"); shoppingCart.Serialize(path); currentCoupon = null; hasDiscount = false; couponComboBox.SelectedIndex = 0; UpdateCartListBox(); }
private void SaveCartClick(object sender, RoutedEventArgs e) { string path = ShopUtils.GetFilePath("Cart.json"); shoppingCart.Serialize(path); }