/// <summary> /// Click event for the open button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openToolStripMenuItem_Click(object sender, EventArgs e) { personalLibrary = new PersonalLibrary(); OpenFileDialog opendlg = new OpenFileDialog(); opendlg.Filter = "Text Files|*.txt;*.text"; opendlg.InitialDirectory = @"..\..\LibraryData"; //this isn't working? opendlg.Title = "Open this Personal Library"; if (DialogResult.OK != opendlg.ShowDialog()) { MessageBox.Show(null, "File not selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { String path = opendlg.FileName; personalLibrary.FilePath = path; StreamReader rdr = null; try { Person LibOwner = null; rdr = new StreamReader(path); Regex numPat = new Regex(@"\d"); int counter = 0; bool error = true; Project2MainWindow.ActiveForm.Text = "Data Structures Project 2" + " ~ " + path; while (rdr.Peek() != -1 && error) { String[] fields = rdr.ReadLine().Split('|'); error = true; //the first line is for the owner information if (counter == 0) { if (fields.Length != 8) { MessageBox.Show(null, "Error reading from file, invalid Owner data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); error = false; } else { //checking name to make sure there are no numbers Match match = numPat.Match(fields[0]); int counter_fields0 = 0; while (match.Success) { error = false; match = match.NextMatch(); if (counter_fields0 == 0) { MessageBox.Show(null, "Error reading from file, invalid Owner name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } counter_fields0++; } //checking city to make sure there are no numbers match = numPat.Match(fields[2]); int counter_fields2 = 0; while (match.Success) { error = false; match = match.NextMatch(); if (counter_fields2 == 0) { MessageBox.Show(null, "Error reading from file, invalid Owner city", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } counter_fields2++; } //checking state to make sure there are no numbers match = numPat.Match(fields[3]); int counter_fields3 = 0; while (match.Success) { error = false; match = match.NextMatch(); if (counter_fields3 == 0) { MessageBox.Show(null, "Error reading from file, invalid Owner state", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } counter_fields3++; } //making sure zip is valid Regex zipPat = new Regex(@"^([0-9]{5})$|^([0-9]{5}-\d\d\d\d)$"); match = zipPat.Match(fields[4]); if (!(match.Success)) { error = false; MessageBox.Show(null, "Error reading from file, invalid Owner ZIP", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } //deal with whether or not they put an E in front of the number string eNum = fields[5]; Regex IDPat = new Regex(@"^(E\d\d\d\d\d\d\d\d)$|^(e\d\d\d\d\d\d\d\d)$|^(\d\d\d\d\d\d\d\d)$"); match = IDPat.Match(eNum); if (!(match.Success)) { error = false; MessageBox.Show(null, "Error reading from file, invalid Owner ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (eNum[0].Equals('e') && error) { eNum = eNum.Remove(0, 1); } if (eNum[0].Equals('E') && error) { eNum = eNum.Remove(0, 1); } if (error) { fields[5] = eNum; } //make sure phone number is valid Regex PhonePat = new Regex(@"^(\d\d\d-\d\d\d-\d\d\d\d)$|^(\d\d\d\d\d\d\d\d\d\d)$|^(\d\d\d-\d\d\d\d)$|^(\d\d\d\d\d\d\d)$"); match = PhonePat.Match(fields[6]); if (!(match.Success)) { error = false; MessageBox.Show(null, "Error reading from file, invalid Owner Phone Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } //make sure email is valid Regex EmailPat = new Regex(@"^(\w+@\w+\.\w+)$"); match = EmailPat.Match(fields[7]); if (!(match.Success)) { error = false; MessageBox.Show(null, "Error reading from file, invalid Owner Email", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (error) { LibOwner = new Person(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6], fields[7]); } if (error) { personalLibrary.Owner = LibOwner; } if (error) { EnterOwnerInformation(LibOwner); } } counter++; } //this else is for the books else { if (fields.Length != 6) { MessageBox.Show(null, "Error reading from file, invalid Book data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); error = false; } else { Book lineBook = new Book(); if (fields[0].Equals("Print", StringComparison.OrdinalIgnoreCase)) { lineBook.Type = BookType.Print; } else if (fields[0].Equals("Digital", StringComparison.OrdinalIgnoreCase)) { lineBook.Type = BookType.Digital; } else { error = false; MessageBox.Show(null, "Error reading from file, invalid Book data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } lineBook.Title = fields[1]; lineBook.Author = fields[2]; lineBook.CoAuthor = fields[3]; //do category same as type basically if (fields[4].Equals("History", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.History; } if (fields[4].Equals("Biography", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Biography; } if (fields[4].Equals("Science and technology", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Science_Technology; } if (fields[4].Equals("Scienceandtechnology", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Science_Technology; } if (fields[4].Equals("Science_and_technology", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Science_Technology; } if (fields[4].Equals("Science_technology", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Science_Technology; } if (fields[4].Equals("Textbook", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.TextBook; } if (fields[4].Equals("Mystery", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Mystery; } if (fields[4].Equals("Comedy", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Comedy; } if (fields[4].Equals("Other", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Other; } decimal price_holder; bool lineBookPriceBool = Decimal.TryParse(fields[5], out price_holder); if (!lineBookPriceBool) { error = false; MessageBox.Show(null, "Error reading from file, invalid Book data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { lineBook.Price = price_holder; } if (error) { for (int i = 0; i < personalLibrary.BooksCount; i++) { if (personalLibrary.BooksCount > 0) { if (lineBook.GetHashCode().Equals(personalLibrary[i].GetHashCode())) { error = false; MessageBox.Show(null, "Book duplicate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } if (error) { personalLibrary = personalLibrary + lineBook; } if (error) { BooksListBox.Items.Add(lineBook.Title); } } counter++; } //if there was an error reset the fields if (!error) { Array.Clear(fields, 0, fields.Length); } if (error) { personalLibrary.SaveNeeded = false; } if (!personalLibrary.SaveNeeded) { toolStripSaveInfo.Text = "| Save: Not Needed"; } if (error) { toolStripSortInfo.Text = "| Sort: Needed"; } toolStripNumBooksInfo.Text = "| Number of books in the library: " + personalLibrary.BooksCount.ToString(); } } catch (Exception exc) { MessageBox.Show(null, "Error reading from file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (rdr != null) { rdr.Close(); } } } }
/// <summary> /// Click event for the add button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addToolStripMenuItem_Click(object sender, EventArgs e) { if (personalLibrary.Owner != null && personalLibrary.BooksCount != 0) { OpenFileDialog opendlg = new OpenFileDialog(); opendlg.Filter = "Text Files|*.txt;*.text"; opendlg.InitialDirectory = @"..\..\LibraryData"; //this isn't working? opendlg.Title = "Open this Personal Library"; if (DialogResult.OK != opendlg.ShowDialog()) { MessageBox.Show(null, "File not selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { String path = opendlg.FileName; personalLibrary.FilePath = path; StreamReader rdr = null; try { rdr = new StreamReader(path); Regex numPat = new Regex(@"\d"); bool error = true; while (rdr.Peek() != -1 && error) { String[] fields = rdr.ReadLine().Split('|'); error = true; //this is for the books if (fields.Length != 6) { MessageBox.Show(null, "Error reading from file, invalid Book data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); error = false; } else { Book lineBook = new Book(); if (fields[0].Equals("Print", StringComparison.OrdinalIgnoreCase)) { lineBook.Type = BookType.Print; } else if (fields[0].Equals("Digital", StringComparison.OrdinalIgnoreCase)) { lineBook.Type = BookType.Digital; } else { error = false; MessageBox.Show(null, "Error reading from file, invalid Book data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } lineBook.Title = fields[1]; lineBook.Author = fields[2]; lineBook.CoAuthor = fields[3]; //do category same as type basically if (fields[4].Equals("History", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.History; } if (fields[4].Equals("Biography", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Biography; } if (fields[4].Equals("Science and technology", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Science_Technology; } if (fields[4].Equals("Scienceandtechnology", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Science_Technology; } if (fields[4].Equals("Science_and_technology", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Science_Technology; } if (fields[4].Equals("Science_technology", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Science_Technology; } if (fields[4].Equals("Textbook", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.TextBook; } if (fields[4].Equals("Mystery", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Mystery; } if (fields[4].Equals("Comedy", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Comedy; } if (fields[4].Equals("Other", StringComparison.OrdinalIgnoreCase)) { lineBook.Category = Category.Other; } decimal price_holder; bool lineBookPriceBool = Decimal.TryParse(fields[5], out price_holder); if (!lineBookPriceBool) { error = false; MessageBox.Show(null, "Error reading from file, invalid Book data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { lineBook.Price = price_holder; } if (error) { for (int i = 0; i < personalLibrary.BooksCount; i++) { if (personalLibrary.BooksCount > 0) { if (lineBook.GetHashCode().Equals(personalLibrary[i].GetHashCode())) { error = false; MessageBox.Show(null, "Book duplicate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } if (error) { personalLibrary = personalLibrary + lineBook; } if (error) { BooksListBox.Items.Add(lineBook.Title); } } //if there was an error reset the fields if (!error) { Array.Clear(fields, 0, fields.Length); } if (error) { personalLibrary.SaveNeeded = true; } if (!personalLibrary.SaveNeeded) { toolStripSaveInfo.Text = "| Save: Not Needed"; } if (personalLibrary.SaveNeeded) { toolStripSaveInfo.Text = "| Save: Needed"; } if (error) { toolStripSortInfo.Text = "| Sort: Needed"; } toolStripNumBooksInfo.Text = "| Number of books in the library: " + personalLibrary.BooksCount.ToString(); } } catch (Exception exc) { MessageBox.Show(null, "Error reading from file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (rdr != null) { rdr.Close(); } } } } else { MessageBox.Show(null, "Enter info first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }