// Customer Arrays stored into binary file as program is closing down. public void customerSaveFile() { if (File.Exists("customers.dat")) { using (BinaryWriter bw = new BinaryWriter(File.Open("customers.dat", FileMode.OpenOrCreate))) { customerNullPointer = 0; for (int i = 0; i < customerNullPointer + 1; i++) { if (customerArray[i] != null) { // Class called from current array index and saved into binary data. Loops around for each index. CustomerClass a = customerArray[i]; bw.Write(a.gsCustomerID); bw.Write(a.gsCusName); bw.Write(a.gsCusCity); bw.Write(a.gsCusCountry); customerNullPointer++; } else { break; } } } } }
// Customer Loading not yet implemented. public void customerLoadFile() { try { // Binery file is checked and opened. The information is read and added to the list. if (File.Exists("customers.dat")) { using (BinaryReader br = new BinaryReader(File.Open("customers.dat", FileMode.Open))) { //To avoid Null errors the fileLength is defined and assigned as an Integer. int fileLength = (int)br.BaseStream.Length / 4; //NullPointer set to zero --- VERY IMPORTANT customerNullPointer = 0; for (int i = 0; i < fileLength; i++) { CustomerClass a = new CustomerClass(); a.gsCustomerID = br.ReadString(); a.gsCusName = br.ReadString(); a.gsCusCity = br.ReadString(); a.gsCusCountry = br.ReadString(); customerArray[customerNullPointer] = a; customerDisplayBox.Items.Add(customerArray[i].gsCustomerID + " - " + customerArray[i].gsCusName + " - " + customerArray[i].gsCusCity + " - " + customerArray[i].gsCusCountry); //To ensure clear viewing the arrays are loaded into a string which will be displayed in the box. //Nullpointer extends Array to avoid out of index errors.This is why it was set to zero before. //This program has no delete button so there was no way to reduce the array size for the load function. customerNullPointer++; } } } else if (!(File.Exists("customers.dat"))) { using (BinaryReader br = new BinaryReader(File.Open("customers.dat", FileMode.Create))) { errorMessage.Text = "Customer.dat does not currently exist."; return; } } else { MessageBox.Show("Error Loading File", "Check file is in the correct folder"); } } catch (EndOfStreamException) { errorMessage.Items.Clear(); errorMessage.Text = "End of Stream Exception - Customer Loader"; } }
// Method to all User to click onto listboxes to select data for later use. // Once Clicked all Revelevant Information is added to the Relevant Text Boxes. Question 6 Complete. private void customerDisplayBox_SelectedIndexChanged(object sender, EventArgs e) { try { // Assigns the Current index to a temp DroneClass Instance. CustomerClass c = customerArray[customerDisplayBox.SelectedIndex]; // All Values get pt into text boxes in the Customer Section. customerIDTB.Text = c.gsCustomerID; customerNameTB.Text = c.gsCusName; customerCityTB.Text = c.gsCusCity; customerCountryTB.Text = c.gsCusCountry; // Customer ID Number is added to the Transaction Section. transactionCusTB.Text = c.gsCustomerID; } catch (IndexOutOfRangeException) { errorMessage.Text = "Customer Display Box out of Index."; } }
//*************************** OTHER FUNCTIONALITY *************************************** //*************************** OTHER FUNCTIONALITY *************************************** //*************************** OTHER FUNCTIONALITY *************************************** // Basic Bubble sort to sort arrays. Both Customer and Drone arrays are sorted here. public void bubbleSort() { // To get a single method to take care of two different sorts I have used an if > else statement. (Drone sort + Customer Sort) // The text boxes will only be filled if they are about to add. // If the drone box is full. Do this. if (!(string.IsNullOrEmpty(droneSNTB.Text))) { try { for (int j = 0; j < droneNullPointer; j++) { for (int i = 0; i < droneNullPointer; i++) { if (!(string.IsNullOrEmpty(droneSNTB.Text))) { // Uses the serial number from the Drone Class to do the CompareTo Method. int target = droneArray[i].gsSerialNumber.CompareTo(droneArray[i + 1].gsSerialNumber); if (target > 0) { DroneClass temp = droneArray[i + 1]; droneArray[i + 1] = droneArray[i]; droneArray[i] = temp; } else { errorMessage.Items.Clear(); errorMessage.Text = "Null Error Handled at Bubble Sort"; //break; } } else { //Null Reference //break; } } } } catch (NullReferenceException) { errorMessage.Items.Clear(); errorMessage.Text = "Null Error Reference at Bubble Sort / Drone"; } catch (IndexOutOfRangeException) { errorMessage.Items.Clear(); errorMessage.Text = "Index out of Range Error at Bubble Sort / Drone"; } catch (Exception) { errorMessage.Items.Clear(); errorMessage.Text = "Unknown Exception at Bubble Sort / Drone"; } } else { //Null Reference } // If the Customer Text box is full. Do this. if (!(string.IsNullOrEmpty(customerIDTB.Text))) { try { for (int j = 0; j < customerNullPointer; j++) { for (int i = 0; i < customerNullPointer; i++) { if (!(string.IsNullOrEmpty(customerIDTB.Text))) { // Uses the serial Number from the Customer Class to do the CompareTo Method. int target = customerArray[i].gsCustomerID.CompareTo(customerArray[i + 1].gsCustomerID); if (target > 0) { CustomerClass temp = customerArray[i + 1]; customerArray[i + 1] = customerArray[i]; customerArray[i] = temp; } else { errorMessage.Items.Clear(); errorMessage.Text = "Null Error Handled at Bubble Sort"; } } else { } } } } catch (NullReferenceException) { errorMessage.Items.Clear(); errorMessage.Text = "Null Error Reference at Bubble Sort / Customer"; } catch (IndexOutOfRangeException) { errorMessage.Items.Clear(); errorMessage.Text = "Index out of Range Error at Bubble Sort / Customer"; } catch (Exception) { errorMessage.Items.Clear(); errorMessage.Text = "Unknown Exception at Bubble Sort / Customer"; } } // Error Catch for Null values. else { errorMessage.Items.Clear(); errorMessage.Text = "Unknown Exception at Bubble Sort / Customer"; } }
// ************************* CUSTOMER FUNCTIONALITY ********************************************* // ************************* CUSTOMER FUNCTIONALITY ********************************************* // ************************* CUSTOMER FUNCTIONALITY ********************************************* // Add button to allow items within the Customer Text boxes to be loaded into the array and listbox. private void addCustomerButton_Click(object sender, EventArgs e) { if (!(string.IsNullOrEmpty(customerIDTB.Text))) { string[] dataCheck = new string[4]; //Filling up Data Check Array for Testing. dataCheck[0] = customerIDTB.Text; dataCheck[1] = customerNameTB.Text; dataCheck[2] = customerCityTB.Text; dataCheck[3] = customerCountryTB.Text; // Uses DataCheck Array to make sure all the boxes are full. Question 5A Complete. for (int i = 0; i < dataCheck.Length; i++) { if (dataCheck[i] == "") { MessageBox.Show("Text Boxes are Empty", "Please Fill all the Boxes in the Form."); customerArray[i] = null; break; } else if (dataCheck[3] != "") { CustomerClass customer = new CustomerClass(); customer.gsCustomerID = dataCheck[0]; customer.gsCusName = dataCheck[1]; customer.gsCusCity = dataCheck[2]; customer.gsCusCountry = dataCheck[3]; customerArray[customerNullPointer] = customer; } else { errorMessage.Items.Clear(); errorMessage.Text = "Data Checking In progress"; } } try { bubbleSort(); customerDisplayBox.Items.Clear(); // Customer Array is Displayed in Text Box. if (!(string.IsNullOrEmpty(customerIDTB.Text))) { for (int j = 0; j <= customerNullPointer; j++) { customerDisplayBox.Items.Add(customerArray[j].gsCustomerID + " - " + customerArray[j].gsCusName + " - " + customerArray[j].gsCusCity + " - " + customerArray[j].gsCusCountry); } } else { } clearCustomerMethod(); } catch (NullReferenceException) { errorMessage.Items.Clear(); errorMessage.Text = "Null Reference at Add Customer Button."; } } // Else If Statement to give user option to put in a Default Customer. Question 5B Complete. else if (string.IsNullOrEmpty(droneSNTB.Text)) { // Diaglog box to allow a default customer to be made. DialogResult defaultCustomer = MessageBox.Show("Would you like to create a default customer?", "Please press Yes / No", MessageBoxButtons.YesNo, MessageBoxIcon.Information); // If clicked "YES". Default Customer will be Added to text boxes. Must click again to be added to Array. Question 5C Complete. if (defaultCustomer == DialogResult.Yes) { MessageBox.Show("Yes Button Pressed.", "Default Customer created"); MessageBox.Show("Not in Array Yet", "Please push Add Button again to add Default Customer to Array"); customerIDTB.Text = "Default"; customerNameTB.Text = "Default"; customerCityTB.Text = "Default"; customerCountryTB.Text = "Default"; return; } // If Clicked "NO". No Further Action Taken. Question 5D Complete. else if (defaultCustomer == DialogResult.No) { MessageBox.Show("No Button Pressed", "No further action taken."); } } // Error Message if boxes are missing inputs from User. else { MessageBox.Show("Text box empty", "Please fill out all the text boxes."); } // Array Sorted and Boxes are Cleared. Question 5F is complete. bubbleSort(); clearCustomerMethod(); customerNullPointer++; }