发布时间:2023-11-04 07:43来源:www.sf1369.com作者:宇宇
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
//员工类
public class Employee
{
private string userinfo;
private string username;
protected static double salary;//薪水
//构造函数
public Employee(string username,string userinfo)
{
this.username = username;
this.userinfo = userinfo;
salary = 1000;
}
private Employee()
{ }
//获取用户名
public string GetUserName()
{
return username;
}
//打印用户名
public void PrintUserName()
{
Console.WriteLine(姓名: + username);
}
//打印用户基本信息
public void PrintUserInfo()
{
Console.WriteLine(基本信息: + userinfo);
}
//打印用户详细信息
public void PrintEmployeeInfo()
{
PrintUserName();
PrintUserInfo();
}
//虚方法,多态的关键,使不同的员工工资不一样
//我没有打印员工的类别,如果需要打印, 方法和这个类似
public virtual double GetSalary()
{
return salary;
}
//打印薪水
public void PrintSalary()
{
Console.WriteLine(薪水: + GetSalary());
}
//输入薪水
static public void InputSalary(double newsalary)
{
salary = newsalary;
}
//以下是操作符重载
//重载=号
public static bool operator==(Employee e1, Employee e2)
{
if (string.Compare(e1.GetUserName(), e2.GetUserName()) == 0)
return true;
else
return false;
}
//重载!=号
public static bool operator !=(Employee e1, Employee e2)
{
if (string.Compare(e1.GetUserName(), e2.GetUserName()) != 0)
return true;
else
return false;
}
//重载>号
public static bool operator >(Employee e1, Employee e2)
{
if (string.Compare(e1.GetUserName(), e2.GetUserName()) == 1)
return true;
else
return false;
}
//重载public static bool operator <(Employee e1, Employee e2) { if (string.Compare(e1.GetUserName(), e2.GetUserName()) == -1) return true; else return false; } } //正式工 public class Regularstaff:Employee { public Regularstaff(string username,string userinfo) : base(username,userinfo) { } //重写父类的方法 public override double GetSalary() { return salary; } } //行政人员 public class Administrationstaff : Employee { public Administrationstaff(string username, string userinfo) : base(username, userinfo) { } //重写父类的方法 public override double GetSalary() { return salary*1.5; } } //销售人员 public class Salesman : Employee { public Salesman(string username,string userinfo) : base(username,userinfo) { } //重写父类的方法 public override double GetSalary() { return salary*1.8; } } class Program { static void Main(string[] args) { ConsoleKeyInfo cki= new ConsoleKeyInfo(); List emp = new List(); string userinfo = string.Empty; string username = string.Empty; while (true) { //显示选择菜单 Console.WriteLine(1.员工录入); Console.WriteLine(2.员工查询); Console.WriteLine(3.薪水录入); Console.WriteLine(4.工资查询); Console.WriteLine(5.工资平均值); Console.WriteLine(6.退出系统); Console.WriteLine(请输入您的选择:); cki = Console.ReadKey(); Console.WriteLine(); switch (cki.KeyChar) { case '1': int employeetype=0; Console.WriteLine(请输入员工的姓名:); username = Console.ReadLine(); Console.WriteLine(请输入员工的基本信息:); userinfo = Console.ReadLine(); Console.WriteLine(请输入员工的类别:1、正式工 2、行政人员 3、销售人员); employeetype = int.Parse(Console.ReadLine()); Employee newemp; //根据不同的选项实例化不同的对象 switch (employeetype) { case 1: newemp = new Regularstaff(username, userinfo); break; case 2: newemp = new Administrationstaff(username, userinfo); break; case 3: newemp = new Salesman(username, userinfo); break; default: //默认为正式工 newemp = new Regularstaff(username, userinfo); break; } emp.Add(newemp); break; case '2': for (int i = 0; i < emp.Count; i++) { emp[i].PrintEmployeeInfo(); } break; case '3': double newsalary = 0; Console.WriteLine(请输入基本薪水:); newsalary = double.Parse(Console.ReadLine()); Employee.InputSalary(newsalary); break; case '4': for (int i = 0; i < emp.Count; i++) { emp[i].PrintUserName(); emp[i].PrintSalary(); } break; case '5': double salaryCount = 0; for (int i = 0; i < emp.Count; i++) { salaryCount = salaryCount +emp[i].GetSalary(); } if (emp.Count != 0) Console.WriteLine(平均薪水为:+ salaryCount/emp.Count); else Console.WriteLine(平均薪水为:0); break; case '6': return; default: Console.WriteLine(输入错误,请重新输入!); break; } } } } }