MainPage.xaml.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using ClassLibrary2;
  8. namespace WpfApp1
  9. {
  10. /// <summary>
  11. /// Логика взаимодействия для books.xaml
  12. /// </summary>
  13. public partial class MainPage : Page, INotifyPropertyChanged
  14. {
  15. public int CountTakedBooks { get; set; }
  16. List<int> BasketBook = new List<int>();
  17. ViewModel viewModel = new ViewModel();
  18. int totalCost = 0;
  19. int totalSale = 0;
  20. public List<Books> bufferList = new List<Books>();
  21. public Books bufferBook;
  22. public event PropertyChangedEventHandler PropertyChanged;
  23. public string OldPrise { get; set; }
  24. public MainPage()
  25. {
  26. InitializeComponent();
  27. ListBoksAllBooks.ItemsSource = viewModel.books.ToList();
  28. ListBoksAllBooks.Items.Refresh();
  29. OldPrise = "None";
  30. }
  31. private void Add_Click(object sender, RoutedEventArgs e)
  32. {
  33. try
  34. {
  35. CountTakedBooks = 0;
  36. Button button = (Button)sender;
  37. int id = Convert.ToInt32(button.Uid);
  38. Books books = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == id);
  39. int summ = books.CountInStock + books.CountInStore;
  40. if (books.CountInStock == 0 && books.CountInStore == 0)
  41. {
  42. throw new Exception("Данной книги нет в наличии.");
  43. }
  44. books.CountInBasket++;
  45. if (books.CountInBasket > summ)
  46. {
  47. books.CountInBasket--;
  48. throw new Exception("Превышен лимит товара");
  49. }
  50. BasketBook.Add(id);
  51. CountTakedBooks = BasketBook.Count;
  52. totalCost += Convert.ToInt32(books.Cost);
  53. Class1 class1 = new Class1();
  54. totalSale = class1.sale(BasketBook.Count, totalCost);
  55. kolbook.Text = BasketBook.Count.ToString();
  56. price.Text = totalCost.ToString();
  57. sale.Text = totalSale.ToString();
  58. if(totalSale > 0)
  59. {
  60. OldPrise = "Strikethrough";
  61. PropertyChanged(this, new PropertyChangedEventArgs("OldPrise"));
  62. newprice.Visibility = Visibility.Visible;
  63. }
  64. double ab = totalCost - (totalCost * totalSale / 100);
  65. newprice.Text = Math.Floor(ab).ToString();
  66. }
  67. catch (Exception ex)
  68. {
  69. MessageBox.Show(ex.Message);
  70. }
  71. }
  72. private void Basket_Click(object sender, RoutedEventArgs e)
  73. {
  74. try
  75. {
  76. foreach (int y in BasketBook.Distinct().ToList())
  77. {
  78. bufferBook = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == y);
  79. bufferList.Add(bufferBook);
  80. }
  81. ChangePages.changed.Navigate(new YourBasket(bufferList, totalSale));
  82. }
  83. catch
  84. {
  85. MessageBox.Show("Ошибка");
  86. }
  87. }
  88. }
  89. }