Exemplo n.º 1
0
        public int Water(int id)
        {
            Plants = new List <Plant>();

            // fill the PlantTableAdapter with the data in the plant database table
            PlantTableAdapter plantAdapter = new PlantTableAdapter();

            WaterMangoAppDataSet.PlantDataTable plantTable = plantAdapter.GetData();

            // Select the requested Plant object
            WaterMangoAppDataSet.PlantRow plantRow = plantTable.Where(x => x.Id == id).First();

            // Make sure it is not null
            if (plantRow != null)
            {
                // Check that the plant can be watered
                if (Plant.CanPlantBeWatered(plantRow.LastWatered))
                {
                    // iterate the number of seconds the plant has been watered by 1
                    plantRow.SecondsWatered += 1;

                    // if the plant has been watered for 10 seconds or more
                    if (plantRow.SecondsWatered > 9)
                    {
                        // reset the time the plant has been watered and then set the last watered time to Now
                        plantRow.SecondsWatered = 0;
                        plantRow.LastWatered    = DateTime.Now;
                    }

                    // update the plant table in the database
                    plantAdapter.Update(plantTable);

                    // return the number of seconds this plant has been watered
                    return(plantRow.SecondsWatered);
                }
                else
                {
                    return(-1); // Plant cannot be watered within 30 seconds
                }
            }
            else
            {
                return(0);
            }
        }
Exemplo n.º 2
0
        public Plant Get(int id)
        {
            Plants = new List <Plant>();

            // fill the PlantTableAdapter with the data in the plant database table
            PlantTableAdapter plantAdapter = new PlantTableAdapter();

            WaterMangoAppDataSet.PlantDataTable plantTable = plantAdapter.GetData();

            // iterate through the table rows and add the WaterMango.Plant objects to the Plants list
            foreach (WaterMangoAppDataSet.PlantRow row in plantTable.Rows)
            {
                Plants.Add(new Plant(row.Id, row.Name, row.LastWatered, row.SecondsWatered));
            }

            // return the Plant with the currently requested Id
            return(Plants.Where(x => x.Id == id).First());
        }
Exemplo n.º 3
0
        public IEnumerable <Plant> Get()
        {
            Plants = new List <Plant>();

            // fill the PlantTableAdapter with the data in the plant database table
            PlantTableAdapter plantAdapter = new PlantTableAdapter();

            WaterMangoAppDataSet.PlantDataTable plantTable = plantAdapter.GetData();

            // iterate through the table rows and add the WaterMango.Plant objects to the Plants list
            foreach (WaterMangoAppDataSet.PlantRow row in plantTable.Rows)
            {
                Plants.Add(new Plant(row.Id, row.Name, row.LastWatered, row.SecondsWatered));
            }

            // return the Plants list as an array
            return(Plants.ToArray());
        }