public Boolean AddNewRecord(Book book)
        {
            Book thisBook = book;

            try
            {
                using (StreamWriter writerBooks = new StreamWriter(finalPathname, true))
                {
                    writerBooks.WriteLineAsync(String.Format(
                        "{0},{1},{2},{3},${4:0.00},{5}",
                        thisBook.ID,
                        thisBook.name,
                        thisBook.author,
                        thisBook.year,
                        thisBook.price,
                        thisBook.stock
                        ));

                    writerBooks.Close();

                    return true;
                }
            }
            catch (Exception Ex)
            {
                return false;
                throw new FaultException<Exception>(new Exception(Ex.Message));
            }
        }
Example #2
0
 public void Add(Book book)
 {
     if (book == null)
     {
         throw new Exception();
     }
     books.Add(book);
 }
Example #3
0
 static void Main()
 {
     Book regularBook = new Book("Under the Yawk", "Ivan Vazov", 10.50);
     Console.WriteLine(regularBook);
     Console.WriteLine();
     GoldenEditionBook goldenBook = new GoldenEditionBook("Tutun", "Dimityr Dimov", 22.50);
     Console.WriteLine(goldenBook);
 }
Example #4
0
 //public static List<Book> bookCatalog = new List<Book>();
 public static Book AddBookToCatalog(string isbn, string title, string author, int pubYear, decimal price)
 {
     using (var db = new BookModel())
     {
         var book = new Book();
         book.ISBN = isbn;
         book.Title = title;
         book.Author = author;
         book.PubYear = pubYear;
         book.Price = price;
         db.bookcatalog.Add(book);
         db.SaveChanges();
         return book;
     }
 }
Example #5
0
 static void PrintTitle(Book b)
 {
     Console.WriteLine(" {0} " + b.Title);
 }
Example #6
0
 static void PrintTitle(Book book)
 {
     Console.WriteLine(book.Title);
 }
        public Boolean updateRecord(Book book, int updatedStock)
        {
            Book thisBook = book;

            String[] bookDetails = {
                                       thisBook.ID,
                                       thisBook.name,
                                       thisBook.author,
                                       thisBook.year.ToString(),
                                       thisBook.price.ToString(),
                                       updatedStock.ToString()
                                   };
            Book newBook = createBook(bookDetails, 0);

            string thisID = thisBook.ID;
            string type = "ID";
            deleteBook(type, thisID);
            AddNewRecord(newBook);

            return true;
        }
        public Book createBook(String[] bookDetails, int num)
        {
            List<String> fieldError = new List<String>();
            List<String> fieldIsPositive = new List<String>();

            bool isFieldEmpty = false;
            bool isFieldPositive = false;

            for (int i = 0; i <= 5; i++)
            {
                if (String.IsNullOrWhiteSpace(bookDetails[i]))
                {
                    isFieldEmpty = true;

                    switch (i)
                    {
                        case 0:
                            fieldError.Add("ID");
                            break;
                        case 1:
                            fieldError.Add("Name");
                            break;
                        case 2:
                            fieldError.Add("Author");
                            break;
                        case 3:
                            fieldError.Add("Year");
                            break;
                        case 4:
                            fieldError.Add("Price");
                            break;
                        case 5:
                            fieldError.Add("Stock");
                            break;
                        default:
                            break;
                    }
                }
            }

            if (isFieldEmpty)
            {
                string concatFieldErrors = String.Join(", ", fieldError.ToArray());

                throw new ArgumentException("The following field(s) are empty: ", concatFieldErrors);
            }

            if (isFieldPositive)
            {
                string concatFieldPositive = String.Join(", ", fieldIsPositive.ToArray());

                throw new ArgumentException("The following field(s) must be positive: ", concatFieldPositive);
            }

            bool isPositivePrice = float.Parse(bookDetails[4].Trim('$')) > 0;

            if (!isPositivePrice)
            {
                throw new ArgumentException("Input(s) for Year, Price and Stock must be positive.");
            }

            if (!IsYear(bookDetails[3]))
            {
                throw new FormatException("Year must be valid and contain exactly 4 chars.");
            }

            Book thisBook = new Book();

            thisBook.num = num;
            thisBook.ID = bookDetails[0];
            thisBook.name = bookDetails[1];
            thisBook.author = bookDetails[2];
            thisBook.year = int.Parse(bookDetails[3]);
            thisBook.price = float.Parse(bookDetails[4].Trim('$'));
            thisBook.stock = int.Parse(bookDetails[5]);

            return thisBook;
        }
Example #9
0
 internal void AddBookToTotal(Book book)
 {
     countBooks++;
     priceBooks += book.Price;
 }
Example #10
0
 public void AddBook(Book book)
 {
     bookList.Add(book);
 }
Example #11
0
 internal void AddBookToTotal(Book book)
 {
     countBooks++;
     priceBooks += book.Price;
 }
Example #12
0
 public void AddBook(Book book)
 {
     bookList.Add(book);
 }
Example #13
0
        /// <summary>
        /// Интерактивное меню для добавления книги
        /// </summary>
        /// <param name="db"></param>
        static void AddBookMenu(BookStoreContext db)
        {
            Console.WriteLine("1) Обычная книга");
            Console.WriteLine("2) Научная книга");
            Console.WriteLine("3) Журнал");
            Console.Write("Выберите тип книги, который хотите добавить: ");

            byte menuChoice;

            if (Byte.TryParse(Console.ReadLine(), out menuChoice))
            {
                Console.WriteLine();
                PrintAuthorsList(db);
                Console.Write("Введите идентификатор его автора: ");
                Console.WriteLine();

                int authorId;
                if (Int32.TryParse(Console.ReadLine(), out authorId))
                {
                    var author = db.Authors.Find(authorId);

                    if (author != null)
                    {
                        Console.Write("Введите название: ");

                        string name = Console.ReadLine();

                        Book book = null;

                        if (menuChoice == 1)
                        {
                            book = new Book()
                            {
                                Name = name
                            };
                        }
                        else if (menuChoice == 2)
                        {
                            Console.Write("Введите тип книги (учебник, справочник): ");
                            string type = Console.ReadLine();

                            book = new ScienceBook()
                            {
                                Name = name, Type = type
                            };
                        }
                        else if (menuChoice == 3)
                        {
                            Console.Write("Для кого предназначен журнал? ");
                            string reader = Console.ReadLine();

                            book = new Magazine()
                            {
                                Name = name, ReaderType = reader
                            };
                        }
                        db.Books.Add(book);
                        author.AuthorBooks.Add(new AuthorBook()
                        {
                            Book = book
                        });
                        db.SaveChanges();
                    }
                    else
                    {
                        Console.WriteLine("Автора с таким идентификатором нет в базе");
                    }
                }
                else
                {
                    Console.WriteLine("Ожидалось целое число");
                }
            }
        }