Exemple #1
0
        //Preconditions:
        //              00000 <= origin Zip <= 99999
        //              00000 <= destination zip <= 99999
        //              length > 0
        //              width > 0
        //              height > 0
        //              weight > 0
        //Postconditions: takes entered info and calculates the cost of shipping
        private void addPackageButton_Click(object sender, EventArgs e)
        {
            int          originZip;             // declares a int variable to hold the origin zip code
            int          destinationZip;        // declares a int variable to hold the destination zip code
            double       length;                // declares a double variable to hold the length of the package
            double       width;                 // declares a double variable to hold the width of the package
            double       height;                // declares a double variable to hold the height of the package
            double       weight;                // declares a double variable to hold the weight of the package
            const int    MIN_ZIP       = 00000; // declares a constant int variable to hold the minimum value the zip code could have
            const int    MAX_ZIP       = 99999; // declares a constant int variable to hold the maximum value the zip code could have
            const double INVALID_VALUE = 0;     // declares a constant double variable to hold the the invalid value for the length,

            //width, height, and weight


            if (originZipTxtBox.Text.Length > 5 || originZipTxtBox.Text.Length < 5) // test to see if there is a 5 digit number in the origin zip text box
            {
                MessageBox.Show("Please enter a 5 digit zip code");                 //error message if length does not = 5
                originZipTxtBox.Focus();                                            // highlights the textbox where there error message is coming from
                originZipTxtBox.SelectAll();
            }

            else// origin zip has 5 characters in its text box
            {
                if (destinationZipTxtBox.Text.Length > 5 || destinationZipTxtBox.Text.Length < 5)// test to see if there is a 5 digit number in the destination zip text box
                {
                    MessageBox.Show("Please enter a 5 digit zip code"); //error message if length does not = 5
                    destinationZipTxtBox.Focus();                       // highlights the textbox where there error message is coming from
                    destinationZipTxtBox.SelectAll();
                }
                else// destination zip has 5 characters in its text box
                {
                    if (int.TryParse(originZipTxtBox.Text, out originZip) && originZip >= MIN_ZIP && originZip <= MAX_ZIP)// validing if the origin zip is an integer betweem 00000-99999
                    {
                        if (int.TryParse(destinationZipTxtBox.Text, out destinationZip) && destinationZip >= MIN_ZIP && destinationZip <= MAX_ZIP)       // validing if the destination zip is an integer betweem 00000-99999
                        {
                            if (double.TryParse(lengthTxtBox.Text, out length) && length > INVALID_VALUE)                                                // validating if the length is > 0
                            {
                                if (double.TryParse(widthTxtBox.Text, out width) && width > INVALID_VALUE)                                               // validating if the width is > 0
                                {
                                    if (double.TryParse(heightTxtBox.Text, out height) && height > INVALID_VALUE)                                        // validating if the height is > 0
                                    {
                                        if (double.TryParse(weightTxtBox.Text, out weight) && weight > INVALID_VALUE)                                    // validating if the weight is > 0
                                        {
                                            GroundPackage myGroundPackage = new GroundPackage(originZip, destinationZip, length, width, height, weight); // adds a new GroundPackage Object

                                            myGroundPackage.OriginZip      = originZip;                                                                  // sets the new value of the origin Zip to the one entered by the user
                                            myGroundPackage.DestinationZip = destinationZip;                                                             // sets the new value of the destination Zip to the one entered by the user
                                            myGroundPackage.Length         = length;                                                                     // sets the new value of the length to the one entered by the user
                                            myGroundPackage.Width          = width;                                                                      // sets the new value of the width to the one entered by the user
                                            myGroundPackage.Height         = height;                                                                     // sets the new value of the height to the one entered by the user
                                            myGroundPackage.Weight         = weight;                                                                     // sets the new value of the weight to the one entered by the user

                                            myGroundPackage.CalcCost();                                                                                  // takes entered info and calculates cost of shipping
                                            packageList.Add(myGroundPackage);                                                                            // adds the groundpackage object into the list
                                            groundPackageListBox.Items.Add(myGroundPackage.CalcCost().ToString("C"));                                    // displays the price of shipment into the list box

                                            originZipTxtBox.Clear();                                                                                     // clears the origin zip code text box
                                            destinationZipTxtBox.Clear();                                                                                // clears the destination zip code text box
                                            lengthTxtBox.Clear();                                                                                        // clears the length text box
                                            widthTxtBox.Clear();                                                                                         // clears the width text box
                                            heightTxtBox.Clear();                                                                                        // clears the height text box
                                            weightTxtBox.Clear();                                                                                        // clears the weight text box
                                        }
                                        else// weight had invalid entry
                                        {
                                            MessageBox.Show("Please enter a valid weight (number > 0)");
                                            weightTxtBox.Focus();            // highlights the textbox where there error message is coming from
                                            weightTxtBox.SelectAll();
                                        }
                                    }
                                    else// height had invalid entry
                                    {
                                        MessageBox.Show(" Please enter a valid height in inches (number > 0)");
                                        heightTxtBox.Focus();               // highlights the textbox where there error message is coming from
                                        heightTxtBox.SelectAll();
                                    }
                                }
                                else// width had invalid entry
                                {
                                    MessageBox.Show("Please enter a valid width in inches (number > 0)");
                                    widthTxtBox.Focus();                    // highlights the textbox where there error message is coming from
                                    widthTxtBox.SelectAll();
                                }
                            }
                            else// length had invalid entry
                            {
                                MessageBox.Show("Please enter a valid length in inches ( number > 0)");
                                lengthTxtBox.Focus();                       // highlights the textbox where there error message is coming from
                                lengthTxtBox.SelectAll();
                            }
                        }
                        else// origin zip code had invalid entry
                        {
                            MessageBox.Show("Please enter a valid, non negative 5 digit zip code");
                            originZipTxtBox.Focus();                       // highlights the textbox where there error message is coming from
                            originZipTxtBox.SelectAll();
                        }
                    }
                    else// destination zip code had invalid entry
                    {
                        MessageBox.Show("Please enter a valid, non negative 5 digit zip code");
                        destinationZipTxtBox.Focus();                      // highlights the textbox where there error message is coming from
                        destinationZipTxtBox.SelectAll();
                    }
                }
            }
        }
        public List <GroundPackage> myPackageList = new List <GroundPackage>(); //Creates a new list called myPackageList

        //Add package button
        // Precondition:  User clicked on add package button
        // Postcondition: If user's input are an the valid data types, the bool values are true
        //                they are used in a final if statement which clears the textboxes
        //                and
        private void addBtn_Click(object sender, EventArgs e)
        {
            int    originZipInput      = 0;     //Origin zip inputted by the user
            int    destinationZipInput = 0;     //Destination zip inputted by the user
            double lengthValueInput    = 0;     //Length inputted by the user
            double widthValueInput     = 0;     //Width inputted by the user
            double heightValueInput    = 0;     //Height inputted by the user
            double weightValueInput    = 0;     //Weight inputted by the user
            bool   originBool          = false; //origin zips that can be parsed and are >=00000 && <= 99999
            bool   destinationBool     = false; //destination zips that can be parsed and are >=00000 && <=99999
            bool   lengthBool          = false; //length values that can be parsed and are >0
            bool   widthBool           = false; //width values that can be parsed and are >0
            bool   heightBool          = false; //height values that can be parsed and are >0
            bool   weightBool          = false; //weight values that can be parsed and are >0

            GroundPackage myGroundPackage;      //Creates an instance of GroundPackage called myGroundPackage

            //This nested if statement tests to see if all of the 6 values can (1) be parsed and (2) if they
            //fall within the necessary range(s). If they can be parsed, the corresponding bool variable
            //of that value is true. Otherwise an error message appears and specifies what is wrong with the input.
            if (int.TryParse(originZipTxtBox.Text, out originZipInput))
            {
                if (originZipInput >= 00000 && originZipInput <= 99999)
                {
                    originBool = true;
                }
                else
                {
                    MessageBox.Show("Enter a zip code between 00000 and 99999.");
                }
            }
            else
            {
                MessageBox.Show("Enter a zip code between 00000 and 99999.");     //try parse else
            }
            if (int.TryParse(destinationZipTxtBox.Text, out destinationZipInput))
            {
                if (destinationZipInput >= 00000 && destinationZipInput <= 99999)
                {
                    destinationBool = true;
                }
                else
                {
                    MessageBox.Show("Enter a zip code between 00000 and 99999.");
                }
            }
            else
            {
                MessageBox.Show("Enter a zip code between 00000 and 99999.");     //try parse else
            }
            if (double.TryParse(lengthTxtBox.Text, out lengthValueInput))
            {
                if (lengthValueInput > 0)
                {
                    lengthBool = true;
                }
                else
                {
                    MessageBox.Show("Enter a valid numeric length value.");
                }
            }
            else
            {
                MessageBox.Show("Enter a valid numeric length value.");     //try parse else
            }
            if (double.TryParse(widthTxtBox.Text, out widthValueInput))
            {
                if (widthValueInput > 0)
                {
                    widthBool = true;
                }
                else
                {
                    MessageBox.Show("Enter a valid numeric width value.");
                }
            }
            else
            {
                MessageBox.Show("Enter a valid numeric width value.");     //try parse else
            }
            if (double.TryParse(heightTxtBox.Text, out heightValueInput))
            {
                if (heightValueInput > 0)
                {
                    heightBool = true;
                }
                else
                {
                    MessageBox.Show("Enter a numeric height value.");
                }
            }
            else
            {
                MessageBox.Show("Enter a valid numeric height value.");     //try parse else
            }
            if (double.TryParse(weightTxtBox.Text, out weightValueInput))
            {
                if (weightValueInput > 0)
                {
                    weightBool = true;
                }
                else
                {
                    MessageBox.Show("Enter a numeric weight value.");
                }
            }
            else
            {
                MessageBox.Show("Enter a numeric weight value.");     //try parse else
            }
            //If all of the values can be parsed and meet the numeric requirements, then all of their respective
            //textboxes will clear and a new ground package item will be created.
            if (originBool && destinationBool && lengthBool && widthBool && heightBool && weightBool)
            {
                originZipTxtBox.Clear();
                destinationZipTxtBox.Clear();
                lengthTxtBox.Clear();
                widthTxtBox.Clear();
                heightTxtBox.Clear();
                weightTxtBox.Clear();

                //New ground package item created
                myGroundPackage = new GroundPackage(originZipInput, destinationZipInput, lengthValueInput,
                                                    widthValueInput, heightValueInput, weightValueInput);
                myPackageList.Add(myGroundPackage);
                packageListBox.Items.Add(myGroundPackage.CalcCost().ToString("c")); //Displays the cost of the package
                                                                                    //in the listbox.
            }
        }
        // Precondition: None
        // Postcondition: Gets and parses inputted data and then assigns the data to the correct variable.
        public void ValidatePackageData(GroundPackage package)
        {
            int    originZip;           // Holds origin Zip Code
            int    destinationZip;      // Holds destination Zip Code
            double length;              // Holds package length
            double height;              // Holds package height
            double width;               // Holds package width
            double weight;              // Holds package weight

            if (int.TryParse(originZipCodeTextBox.Text, out originZip) && originZip >= 11111 && originZip <= 99999)
            {
                package.OriginZip = originZip;
            }
            else
            {
                MessageBox.Show("Please enter a valid zip code.");
            }

            if (int.TryParse(DestinationZipCodeTextBox.Text, out destinationZip) && destinationZip >= 11111 && destinationZip <= 99999)
            {
                package.DestinationZip = destinationZip;
            }
            else
            {
                MessageBox.Show("Please enter a valid zip code.");
            }

            if (double.TryParse(lengthTextBox.Text, out length) && length >= 1)
            {
                package.Length = length;
            }
            else
            {
                MessageBox.Show("Please enter a number greater than 1.");
            }

            if (double.TryParse(heightTextBox.Text, out height) && height >= 1)
            {
                package.Height = height;
            }
            else
            {
                MessageBox.Show("Please enter a number greater than 1.");
            }

            if (double.TryParse(widthTextBox.Text, out width) && width >= 1)
            {
                package.Width = width;
            }
            else
            {
                MessageBox.Show("Please enter a number greater than 1.");
            }

            if (double.TryParse(weightTextBox.Text, out weight) && weight >= 1)
            {
                package.Weight = weight;
            }
            else
            {
                MessageBox.Show("Please enter a number greater than 1.");
            }
        }