private UserParcelView upv; // The UserParcelView // Preconditon: None // Postconditon: Prog2 form is initialized public Prog2Form() { InitializeComponent(); upv = new UserParcelView(); // test data upv.AddAddress(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 upv.AddAddress("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 upv.AddAddress("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 upv.AddAddress("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 upv.AddAddress("John Doe", "111 Market St.", "Jeffersonville", "IN", 47130); // Test Address 5 upv.AddAddress("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 upv.AddAddress("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 upv.AddAddress("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 upv.AddLetter(upv.AddressAt(0), upv.AddressAt(1), 5.00M); // Letter test object upv.AddLetter(upv.AddressAt(2), upv.AddressAt(3), 2.50M); // Letter test object upv.AddGroundPackage(upv.AddressAt(4), upv.AddressAt(5), 4.5, 5.0, 10, 25); //groundpackage test object upv.AddGroundPackage(upv.AddressAt(6), upv.AddressAt(7), 7, 5, 10, 30); // groundpackage test object upv.AddNextDayAirPackage(upv.AddressAt(6), upv.AddressAt(0), 10, 15, 8.5, 10, 20.50M); //NextDayAirPackage test object upv.AddNextDayAirPackage(upv.AddressAt(3), upv.AddressAt(7), 15, 18, 20, 45, 50.75M); //NextDayAirPackage test object upv.AddTwoDayAirPackage(upv.AddressAt(2), upv.AddressAt(7), 12, 14, 11, 34, TwoDayAirPackage.Delivery.Saver); //TwoDayAirPackage test object upv.AddTwoDayAirPackage(upv.AddressAt(1), upv.AddressAt(5), 10, 5, 19, 48, TwoDayAirPackage.Delivery.Early); //TwoDayAirPackage test objec }
public Prog2Form() { InitializeComponent(); upv = new UserParcelView(); // Test Data - Magic Numbers OK upv.AddAddress("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 upv.AddAddress("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // Test Address 2 upv.AddAddress("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 upv.AddAddress("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 upv.AddAddress("Ryan Leezer", "666 Spring St", "", "New York CIty", "NY", 37219); //Test Address 5 upv.AddAddress("Jessica Lewis", "100 7th St", "", "Tampa", "FL", 57611); //Test Address 6 upv.AddAddress("Mark Hunt", "123 11th St", "Apt. 2", "Phoenix", "AZ", 21399); //Test Address 7 upv.AddAddress("Mary Gates", "2838 W Jefferson St", "", "Atlanta", "GA", 18042); //Test Address 8 upv.AddLetter(upv.AddressAt(0), upv.AddressAt(1), 3.55M); //Letter upv.AddLetter(upv.AddressAt(2), upv.AddressAt(4), 4.20M); //Letter 2 upv.AddLetter(upv.AddressAt(3), upv.AddressAt(6), 5.35M); //Letter 3 }
//PreConditions: UPV is not null //PostConditions: Saves the upv to file, returning true if successful private bool SaveUPVToFile(UserParcelView upv, string path = "../UserParcel.upv") { //Lets attempt to save catching any exceptions try { //Create the formatter BinaryFormatter formatter = new BinaryFormatter(); //Setup the stream, need to open or create and have write access FileStream fStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); //Serialize the upv to the file formatter.Serialize(fStream, upv); } //There was a problem opening or creating the file, maybe rights problem? Lets just tell the user and return false catch (IOException) { MessageBox.Show("Error creating file. Please try again"); return(false); } //Error writting the file, lets just tell the user and return false catch (Exception) { MessageBox.Show("Error saving file. Please try again"); return(false); } //Everything went smoothly return true return(true); }
//Precondition: None //Postcondition: creates Address' and Letters to their respective // list using the Add(Address/Letter) method public Prog2Form() { InitializeComponent(); upv = new UserParcelView(); upv.AddAddress("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // test address 1 upv.AddAddress("Jane Doe", "987 Main St.", "", "Beverly Hills", "CA", 90210); // test address 2 upv.AddAddress("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // test address 3 upv.AddAddress("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // test address 4 upv.AddAddress("Stan Marsh", "ShTapa Town", "4321 Your Street", "Pittsburgh", "Pennsylvania", 15201); // test address 5 upv.AddAddress("Eric Cartman", "South Park Elementary", "Detention Room 102", "New York City", "New York", 10001); // test address 6 upv.AddAddress("Kenny McCormick", "SoDoSoapa", "1111 Gheto Avenue", "South Park", "Colorado", 80019); // test address 7 upv.AddAddress("Kyle Broflovski", "The Church of God", "5676 Jewish Lane", "Jersey City", "New Jersey", 07208); // test address 8 upv.AddLetter(upv.AddressAt(1), upv.AddressAt(0), 2.50M); //test letter 1 upv.AddLetter(upv.AddressAt(3), upv.AddressAt(4), 1.50M); //test letter 2 upv.AddLetter(upv.AddressAt(2), upv.AddressAt(5), 2.00M); //test letter 3 upv.AddLetter(upv.AddressAt(7), upv.AddressAt(6), 0.75M); //test letter 4 }
//PreConditions: The path cant be null //PostConditions: Returns a loaded UPV or null private UserParcelView ReadUserParcelView(string path) { //Lets attempt to laod catching any exceptions try { //Setup formatter BinaryFormatter formatter = new BinaryFormatter(); //Setup stream we need read access and to open the file FileStream fStream = new FileStream(path, FileMode.Open, FileAccess.Read); //Attempt to deserialize UserParcelView read_upv = (UserParcelView)formatter.Deserialize(fStream); //All went well return upv return(read_upv); } //File was null show message and return null catch (ArgumentNullException) { MessageBox.Show("File not correct format, try again."); } //Couldnt read the file, tell user and return null catch (IOException) { MessageBox.Show("Unable to read file"); } //Something went wrong return null return(null); }
private void openButton_Click(object sender, EventArgs e) { DialogResult result; //temporary variable used to hold the dialog result of the open dialog string fileName; //temporary variable, holds file name. using (OpenFileDialog openMe = new OpenFileDialog()) //Use an open dialog to open the file { result = openMe.ShowDialog(); //show the open dialog fileName = openMe.FileName; //set variable to open dialog filename openMe.CheckFileExists = false; //don't check to see if the file exists. } if (fileName == string.Empty) //checks to see if you actually picked a file { MessageBox.Show("Please pick a file and try again"); //Show this if use didn't pick a file } else { try { FileStream openFile = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite); //Filestream for opening the file with read/write access. upv = (UserParcelView)formatter.Deserialize(openFile); //Set the upv object of the program to the upv of the file. } catch (IOException) { MessageBox.Show("Couldn't open file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);//throw this if the user picks a file that cannot be opened by program. } catch (SerializationException) { MessageBox.Show("Wrong kind of file, try again.");//Show if user picks a file of the wrong type of file to open } } }
private FileStream output; // stream for writing to a file // Precondition: None // Postcondition: The form's GUI is prepared for display. public Prog2Form() { InitializeComponent(); upv = new UserParcelView(); //// Test Data - Magic Numbers OK //upv.AddAddress("Rrose Selavy", "5454 Dada Ave.", "Apt. 66", "New York", "Ny", 10019); //upv.AddAddress("Leonora Carrington", "9632 Hyena Blvd.", "Ojai", "CA", 90322); //upv.AddAddress("Meret Oppenheim", "54 Teacup Ln.", "Apt. 3", "Chicago", "IL", 34998); //upv.AddAddress("Joan Miro", "9021 Sculpture Dr.", "Portland", "OR", 98069); //upv.AddAddress("Hugo Ball", "901 Century Ct.", "Miami", "FL", 20001); //upv.AddAddress("Leonor Fini", "306 Lake Pl.", "Unit 4C", "Denver", "CO", 76663); //upv.AddAddress("Tristan Tzara", "5050 Paris St.", "Omaha", "NE", 55091); //upv.AddAddress("Marcel Duchamp", "669 R. Mutt NE", "Woodinville", "WA", 99886); ////Test data -Parcel objects //upv.AddLetter(upv.AddressAt(0), upv.AddressAt(1), 3.75M); //upv.AddLetter(upv.AddressAt(3), upv.AddressAt(4), 2.25M); //upv.AddGroundPackage(upv.AddressAt(5), upv.AddressAt(6), 10, 5.5, 2, 2); //upv.AddGroundPackage(upv.AddressAt(6), upv.AddressAt(7), 7, 3, 1.5, 17); //upv.AddNextDayAirPackage(upv.AddressAt(0), upv.AddressAt(7), 10, 12, 6.5, 9, 12.50M); //upv.AddNextDayAirPackage(upv.AddressAt(1), upv.AddressAt(6), 30, 24, 12, 83, 25.0M); //upv.AddTwoDayAirPackage(upv.AddressAt(2), upv.AddressAt(5), 24, 13.5, 3, 77, TwoDayAirPackage.Delivery.Early); //upv.AddTwoDayAirPackage(upv.AddressAt(3), upv.AddressAt(4), 36, 36, 36, 45, TwoDayAirPackage.Delivery.Saver); addressList = new List <Address>(); parcelList = new List <Parcel>(); }
private FileStream openFile; // stream for reading from a file // Precondition: None // Postcondition: The form's GUI is prepared for display. A few test addresses are // added to the list of addresses public Prog2Form() { InitializeComponent(); upv = new UserParcelView(); // // Test Data - Magic Numbers OK //upv.AddAddress(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 //upv.AddAddress("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 //upv.AddAddress("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 //upv.AddAddress("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 //upv.AddAddress("John Doe", "111 Market St.", "", "Jeffersonville", "IN", 47130); // Test Address 5 //upv.AddAddress("Jane Smith", "55 Hollywood Blvd.", "Apt. 9", "Los Angeles", "CA", 90212); // Test Address 6 //upv.AddAddress("Captain Robert Crunch", "21 Cereal Rd.", "Room 987", "Bethesda", "MD", 20810); // Test Address 7 //upv.AddAddress("Vlad Dracula", "6543 Vampire Way", "Apt. 1", "Bloodsucker City", "TN", 37210); // Test Address 8 //upv.AddLetter(upv.AddressAt(0), upv.AddressAt(1), 3.95M); // Letter test object //upv.AddLetter(upv.AddressAt(2), upv.AddressAt(3), 4.25M); // Letter test object //upv.AddGroundPackage(upv.AddressAt(4), upv.AddressAt(5), 14, 10, 5, 12.5); // Ground test object //upv.AddGroundPackage(upv.AddressAt(6), upv.AddressAt(7), 8.5, 9.5, 6.5, 2.5); // Ground test object //upv.AddNextDayAirPackage(upv.AddressAt(0), upv.AddressAt(2), 25, 15, 15, 85, 7.50M); // Next Day test object //upv.AddNextDayAirPackage(upv.AddressAt(2), upv.AddressAt(4), 9.5, 6.0, 5.5, 5.25, 5.25M); // Next Day test object //upv.AddNextDayAirPackage(upv.AddressAt(1), upv.AddressAt(6), 10.5, 6.5, 9.5, 15.5, 5.00M); // Next Day test object //upv.AddTwoDayAirPackage(upv.AddressAt(4), upv.AddressAt(6), 46.5, 39.5, 28.0, 80.5, TwoDayAirPackage.Delivery.Saver); // Two Day test object //upv.AddTwoDayAirPackage(upv.AddressAt(7), upv.AddressAt(0), 15.0, 9.5, 6.5, 75.5, TwoDayAirPackage.Delivery.Early); // Two Day test object //upv.AddTwoDayAirPackage(upv.AddressAt(5), upv.AddressAt(3), 12.0, 12.0, 6.0, 5.5, TwoDayAirPackage.Delivery.Saver); // Two Day test object }
//PreConditions: Clicked the Open Tool Item //PostConditions: Opens a dialog and loads any data private void openToolStripMenuItem_Click(object sender, EventArgs e) { //Open a file explorer OpenFileDialog ofd = new OpenFileDialog(); //We only want to show .dat as specified in the instructions ofd.Filter = "UserParcelViewData (*.dat)|*.dat"; //Show the dialog and wait for a response DialogResult ofdResult = ofd.ShowDialog(); //If Ok lets read the data and put it into the UPV if (ofdResult == DialogResult.OK) { //Read the file UserParcelView read_upv = ReadUserParcelView(ofd.FileName); //If the result is not null lets use it as the UPV if (read_upv != null) { this.upv = read_upv; } } //Lets display the list of parcels to the user, to indicate that it indeed loaded listParcelsToolStripMenuItem_Click(sender, e); //Dispose the dialog ofd.Dispose(); }
// Precondition: File, Open menu item activated // Postcondition: The UserParcelView is read in from a file using // object deserialization, replacing the existing upv private void openToolStripMenuItem_Click(object sender, EventArgs e) { BinaryFormatter reader = new BinaryFormatter(); // Object for deserializing UPV in binary format FileStream input = null; // Stream for reading from a file DialogResult result; // Result of file dialog box string fileName; // Name of file to save data UserParcelView temp; // Temporary holder for UPV using (OpenFileDialog fileChooser = new OpenFileDialog()) // Create Open Dialog box { result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; // get specified name } // end using // ensure that user clicked "OK" if (result == DialogResult.OK) { // show error if user specified invalid file if (fileName == string.Empty) { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { // create FileStream to obtain read access to file try { input = new FileStream(fileName, FileMode.Open, FileAccess.Read); // get UPV from file temp = (UserParcelView)reader.Deserialize(input); upv = temp; // Separated in case deserialization failed } // end try // handle exception if there is a problem opening the file catch (IOException) { // notify user if file could not be opened MessageBox.Show("I/O Error Reading From File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // end catch catch (SerializationException) { MessageBox.Show("Serialization Error Reading From File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // end catch finally { input?.Close(); // close FileStream if not null } } // end else } // end if }
// Precondition: Pass a userparcelview // Postcondition: Creates Form public AddressForm(UserParcelView upv) { InitializeComponent(); //Set the Upv _UPV = upv; //Fill state boxes with some states fillStateBox(); }
// Precondition: Pass a UPV // Postcondition: Instatiates a letter form public LetterForm(UserParcelView upv) { InitializeComponent(); //Set the UPV _UPV = upv; //Fill the combo boxes with the addresses names setAddresses(_UPV.AddressList); }
//Precondition: None //Postcondition: The Letter_Form GUI is initialized public Letter_Form(UserParcelView upv) { InitializeComponent(); userParcel = upv; //For each orig address and dest address, display the name in the combobox. foreach (Address a in userParcel.addresses) { comboBox_Origin.Items.Add(a.Name); comboBox_Dest.Items.Add(a.Name); } }
//Precondition: Open menu item selected //Postcondition: File selected by the user is opened private void openMenuStripItem_Click(object sender, EventArgs e) { BinaryFormatter bireader = new BinaryFormatter(); //Holds instructions to serialize and deserialize an object DialogResult result; string serialFile; FileStream input = null; // Maintains connection to file using (OpenFileDialog chooser = new OpenFileDialog()) { result = chooser.ShowDialog(); serialFile = chooser.FileName; } //Checks validation of file if (result == DialogResult.OK) { //Error if file not valid if (serialFile == string.Empty) { MessageBox.Show("Is that a file? No.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { //Creates new filestream input = new FileStream(serialFile, FileMode.Open, FileAccess.Read); //Deserialize current file upv = (UserParcelView)bireader.Deserialize(input); } catch (IOException) { MessageBox.Show("Error Opening File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (SerializationException) { MessageBox.Show("Error Reading from File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } //Closes file at the end finally { //Close file stream if (input != null) { input.Close(); } } } } }
private UserParcelView upv; // The UserParcelView public SelectAddress(List <Address> addresses) { InitializeComponent(); addressList = addresses; upv = new UserParcelView(); // Combo box is populated with Address items in addressList foreach (Address a in addressList) { editAddressCB.Items.Add(a.Name); } }
// Precondition: File, Open menu item activated // Postcondition: Dialog box opens and allows you to choose file from directory private void openToolStripMenuItem_Click(object sender, EventArgs e) { // create and show dialog box enabling user to open file DialogResult result; // result of OpenFileDialog string fileName; // name of file containing data using (OpenFileDialog chooseFile = new OpenFileDialog()) // making chooseFile relate to open file dialog { result = chooseFile.ShowDialog(); // result equal to showing dialog fileName = chooseFile.FileName; // get specified name } // end using // ensure that user clicked "OK" if (result == DialogResult.OK) { // show error if user specified invalid file if (fileName == string.Empty) { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { // create FileStream to obtain read access to file input = new FileStream( fileName, FileMode.Open, FileAccess.Read); // deserialize upv // get upv for input upv = (UserParcelView)reader.Deserialize(input); } // end try // handle exception when there are no upvs that are serializable in file catch (System.IO.IOException) { // notify user if file could not be opened MessageBox.Show("Error opening file. Close the Application and Run it again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (SerializationException) { input.Close(); // close FileStream // notify user could not open file MessageBox.Show("File not Serializable", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information); } // end catch } } }
// Precondition: None // Postcondition: Program exits public Prog2Form() { InitializeComponent(); //Create an instance of the singleton UserParcelView = new UserParcelView(); //Lets add some fake data to the singleton pseudoData(); //Output all the data outputAddressesAndLetters(); }
//event handler for the open button //preconditions: File, Open buttone clicked //postconditions: Displays interface to open a file private void openToolStripMenuItem_Click(object sender, EventArgs e) { //dialog result to hold result of open dialog DialogResult openFileResult; //string to hold open location string openFileName; using (OpenFileDialog fileOpen = new OpenFileDialog()) { //set results variable to hold outcome openFileResult = fileOpen.ShowDialog(); //set open path to result openFileName = fileOpen.FileName; //try block to catch various errors in file process try { //first make sure user has selected to open file if (openFileResult == DialogResult.OK) { //next check that file name is valid if (!string.IsNullOrWhiteSpace(openFileName)) { //set filestream to read stored upv upvStream = new FileStream(openFileName, FileMode.Open, FileAccess.Read); //deserialize stored information upv = (UserParcelView)formatter.Deserialize(upvStream); } else { MessageBox.Show("Invalid File name entered", "File open error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } //catch blocks for potential errors; catch (SerializationException) { MessageBox.Show("Invalid data in file, or bad data read", "File open error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (IOException) { MessageBox.Show("Error opening file", "File open error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { //close filestream upvStream?.Close(); } } }
public UserParcelView upv; //The UserParcelView // Precondition: None // Postcondition: The form's GUI is prepared for display. public Prog2Form() { InitializeComponent(); upv = new UserParcelView(); //Test Data upv.AddAddress("John Smith", "123 Any St.", "Apt. 45", "Louisville", "KY", 40202); // Test Address 1 upv.AddAddress("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 upv.AddAddress("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 upv.AddAddress("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 upv.AddAddress("Billy Walsh", "123 Hollywood Lane", "Pasadena", "CA", 10100); // Test Address 5 upv.AddAddress("Vincent Chase", "456 Rich Road", "Los Angeles", "CA", 75432); // Test Address 6 upv.AddAddress("Eric Murphy", "111 Short Street", "Benton", "KY", 42025); // Test Address 7 upv.AddAddress("Ari Gold", "414 Fairview Street", "Apt. 12", "Paducah", "KY", 83325); // Test Address 8 // Letter test objects upv.AddLetter(upv.AddressAt(0), upv.AddressAt(1), 3.95M); upv.AddLetter(upv.AddressAt(2), upv.AddressAt(3), 4.25M); // Ground Package test objects upv.AddGroundPackage(upv.AddressAt(4), upv.AddressAt(5), 14, 10, 5, 12.5); upv.AddGroundPackage(upv.AddressAt(6), upv.AddressAt(7), 20, 7, 4, 20); // Next Day Air Package test objects upv.AddNextDayAirPackage(upv.AddressAt(0), upv.AddressAt(2), 25, 15, 15, 85, 7.50M); upv.AddNextDayAirPackage(upv.AddressAt(2), upv.AddressAt(4), 11, 22, 9, 70, 7.38M); upv.AddNextDayAirPackage(upv.AddressAt(1), upv.AddressAt(6), 10.5, 20, 11, 75, 7.38M); // Two Day Air Package test objects upv.AddTwoDayAirPackage(upv.AddressAt(6), upv.AddressAt(3), 46.5, 39.5, 28.0, 80.5, TwoDayAirPackage.Delivery.Saver); upv.AddTwoDayAirPackage(upv.AddressAt(7), upv.AddressAt(5), 46.5, 39.5, 28.0, 80.5, TwoDayAirPackage.Delivery.Early); upv.AddTwoDayAirPackage(upv.AddressAt(1), upv.AddressAt(0), 46.5, 39.5, 28.0, 80.5, TwoDayAirPackage.Delivery.Saver); }
// Precondition: File, Open menu item activated // Postcondition: File chosen by user is opened and // its data is stored in the upv private void openToolStripMenuItem_Click(object sender, EventArgs e) { // create and show dialog box enabling user to open file DialogResult result; // result of OpenFileDialog string fileName; // name of file containing data using (OpenFileDialog fileChooser = new OpenFileDialog()) { // retrieve the result of the dialog box result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; // Holds the selected file name } // ensure that user clicked "OK" if (result == DialogResult.OK) { // show error if user specified invalid file if (fileName == string.Empty) { MessageBox.Show("Enter a File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { // create FileStream to obtain read access to file inputUPV = new FileStream(fileName, FileMode.Open, FileAccess.Read); upv = (UserParcelView)formatter.Deserialize(inputUPV); } catch (IOException) { // notify user if file could not be opened MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (SerializationException) { // notify user if no RecordSerializables in file MessageBox.Show("No more records in file", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { inputUPV.Close(); } } } }
//Precondtion:NONE //Postcondtion: Adds the Address names for the origin and destination comboboxes public LettersForm(UserParcelView upv) { InitializeComponent(); // Save reference to user parcel view userParcelView = upv; // Add all our names from addresses to the comboboxes foreach (var address in userParcelView.addresses) { originComboBox.Items.Add(address.Name); destinComboBox.Items.Add(address.Name); } }
// Precondition: File, Open menu item activated // Postcondition: User can select data file to open private void openToolStripMenuItem_Click(object sender, EventArgs e) { // create and show dialog box enabling user to open file DialogResult result; // result of OpenFileDialog string fileName; // name of file containing data using (OpenFileDialog fileChooser = new OpenFileDialog()) { result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; // get specified name } // ensure that user clicked "OK" if (result == DialogResult.OK) { // show error if user specified invalid file if (string.IsNullOrEmpty(fileName)) { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { // create FileStream to obtain read access to file input = new FileStream( fileName, FileMode.Open, FileAccess.Read); } } // deserialize and store data try { // get next UPV in file UserParcelView thisUPV = (UserParcelView)reader.Deserialize(input); upv = thisUPV; } catch (SerializationException) { input?.Close(); // close FileStream // notify user if no items in file MessageBox.Show("No more records in file", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private UserParcelView upv; //UserParcelView //Precondition: None //Postcondition: GUI form is created // Creates address form and letter form // if the user selects those options from // the menu. // Data collected from this form is sent to // the UserParcelView class to be created // into lists. public Prog2Form() { //Precondition: None //Postcondition: Form is created InitializeComponent(); //Creating an object of the UserParcelView upv = new UserParcelView(); //Test Data: Addresses upv.AddAddress(" John Smith ", " 123 Any St. ", " Apt. 45 ", " Louisville ", " KY ", 40202); // Test Address 1 upv.AddAddress("Jane Doe", "987 Main St.", "Beverly Hills", "CA", 90210); // Test Address 2 upv.AddAddress("James Kirk", "654 Roddenberry Way", "Suite 321", "El Paso", "TX", 79901); // Test Address 3 upv.AddAddress("John Crichton", "678 Pau Place", "Apt. 7", "Portland", "ME", 04101); // Test Address 4 upv.AddAddress("Charlotte Jones", "123 Maple St. ", "Clarksville", "IN", 47129); // Test Address 5 upv.AddAddress("Kayla Kay", "989 Chipp Avenue", "Apt. 2", "Salem", "IN", 52143); // Test Address 6 upv.AddAddress("Hunter Ansleigh", "554 Herrick Way", "Jeffersonville", "IN", 47130); // Test Address 7 upv.AddAddress("Mckenzie Caldwell", "2201 Morgan Avenue", "Charlestown", "KY", 42450); // Test Address 8 //Test Data: Letters upv.AddLetter(upv.AddressAt(0), upv.AddressAt(1), 3.95M); upv.AddLetter(upv.AddressAt(3), upv.AddressAt(2), 2.05M); //Test Data: GroundPackages upv.AddGroundPackage(upv.AddressAt(2), upv.AddressAt(3), 14, 10, 5, 12.5); upv.AddGroundPackage(upv.AddressAt(1), upv.AddressAt(5), 22, 15, 16, 28); //Test Data: NextDayAirPackages upv.AddNextDayAirPackage(upv.AddressAt(5), upv.AddressAt(0), 25, 15, 15, 85, 7.50M); upv.AddNextDayAirPackage(upv.AddressAt(1), upv.AddressAt(2), 50, 20, 15, 120, 9.75M); //Test Data: TwoDayAirPackages upv.AddTwoDayAirPackage(upv.AddressAt(3), upv.AddressAt(2), 46.5, 39.5, 28.0, 80.5, TwoDayAirPackage.Delivery.Saver); upv.AddTwoDayAirPackage(upv.AddressAt(7), upv.AddressAt(6), 20.75, 16.20, 20.50, 120.75, TwoDayAirPackage.Delivery.Early); }
// Precondition: none // Postcondition: Opens a file to use with program private void openToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult results; // The result of showing form as dialog string fileName; // The string to hold the file name //using block to select file using (OpenFileDialog fileChooser = new OpenFileDialog()) { fileChooser.CheckFileExists = false; results = fileChooser.ShowDialog(); fileName = fileChooser.FileName; } if (results == DialogResult.OK) // If OK, attempt to deserialize opened file { //if null or empty display error message if (string.IsNullOrEmpty(fileName)) { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { //try to read and deserialize info from file try { upvFile = new FileStream(fileName, FileMode.Open, FileAccess.Read); upv = (UserParcelView)formatter.Deserialize(upvFile); } catch (IOException) { MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (SerializationException) { MessageBox.Show("Error reading from file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { upvFile.Close(); } } } }
// Precondition: File, Open menu item is activated // Postcondition: Opens a sequential-access file using deserialization private void openButton_Click(object sender, EventArgs e) { DialogResult result; // Store result string fileName; // Name of file containing data using (OpenFileDialog fileOpen = new OpenFileDialog()) { result = fileOpen.ShowDialog(); fileName = fileOpen.FileName; // Get specified name } if (result == DialogResult.OK) // Ensure user clicked "OK" { if (string.IsNullOrEmpty(fileName)) // Is file valid? { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { input = new FileStream(fileName, FileMode.Open, FileAccess.Read); // Create FileStream to obtain read access to file upv = (UserParcelView)formatter.Deserialize(input); // Get next upv object available in file } // Can file be opened? catch (IOException) { MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Is file the right file type? catch (SerializationException) { MessageBox.Show("Wrong file type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
//Precondition: File, open menu item activated. //Postcondition: A dialog box to open a file appears. private void openToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result; //Create and show dialog box. string fileName; //Name of file containing data. using (OpenFileDialog fileChooser = new OpenFileDialog()) { result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; //Get file name. } //Ensure user clicked OK. if (result == DialogResult.OK) { if (string.IsNullOrEmpty(fileName)) { MessageBox.Show("Invalid file name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { input = new FileStream(fileName, FileMode.Open, FileAccess.Read); upv = (UserParcelView)reader.Deserialize(input); } catch (IOException) { MessageBox.Show("Error reading from file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (SerializationException) { MessageBox.Show("Error reading from file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (input != null) { input.Close(); } } } } }
// Precondition: File, open menu item selected // Postcondition: opens a previously saved file, Deserializes it and reads the contents to the form private void openToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result; // creates and shows a dialog box that allows users to open files string fileName; //declares a string variable to hold the name of the file that contains data using (OpenFileDialog fileChooser = new OpenFileDialog()) { result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; // returns the specified file name } if (result == DialogResult.OK) // makes sure the user pressed OK { if (fileName == string.Empty) // if user selected an invalid file // displays an error message { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { // creates a filestream to obtain rea access to file input = new FileStream(fileName, FileMode.Open, FileAccess.Read); upv = (UserParcelView)reader.Deserialize(input); // gets next UserParcelView available in file } catch (IOException) // handles the exception if there is a problem opening the file { // lets the user know if the file could not be opened MessageBox.Show("error Opening the File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }// handles exception when there are no Serializables in the file catch (SerializationException) { // lets the user know if there are no Serializables in the file MessageBox.Show("No more records in File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
// Precondition: File, Open menu item activated // Postcondition: File is deserialized and contents inserted into upv object private void openToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result; // Variable to hold result of DialogResult string fileName; // Variable of type string to hole file name using (OpenFileDialog fileChooser = new OpenFileDialog()) { result = fileChooser.ShowDialog(); // Sets result = to ShowDialog fileName = fileChooser.FileName; // get specified file name } if (result == DialogResult.OK) // Make sure file name is acceptable { if (string.IsNullOrEmpty(fileName)) { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // Displays message if error occurs } else { input = new FileStream(fileName, FileMode.Open, FileAccess.Read); // variable input is assigned contents of file } } if (result == DialogResult.Cancel) // Cancel button clicked returns to application { return; } try { upv = (UserParcelView)formatter.Deserialize(input); // Assigns deserialized input to upv object } catch (SerializationException) // Catches Serialization exception { MessageBox.Show("No more records in file", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information); // Gives message when error exception is caught } finally // ensures close always occurs { input?.Close(); // Closes the operation } }
//precondition: the open toolstrip menu item is clicked //postcondition: the selected file is loaded into the program private void openToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result; // stores the DialogResult string fileName; // name of file containing data //opens the filedialog using (OpenFileDialog fileChooser = new OpenFileDialog()) { result = fileChooser.ShowDialog(); //gets the result fileName = fileChooser.FileName; // get specified name } //if the result was ok, tries to load the upv object with the file. if (result == DialogResult.OK) { try { // create FileStream to obtain read access to file input = new FileStream( fileName, FileMode.Open, FileAccess.Read); upv = (UserParcelView)reader.Deserialize(input); //loads the upv object with the input } catch (IOException) //catches IOException error { // notify user if file could not be saved MessageBox.Show("Error saving file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (SerializationException) //catches SerializationException error { MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally //closes the filestream if no errors occur { input?.Close(); // close FileStream } } }
private void openAsToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result; //result of dialog box string fileName; //file name to save data using (OpenFileDialog fileChooser = new OpenFileDialog()) { result = fileChooser.ShowDialog(); fileName = fileChooser.FileName; //get specified name } //user clicked OK if (result == DialogResult.OK) { //show error if invalid file if (fileName == string.Empty) { MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { try { //create FileStream to obtain read access to file input = new FileStream(fileName, FileMode.Open, FileAccess.Read); upv = (UserParcelView)reader.Deserialize(input); //upv is set to deserialized data from file } catch (IOException) { MessageBox.Show("Error Reading From File", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (SerializationException) { //notify if error occurs in serialization MessageBox.Show("Error Reading From File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }