コード例 #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);
            }
        }