using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ch5_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        const int num = 10;
        float []grades = new float[num];
        int count = 0;  //number of entered data

        private void Form1_Load(object sender, EventArgs e)
        {
            label1 .Text  += (count + 1) .ToString ();
            button1.Enabled = false;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                grades[count++] = Convert.ToInt32(textBox1.Text);
                if (count < num)
                {
                    textBox1.Text = "";
                    textBox1.Focus();
                    label1.Text = "Number : " + (count + 1).ToString();
                }
                else
                {
                    button1.Enabled = true;
                    MessageBox.Show("Please click Process button");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int lessThan = 0, greaterThan = 0;
            float average, sum = 0;
            //process array with foreach
            foreach (float grade in grades )
            {
                sum += grade; 
                if (grade < 12)
                {
                    lessThan += 1;
                    listBox1.Items.Add(grade);
                }
                else
                {
                    greaterThan += 1;
                    listBox2.Items.Add(grade);
                }
            }
            average = sum / num;
            label4.Text += average.ToString();
            //process array with for
            for (int i = num - 1; i >= 0; i--)
                listBox3.Items.Add(grades[i]);
        }
    }
}
