コード例 #1
0
        // Given a Book ISBN (entered by the user) and a book record (read from currentBookFile)
        // checks to see if the user ISBN matches the record ISBN (the first field of the record).
        // If there is a match, this method splits the record into its 6 component parts and saves them.
        // Every Record that does not match the ISBN entered by the user will be written to updatedBookFile
        public Boolean findAndSaveBook(string ISBN)
        {
            string  nextRecord;
            Boolean isEndOfFile = true;
            Boolean success;
            Boolean found = false;

            nextRecord = BookStoreClass.currentBookFile.getNextRecord(ref isEndOfFile); // Get the first record
            while (!isEndOfFile)
            {
                BookClass newBook = new BookClass();                                     // Object declared and referenced
                success = newBook.createBookObject(nextRecord);                          // Create a new book object with the attributes assigned

                if (success == true)                                                     // Book object not successfully created
                {
                    if (newBook.bookMatch(ISBN) == true)                                 // Comparing Book hiddenISBN to user's inputted ISBN
                    {
                        this.Book = newBook;                                             // Book assigned to this BookStoreClass object
                        MessageBox.Show("Book Found!\n" + Book.createStringToDisplay()); // Display the book
                        found = true;                                                    // (Book found)
                    }
                    else // Book doesn't match ISBN
                    {
                        updatedBookFile.writeNextRecord(newBook.getBookDataString());           // Write out book that didn't match ISBN to updatedBookFile
                    }
                    nextRecord = BookStoreClass.currentBookFile.getNextRecord(ref isEndOfFile); // Get the next record to analyze
                }
            }
            return(found);
        }
コード例 #2
0
        string bookDataString;              // String containing the 6 attributes in this class used for display


        // Creates an object using attributes obtained from currentBookFile Object
        // ISBN TITLE AUTHOR PRICE NUMBERONHAND TRANSACTIONDATE
        public Boolean createBookObject
            (string s)  // IN: string from the Book Text File
        {
            BookClass thisBook = this;

            string[] bookString = s.Split('*');   // Seperate strings using asterisk seperator
            bookString[0] = bookString[0].Trim(); // Trim starting and trailing whitespaces from ISBN
            bookString[3] = bookString[3].Trim(); // Trim starting and trailing whitespaces from Price
            bookString[4] = bookString[4].Trim(); // Trim starting and trailing whitespaces from NumberOnHand

            int bookStringSize = bookString.GetLength(0);

            // Check that ISBN is of required length
            hiddenISBN = bookString[0];
            if (bookString[0].Length != validISBNLength)
            {
                MessageBox.Show(bookString[0]
                                + ": ISBN string is not exactly 7 characters. Book File Corrupt. Execution Terminated.",
                                "ISBN in Book File Invalid",
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return(false); // ISBN not of required length retun false
            }

            // Check that Title is not blank
            hiddenTitle = bookString[1];
            if (hiddenTitle == " " || hiddenTitle == "")
            {
                MessageBox.Show(hiddenTitle
                                + ": Title string is empty or Blank. Book File Corrupt. Execution Terminated.",
                                "Title in Book File Invalid",
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return(false); // Title blank return false
            }

            // Check that Author is not blank
            hiddenAuthor = bookString[2];
            if (hiddenAuthor == " " || hiddenAuthor == "")
            {
                MessageBox.Show(hiddenAuthor
                                + ": Author string is empty or Blank. Book File Corrupt. Execution Terminated.",
                                "Author in Book File Invalid",
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return(false); // Author blank return false
            }

            // Check that string of price converted to decimal greater than zero
            try
            {
                hiddenPrice = decimal.Parse(bookString[3].Replace(",", "").Replace("$", ""));
                if (hiddenPrice < 0.00m)
                {
                    MessageBox.Show(bookString[3]
                                    + ": Price cannot be less than zero. Book File Corrupt.  Execution Terminated.",
                                    "Price in Book File Invalid", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return(false); // price less than zero return false
                }
            }
            catch
            {
                MessageBox.Show(bookString[3]
                                + ": Price string is empty or Blank. Book File Corrupt.  Execution Terminated.",
                                "Price in Book File Invalid", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return(false); // price invalid format return false
            }

            // Convert number on hand to integer number stored as hiddenNumberOnHand
            try
            {
                hiddenNumberOnHand = Convert.ToInt32(bookString[4]);
            }
            catch
            {
                MessageBox.Show(bookString[4]
                                + ": Number on hand string is not a valid integer. Book File Corrupt. Execution Terminated.",
                                "Number on hand in Book File Invalid",
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return(false); // Not a valid integer return false
            }

            // Convert last Date of Access to a date stored as hiddenLastTransactionDate
            try
            {
                hiddenLastTransactionDate = DateTime.Parse(bookString[5]);
            }
            catch
            {
                MessageBox.Show(bookString[5]
                                + ": Date of Last Access string is not a valid date. Employee File Corrupt.  Execution Terminated.",
                                "Date of last access in Employee File Invalid",
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return(false); // invalid date format return false
            }

            // All data valid
            return(true);
        }  // end createEmployeeObject