// Allows the Drone File to be created and store arrays once the closing of the program has been initiated. public void droneSaveFile() { if (File.Exists("drones.dat")) { using (BinaryWriter bw = new BinaryWriter(File.Open("drones.dat", FileMode.OpenOrCreate))) { droneNullPointer = 0; for (int i = 0; i < droneNullPointer + 1; i++) { if (droneArray[i] != null) { // Current drone array index set to I and written one by one in for loop. DroneClass a = droneArray[i]; bw.Write(a.gsSerialNumber); bw.Write(a.gsModel); bw.Write(a.gsEngineConfig); bw.Write(a.gsRange); bw.Write(a.gsAccessories); bw.Write(a.gsPrice); bw.Write(a.gsDate); droneNullPointer++; } else { break; } } } } }
// 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 droneDisplayBox_SelectedIndexChanged(object sender, EventArgs e) { try { clearDroneMethod(); // int index = droneDisplayBox.SelectedIndex; // Assings the Current index to a temp DroneClass Instance. DroneClass d = droneArray[droneDisplayBox.SelectedIndex]; // All values get put into text boxes in the Drone Section. droneSNTB.Text = d.gsSerialNumber; droneModelTB.Text = d.gsModel; droneEngineTB.Text = d.gsEngineConfig; droneRangeTB.Text = d.gsRange; droneAccTB.Text = d.gsAccessories; dronePriceTB.Text = d.gsPrice; dronePurchTB.Text = d.gsDate; // The Serial Number is added to the Transaction Section. transactionSNTB.Text = d.gsSerialNumber; } catch (IndexOutOfRangeException) { errorMessage.Items.Clear(); errorMessage.Text = "Out of Index Exception - Drone DisplayBox"; } }
// Drone Loading is not yet Implemented. public void droneLoadFile() { try { // Binery file is checked and opened. The information is read and added to the list. if (File.Exists("drones.dat")) { using (BinaryReader br = new BinaryReader(File.Open("drones.dat", FileMode.Open))) { //To avoid Null errors the fileLength is defined and assigned as an Integer. int fileLength = (int)br.BaseStream.Length / 7; //NullPointer set to zero --- VERY IMPORTANT droneNullPointer = 0; for (int i = 0; i < fileLength; i++) { DroneClass a = new DroneClass(); a.gsSerialNumber = br.ReadString(); a.gsModel = br.ReadString(); a.gsEngineConfig = br.ReadString(); a.gsRange = br.ReadString(); a.gsAccessories = br.ReadString(); a.gsPrice = br.ReadString(); a.gsDate = br.ReadString(); droneArray[droneNullPointer] = a; //To ensure clear viewing the arrays are loaded into a string which will be displayed in the box. droneDisplayBox.Items.Add(droneArray[i].gsSerialNumber + " - " + droneArray[i].gsEngineConfig + " - " + droneArray[i].gsPrice); //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. droneNullPointer++; } } } else if (!(File.Exists("drones.dat"))) { using (BinaryReader br = new BinaryReader(File.Open("drones.dat", FileMode.Create))) { errorMessage.Items.Clear(); errorMessage.Text = "Drone File 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 Drone Load File"; } }
//*************************** 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"; } }
// ***************************** DRONE FUNCTIONALITY ************************************************ // ***************************** DRONE FUNCTIONALITY ************************************************ // ***************************** DRONE FUNCTIONALITY ************************************************ // Add button to allow items within the Drone Text boxes to be loaded into the array and listbox. private void addDroneButton_Click(object sender, EventArgs e) { if (!(string.IsNullOrEmpty(droneSNTB.Text))) { string[] dataCheck = new string[7]; //Filling up Data Check Array for Testing. dataCheck[0] = droneSNTB.Text; dataCheck[1] = droneModelTB.Text; dataCheck[2] = droneEngineTB.Text; dataCheck[3] = droneRangeTB.Text; dataCheck[4] = droneAccTB.Text; dataCheck[5] = dronePriceTB.Text; dataCheck[6] = dronePurchTB.Text; // Uses DataCheck Array to make sure all the boxes are full. Question 4A Complete. for (int i = 0; i < dataCheck.Length; i++) { // Checks each box to see if any null values are present. if (dataCheck[i] == "") { // Question 4B Complete. Error Message if too many or too few boxes are complete. MessageBox.Show("Please Fill all the boxes in the Form."); droneArray[i] = null; clearDroneMethod(); break; } // Once checking is complete (Last Index) adds the Instance of the Drone Class to the array. else if (dataCheck[6] != "") { // Creates new Class Instance. 4C. DroneClass drone = new DroneClass(); // Object Filled up with values. 4C drone.gsSerialNumber = dataCheck[0]; drone.gsModel = dataCheck[1]; drone.gsEngineConfig = dataCheck[2]; drone.gsRange = dataCheck[3]; drone.gsAccessories = dataCheck[4]; drone.gsPrice = dataCheck[5]; drone.gsDate = dataCheck[6]; // Class Object Added to Array. 4C droneArray[droneNullPointer] = drone; } else { errorMessage.Items.Clear(); errorMessage.Text = "Add Drone Button Null Reference."; } } // Bubble Sort Method organises Array Based on Serial Number. 4D Complete. bubbleSort(); // Clear Happens before the Array is Displayed (After Sort) but the values are in the Array. 4E Complete. droneDisplayBox.Items.Clear(); try { if (!(string.IsNullOrEmpty(droneSNTB.Text))) { for (int j = 0; j <= droneNullPointer; j++) { // Using values from Drone Class Instance inside the J Index. Add 3 values to the list box. Question 4C + 4D Complete. droneDisplayBox.Items.Add(droneArray[j].gsSerialNumber + " - " + droneArray[j].gsEngineConfig + " - " + droneArray[j].gsPrice); } } else { } clearDroneMethod(); } catch (NullReferenceException) { errorMessage.Items.Clear(); errorMessage.Text = "Add Drone Button Null Reference / List Filling."; } // Null Pointer for drone Array. droneNullPointer++; } else { MessageBox.Show("Text box empty", "Please fill out all the text boxes."); } }