//////////////////////////////////////////////////////////// // Saves a Unit & Point Of Contact to the Unit Information // file. Also used for editing a unit which follows // the same save/write methods private void btnSavePointOfContact_Click(object sender, EventArgs e) { _validField = _fieldValidation(txtFName.Text, txtLName.Text, txtEmail.Text, txtPhone.Text); if (_validField) { if (_edit) { PointOfContact tempContact = new PointOfContact(_contacts[_index].Uic, _contacts[_index].Firstname, _contacts[_index].Lastname, _contacts[_index].Email, _contacts[_index].Phone, _contacts[_index].Station); PointOfContact poc = new PointOfContact(txtUIC.Text, txtFName.Text, txtLName.Text, txtEmail.Text, txtPhone.Text, txtStation.Text); _rdsDriver.EditUnit(poc, tempContact); _rdsDriver.WriteUnits(); //Used to catch Index Out of Range Exception try { if (_unit.Count > _index || _index != 0) { txtUnit.Text = _unit[_index].Unitname; txtUIC.Text = _unit[_index].Uic; SetTextFields(); _edit = false; MakeFieldsReadOnly(); } } catch (Exception indException) { MessageBox.Show(indException.ToString()); } } else { bool isdupe = _rdsDriver.CreateUnitProfile(txtUIC.Text, txtUnit.Text, txtFName.Text, txtLName.Text, txtEmail.Text, txtPhone.Text, txtStation.Text); if (isdupe) { MessageBox.Show(@"Contact already exist"); } else { MessageBox.Show(@"Contact Saved Successfully"); } } ClearTxtFeilds(); btnNext.Enabled = false; btnDelete.Visible = false; } else { MessageBox.Show(@"Please enter values in required fields"); } }
//********************************************************************* // AddContact Function // This Function Creates a PointOfContact and returns it //********************************************************************* public PointOfContact AddContact(string uic, string firstName, string lastName, string email, string phone, string station) { PointOfContact poc = new PointOfContact(uic, firstName, lastName, email, phone, station); return(poc); }
public void CreateSourceObjects() { ////////////////////////////////////// //Create Unit and Unit Contact Objects try { StreamReader file = new StreamReader(@"UnitInformation.csv"); _unitInfoHeaders = file.ReadLine(); while (!file.EndOfStream) { var line = file.ReadLine(); string[] row = line.Split(','); Unit unit = new Unit(row[1], row[0]); PointOfContact contact = new PointOfContact(row[1], row[2], row[3], row[4], row[5], row[6]); Units.Add(unit); Contacts.Add(contact); } file.Close(); } catch (FileNotFoundException ex) { MessageBox.Show("UnitInformation.csv does not exisit", "File Not Found"); } _uniqueUnitUic = Units.Select(u => u.Uic).Distinct().ToList(); /////////////////////////////////////// //Create CargoObject try { StreamReader cargoFile = new StreamReader(@"AddCargoLog.csv"); _cargoLogHeaders = cargoFile.ReadLine(); while (!cargoFile.EndOfStream) { var line = cargoFile.ReadLine(); string[] row = line.Split(','); string uic = GetUic(row[9]); CargoLogObject cargoObj = new CargoLogObject(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], uic, row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]); CargoLogObjects.Add(cargoObj); } } catch (FileNotFoundException e) { MessageBox.Show("AddCargoLog.csv does not exist", "File Not Found"); } }
//********************************************************************* // DeleteContact // This Function Deletes a Contact associated with a Unit //********************************************************************* public List <PointOfContact> DeleteContact(PointOfContact contact, List <PointOfContact> pList) { for (int i = 0; i < pList.Count; i++) { if (contact.Uic == pList[i].Uic && contact.Firstname == pList[i].Firstname && contact.Lastname == pList[i].Lastname && contact.Email == pList[i].Email) { pList.Remove(contact); } } return(pList); }
//********************************************************************* // Loads unit information if file exist //********************************************************************* public void LoadUnits() { try { if (File.Exists(@"UnitInformation.csv")) { var file = new StreamReader(@"UnitInformation.csv"); while (!file.EndOfStream) { string line = file.ReadLine(); if (line == null) { continue; } string[] row = line.Split(','); Unit u = new Unit(row[1], row[0]); _uList.Add(u); PointOfContact pointOfContact = new PointOfContact(row[1], row[2], row[3], row[4], row[5], row[6]); _pList.Add(pointOfContact); } file.Close(); } else { var file = new StreamWriter(@"UnitInformation.csv"); file.WriteLine("Unit" + "," + "UIC" + "," + "Contact First Name" + "," + "Contact Last Name" + "," + "Contact Email" + "," + "Contact Phone" + "," + "Station"); Unit u = new Unit("UIC", "Unit"); _uList.Add(u); PointOfContact pointOfContact = new PointOfContact ("UIC", "Contact First Name", "Contact Last Name", "Contact Email", "Contact Phone", "Station"); _pList.Add(pointOfContact); file.Close(); } } catch (IOException) { MessageBox.Show(@"File that contains Unit Information is being used by another process, Units Not Loaded."); } }
//***************************************************************************** // EditUnit Function // This function takes in a 2 PointOfContact Objects and a List // of the PointOfContact Objects to be used to perform an Update on // the Point of Contact object. Then it returns the List with // updated object included //***************************************************************************** public List <PointOfContact> EditUnit(PointOfContact p, PointOfContact temp, List <PointOfContact> pList) { foreach (PointOfContact t in pList) { if (temp.Firstname == t.Firstname && temp.Lastname == t.Lastname && temp.Email == t.Email && temp.Phone == t.Phone) { t.Firstname = p.Firstname; t.Lastname = p.Lastname; t.Email = p.Email; t.Phone = p.Phone; t.Station = p.Station; } } return(pList); }
//********************************************************************* // Function called to delete contact from Unit Profile Form //********************************************************************* public List <Unit> DeleteContact(PointOfContact contact) { int index = 0; Unit u = new Unit(); _pList = u.DeleteContact(contact, _pList); for (int i = 0; i < _uList.Count; i++) { if (contact.Uic == _uList[i].Uic) { index = i; } } _uList.RemoveAt(index); return(_uList); }
//********************************************************************* // SearchContact // This Function Searches for and returns the list of // PointOfContacts associated with a Unit. //********************************************************************* public PointOfContact[] SearchContact(List <Unit> unit, List <PointOfContact> lcontacts) { PointOfContact[] contacts = new PointOfContact[unit.Count]; List <PointOfContact> tempList = new List <PointOfContact>(lcontacts); for (int i = 0; i < unit.Count; i++) { for (int u = 0; u < tempList.Count; u++) { if (unit[i].Uic == tempList[u].Uic) { contacts[i] = tempList[u]; tempList.RemoveAt(u); u = tempList.Count; } } } return(contacts); }
//********************************************************************* // Function to Create a Unit Profile (Unit and Point of Contact) //********************************************************************* public bool CreateUnitProfile(string uic, string unitName, string firstName, string lastName, string email, string phone, string station) { bool dupecontact = CheckContactDupe(uic, firstName, lastName, email, phone); if (!dupecontact) { User user = new User(); Unit unit = user.CreateUnit(uic, unitName); _uList.Add(unit); PointOfContact poc = unit.AddContact(uic, firstName, lastName, email, phone, station); _pList.Add(poc); WriteUnits(); return(false); } return(true); }
//********************************************************************* // Function used to Edit a unit contact from the Unit Profile from //********************************************************************* public void EditUnit(PointOfContact p, PointOfContact temp) { _pList = _users.EditUnit(p, temp, _pList); }