Example #1
0
        // #4 Add Drome Method
        private void buttonAddDrone_Click(object sender, EventArgs e)
        {
            try
            {
                if (droneCounter < max) // check array capacity
                {
                    if (!(string.IsNullOrEmpty(textBoxModel.Text)) &&
                        !(string.IsNullOrEmpty(textBoxEngConfig.Text)) &&
                        !(string.IsNullOrEmpty(textBoxRange.Text)) &&
                        !(string.IsNullOrEmpty(textBoxAccessories.Text)) &&
                        !(string.IsNullOrEmpty(textBoxPrice.Text)) &&
                        !(string.IsNullOrEmpty(textBoxDate.Text)))
                    {
                        var ci      = new CultureInfo("en-AU");
                        var formats = new[] { "M-d-yyyy", "dd-MM-yyyy", "MM-dd-yyyy", "M.d.yyyy", "dd.MM.yyyy", "MM.dd.yyyy" }
                        .Union(ci.DateTimeFormat.GetAllDateTimePatterns()).ToArray();

                        //DateTime.ParseExact(textBoxDate.Text, formats, ci, DateTimeStyles.AssumeLocal);

                        drones[droneCounter] = new Drone(0, textBoxModel.Text, textBoxEngConfig.Text, textBoxRange.Text, textBoxAccessories.Text, int.Parse(textBoxPrice.Text), DateTime.ParseExact(textBoxDate.Text, formats, ci, DateTimeStyles.AssumeLocal));
                        droneCounter++;
                    }
                    else
                    {
                        MessageBox.Show("Data is incorrect or missing");
                    }
                }
                else
                {
                    MessageBox.Show("Your array is full");
                }
                clearBoxes();    // Method to clrear TextBox
                displayDrones(); // Method to display Drone array in the listBox
            } catch (Exception ex)
            {
                MessageBox.Show("Invalide input, Price - integer, Date - dd/mm/yyyy");
            }
        }
Example #2
0
        // #1 Load data Method
        private void loadData()
        {
            // Loading Drones
            try
            {
                int row = 0;
                droneCounter = 0;

                using (Stream stream = File.Open(droneFileName, FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    {
                        drones[droneCounter] = new Drone();
                        while (stream.Position < stream.Length)
                        {
                            if (row < 7)
                            {
                                string word = bin.Deserialize(stream).ToString();
                                switch (row)
                                {
                                case 0:
                                    drones[droneCounter].SerialNumber = int.Parse(word);
                                    row++;
                                    break;

                                case 1:
                                    drones[droneCounter].Model = word;
                                    row++;
                                    break;

                                case 2:
                                    drones[droneCounter].EngineConfiguration = word;
                                    row++;
                                    break;

                                case 3:
                                    drones[droneCounter].Range = word;
                                    row++;
                                    break;

                                case 4:
                                    drones[droneCounter].Accessories = word;
                                    row++;
                                    break;

                                case 5:
                                    drones[droneCounter].Price = int.Parse(word);
                                    row++;
                                    break;

                                case 6:
                                    drones[droneCounter].PurchaseDate = DateTime.Parse(word);
                                    row++;
                                    droneCounter++;
                                    drones[droneCounter] = new Drone();
                                    break;
                                }
                            }
                            else
                            {
                                row = 0;
                            }
                        }
                    }
                }
            }
            catch (IOException)
            {
                MessageBox.Show("cannot read Drones data");
            }
            // Loading Customers
            try
            {
                int row = 0;
                customerCounter = 0;

                using (Stream stream = File.Open(customerFileName, FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    {
                        customers[customerCounter] = new Customer();
                        while (stream.Position < stream.Length)
                        {
                            if (row < 4)
                            {
                                string word = bin.Deserialize(stream).ToString();
                                switch (row)
                                {
                                case 0:
                                    customers[customerCounter].CustomerID = int.Parse(word);
                                    row++;
                                    break;

                                case 1:
                                    customers[customerCounter].Name = word;
                                    row++;
                                    break;

                                case 2:
                                    customers[customerCounter].City = word;
                                    row++;
                                    break;

                                case 3:
                                    customers[customerCounter].Country = word;
                                    row++;
                                    customerCounter++;
                                    customers[customerCounter] = new Customer();
                                    break;
                                }
                            }
                            else
                            {
                                row = 0;
                            }
                        }
                    }
                }
            }
            catch (IOException)
            {
                MessageBox.Show("cannot read Customers data");
            }
            // Loading Transactions
            try
            {
                int row = 0;
                int col = 0;
                transCounter = 0;
                using (Stream stream = File.Open(transFileName, FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    {
                        while (stream.Position < stream.Length)
                        {
                            if (row < 3)
                            {
                                string word = bin.Deserialize(stream).ToString();
                                transactions[col, row] = int.Parse(word);
                                row++;
                            }
                            else
                            {
                                row = 0;
                                transCounter++;
                                col++;
                            }
                        }
                        transCounter++;
                    }
                }
            }
            catch (IOException)
            {
                MessageBox.Show("cannot read Transactions data");
            }
        }