Exemple #1
0
        }//end of ochestra click method

        private void btnCreateCDChamber_Click(object sender, EventArgs e)
        {
            if (btnCreateCDChamber.Text == "Create CD Chamber")
            {
                DisplayCDChamberForm();      //shows proper textboxes for cd chamber
                btnEnterUPC.Enabled = false; //no other actions during add
            }
            else
            {
                MessageBox.Show("Saving");
                if (Validators.ValidateCDChamber(txtProductUPC.Text, txtProductPrice.Text, txtProductTitle.Text, txtProductQuantity.Text,
                                                 txtCDClassicalLabel.Text, txtCDClassicalArtists.Text, txtCDChamberInstrumentList.Text))//validates chamber product
                {
                    MessageBox.Show("Passed CD Chamber Test");
                    CDChamber x = new CDChamber();
                    x.Save(this);//now x has all values in textboxes
                    MessageBox.Show(x.ToString());
                    thisProductList.addAnItem(x);

                    dbFunctions.InsertProduct(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text,
                                              Convert.ToInt32(txtProductQuantity.Text), "CDChamber");                                         //add to product table

                    dbFunctions.InsertCDClassical(Convert.ToInt32(txtProductUPC.Text), txtCDClassicalLabel.Text, txtCDClassicalArtists.Text); //add to classical table

                    dbFunctions.InsertCDChamber(Convert.ToInt32(txtProductUPC.Text), txtCDChamberInstrumentList.Text);                        //add to chamber table

                    btnCreateCDChamber.Enabled = false;                                                                                       //makes sure cannot accidently add twice
                }
            }
        }//end of create chamber on click method
        //this is what happens when we try to save an object
        //validate text box attributes then insert into the database based on type (which parts of form are enabled)
        private void BtnSave_Click(object sender, EventArgs e)
        {
            //first check that all product attrs have the correct formats
            bool correct = Validator.validateUPC(txtProductUPC.Text) && Validator.validatePrice(txtProductPrice.Text) &&
                           Validator.validateTitle(txtProductTitle.Text) && Validator.validateIntNumber(txtProductQuantity.Text);

            if (correct) //next check each if the upc entered is not already taken (unique)
            {
                bool            j;
                string          pstring;
                OleDbDataReader odb = pdb.SelectProductFromProduct(Convert.ToInt32(txtProductUPC.Text), out j, out pstring);


                if (!j) //not found == means unique == ok to continue
                {
                    //check each product types' specific attr formats depending on the product type
                    //create the product if format is ok and add it to the database

                    if (i == 1)
                    {
                        // validation is correct the the boolean would be true and would add to the datatbase
                        bool temp = Validator.validateName(txtBookAuthor.Text) && Validator.validateISBN(txtBookISBNLeft.Text) &&
                                    Validator.validateISBN(txtBookISBNRight.Text) && Validator.validateIntNumber(txtBookPages.Text);
                        if (temp)
                        {
                            Book book = new Book(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text),
                                                 txtBookISBNLeft.Text, txtBookISBNRight.Text, txtBookAuthor.Text, Convert.ToInt32(txtBookPages.Text));
                            //productList.add(book);
                            MessageBox.Show("Book Added to Library");

                            // Using Frank's method in PrductDB to add to the library
                            pdb.InsertProduct(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text), "Book");
                            pdb.InsertBook(Convert.ToInt32(txtProductUPC.Text), Convert.ToInt32(txtBookISBNLeft.Text + txtBookISBNRight.Text), txtBookAuthor.Text, Convert.ToInt32(txtBookPages.Text));
                            FormController.resetForm(this);
                        }
                        else
                        {
                            MessageBox.Show("Please check input in Book Fields");
                        }
                    }
                    else if (i == 2)
                    {
                        bool temp = Validator.validateName(txtBookAuthor.Text) && Validator.validateISBN(txtBookISBNLeft.Text) &&
                                    Validator.validateISBN(txtBookISBNRight.Text) && Validator.validateIntNumber(txtBookPages.Text) && (txtBookCISCISArea.Text != "");
                        if (temp)
                        {
                            BookCIS book = new BookCIS(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text),
                                                       txtBookISBNLeft.Text, txtBookISBNRight.Text, txtBookAuthor.Text, Convert.ToInt32(txtBookPages.Text), txtBookCISCISArea.Text);
                            // productList.add(book);
                            MessageBox.Show("Book Added to Library");

                            pdb.InsertProduct(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text), "BookCIS");
                            pdb.InsertBook(Convert.ToInt32(txtProductUPC.Text), Convert.ToInt32(txtBookISBNLeft.Text + txtBookISBNRight.Text), txtBookAuthor.Text, Convert.ToInt32(txtBookPages.Text));
                            pdb.InsertBookCIS(Convert.ToInt32(txtProductUPC.Text), txtBookCISCISArea.Text);

                            FormController.resetForm(this);
                        }
                        else
                        {
                            MessageBox.Show("Please check input in Book Fields");
                        }
                    }
                    else if (i == 3)
                    {
                        bool temp = Validator.validateName(txtDVDLeadActor.Text) && Validator.validateRunTime(txtDVDRunTime.Text) &&
                                    Validator.validateReleaseDate(txtDVDReleaseDate.Value);
                        if (temp)
                        {
                            DVD dvd = new DVD(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text),
                                              txtDVDLeadActor.Text, txtDVDReleaseDate.Value, Convert.ToInt32(txtDVDRunTime.Text));
                            // productList.add(dvd);
                            MessageBox.Show("DVD Added to Library");

                            pdb.InsertProduct(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text), "DVD");
                            pdb.InsertDVD(Convert.ToInt32(txtProductUPC.Text), txtDVDLeadActor.Text, txtDVDReleaseDate.Value, Convert.ToInt32(txtDVDRunTime.Text));

                            FormController.resetForm(this);
                        }
                        else
                        {
                            MessageBox.Show("Please check input in DVD Fields");
                        }
                    }
                    else if (i == 4)
                    {
                        bool temp = Validator.validateName(txtCDClassicalLabel.Text) && Validator.validateArtists(txtCDClassicalArtists.Text) &&
                                    Validator.validateName(txtCDOrchestraConductor.Text);
                        if (temp)
                        {
                            CDOrchestra cd = new CDOrchestra(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text),
                                                             txtCDClassicalLabel.Text, txtCDClassicalArtists.Text, txtCDOrchestraConductor.Text);
                            // productList.add(cd);
                            MessageBox.Show("CD Added to Library");
                            pdb.InsertProduct(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text), "CDOrchestra");
                            pdb.InsertCDClassical(Convert.ToInt32(txtProductUPC.Text), txtCDClassicalLabel.Text, txtCDClassicalArtists.Text);
                            pdb.InsertCDOrchestra(Convert.ToInt32(txtProductUPC.Text), txtCDOrchestraConductor.Text);

                            FormController.resetForm(this);
                        }
                        else
                        {
                            MessageBox.Show("Please check input in CD Fields");
                        }
                    }
                    else if (i == 5)
                    {
                        bool temp = Validator.validateName(txtCDClassicalLabel.Text) && Validator.validateArtists(txtCDClassicalArtists.Text) &&
                                    Validator.validateInstruments(txtCDChamberInstrumentList.Text);
                        if (temp)
                        {
                            CDChamber cd = new CDChamber(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text),
                                                         txtCDClassicalLabel.Text, txtCDClassicalArtists.Text, txtCDChamberInstrumentList.Text);
                            //productList.add(cd);
                            MessageBox.Show("CD Added to Library");

                            pdb.InsertProduct(Convert.ToInt32(txtProductUPC.Text), Convert.ToDecimal(txtProductPrice.Text), txtProductTitle.Text, Convert.ToInt32(txtProductQuantity.Text), "CDChamber");
                            pdb.InsertCDClassical(Convert.ToInt32(txtProductUPC.Text), txtCDClassicalLabel.Text, txtCDClassicalArtists.Text);
                            pdb.InsertCDChamber(Convert.ToInt32(txtProductUPC.Text), txtCDChamberInstrumentList.Text);

                            FormController.resetForm(this);
                        }
                        else
                        {
                            MessageBox.Show("Please check input in CD Fields");
                        }
                    }
                }
                else //not unique
                {
                    MessageBox.Show("Product with same UPC already exists");
                }
            }
            else //incorrect format used in general product fields
            {
                MessageBox.Show("Please check input in Prodcut Fields");
            }
        }
        //this is what happens when you click search
        //use output of productdb query to build an object to then display its attributes on the form
        private void BtnSearchUPC_Click(object sender, EventArgs e)
        {
            bool temp = Validator.validateUPC(txtProductUPCSearch.Text); //first make sure the format is correct

            if (temp)
            {
                bool   j;       // boolean object reference for return
                string pstring; // Product string updated upon product DB search call.

                //p = productList.find(Convert.ToInt32(txtProductUPCSearch.Text)); //search our list for this upc
                Product k; //used to create an object after learning which type we have
                //get table from database to read row attributes for our product
                OleDbDataReader odb = pdb.SelectProductFromProduct(Convert.ToInt32(txtProductUPCSearch.Text), out j, out pstring);
                //MessageBox.Show(pstring);
                if (!j) //not found
                {
                    MessageBox.Show("Product not found");
                    txtProductUPCSearch.Clear();
                    txtProductUPCSearch.Focus();
                } // Creates a ne product to dislay in form.
                else
                {
                    string[] attributes = pstring.Split('\n'); // splits product attributes into  array
                    for (int i = 0; i < attributes.Length; i++)
                    {
                        attributes[i] = attributes[i].Trim('\r');
                    }
                    string ptype = attributes[4]; // gets the product type from this attribute and then creates new product to display in form

                    if (ptype == "Book")
                    {
                        string left  = attributes[5].Substring(0, 3);
                        string right = attributes[5].Substring(3);

                        k = new Book(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                     left, right, attributes[6], Convert.ToInt32(attributes[7]));
                        k.Display(this);
                        FormController.searchForm(this);
                        txtProductUPCSearch.Clear();
                    }
                    else if (ptype == "BookCIS")
                    {
                        string left  = attributes[5].Substring(0, 3);
                        string right = attributes[5].Substring(3);

                        k = new BookCIS(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                        left, right, attributes[6], Convert.ToInt32(attributes[7]), attributes[8]);
                        k.Display(this);
                        FormController.searchForm(this);
                        txtProductUPCSearch.Clear();
                    }
                    else if (ptype == "DVD")
                    {
                        k = new DVD(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                    attributes[5], DateTime.Parse(attributes[6]), Convert.ToInt32(attributes[7]));
                        k.Display(this);
                        FormController.searchForm(this);
                        txtProductUPCSearch.Clear();
                    }
                    else if (ptype == "CDOrchestra")
                    {
                        k = new CDOrchestra(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                            attributes[5], attributes[6], attributes[7]);
                        k.Display(this);
                        FormController.searchForm(this);
                        txtProductUPCSearch.Clear();
                    }
                    else if (ptype == "CDChamber")
                    {
                        k = new CDChamber(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                          attributes[5], attributes[6], attributes[7]);
                        k.Display(this);
                        FormController.searchForm(this);
                        txtProductUPCSearch.Clear();
                    }
                }
            }
            else //user needs to fix format
            {
                MessageBox.Show("Invalid UPC format");
                txtProductUPCSearch.Clear();
                txtProductUPCSearch.Focus();
            }
        }
        }     // end getItem

        //Searches item by the UPC number since it is guarenteed that this will be a unique identifier
        private void btnProductUPCSearch_Click(object sender, EventArgs e)
        {
            bool temp = Validation.validateProductUPC(txtProductUPCSearch.Text); //first make sure the format is correct

            if (temp)
            {
                bool    found;   // boolean reference for search success
                string  pstring; // Product string updated upon product DB search call.
                Product prod;

                //  this returns an OleDbDataReader object, but you don't really need to use it
                //  the boolean flag and string that are returned are important
                //  pstring will hold the attributes of a product from the database in a single string, separated by newline characters
                //  split it below

                OleDbDataReader odb = dbFunctions.SelectProductFromProduct(Convert.ToInt32(txtProductUPCSearch.Text), out found, out pstring);

                if (!found) //not found
                {
                    MessageBox.Show("Product not found");
                    txtProductUPCSearch.Clear();
                    txtProductUPCSearch.Focus();
                } // Creates a new product to display in form.
                else
                {
                    txtProductUPC.ReadOnly      = true;
                    txtProductTitle.ReadOnly    = true;
                    txtProductQuantity.ReadOnly = true;
                    txtProductPrice.ReadOnly    = true;

                    btnDelete.Enabled = true;
                    string[] attributes = pstring.Split('\n'); // splits product attributes into array

                    for (int i = 0; i < attributes.Length; i++)
                    {
                        attributes[i] = attributes[i].Trim('\r'); // clears "junk" from each field
                    }

                    string ptype = attributes[4]; // gets the product type from this attribute and then creates new product to display in form

                    if (ptype == "DVD")
                    {
                        txtDVDLeadActor.ReadOnly   = true;
                        txtDVDReleaseDate.ReadOnly = true;
                        txtDVDRunTime.ReadOnly     = true;
                        prod = new DVD(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                       attributes[5], DateTime.Parse(attributes[6]), Convert.ToInt32(attributes[7]));
                        prod.Display(this);
                        FormController.searchForm(this);
                        FormController.activateDVD(this);
                        thisProductList.Add(prod);
                        currentIndex++;
                    }

                    if (ptype == "Book")
                    {
                        String isbnS = attributes[5];
                        String isbn1 = isbnS.Substring(0, 3);
                        String isbn2 = isbnS.Substring(3, 3);
                        txtBookISBNLeft.ReadOnly  = true;
                        txtBookISBNRight.ReadOnly = true;
                        txtBookAuthor.ReadOnly    = true;
                        txtBookPages.ReadOnly     = true;
                        prod = new Book(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                        Convert.ToInt32(isbn1), Convert.ToInt32(isbn2), attributes[6], Convert.ToInt32(attributes[7]));
                        prod.Display(this);
                        FormController.searchForm(this);
                        FormController.activateBook(this);
                        thisProductList.Add(prod);
                        currentIndex++;
                    }

                    if (ptype == "BookCIS")
                    {
                        String isbnS = attributes[5];
                        String isbn1 = isbnS.Substring(0, 3);
                        String isbn2 = isbnS.Substring(3, 3);
                        txtBookISBNLeft.ReadOnly   = true;
                        txtBookISBNRight.ReadOnly  = true;
                        txtBookAuthor.ReadOnly     = true;
                        txtBookPages.ReadOnly      = true;
                        txtBookCISCISArea.ReadOnly = true;
                        prod = new BookCIS(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                           Convert.ToInt32(isbn1), Convert.ToInt32(isbn2), attributes[6], Convert.ToInt32(attributes[7]), attributes[8]);
                        prod.Display(this);
                        FormController.searchForm(this);
                        FormController.activateBookCIS(this);
                        thisProductList.Add(prod);
                        currentIndex++;
                    }

                    if (ptype == "CDOrchestra")
                    {
                        txtCDClassicalArtists.ReadOnly   = true;
                        txtCDClassicalLabel.ReadOnly     = true;
                        txtCDOrchestraConductor.ReadOnly = true;
                        prod = new CDOrchestra(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                               attributes[5], attributes[6], attributes[7]);
                        prod.Display(this);
                        FormController.searchForm(this);
                        FormController.activateCDOrchestra(this);
                        thisProductList.Add(prod);
                        currentIndex++;
                    }

                    if (ptype == "CDChamber")
                    {
                        txtCDClassicalArtists.ReadOnly      = true;
                        txtCDClassicalLabel.ReadOnly        = true;
                        txtCDChamberInstrumentList.ReadOnly = true;
                        //String[] InstrumentList = attributes[6].Split(' ');
                        prod = new CDChamber(Convert.ToInt32(attributes[0]), Convert.ToDecimal(attributes[1]), attributes[2], Convert.ToInt32(attributes[3]),
                                             attributes[5], attributes[6], attributes[7]);
                        prod.Display(this);
                        FormController.searchForm(this);
                        FormController.activateCDChamber(this);
                        thisProductList.Add(prod);
                        currentIndex++;
                    }

                    /*
                     *
                     * add else ifs for the other product types and handle each accordingly
                     *
                     */
                    else
                    {
                        // this is an invalid record (since it does not fit one of our types)
                    }
                }
            }
            else
            {
                // UPC is invalid
                MessageBox.Show("Invalid URL", "Error:");
            }
        }