Exemplo n.º 1
0
        private void practitioner_most_button_Click(object sender, EventArgs e)
        {
            //Get all practitioners
            DataSet practitioners = SqlWorker.GetDataSet("Select * from Users Where type='Practitioner'");

            //Show error message and exit if there is no practitioners
            if (practitioners.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("There is no practitioners in the system!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int maxWorkHours = 0, id, numOfLecturers = 0;

            //Find max of work hours
            for (int i = 0; i < practitioners.Tables[0].Rows.Count; i++)
            {
                id = Convert.ToInt32(practitioners.Tables[0].Rows[i]["ID"].ToString());
                int tmpWorkHours = SqlWorker.getWorkHours(id, true, -1);
                if (tmpWorkHours > maxWorkHours)
                {
                    maxWorkHours = tmpWorkHours;
                }
            }
            //Show error message and exit if none of the practitioners is working
            if (maxWorkHours == 0)
            {
                MessageBox.Show("There is no practitioner that is working!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //Find the amount of practitioners with the maximum amount of work hours
            for (int i = 0; i < practitioners.Tables[0].Rows.Count; i++)
            {
                id = Convert.ToInt32(practitioners.Tables[0].Rows[i]["ID"].ToString());
                if (maxWorkHours == SqlWorker.getWorkHours(id, true, -1))
                {
                    numOfLecturers++;
                }
            }
            String msg;

            //Create the message
            if (numOfLecturers == 1)
            {
                msg = "The practitioner with the most hours is\n";
            }
            else
            {
                msg = "The practitioners with the most hours are\n";
            }
            //Add all practitioners to the message
            for (int i = 0; i < practitioners.Tables[0].Rows.Count; i++)
            {
                id = Convert.ToInt32(practitioners.Tables[0].Rows[i]["ID"].ToString());
                if (maxWorkHours == SqlWorker.getWorkHours(id, true, -1))
                {
                    msg += "Name: " + SqlWorker.getNameById(id.ToString()) + "\n";
                    msg += "ID: " + id + "\n";
                    msg += "Number of work hours: " + maxWorkHours.ToString() + "\n\n";
                }
            }
            //Show all lecturers with maximum amount of work hours
            MessageBox.Show(msg, "Works the most", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }