예제 #1
0
        /// <summary>
        /// Saves the book objects from a listbox as plain text to a html file.
        /// </summary>
        /// <param name="booksToSave">Use a list of book objects as parameter and use it
        /// for what texts to write on the html file</param>
        /// <param name="filename">Use a string parameter called filename and use it as
        /// filename when saving</param>
        /// <returns>Void</returns>
        public void SaveAsHTML(List <Book> booksToSave, string filename)
        {
            // Create a new stream writer object called sw to write on the file filename and add .html to the end
            StreamWriter sw = new StreamWriter(filename);

            // Declare a new list of string called books to store the texts to write onto the file
            List <string> books = new List <string>();

            // for loop to loop through each book objects from the list booksToSave
            for (int i = 0; i < booksToSave.Count(); i++)
            {
                // if statement to check if the book at [i] is a physical book object, do the following code
                if (booksToSave[i] is PhysicalBook)
                {
                    // Cast the book at [i] to a physical book and call it n
                    PhysicalBook n = (PhysicalBook)booksToSave[i];
                    // Add to the list of string PB + the properties of physical book n separated by commas
                    books.Add("PB" + "," + n.Title + "," + n.Author + "," + n.Year.ToString() + "," + n.Weight + "," + n.PageCount);
                }
                // if statement to check if the book at [i] is an audio book object, do the following code
                else if (booksToSave[i] is AudioBook)
                {
                    // Cast the book at [i] to an audio book and call it n
                    AudioBook n = (AudioBook)booksToSave[i];
                    // Add to the list of string AB + the properties of audio book n separated by commas
                    books.Add("AB" + "," + n.Title + "," + n.Author + "," + n.Year.ToString() + "," + n.FileSize + "," + n.Length + "," + n.Narrator);
                }
                // if statement to check if the book at [i] is an e book object, do the following code
                else if (booksToSave[i] is EBook)
                {
                    // Cast the book at [i] to an e book and call it n
                    EBook n = (EBook)booksToSave[i];
                    // Add to the list of string EB + the properties of e book n separated by commas
                    books.Add("EB" + "," + n.Title + "," + n.Author + "," + n.Year.ToString() + "," + n.FileSize + "," + n.FileFormat + "," + n.PageCount);
                }
            }

            // Write the word <html> to the file
            sw.WriteLine("<html>");
            // Write the word <head><title> </title></head> to the file
            sw.WriteLine("<head><title> </title></head>");
            // Write the word <body> to the file
            sw.WriteLine("<body>");

            //foreach loop to loop through each string in books and name it n
            foreach (string n in books)
            {
                // Write onto the file the string stored in n
                sw.WriteLine(n);
                // Write the word <br> to the file
                sw.WriteLine("<br>");
            }
            // Write the word </body> to the file
            sw.WriteLine("</body>");
            // Write the word </html> to the file
            sw.WriteLine("</html>");
            // Close the stream writer
            sw.Dispose();
        }
예제 #2
0
        /// <summary>
        /// Saves the book objects from a listbox as plain text to a text file.
        /// </summary>
        /// <param name="filename">Use a string parameter called filename and use it as
        /// filename when saving</param>
        /// <param name="lbxItems">Use a list of book objects as a parameter and use it
        /// for what texts to write on the text file</param>
        /// <returns>Void</returns>
        public void SaveBooks(string filename, List <Book> lbxItems)
        {
            // Create a new stream writer object called sw to write on the file filename
            StreamWriter sw = new StreamWriter(filename);

            // Declare a new list of string called books to store the texts to write onto the file
            List <string> books = new List <string>();

            // for loop to loop through each book objects from the list lbxItems
            for (int i = 0; i < lbxItems.Count(); i++)
            {
                // if statement to check if the book at [i] is a physical book object, do the following code
                if (lbxItems[i] is PhysicalBook)
                {
                    // Cast the book at [i] to a physical book and call it n
                    PhysicalBook n = (PhysicalBook)lbxItems[i];
                    // Add to the list of string PB + the properties of physical book n separated by commas
                    books.Add("PB" + "," + n.Title + "," + n.Author + "," + n.Year.ToString() + "," + n.Weight + "," + n.PageCount);
                }

                // if statement to check if the book at [i] is an audio book object, do the following code
                else if (lbxItems[i] is AudioBook)
                {
                    // Cast the book at [i] to an audio book and call it n
                    AudioBook n = (AudioBook)lbxItems[i];
                    // Add to the list of string AB + the properties of audio book n separated by commas
                    books.Add("AB" + "," + n.Title + "," + n.Author + "," + n.Year.ToString() + "," + n.FileSize + "," + n.Length + "," + n.Narrator);
                }
                // if statement to check if the book at [i] is an e book object, do the following code
                else if (lbxItems[i] is EBook)
                {
                    // Cast the book at [i] to an e book and call it n
                    EBook n = (EBook)lbxItems[i];
                    // Add to the list of string EB + the properties of e book n separated by commas
                    books.Add("EB" + "," + n.Title + "," + n.Author + "," + n.Year.ToString() + "," + n.FileSize + "," + n.FileFormat + "," + n.PageCount);
                }
            }
            // foreach loop to loop through each string in the list of strings called books
            foreach (string n in books)
            {
                // Write onto the text file the string contained in n
                sw.WriteLine(n);
            }

            // Close the stream writer
            sw.Dispose();
        }
예제 #3
0
        public List <Book> LoadAll()
        {
            List <Book> ListAll = new List <Book>();

            StreamReader sr = new StreamReader("BookSourceFile.txt");

            while (!sr.EndOfStream)
            {
                string bookInfo = sr.ReadLine();

                string[] bookVals = bookInfo.Split(',');

                PhysicalBook newPhysicalBook = new PhysicalBook();
                newPhysicalBook.Title  = bookVals[1];
                newPhysicalBook.Author = bookVals[2];
                newPhysicalBook.Year   = int.Parse(bookVals[3]);
                newPhysicalBook.Weight = int.Parse(bookVals[4]);

                AudioBook newAudioBook = new AudioBook();
                newAudioBook.Title    = bookVals[1];
                newAudioBook.Author   = bookVals[2];
                newAudioBook.Year     = int.Parse(bookVals[3]);
                newAudioBook.FileSize = bookVals[4];
                newAudioBook.Length   = bookVals[6];
                newAudioBook.Narrator = bookVals[7];

                EBook newEbook = new EBook();
                newEbook.Title      = bookVals[1];
                newEbook.Author     = bookVals[2];
                newEbook.Year       = int.Parse(bookVals[3]);
                newEbook.FileSize   = bookVals[4];
                newEbook.FileFormat = bookVals[5];

                ListAll.Add(newPhysicalBook);
                ListAll.Add(newAudioBook);
                ListAll.Add(newEbook);
            }
            sr.Dispose();

            return(ListAll);
        }
예제 #4
0
        /// <summary>
        /// Handles the opening of an external file, read what is stored and pass it through as a list of book objects
        /// </summary>
        /// <param name="filename">Uses a string called filename as parameter</param>
        /// <returns>Returns a list of book objects</returns>
        public List <Book> OpenFile(string filename)
        {
            // Declare a new list of book objects called ListAll
            List <Book> ListAll = new List <Book>();
            // Declare a new stream reader object called sr and read from a file which filename is stored in the filename variable
            StreamReader sr = new StreamReader(filename);

            // While loop read each line of text from the file
            while (!sr.EndOfStream)
            {
                // Store the read line of text to a string called bookInfo
                string bookInfo = sr.ReadLine();

                // Split the text stored in bookInfo where there are commas and store each into a string array
                string[] bookVals = bookInfo.Split(',');

                // try catch block to prevent crashing
                try
                {
                    // if statement to check if the string stored in bookVals[0] is the word PB, do the following code
                    if (bookVals[0] == "PB")
                    {
                        // Declare a new physical book object and call it newPhysicalBook
                        PhysicalBook newPhysicalBook = new PhysicalBook();
                        // Use the string stored at bookVals[1] as title property of newPhysicalBook
                        newPhysicalBook.Title = bookVals[1];
                        // Use the string stored at bookVals[2] as author property of newPhysicalBook
                        newPhysicalBook.Author = bookVals[2];
                        // Use the string stored at bookVals[3], convert in into an int and use it as year property of newPhysicalBook
                        newPhysicalBook.Year = int.Parse(bookVals[3]);
                        // Use the string stored at bookVals[4] as weight property of newPhysicalBook
                        newPhysicalBook.Weight = bookVals[4];
                        // Use the string stored at bookVals[5], convert in into an int and use it as pagecount property of newPhysicalBook
                        newPhysicalBook.PageCount = int.Parse(bookVals[5]);
                        // Add to the list of books the newly created physical book
                        ListAll.Add(newPhysicalBook);
                    }
                    // if statement to check if the string stored in bookVals[0] is the word EB, do the following code
                    else if (bookVals[0] == "EB")
                    {
                        // Declare a new e book object and call it newEBook
                        EBook newEBook = new EBook();
                        // Use the string stored at bookVals[1] as title property of newEBook
                        newEBook.Title = bookVals[1];
                        // Use the string stored at bookVals[1] as author property of newEBook
                        newEBook.Author = bookVals[2];
                        // Use the string stored at bookVals[3], convert in into an int and use it as year property of newEBook
                        newEBook.Year = int.Parse(bookVals[3]);
                        // Use the string stored at bookVals[4] as filesize property of newEBook
                        newEBook.FileSize = bookVals[4];
                        // Use the string stored at bookVals[5] as fileformat property of newEBook
                        newEBook.FileFormat = bookVals[5];
                        // Use the string stored at bookVals[6], convert in into an int and use it as pagecount property of newEBook
                        newEBook.PageCount = int.Parse(bookVals[6]);
                        // Add to the list of books the newly created e book
                        ListAll.Add(newEBook);
                    }
                    else if (bookVals[0] == "AB")
                    {
                        // Declare a new physical book object and call it newAudioBook
                        AudioBook newAudioBook = new AudioBook();
                        // Use the string stored at bookVals[1] as title property of newAudioBook
                        newAudioBook.Title = bookVals[1];
                        // Use the string stored at bookVals[2] as title property of newAudioBook
                        newAudioBook.Author = bookVals[2];
                        // Use the string stored at bookVals[3], convert in into an int and use it as year property of newAudioBook
                        newAudioBook.Year = int.Parse(bookVals[3]);
                        // Use the string stored at bookVals[4] as filesize property of newAudioBook
                        newAudioBook.FileSize = bookVals[4];
                        // Use the string stored at bookVals[5] as length property of newAudioBook
                        newAudioBook.Length = bookVals[5];
                        // Use the string stored at bookVals[6] as narrator property of newAudioBook
                        newAudioBook.Narrator = bookVals[6];
                        // Add to the list of books the newly created audio book
                        ListAll.Add(newAudioBook);
                    }
                }
                // Catch if code in try has errors
                catch
                {
                    // Show a messagebox
                    MessageBox.Show("lolwut");
                }
            }
            // Close the stream reader
            sr.Dispose();

            // Return ListAll, which is a list of book objects populated by the information stored within the file
            return(ListAll);
        }
예제 #5
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            // Try catch block to prevent crashing
            try
            {
                // If statement to check if radiobutton Physical Book is checked
                if (rdoPhysicalBook.Checked == true)
                {
                    // Create a new PhysicalBook object call it pBook
                    PhysicalBook pBook = new PhysicalBook();
                    // Set the Title property to the text in tbxTitle
                    pBook.Title = tbxTitle.Text;
                    // Set the Author property to the text in tbxAuthor
                    pBook.Author = tbxAuthor.Text;
                    // Set the Year property to the text in tbxYear converted into an int
                    pBook.Year = int.Parse(tbxYear.Text);
                    // Set the Weight property to the text in tbxWeight
                    pBook.Weight = tbxWeight.Text;
                    // Set the PageCount property to the text in tbxPageCount converted into an int
                    pBook.PageCount = int.Parse(tbxPageCount.Text);
                    // Add the book to the list of books called allBooks
                    allBooks.Add(pBook);
                }
                // If statement to check if radiobutton Audio Book is checked
                else if (rdoAudioBook.Checked == true)
                {
                    // Create a new AudioBook object call it aBook
                    AudioBook aBook = new AudioBook();
                    // Set the Title property to the text in tbxTitle
                    aBook.Title = tbxTitle.Text;
                    // Set the Author property to the text in tbxAuthor
                    aBook.Author = tbxAuthor.Text;
                    // Set the Year property to the text in tbxYear converted into an int
                    aBook.Year = int.Parse(tbxYear.Text);
                    // Set the FileSize property to the text in tbxFileSize
                    aBook.FileSize = tbxFileSize.Text;
                    // Set the Length property to the text in tbxLength
                    aBook.Length = tbxLength.Text;
                    // Set the Narrator property to the text in tbxNarrator
                    aBook.Narrator = tbxNarrator.Text;
                    // Add the book to the list of books called allBooks
                    allBooks.Add(aBook);
                }
                // If statement to check if radiobutton Audio Book is checked
                else if (rdoEbook.Checked == true)
                {
                    // Create a new EBook object call it eBook
                    EBook eBook = new EBook();
                    // Set the Title property to the text in tbxTitle
                    eBook.Title = tbxTitle.Text;
                    // Set the Author property to the text in tbxAuthor
                    eBook.Author = tbxAuthor.Text;
                    // Set the Year property to the text in tbxYear converted into an int
                    eBook.Year = int.Parse(tbxYear.Text);
                    // Set the FileSize property to the text in tbxFileSize
                    eBook.FileSize = tbxFileSize.Text;
                    // Set the FileFormat property to the text in tbxFileFormat
                    eBook.FileFormat = tbxFileFormat.Text;
                    // Set the PageCount property to the text in tbxPageCount converted into an int
                    eBook.PageCount = int.Parse(tbxPageCount.Text);
                    // Add the book to the list of books called allBooks
                    allBooks.Add(eBook);
                }
            }
            // Catch the error and instead do the following code
            catch
            {
                // Show a messagebox
                MessageBox.Show("Error");
            }
            // Call the UpdateBooksListBox method
            UpdateBooksListBox();
            // Create a new FileIO call it fm
            FileIO fm = new FileIO();

            // Use the AddBook to add the created book to the list of books
            fm.AddBook(tbxName.Text, allBooks);
        }
예제 #6
0
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            // Create a new OpenFileDialog class called openFileDialog
            OpenFileDialog openFileDialog = new OpenFileDialog();

            // Use the method ShowDialog() to pop up an open file dialog window
            openFileDialog.ShowDialog();
            // Create a new FileIO class caled fm
            FileIO fm = new FileIO();

            // Get the extension of the file selected from the openfiledialog
            // then store it into a string
            string path = Path.GetExtension(openFileDialog.FileName);

            // If the extension is .dat
            if (path == ".dat")
            {
                // Use the FileIO method LoadFromDat and use the filename of the file selected from the openFileDialog
                // then store it into the list of books
                allBooks = fm.LoadFromDat(openFileDialog.FileName);
            }
            // If the extension is .txt
            else if (path == ".txt")
            {
                // Use the FileIO method OpenFile and use the filename of the file selected from the openFileDialog
                // then store it into the list of books
                allBooks = fm.OpenFile(openFileDialog.FileName);
            }
            // If the extension is .html
            else if (path == ".html")
            {
                // Use the FileIO method OpenFile and use the filename of the file selected from the openFileDialog
                // then store it into the list of books
                allBooks = fm.OpenFile(openFileDialog.FileName);
            }
            // If the extension is .xml
            else if (path == ".xml")
            {
                //allBooks = fm.LoadFromXML(openFileDialog.FileName);

                // Use the FileIO method LoadFromSOAP and use the filename of the file selected from the openFileDialog
                // then store it into the list of books
                allBooks = fm.LoadFromSOAP(openFileDialog.FileName);
            }

            // Store the filename of the file opened into the textbox
            tbxName.Text = openFileDialog.FileName;
            // Empty the list items of lbxInformation
            lbxInformation.Items.Clear();
            // Add to the listbox the list of book objects allBooks converted to array
            lbxInformation.Items.AddRange(allBooks.ToArray());

            // Foreach loop to loop through each book objects in allbooks
            foreach (Book n in allBooks)
            {
                // If the book is a physical book
                if (n is PhysicalBook)
                {
                    // Cast the book into a physical book
                    PhysicalBook pBook = (PhysicalBook)n;
                    // Add to the list of physical books
                    physicalBooks.Add(pBook);
                }
                // If the book is a e book
                else if (n is EBook)
                {
                    // Cast the book into an e book
                    EBook eBook = (EBook)n;
                    // Add to the list of e books
                    eBooks.Add(eBook);
                }
                // If the book is a audio book
                else if (n is AudioBook)
                {
                    // Cast the book into an audio book
                    AudioBook audioBook = (AudioBook)n;
                    // Add to the list of audio books
                    audioBooks.Add(audioBook);
                }
            }
        }
예제 #7
0
        private void btnMoreInformation_Click(object sender, EventArgs e)
        {
            // Clear the text of all textboxes
            tbxTitle.Clear();
            tbxAuthor.Clear();
            tbxYear.Clear();
            tbxWeight.Clear();
            tbxFileSize.Clear();
            tbxFileFormat.Clear();
            tbxLength.Clear();
            tbxNarrator.Clear();
            tbxPageCount.Clear();

            // If the selected item on the listbox is an audio book
            if (lbxInformation.SelectedItem is AudioBook)
            {
                // Cast the selected item into an audio book, call it item
                AudioBook item = (AudioBook)lbxInformation.SelectedItem;
                // Store the title property of item to the textbox tbxTitle
                tbxTitle.Text = item.Title;
                // Store the author property of item to the textbox tbxAuthor
                tbxAuthor.Text = item.Author;
                // Store the year property of item, converted to a string, to the textbox tbxYear
                tbxYear.Text = item.Year.ToString();
                // Store the filesize property of item to the textbox tbxFileSize
                tbxFileSize.Text = item.FileSize;
                // Store the length property of item to the textbox tbxLength
                tbxLength.Text = item.Length;
                // Store the narrator property of item to the textbox tbxNarrator
                tbxNarrator.Text = item.Narrator;
            }
            // If the selected item on the listbox is an e book
            else if (lbxInformation.SelectedItem is EBook)
            {
                // Cast the selected item into an e book, call it item
                EBook item = (EBook)lbxInformation.SelectedItem;
                // Store the title property of item to the textbox tbxTitle
                tbxTitle.Text = item.Title;
                // Store the author property of item to the textbox tbxAuthor
                tbxAuthor.Text = item.Author;
                // Store the year property of item, converted to a string, to the textbox tbxYear
                tbxYear.Text = item.Year.ToString();
                // Store the filesize property of item to the textbox tbxFileSize
                tbxFileSize.Text = item.FileSize;
                // Store the fileformat property of item to the textbox tbxFileFormat
                tbxFileFormat.Text = item.FileFormat;
                // Store the pagecount property of item, converted to a string, to the textbox tbxPageCount
                tbxPageCount.Text = item.PageCount.ToString();
            }
            // If the selected item on the listbox is an physical book
            else if (lbxInformation.SelectedItem is PhysicalBook)
            {
                // Cast the selected item into a physical book, call it item
                PhysicalBook item = (PhysicalBook)lbxInformation.SelectedItem;
                // Store the title property of item to the textbox tbxTitle
                tbxTitle.Text = item.Title;
                // Store the author property of item to the textbox tbxAuthor
                tbxAuthor.Text = item.Author;
                // Store the year property of item, converted to a string, to the textbox tbxYear
                tbxYear.Text = item.Year.ToString();
                // Store the weight property of item to the textbox tbxWeight
                tbxWeight.Text = item.Weight;
                // Store the pagecount property of item, converted to a string, to the textbox tbxPageCount
                tbxPageCount.Text = item.PageCount.ToString();
            }
        }