|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求助各位大佬,这学校选的C#书一堆错误,修改窗体的代码还略了,我一个学电商的根本看不懂,救救孩子吧
题目:设计一个主界面,根据需要用于显示员工基本信息和工资信息,要求实现员工数据的佛加,修改、删除、查找等基本功能。设计普通员工,管避人员信息的录人窗体,设计两关员信息的修改窗体
(I)创建一个 Windows 窗体项目 EmpMis。(2)定义一个抽象类 Person 和 Employee,Employee 继承 Person,Employee 派生密封类 HourlyEmployee 和 Manager,分别表示普通员工和管理人员,参考任务 5-3 的代码(3)设计一个接口,实现增加、删除、修改、查找、索引等功能public abstract class Person
{
public string ID { get; set; }
public string Name { get; set; }
public Person(string ID, string Name) { this.ID = ID; this.Name = Name; }
public Person() { }
public override string ToString() { return ID + "\t " + Name; }
public abstract String Pay();
}
public abstract class Employee : Person
{
public float Salary { get; set; }
public Employee(string ID, string Name, float salary) : base(ID, Name) { this.Salary = salary; }
protected abstract float Bonus { get; }
public sealed override string Pay()
{
float baseSalary, bonus, total; baseSalary = Salary;
bonus = Bonus;
total = baseSalary + bonus;
return base.ToString() + "\t" + baseSalary + "\t" + bonus + "\t" + total;
}
}
public sealed class HourlyEmployee : Employee
{
public int Hours { get; set; }
public float price { get; set; }
protected override float Bonus
{
get
{
return price*Hours;
}
}
public HourlyEmployee(string ID, string Name, float Salary, int Hours, float price) : base(ID, Name, Salary)
{ this.Hours = Hours; this.price = price; }
public override string ToString()
{
return base.ToString() + "\t普通\t " + Salary + "\t" + price + "\t" + Hours;
}
}
public sealed class Manager : Employee
{
public float Allowance { get; set; }
public int Level { get; set; }
protected override float Bonus
{
get { return Allowance + 10*Level; }
}
public Manager(string ID, string Name, float Salary, float Allowance, int lel) : base(ID, Name, Salary)
{ this.Allowance = Allowance; this.Level = lel; }
public override string ToString()
{
return base.ToString() + "\t管理\t" + Salary + "\t" + Allowance + "\t" + Level;
}
}
interface IPersonList
{
bool Add(Person p);
Boolean Delete(string id);
void update(Person p);
Person FindByID(string id);
Person this[int index]
{ get; set; }
}
public class PersonSet : IPersonList
{
private int _count, _max;
private Person[] record;
public PersonSet()
{
_count = 0; _max = 100;
record = new Person[_max];
}
public int Count
{
get { return _count; }
}
public int Max
{
get { return _max; }
}
public int Locate(string id)
{
int i;
for (i = 0; i < _count && id != record[i].ID; i++) { }
if (i < _count) return i + 1; else return 0;
}
public Person this[int index]
{
get { return record[index]; }
set { record[index] = value; }
}
public bool Add(Person p)
{
if (_count < _max) { record[_count++] = p; return true; }
else return false;
}
public bool Delete(string id)
{
int pos, i;
pos = Locate(id);
if (pos == 0) return false; for (i = pos; i < Count; i++) record[i - 1] = record[i];
_count--; return true;
}
public Person FindByID(string id)
{
int pos;
pos = Locate(id);
if (pos == 0) return null; else return record[pos - 1];
}
public virtual void update(Person p)
{
Person p1 = FindByID(p.ID);
if (p1 != null)
{
p1.ID = p.ID;
p1.Name = p.Name;
}
}
}
主窗体代码:public partial class FormMain : Form
{
PersonSet PsList = new PersonSet();
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
}
public void listSaly()
{
tb_list.Text = "编号\t姓名\t基本\t奖金\t工资总额\r\n"; for (int i = 0; i < PsList.Count; i++) { tb_list.Text += PsList[i].Pay() + "\r\n"; }
}
public void listBase()
{
tb_list.Text = "编号\t姓名\t类别\t基本\t小时酬金(津贴)\t工时(级别)\r\n";
for (int i = 0; i < PsList.Count; i++) tb_list.Text += PsList[i] + "\r\n";
}
private void bt_addEmploye_Click(object sender, EventArgs e)
{
FormEmpAdd f = new FormEmpAdd(this,PsList);
f.Show();
}
private void bt_base_Click(object sender, EventArgs e)
{
listBase();
}
private void bt_addmange_Click(object sender, EventArgs e)
{
FormMangerAdd fm = new FormMangerAdd(this,PsList);
fm.Show();
}
private void bt_listSaly_Click(object sender, EventArgs e)
{
listSaly();
}
private void bt_delete_Click(object sender, EventArgs e)
{
if (!PsList.Delete(tb_id.Text.Trim())) MessageBox.Show("不存在!"); else listBase();
}
//就是这个修改的两个窗体(FormMangerFind,FormEmpFind)代码写不来
private void bt_modify_Click(object sender, EventArgs e)
{
Person p = PsList.FindByID(tb_id.Text.Trim());
if (p is Manager)
{
FormMangerFind fm = new FormMangerFind(this, p); fm.Show();
}
else
{
FormEmpFind pfm = new FormEmpFind(this, p); pfm.Show();
}
}
}
看起来你需要为 Manager 和 HourlyEmployee 创建两个修改窗体,分别为 FormMangerFind 和 FormEmpFind 。修改窗体应该能够接收当前的员工信息,并允许用户进行修改,最后把修改后的数据返回到主窗体。下面我将给出一个可能的实现方式,代码可能不是完全正确的,但是我相信它会对你有所帮助。我假设你的修改窗体有几个文本框,允许用户输入新的值。
首先是 FormMangerFind 的实现:
public partial class FormMangerFind : Form
{
private FormMain _parentForm;
private Manager _currentManager;
public FormMangerFind(FormMain parentForm, Person person)
{
InitializeComponent();
_parentForm = parentForm;
_currentManager = person as Manager;
// 初始化文本框的值
textBoxID.Text = _currentManager.ID;
textBoxName.Text = _currentManager.Name;
textBoxSalary.Text = _currentManager.Salary.ToString();
textBoxAllowance.Text = _currentManager.Allowance.ToString();
textBoxLevel.Text = _currentManager.Level.ToString();
}
private void bt_submit_Click(object sender, EventArgs e)
{
// 将用户输入的新数据写回到_manager对象
_currentManager.ID = textBoxID.Text;
_currentManager.Name = textBoxName.Text;
_currentManager.Salary = float.Parse(textBoxSalary.Text);
_currentManager.Allowance = float.Parse(textBoxAllowance.Text);
_currentManager.Level = int.Parse(textBoxLevel.Text);
// 更新主窗体中的员工信息
_parentForm.update(_currentManager);
this.Close();
}
}
接下来是 FormEmpFind 的实现:
public partial class FormEmpFind : Form
{
private FormMain _parentForm;
private HourlyEmployee _currentEmployee;
public FormEmpFind(FormMain parentForm, Person person)
{
InitializeComponent();
_parentForm = parentForm;
_currentEmployee = person as HourlyEmployee;
// 初始化文本框的值
textBoxID.Text = _currentEmployee.ID;
textBoxName.Text = _currentEmployee.Name;
textBoxSalary.Text = _currentEmployee.Salary.ToString();
textBoxHours.Text = _currentEmployee.Hours.ToString();
textBoxPrice.Text = _currentEmployee.price.ToString();
}
private void bt_submit_Click(object sender, EventArgs e)
{
// 将用户输入的新数据写回到_employee对象
_currentEmployee.ID = textBoxID.Text;
_currentEmployee.Name = textBoxName.Text;
_currentEmployee.Salary = float.Parse(textBoxSalary.Text);
_currentEmployee.Hours = int.Parse(textBoxHours.Text);
_currentEmployee.price = float.Parse(textBoxPrice.Text);
// 更新主窗体中的员工信息
_parentForm.update(_currentEmployee);
this.Close();
}
}
你还需要在 FormMain 中添加一个新的 update 方法来更新员工信息:
public void update(Person person)
{
PsList.update(person);
listBase();
}
请注意,你可能需要在你的修改窗体设计中添加更多的错误处理代码,例如当用户输入的数据不符合要求时。还需要注意,我
这里的代码并未处理并发问题,如果你的应用需要处理多线程,你可能需要添加更多的代码来处理这个问题。
|
|