Пример #1
0
        //Handles searching the book file for the specified book, as well as deleting/updating files.
        //Flags are "delete"/"update". Add is handled with a boolean and then calling the addBook method.
        //Doing it this way prevents having to re-search/scan the file for deleting/updating.
        //While you could do delete/update in similar ways it revolves around constantly keeping track
        //of the Reader/Writer which I feel is more difficult to follow than this method.
        //However, this method does kind of do a lot and is a pain to read through.
        public bool bookSearch(ref Book book, string isbn, string flag = "")
        {
            sw          = new StreamWriter(tempFile);
            sr          = new StreamReader(sourceFile);
            currentBook = book; //Store original book passed
            string line;
            bool   found = false;

            flag.ToLower();

            while ((line = sr.ReadLine()) != null)
            {
                string[] bookInfo = line.Split('|');
                //ISBN ID Validatio(Two groups of 3 digits delimited by hyphen)
                if (Book.ValidateISBNFormat(bookInfo[0]))
                {
                    if (bookInfo[0] == isbn && flag == "")    //Just a search, return the book that was being looked for/dont modify file
                    {
                        book  = new Book(bookInfo);
                        found = true;
                        sw.Close();
                        sr.Close();
                        return(found);
                    }
                    else if (bookInfo[0] == isbn && flag == "delete")   //Record found, delete it? If yes, just skip writing.
                    {
                        Book         deleteBook = new Book(bookInfo);
                        DialogResult dr         = MessageBox.Show("Book with ISBN: " + isbn + " found.\r\n" +
                                                                  String.Join(Environment.NewLine, deleteBook.BookInfo()) + "\r\nIs this the book you wish to delete?",
                                                                  "Delete Book", MessageBoxButtons.YesNo);
                        if (dr == DialogResult.Yes)
                        {
                            found = true;
                        }
                    }
                    else if (bookInfo[0] == isbn && flag == "update")    //Update a record
                    {
                        sw.WriteLine(currentBook.ToString());
                        found = true;
                    }
                    else    //This book doesn't match the book being searched for, safe to just write it.
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            sw.WriteLine(line);
                        }
                    }
                }
                else    //What to do if we have invalid entry in txt file/data corruption
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        MessageBox.Show("Inventory file has issue at ISBN: " + bookInfo[0],
                                        "Data Corruption");//Move to form code probably
                        return(false);
                    }
                }
            }
            sr.Close();
            sw.Close();
            return(found);
        }