//Precondition: addButton is clicked. //Postcondition: If data is valid a new package is added to the list box. private void addButton_Click(object sender, EventArgs e) { GroundPackage myPackage; //Holds new package from valid data int oZip; //To hold entered origin zip. int dZip; //To hold entered destination zip. double height; //To hold entered height. double length; //To hold entered length. double width; //To hold entered width. double weight; //To hold entered weight. if (ValidatePackageData(out oZip, out dZip, out length, out height, out width, out weight)) { //Create new package myPackage = new GroundPackage(oZip, dZip, length, height, width, weight); packageList.Add(myPackage); outputListBox.Items.Add(myPackage.CalcCost().ToString("c")); originTxt.Clear(); destinationTxt.Clear(); lengthTxt.Clear(); heightTxt.Clear(); widthTxt.Clear(); weightTxt.Clear(); originTxt.Focus(); } }
//Precondition: Valid data entered //Postcondition: None private void addPackageButton_Click(object sender, EventArgs e) { GroundPackage inputPackage = new GroundPackage(); { GetPackageInfo(inputPackage); packageInfo.Add(inputPackage); costListBox.Items.Add(inputPackage.CalcCost()); origenInput.Clear(); // Clears the origen textbox destinationInput.Clear(); // Clears the destination textbox lenghtInput.Clear(); // Clears the length textbox widthInput.Clear(); // Clears width the textbox heightInput.Clear(); // Clears the height textbox weightInput.Clear(); // Clears the weight textbox } }
double weight; //variable to hold weight // Precondition: User clicked on ADD button // Postcondition: If user's input in the six textboxes, all six are parsed and validated and added to ground package object. private void addButton_Click(object sender, EventArgs e) { //All six textboxes are parsed to a string if (int.TryParse(originZipTextBox.Text, out originZip) && int.TryParse(destinZipTextBox.Text, out destinationZip) && double.TryParse(lengthTextBox.Text, out length) && double.TryParse(widthTextBox.Text, out width) && double.TryParse(heightTextBox.Text, out height) && double.TryParse(weightTextBox.Text, out weight)) { if (originZip <= minZip && originZip > maxZip) { MessageBox.Show("Invaild Origin Zip"); return; } if (destinationZip <= minZip && destinationZip > maxZip) { MessageBox.Show("Invaid Destination Zip"); return; } if (length <= 0) { MessageBox.Show("Length must be >0"); return; } if (width <= 0) { MessageBox.Show("Width must be >0"); return; } if (height <= 0) { MessageBox.Show("Height must be >0"); return; } if (weight <= 0) { MessageBox.Show("Weight must be >0"); return; } //Groundpackage object created as my package with data from and six textboxes stored as its data GroundPackage mypackage = new GroundPackage(originZip, destinationZip, length, width, height, weight); packageList.Add(mypackage); //mypackage added to package list //Price from method calcCost stored in package listbox packageListbox.Items.Add(mypackage.CalcCost().ToString("C")); } }
private void addButton_Click(object sender, EventArgs e) { GroundPackage package = new GroundPackage(); // Holds new package ValidatePackageData(package); // Makes sure data is valid packageList.Add(package); // Adds package to list double cost = package.CalcCost(); // Holds the package cost packageListBox.Items.Add(cost.ToString("c2")); // Adds package cost to list // Clear textbox controls originZipCodeTextBox.Clear(); DestinationZipCodeTextBox.Clear(); lengthTextBox.Clear(); heightTextBox.Clear(); widthTextBox.Clear(); weightTextBox.Clear(); // Refocus originZipCodeTextBox.Focus(); }
static void display(GroundPackage allpackages) { Console.WriteLine(allpackages); // Printing packages Console.WriteLine($"{"Shipping Cost",-20}: ${allpackages.CalcCost()}"); // Printing the shipping costs with call function Console.WriteLine(); }
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. } }
//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(); } } } }