Beispiel #1
0
        //Gets all animals and adds to dictionary
        public void get_animals()
        {
            //String with animal names in to help with iterating through the database tables
            string[] animalNamesArray = { "Cows", "Dogs", "Goats", "Sheep" };

            //
            for (int i = 0; i < animalNamesArray.Length; i++)
            {
                //Select query concatenated the table-name of the Database's animal tables to make
                //it easier to iterate through the tables when searching through all the rows
                String query = "SELECT * FROM " + animalNamesArray[i];

                //querying with the query using the conn string to the database
                OleDbCommand queryConn = new OleDbCommand(query, conn);

                using (OleDbDataReader reader = queryConn.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        //reads all of the values to strings first
                        //these are the attributes that ALL animals have in common
                        string ID        = reader["ID"].ToString();
                        string DailyCost = reader["Daily cost"].ToString();
                        string Weight    = reader["Weight"].ToString();
                        string AmtWater  = reader["Amount of water"].ToString();
                        string Color     = reader["Color"].ToString();
                        string Age       = reader["Age"].ToString();

                        //then converts them to their respective values
                        int    id        = Convert.ToInt32(ID);
                        double dailyCost = Convert.ToDouble(DailyCost);
                        double weight    = Convert.ToDouble(Weight);
                        double amtwater  = Convert.ToDouble(AmtWater);
                        string color     = Convert.ToString(Color);
                        int    age       = Convert.ToInt32(Age);

                        //0 = Cows - So we also need to retrieve the attributes unique to cows
                        if (i == 0)
                        {
                            string AmtMilk = reader["Amount of Milk"].ToString();
                            double amtmilk = Convert.ToDouble(AmtMilk);
                            string IsJersy = reader["Is Jersy"].ToString();
                            bool   isjersy = Convert.ToBoolean(IsJersy);

                            //we then determine whether the cow is a jersy cow or a regular cow and then, using all
                            //of the data from the variables above, create cow/jerseycow objects using them
                            if (isjersy == false)
                            {
                                animal = new Cow(id, amtwater, dailyCost, weight, age, color, amtmilk);
                                allAnimals.Add(id, animal);
                                cowIDs.Add(id);
                            }
                            else
                            {
                                animal = new JersyCow(id, amtwater, dailyCost, weight, age, color, amtmilk, isjersy);
                                allAnimals.Add(id, animal);
                                jersyCowIDs.Add(id);
                            }
                        }
                        //1 - Dogs. There aren't any attributes unique to dogs, so this is a short loop
                        else if (i == 1)
                        {
                            dogIDs.Add(id);
                            animal = new Dog(id, amtwater, dailyCost, weight, age, color);
                            allAnimals.Add(id, animal);
                        }
                        //2 = Goats
                        else if (i == 2)
                        {
                            goatIDs.Add(id);
                            string AmtMilk = reader["Amount of milk"].ToString();
                            double amtmilk = Convert.ToDouble(AmtMilk);
                            animal = new Goat(id, amtwater, dailyCost, weight, age, color, amtmilk);
                            allAnimals.Add(id, animal);
                        }
                        //Sheep catch-all-else: There will be no tables after '3', so there is no need
                        //for an 'else-if (i==3)'
                        else
                        {
                            sheepIDs.Add(id);
                            string AmtWool = reader["Amount of wool"].ToString();
                            double amtwool = Convert.ToDouble(AmtWool);
                            animal = new Sheep(id, amtwater, dailyCost, weight, age, color, amtwool);
                            allAnimals.Add(id, animal);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        //displayinfo for animal
        private static string displayInfo(int i, Dictionary <int, Animal> allAnimals)
        {
            Animal animal = allAnimals[i];

            return(animal.ShowInf());
        }