Beispiel #1
0
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            var book = new RememberUtility.Model.Books();

            // txtBookName
            if (txtBookName.Text != "")
            {
                book.BookName = txtBookName.Text;
            }
            else
            {
                AddBookResult.Foreground = Brushes.Red;
                AddBookResult.Content    = "Book name cannot be null. Please choose a type";
            }

            if (txtBookName.Text == "")
            {
                AddBookResult.Foreground = Brushes.Red;
                AddBookResult.Content    = "You must input book name";
            }
            else // has input
            {
                var findBook = booksUtil.FindBookBy(txtBookName.Text);
                if (findBook != null)
                {
                    if (book.BookName.ToLower() == findBook.BookName.ToLower())
                    {
                        if (MessageBox.Show($"Found '{txtBookName.Text}'." +
                                            $" Do you wanna to delete '{txtBookName.Text}'?", "Confirm delete",
                                            MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                        {
                            booksUtil.DeleteBook(book.BookName);
                            AddBookResult.Foreground = Brushes.Green;
                            AddBookResult.Content    = "Delete Book Successful";

                            txtBookName.Text     = string.Empty;
                            txtAuthor.Text       = string.Empty;
                            lstListCategory.Text = string.Empty;
                        }
                        else
                        {
                            AddBookResult.Foreground = Brushes.YellowGreen;
                            AddBookResult.Content    = $"Delete operate was canceled.";
                        }
                    }
                } // input and not found
                else
                {
                    AddBookResult.Foreground = Brushes.Red;
                    AddBookResult.Content    = $" Cannot find '{txtBookName.Text}'. Delete failed.";
                }
            }
        }
Beispiel #2
0
        private void btnFind_Click(object sender, RoutedEventArgs e)
        {
            books = booksUtil.FindBookBy(txtUpdateBookName.Text);
            if (books != null)
            {
                lblResult.Foreground = Brushes.Green;
                lblResult.Content    = $"Found '{books.BookName}'.";

                txtUpdateBookName.Text         = books.BookName;
                txtBookAuthor.Text             = books.Author;
                cbbListBookUpdateCategory.Text = books.Category;
                btnUpadte.IsEnabled            = true;
            }
            else
            {
                lblResult.Foreground = Brushes.Red;
                lblResult.Content    = $"Nothing found.";
            }
        }
Beispiel #3
0
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            var book = new RememberUtility.Model.Books();

            // txtBookName
            if (txtBookName.Text != "")
            {
                book.BookName = txtBookName.Text.Trim();
            }
            else
            {
                AddBookResult.Foreground = Brushes.Red;
                AddBookResult.Content    = "Book name cannot be null. Please choose a type";
            }

            // txtAuthor
            if (txtAuthor.Text != "")
            {
                book.Author = txtAuthor.Text.Trim();
            }
            else
            {
                AddBookResult.Foreground = Brushes.Red;
                AddBookResult.Content    = "Author cannot be null. Please choose a type";
            }

            if (lstListCategory.SelectedIndex == -1)
            {
                AddBookResult.Foreground = Brushes.Red;
                AddBookResult.Content    = "Category aren't choose. Please choose a type";
            }

            if (txtAuthor.Text != "" && txtBookName.Text != "" && lstListCategory.SelectedIndex >= 0)
            {
                // use dynamic as type to cast your anonymous object to
                dynamic categoryInList = (lstListCategory.SelectedItem);

                book.Category = categoryInList as string;

                var findBook = booksUtil.FindBookBy(txtBookName.Text);
                if (findBook != null)
                {
                    if (book.BookName.ToLower() != findBook.BookName.ToLower())
                    {
                        booksUtil.AddBook(book);
                        AddBookResult.Foreground = Brushes.Green;


                        if (book.BookName.Length <= 10)
                        {
                            AddBookResult.Content = $"Add '{book.BookName}' Successful";
                        }
                        else
                        {
                            AddBookResult.Content = "Add Book Successful";
                        }

                        txtBookName.Clear();
                        txtAuthor.Clear();
                        lstListCategory.Text = string.Empty;
                    }
                    else
                    {
                        AddBookResult.Foreground = Brushes.Red;
                        AddBookResult.Content    = $"'{txtBookName.Text}' duplicate. Add failed.";
                    }
                }
                else
                {
                    booksUtil.AddBook(book);
                    AddBookResult.Foreground = Brushes.Green;
                    AddBookResult.Content    = "Add Book Successful";

                    txtBookName.Clear();
                    txtAuthor.Clear();
                    lstListCategory.Text = string.Empty;
                }
            }
        }