private Animal CreateNewAnimalFromDatabas(string id, string categori, string name, string age, string gender, string info) { Animal newAnimalFromDatabas = new Animal(); newAnimalFromDatabas.ID = new Guid(id); newAnimalFromDatabas.CategoryType = (CategoryType)System.Enum.Parse(typeof(CategoryType), categori); newAnimalFromDatabas.Name = name; newAnimalFromDatabas.Age = Convert.ToInt32(age); newAnimalFromDatabas.GenderType = (GenderType)System.Enum.Parse(typeof(GenderType), gender); if (newAnimalFromDatabas.CategoryType == CategoryType.Mammal) { var extraInfo = DataAccess.LoadExtraAnimalInfo(id); foreach (DataRow row in extraInfo.Rows) { Mammal newMammal = new Mammal(); var teeth = row["teeth"]; var quarantine = row["quarantine"]; MammalFactory mammalFactory = new MammalFactory(); newAnimalFromDatabas.GetAnimalSpecificData += String.IsNullOrEmpty(info) ? newAnimalFromDatabas.GetAnimalSpecificData = "" : "Important info: " + info; newMammal = mammalFactory.AddNewSpecifications(newMammal, Convert.ToInt32(quarantine), true, Convert.ToInt32(teeth)); newAnimalFromDatabas.GetAnimalSpecificData += newMammal.AnimalSpecificData(); } } else { newAnimalFromDatabas.GetAnimalSpecificData += String.IsNullOrEmpty(info) ? newAnimalFromDatabas.GetAnimalSpecificData = "" : "Important info: " + info; } return(newAnimalFromDatabas); }
public static Mammal CreateMammal(MammalSpecies species) { Mammal animalObject = null; try { switch (species) { case MammalSpecies.Cat: animalObject = new Cat(); break; case MammalSpecies.Dog: animalObject = new Dog(); break; default: Debug.Assert(false, "Not implemented yet"); break; } } catch (Exception ex) { ex.Message.ToString(); } finally { animalObject.Category = Category.Mammal; } return(animalObject); }
//Add a new object to the list - overloaded function public int AddProduct(Mammal prod) { if (prod != null) { //TODO: prod.ID = SetNewID(); m_productRegistry.Add(prod); } return(m_productRegistry.Count - 1); }
//Change the values of the object at the position=index in the list public bool ChangeProduct(Mammal prod, int index) { bool ok = false; if ((prod != null) && CheckIndex(index)) { m_productRegistry[index] = prod; ok = true; } return(ok); }
public Mammal AddNewSpecifications(Mammal animalObj, int daysOfQuarantine, bool underQuarantine, int numberOfteeth) { animalObj.DagInQuantine = daysOfQuarantine; animalObj.AnimalIsUnderQuarantine = underQuarantine; animalObj.NumberOFTeeth = numberOfteeth; StringBuilder builder = new StringBuilder(); if (underQuarantine) { animalObj.GetAnimalSpecificData += String.Format(" To be quarantine in {0} day(s) \r\n", animalObj.DagInQuantine); } animalObj.GetAnimalSpecificData += String.Format("The animals has {0} teeth.\r\n", animalObj.NumberOFTeeth); return(animalObj); }
public Animal CreateMammal(MammalSpecies mammalSpecies, int daysOfQuarantine, bool underQuarantine, int numberOfteeth) { Mammal animalObj = null; switch (mammalSpecies) { case MammalSpecies.Bear: animalObj = new Bear(); break; case MammalSpecies.Cat: animalObj = new Cat(); break; case MammalSpecies.Deer: animalObj = new Deer(); break; case MammalSpecies.Lion: animalObj = new Lion(); break; case MammalSpecies.Dog: animalObj = new Dog(); break; case MammalSpecies.Horse: animalObj = new Horse(); break; case MammalSpecies.Panda: animalObj = new Panda(); break; case MammalSpecies.Wolf: animalObj = new Wolf(); break; default: Debug.Assert(false, "To be completed!"); break; } animalObj.CategoryType = CategoryType.Mammal; animalObj = AddNewSpecifications(animalObj, daysOfQuarantine, underQuarantine, numberOfteeth); return(animalObj); //return the created animal object. }
}//WriteToTextFile //Retrieve data from a textfile to arraylist //As an alternative, you can use TextReader //1. open the file //2. get data from file //3. close file public bool ReadFromTextFile(string fileName, out string errorMsg) { bool ok = false; StreamReader reader = null; errorMsg = string.Empty; try { //1. File must exit - otherwise exception is thrown reader = new StreamReader(fileName, Encoding.UTF8); while (!reader.EndOfStream) //read to end of file { Mammal prod = new Mammal(); //2. get the values from a row in the file in the same ordning there were written string strRowData = reader.ReadLine(); //read the whole row string[] strValues = strRowData.Split('\t'); //tab separated prod.Name = strValues[0].Trim(); //delete spaces //TODO: //prod.Age = Convert.ToDouble(strValues[1].Trim()); //prod. = Convert.ToInt32(strValues[2].Trim()); //m_productRegistry.Add(Mammal); } ok = true; } catch (Exception e) { errorMsg = "WritetoTextFile: " + e.Message; } finally //always performed even when no exception is thrown { //3. Close the file reader.Close(); } return(ok); }//ReadFrom..
//Write data from arrayList to an xml file //1. open the file //2. write data to file //3. close file public bool WriteToTextFile(string fileName, out string errorMsg) { bool ok = false; StreamWriter writer = null; errorMsg = string.Empty; try { fileName = FileUtility.ChangeFileExtension(fileName, "txt"); //1. open the file for writing. If the file exist, it will be emptied first, //if not the file will be created bool append = false; writer = new StreamWriter(fileName, append, Encoding.UTF8); for (int i = 0; i < m_productRegistry.Count; i++) { Mammal prod = (Mammal)m_productRegistry[i]; //2. write the values to the file with tab-separated //Note: writer.WriterLine works in the same way as Console.WriteLine //Save data and separate them by a tab //TODO: //writer.WriteLine("{0}\t{1}\t{2}", prod.Name, prod.Price, prod.Count); } writer.Flush(); //Empty the buffer to file (immediately) ok = true; } catch (Exception e) { errorMsg = "WritetoTextFile: " + e.Message; } finally //always performed even when no exception is thrown { //3. Close the file writer.Close(); } return(ok); }//WriteToTextFile
private void saveDataToolStripMenuItem_Click(object sender, EventArgs e) { string sql; for (int i = 0; i < listViewListOfRegisteredAnimals.Items.Count; i++) { string name; int age = 0; string category; string gender = ""; string info = ""; Guid id = new Guid(listViewListOfRegisteredAnimals.Items[i].SubItems[1].Text); if (!CheckIfAnimalExists(id)) { category = listViewListOfRegisteredAnimals.Items[i].SubItems[2].Text; name = listViewListOfRegisteredAnimals.Items[i].SubItems[3].Text; age = Convert.ToInt32(listViewListOfRegisteredAnimals.Items[i].SubItems[4].Text); gender = listViewListOfRegisteredAnimals.Items[i].SubItems[5].Text; sql = ""; sql = "INSERT INTO Animal (id,name,age,categori, gender, info)" + " VALUES ('" + id + "','" + name + "','" + age + "','" + category + "','" + gender + "','" + info + "')"; DataAccess.SaveAnimalData(sql); foreach (Animal animal in animalList) { if (animal.CategoryType == CategoryType.Mammal && animal.ID == id) { Mammal mammal = animal as Mammal; sql = "INSERT INTO Mammal (id_fk,teeth,quarantine)" + " VALUES ('" + animal.ID + "','" + mammal.NumberOFTeeth + "','" + mammal.DagInQuantine + "')"; DataAccess.SaveAnimalData(sql); } } } } }