private Rebate[] LoadRebates() { if (!File.Exists(RebateFilePath)) { MessageBox.Show(RebateFilePath + " finns inte, eller har inte blivit satt till 'Copy Always'."); Environment.Exit(1); } //Splits the CSV-file into a list of the class Product List <Rebate> rebates = new List <Rebate>(); string[] lines = File.ReadAllLines(RebateFilePath); foreach (string line in lines) { try { string[] parts = line.Split(','); Rebate rebate = new Rebate { //ToUpper to make the codes caseinsensitive Code = parts[0].ToUpper(), Percent = parts[1] }; rebates.Add(rebate); } catch { MessageBox.Show("Fel vid inläsning av en produkt!"); } } return(rebates.ToArray()); }
private void ConfirmRebateCodeButton_Click(object sender, RoutedEventArgs e) { string codeText = rebateCodeTextBox.Text.ToUpper(); bool codeIsValid = false; //This variable is important so the SumPurchase doesn't become the wrong value, if one presses //the confirmationbutton (OK), multiple times. decimal sumAfterRebate; if (codeText.Length > 3 && codeText.Length < 20) { foreach (Rebate r in Rebates) { if (r.Code == codeText) { codeIsValid = true; rebateMultiplier = (100 - decimal.Parse(r.Percent)) / 100; currentRebateCode = r; } } if (codeIsValid) { sumAfterRebate = TotalSumWithRebate(); DrawSumLabel(); } else { MessageBox.Show("Fel kod!"); currentRebateCode = null; DrawSumLabel(); } } else { MessageBox.Show("Fel antal tecken!"); currentRebateCode = null; DrawSumLabel(); } }
private void Start() { //Loads the products from the CSV-file Products = LoadProducts(); //Loads the rebates from the CSV-file Rebates = LoadRebates(); Cart = new Dictionary <Product, int>(); rebateMultiplier = 1; currentRebateCode = null; //Loads the cart from the CSV-file if (File.Exists(CartFilePath)) { Cart = LoadCart(CartFilePath, Products); } // Window options Title = "Butik"; Width = 800; Height = 1000; WindowStartupLocation = WindowStartupLocation.CenterScreen; // Scrolling ScrollViewer root = new ScrollViewer(); root.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; Content = root; // Main grid StackPanel mainStack = new StackPanel { Orientation = Orientation.Vertical, Margin = new Thickness(5) }; root.Content = mainStack; Grid storeHeader = new Grid { //Margin = new Thickness(0, 0, 0, 10) }; storeHeader.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); storeHeader.ColumnDefinitions.Add(new ColumnDefinition()); storeHeader.ColumnDefinitions.Add(new ColumnDefinition()); storeHeader.ColumnDefinitions.Add(new ColumnDefinition()); mainStack.Children.Add(storeHeader); string logoImagePath = @"Images\store-header-logo.png"; ImageSource source = new BitmapImage(new Uri(logoImagePath, UriKind.RelativeOrAbsolute)); Image logoImage = new Image { Source = source, Width = 200, Height = 200, Stretch = Stretch.UniformToFill, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(5) }; storeHeader.Children.Add(logoImage); Grid.SetRow(logoImage, 0); Grid.SetColumn(logoImage, 1); WrapPanel wrap = new WrapPanel { Orientation = Orientation.Horizontal }; mainStack.Children.Add(wrap); foreach (Product product in Products) { wrap.Children.Add(CreateProductPanel(product)); } cartStack = new StackPanel { Orientation = Orientation.Vertical, Margin = new Thickness(5) }; mainStack.Children.Add(cartStack); DrawCart(); #region cartGrid Grid cartGrid = new Grid(); cartGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); cartGrid.ColumnDefinitions.Add(new ColumnDefinition()); cartGrid.ColumnDefinitions.Add(new ColumnDefinition()); cartGrid.ColumnDefinitions.Add(new ColumnDefinition()); mainStack.Children.Add(cartGrid); #endregion cartGrid #region saveCartButton Button saveCartButton = CreateButton("Spara kundvagnen"); cartGrid.Children.Add(saveCartButton); Grid.SetRow(saveCartButton, 0); Grid.SetColumn(saveCartButton, 0); saveCartButton.Click += SaveCartButton_Click; #endregion saveCartButton #region emptyCartButton Button emptyCartButton = CreateButton("Töm kundvagnen"); cartGrid.Children.Add(emptyCartButton); Grid.SetRow(emptyCartButton, 0); Grid.SetColumn(emptyCartButton, 1); emptyCartButton.Click += EmptyCartButton_Click; #endregion emptyCartButton #region confirmPurchaseButton Button confirmPurchaseButton = CreateButton("Avsluta köp"); cartGrid.Children.Add(confirmPurchaseButton); Grid.SetRow(confirmPurchaseButton, 0); Grid.SetColumn(confirmPurchaseButton, 2); confirmPurchaseButton.Click += ConfirmPurchaseButton_Click; #endregion confirmPurchaseButton #region rebateCodeGrid Grid rebateCodeGrid = new Grid(); rebateCodeGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); rebateCodeGrid.ColumnDefinitions.Add(new ColumnDefinition()); rebateCodeGrid.ColumnDefinitions.Add(new ColumnDefinition()); rebateCodeGrid.ColumnDefinitions.Add(new ColumnDefinition()); mainStack.Children.Add(rebateCodeGrid); #endregion rebateCodeGrid #region Rebatecode Label Label rebateCodeLabel = CreateLabel("Rabbatkod"); rebateCodeLabel.HorizontalContentAlignment = HorizontalAlignment.Right; rebateCodeGrid.Children.Add(rebateCodeLabel); Grid.SetRow(rebateCodeLabel, 0); Grid.SetColumn(rebateCodeLabel, 0); #endregion Rebatecode Label #region Rebatecode TextBox rebateCodeTextBox = new TextBox { Margin = new Thickness(5), }; rebateCodeGrid.Children.Add(rebateCodeTextBox); Grid.SetRow(rebateCodeTextBox, 0); Grid.SetColumn(rebateCodeTextBox, 1); #endregion Rebatecode TextBox #region confirmRebateCodeButton Button confirmRebateCodeButton = CreateButton("OK"); confirmRebateCodeButton.IsDefault = true; rebateCodeGrid.Children.Add(confirmRebateCodeButton); Grid.SetRow(confirmRebateCodeButton, 0); Grid.SetColumn(confirmRebateCodeButton, 2); confirmRebateCodeButton.Click += ConfirmRebateCodeButton_Click; #endregion confirmRebateCodeButton #region sumPurchaseGrid Grid sumPurchaseGrid = new Grid(); sumPurchaseGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); sumPurchaseGrid.ColumnDefinitions.Add(new ColumnDefinition()); mainStack.Children.Add(sumPurchaseGrid); #endregion sumPurchaseGrid #region sumPurchaseLabel sumPurchaseLabel = CreateLabel("Summa: " + Math.Round(TotalSumWithRebate(), 2) + " kr"); sumPurchaseLabel.HorizontalContentAlignment = HorizontalAlignment.Right; sumPurchaseGrid.Children.Add(sumPurchaseLabel); Grid.SetRow(sumPurchaseLabel, 0); Grid.SetColumn(sumPurchaseLabel, 0); #endregion sumPurchaseLabel receiptProductPanel = new StackPanel { Orientation = Orientation.Vertical }; mainStack.Children.Add(receiptProductPanel); }