C#创建并操作Employee对象数组

时间:2014-11-18 23:15:18

标签: c# arrays

创建:

此类定义一个Employee。 成员变量:ID(int),name(字符串),salary(double) 成员方法:为员工执行I / O的构造函数,输入/输出方法

创建:

此类定义Employees数组 成员变量:Employee,SIZE(int),currentSize(int)数组 成员方法:构造函数,输入/输出方法,搜索/插入/删除/更新方法。

不确定如何在单个数组索引中存储3个Employee变量。

更新:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;

   namespace ConsoleApplication16
   {

    class Employee
    {
        protected int id;
        protected string name;
        protected double salary;


        public Employee()
        {
        }

        public Employee(int _id, string nm, double sal)
        {
            id = _id;
            name = nm;
            salary = sal;
        }


        public void PrintEmployee()
        {

            Console.WriteLine("id: {0}", id);
            Console.WriteLine("Name: {0}", name);
            Console.WriteLine("Salary: {0}", salary);


        }


    }


    class EmployeeArray : Employee
    {
        private Employee[] a;
        private int currSize;
        private const int SIZE = 100;

        public EmployeeArray()
            : base()
        {

        }
        public EmployeeArray(int s, int _id, string nm, double sal)
            : base(_id, nm, sal)
        {
            a = new Employee[SIZE];
            if (s > SIZE)
                Console.WriteLine("Array size is overflow!");
            else if (s == SIZE)
                Console.WriteLine("Array is full.");
            else
                currSize = s;




        }

        public void Input()
        {    
            a = new Employee[3];
            for (int i = 0; i < currSize; i++)
            {
                a[i] = new Employee(id, name, salary);
            }



        }
        public void Output()
        {
            Console.WriteLine("Array is: ");
            foreach (Employee x in a)
                Console.WriteLine("a[{0}]= {1}", name, x);


        }




        public int Search(int key)
        {
            for (int i = 0; i < currSize; i++)
            {
                //if (a[i] == key)
                //    return i;
            }
            return -1;
        }

        public void Insert()
        {
            if (currSize == SIZE)
            {
                Console.Write("Array is full! ");
                return;
            }
            Console.WriteLine("Enter a number to insert: ");
            int y = int.Parse(Console.ReadLine());
            Console.Write("Enter the index to where it is to insert: ");
            int pos = int.Parse(Console.ReadLine());
            for (int i = currSize; i > pos; i--)
                a[i] = a[i - 1];


            //a[pos] = y;
            currSize++;
        }

        public void Delete()
        {
            if (currSize == 0)
            {
                Console.WriteLine("Array is empty! ");
                return;
            }
            Console.Write("Delete by value (1) or by index (2):  ");
            int key = int.Parse(Console.ReadLine());
            int pos = -1;
            if (key == 1)
            {
                Console.WriteLine("Enter the number to delete: ");
                int d = int.Parse(Console.ReadLine());
                pos = Search(d);
                while (pos == -1)
                {
                    Console.WriteLine("The number does not exist, enter again: ");
                    d = int.Parse(Console.ReadLine());
                    pos = Search(d);
                }
            }
            else if (key == 2)
            {
                Console.WriteLine("Enter the index to delete from: ");
                pos = int.Parse(Console.ReadLine());

                while (pos < 0 || pos > currSize)
                {
                    Console.WriteLine("The index is out of range, enter again: ");
                    pos = int.Parse(Console.ReadLine());
                }

            }
            else
                return;

            for (int i = pos; i < currSize; i++)
                a[i] = a[i + 1];

            currSize--;
            if (currSize <= 0)
                Console.Write("Array is empty! ");
        }

        public void Update()
        {
            Console.WriteLine("Enter the index where to update: ");
            int pos = int.Parse(Console.ReadLine());
            while (pos < 0 || pos >= currSize)
            {
                Console.WriteLine("The index you entered is not valid, enter again: ");
                pos = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("Enter the new value: ");
            int x = int.Parse(Console.ReadLine());
            //a[pos] = x;
            Console.Write("Update complete! ");
        }


    }
    class Program
    {
        //static char ShowMenu()
        //{
        //    Console.WriteLine("\nEnter the letter of operation: \n(o)Print, (s)Search, (i)Insertion, (d)Deletion, (u)Update, (e)Exit\n");
        //    return char.Parse(Console.ReadLine());
        //}
        static void Main(string[] args)
        {
            //char sel = ' ';
            Console.Write("Enter number of Employees; ");
            int i = 0;
            int s = int.Parse(Console.ReadLine());

            while ( i < s )
            {


                Console.WriteLine("");
                Console.WriteLine("");

                Console.WriteLine("Enter id: ");
                int _id = int.Parse(Console.ReadLine());


                Console.WriteLine("");
                Console.WriteLine("Enter Name: ");
                string nm = Console.ReadLine();

                Console.WriteLine("");
                Console.WriteLine("Enter Salary: ");
                double sal = int.Parse(Console.ReadLine());

                EmployeeArray arr = new EmployeeArray(s, _id, nm, sal);


                i++;
            }



        }
    }
}

2 个答案:

答案 0 :(得分:2)

这就像家庭作业,但我会帮助你。

您可以使用三个成员和方法创建员工类。

然后创建一个员工对象数组。

//How Many employee objects do you need. Let's assume 10 for example
int NUM = 10; 

Employee[] employees = new Employee[NUM]; // Declare an An array of employee's of length 10  (zero indexed from 0 - 9)

//Assuming you have made Getters and Setters

//Then if you need to access an employee object's member(s)
employees[0].getId(); //get the first employee's id
employees[0].getName(); //get the first employee's name
employees[0].getSalary(); //get the first employee's name

// Or set them like
employees[4].setId(12); //set the fifth employee's id to 12
employees[4].setName("Joe Bloggs"); //set the fifth employee's name to Joe Bloggs
employees[4].setSalary("30000"); //set the fifth employee's salary to 30000

MSDN C# Arrays

MSDN Classes

答案 1 :(得分:0)

您发布的代码存在很多问题。我只是要强调几个问题,特别是因为这个实现数组听起来很像计算机科学作业。

  1. EmployeeArray继承自Employee。考虑publicprivate关键字和基类的含义。
  2. 你的功能不仅仅是一件事。这确实使您的代码复杂化,并将导致错误。专注于简单的代码,它完成一件事并传达它的作用。
  3. 用户可以通过输入数字以外的其他内容来破坏您的程序。
  4. 最后,要直接回答您的问题,您可以通过索引访问数组。您不能在同一索引中存储三个单独的Employee对象。您已经在代码的其他部分访问了数组对象,因此您似乎可以理解可以在不同的索引中存储不同的值。