C#创建和操作员工对象数组
创建: 此类定义了一个雇员。 成员变量:ID(int)、name(string)、salary(double) 成员方法:构造函数,为员工执行I/O的输入/输出方法 创建: 此类定义了一个雇员数组 成员变量:Employee、SIZE(int)、currentSize(int)的数组 成员方法:构造函数、输入/输出方法、搜索/插入/删除/更新方法 不确定如何在单个数组索引中存储3个Employee变量 更新:C#创建和操作员工对象数组,c#,arrays,C#,Arrays,创建: 此类定义了一个雇员。 成员变量:ID(int)、name(string)、salary(double) 成员方法:构造函数,为员工执行I/O的输入/输出方法 创建: 此类定义了一个雇员数组 成员变量:Employee、SIZE(int)、currentSize(int)的数组 成员方法:构造函数、输入/输出方法、搜索/插入/删除/更新方法 不确定如何在单个数组索引中存储3个Employee变量 更新: using System; using System.Collection
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++;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序16
{
班级员工
{
受保护的int-id;
受保护的字符串名称;
受保护的双薪;
公职人员()
{
}
公共雇员(整数id、字符串nm、双sal)
{
id=_id;
名称=纳米;
薪金=sal;
}
公共部门雇员()
{
WriteLine(“id:{0}”,id);
WriteLine(“名称:{0}”,名称);
Console.WriteLine(“工资:{0}”,工资);
}
}
类EmployeeArray:Employee
{
私人雇员[]a;
私家车大小;
私有常量int SIZE=100;
公共雇员数组()
:base()
{
}
公共EmployeeArray(整数s,整数id,字符串nm,双sal)
:基础(_id,nm,sal)
{
a=新员工[人数];
如果(s>大小)
WriteLine(“数组大小溢出!”);
如果(s==大小),则为else
WriteLine(“数组已满”);
其他的
currSize=s;
}
公共无效输入()
{
a=新员工[3];
对于(int i=0;ipos;i--)
a[i]=a[i-1];
//a[pos]=y;
currSize++;
}
公共作废删除()
{
如果(currSize==0)
{
WriteLine(“数组为空!”);
回来
}
控制台。写入(“按值(1)或按索引(2)删除):”;
int key=int.Parse(Console.ReadLine());
int pos=-1;
如果(键==1)
{
Console.WriteLine(“输入要删除的编号:”);
int d=int.Parse(Console.ReadLine());
pos=搜索(d);
而(位置==-1)
{
Console.WriteLine(“号码不存在,请再次输入:”);
d=int.Parse(Console.ReadLine());
pos=搜索(d);
}
}
否则如果(键==2)
{
WriteLine(“输入要从中删除的索引:”);
pos=int.Parse(Console.ReadLine());
而(pos<0 | | pos>currSize)
{
WriteLine(“索引超出范围,请再次输入:”);
pos=int.Parse(Console.ReadLine());
}
}
其他的
回来
对于(int i=pos;i
这看起来像是家庭作业,但我会帮你的
您创建了您的员工cl
//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