Monday, March 24, 2014

Question No. 29(Parallel Array with Form)

Ex 29.    The salespeople at a health club keep track of the members who have joined in the last month. Their names and types of membership, Bronze, Silver, or Gold are stored in 2 parallel array. One array holds member names and the second array holds their membership types (“Silver”, “Bronze”, or “Gold”). Create two arrays of size 20 and initialize these arrays directly during ‘array creation’. Load 20 names in one and membership types in the other one. Write a program that provide a menu to perform the following functions:
  1. Display all the members’ name only
  2. Request a name from user and display his/her membership type 
  3. Request a name and change  his/her membership type
  4. Request a membership type (either Silver, Bronze, or Gold), then display all the names with the given membership type
  5. Exit

namespace Final_Review_Ex_29
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[] namesA = { "Joe", "Bob", "John", "Jack", "Jill", "Homer", "Sally", "Wilma", "Jimmy", "Jason", "Michal", "Kellan", "Sara", "Silvia", "Janelle",

"Yulka", "Yevette", "Elizabeth", "Silvester", "Alice" };
        string[] mbtypeA = { "Gold", "Silver", "Silver", "Bronze", "Bronze", "Gold", "Silver", "Silver", "Bronze", "Bronze", "Gold", "Silver", "Silver", "Bronze",

"Bronze", "Gold", "Gold", "Silver", "Gold", "Gold" };

        private void btnMemberOnly_Click(object sender, EventArgs e)
        {
            txtMainBox.Clear();
            for (int i = 0; i < namesA.Length; i++)
            {
                txtMainBox.Text += i.ToString() +" "+ namesA[i] + "\n";
            }
        }

        private void btnMtypes_Click(object sender, EventArgs e)
        {
            txtMainBox.Clear();
            string getname = txtNforM.Text;
            for (int i = 0; i < namesA.Length; i++)
            {
                if (getname.ToLower() == namesA[i].ToLower())
                {
                    txtMainBox.Text = mbtypeA[i];
                    break;
                }
                else
                {
                    txtMainBox.Text = "Name doesn't exist";
                }
            }

        }

        private void btnChangeMT_Click(object sender, EventArgs e)
        {

            string getname = txtN2change.Text;
            for (int i = 0; i < namesA.Length; i++)
            {
                if (getname.ToLower() == namesA[i].ToLower())
                {
                    if (txtmbtype.Text.ToLower() == "gold" || txtmbtype.Text.ToLower() == "silver" || txtmbtype.Text.ToLower() == "bronze")
                    {
                        mbtypeA[i] = txtmbtype.Text;
                        txtMainBox.Text = String.Format("{0}'s membership type has changed to {1}", namesA[i], mbtypeA[i]);
                        break;
                    }
                    else
                    {
                        MessageBox.Show("Please enter a valid service level");
                        break;
                    }
                }
                else
                    txtMainBox.Text = "Name doesn't exist";
            }

        }

        private void btnMtoN_Click(object sender, EventArgs e)
        {
            string membershiptype = txtReMbt.Text;
            for (int i = 0; i < mbtypeA.Length; i++)
            {
                if (membershiptype.ToLower() == mbtypeA[i].ToLower())
                {
                    txtMainBox.Text += namesA[i] + "\n";
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

No comments:

Post a Comment