Answer to Question #238598 in C# for Phamela

Question #238598

This question continues with this one posted now:

In the application class (main method), do the following Implement method addMember(): static void addMember(Member[] memberList, ref int nrEl) //Request the name of a member and adds that member to the list Implement the main method which contains the following:  A declaration of an array of members, which can take 5 members  A loop that adds 5 members to the array, using addMember()  A loop that sets the points of the first two members to 30  A call to an array method to sort the objects in the array in ascending order of name  A loop that uses displayMember() to display all the objects


1
Expert's answer
2021-09-17T15:01:04-0400




using System;
using System.Collections.Generic;
namespace MemberApp
{
    class Member : IComparable
    {
        private string Name;
        private int Points;


        public Member(string name, int points = 0)
        {
            this.Name = name;
            this.Points = points;
        }


        public string getName()
        {
            return Name;
        }


        public int getPoints()
        {
            return Points;
        }


        public void setPoints(int P)
        {
            this.Points = P;
        }


        public void diplayMember()
        {
            Console.WriteLine("Name: " + Name + "\n" + "Points: " + Points + "\n");
        }


        int IComparable.CompareTo(object obj)
        {
            return this.Name.CompareTo(((Member)obj).Name);
        }
    }






    class Program
    {


        static void addMember(Member[] memberList, ref int nrEl)
        {
            Console.Write("Ente a member name: ");
            string Name = Console.ReadLine();
            
            if (nrEl < 2)
            {
                memberList[nrEl] = new Member(Name, 30);
            }
            else {
                Console.Write("Ente a member Points: ");
                int Points = int.Parse(Console.ReadLine());
                memberList[nrEl] = new Member(Name, Points);
            }
            
            nrEl++;
        }
        static void Main(string[] args)
        {


            Member[] memberList = new Member[5];
            int nrEl = 0;
            //A declaration of an array of members, which can take 5 members
            //A loop that adds 5 members to the array, using addMember()
            for (int i = 0; i < 5; i++)
            {
                //A loop that sets the points of the first two members to 30 
                addMember(memberList, ref nrEl);
            }


            //A call to an array method to sort the objects in the array in ascending order of name 
            Array.Sort(memberList);
            //A loop that uses displayMember() to display all the objects
            for (int i = 0; i < 5; i++)
            {
                memberList[i].diplayMember();
            }


            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS