鱼C论坛

 找回密码
 立即注册
查看: 338|回复: 2

[已解决]有没有大哥帮忙看看这个项目的修改窗体代码怎么写,电商被迫学C#完全看不懂?

[复制链接]
发表于 2023-6-20 12:21:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求助各位大佬,这学校选的C#书一堆错误,修改窗体的代码还略了,我一个学电商的根本看不懂,救救孩子吧
题目:设计一个主界面,根据需要用于显示员工基本信息和工资信息,要求实现员工数据的佛加,修改、删除、查找等基本功能。设计普通员工,管避人员信息的录人窗体,设计两关员信息的修改窗体

(I)创建一个 Windows 窗体项目 EmpMis。(2)定义一个抽象类 Person 和 Employee,Employee 继承 Person,Employee 派生密封类 HourlyEmployee 和 Manager,分别表示普通员工和管理人员,参考任务 5-3 的代码(3)设计一个接口,实现增加、删除、修改、查找、索引等功能
  1. public abstract class Person
  2.     {
  3.         public string ID { get; set; }
  4.         public string Name { get; set; }
  5.         public Person(string ID, string Name) { this.ID = ID; this.Name = Name; }
  6.         public Person() { }
  7.         public override string ToString() { return ID + "\t " + Name; }
  8.         public abstract String Pay();
  9.     }


  10.     public abstract class Employee : Person
  11.     {
  12.         public float Salary { get; set; }
  13.         public Employee(string ID, string Name, float salary) : base(ID, Name) { this.Salary = salary; }
  14.         protected abstract float Bonus { get; }
  15.         public sealed override string Pay()
  16.         {
  17.             float baseSalary, bonus, total; baseSalary = Salary;
  18.             bonus = Bonus;
  19.             total = baseSalary + bonus;
  20.             return base.ToString() + "\t" + baseSalary + "\t" + bonus + "\t" + total;
  21.         }

  22.     }


  23.     public sealed class HourlyEmployee : Employee
  24.     {
  25.         public int Hours { get; set; }
  26.         public float price { get; set; }
  27.         protected override float Bonus
  28.         {
  29.             get
  30.             {
  31.                 return price*Hours;
  32.             }
  33.         }
  34.         public HourlyEmployee(string ID, string Name, float Salary, int Hours, float price) : base(ID, Name, Salary)

  35.         { this.Hours = Hours; this.price = price; }

  36.         public override string ToString()
  37.         {
  38.             return base.ToString() + "\t普通\t " + Salary + "\t" + price + "\t" + Hours;
  39.         }
  40.     }


  41.     public sealed class Manager : Employee
  42.     {
  43.         public float Allowance { get; set; }
  44.         public int Level { get; set; }
  45.         protected override float Bonus
  46.         {
  47.             get { return Allowance + 10*Level; }
  48.         }
  49.         public Manager(string ID, string Name, float Salary, float Allowance, int lel) : base(ID, Name, Salary)
  50.         { this.Allowance = Allowance; this.Level = lel; }
  51.         public override string ToString()
  52.         {
  53.             return base.ToString() + "\t管理\t" + Salary + "\t" + Allowance + "\t" + Level;
  54.         }
  55.     }



  56.    
  57.     interface IPersonList
  58.     {
  59.         bool Add(Person p);
  60.         Boolean Delete(string id);
  61.         void update(Person p);
  62.         Person FindByID(string id);
  63.         Person this[int index]
  64.         { get; set; }
  65.     }
  66.     public class PersonSet : IPersonList
  67.     {
  68.         private int _count, _max;
  69.         private Person[] record;
  70.         public PersonSet()
  71.         {
  72.             _count = 0; _max = 100;
  73.             record = new Person[_max];
  74.         }
  75.         public int Count
  76.         {
  77.             get { return _count; }
  78.         }
  79.         public int Max
  80.         {
  81.             get { return _max; }
  82.         }

  83.         public int Locate(string id)
  84.         {
  85.             int i;
  86.             for (i = 0; i < _count && id != record[i].ID; i++) { }
  87.                 if (i < _count) return i + 1; else return 0;
  88.         }
  89.         public Person this[int index]
  90.         {
  91.             get { return record[index]; }
  92.             set { record[index] = value; }
  93.         }
  94.         public bool Add(Person p)
  95.         {
  96.             if (_count < _max) { record[_count++] = p; return true; }

  97.             else return false;
  98.         }
  99.         public bool Delete(string id)
  100.         {
  101.             int pos, i;
  102.             pos = Locate(id);
  103.             if (pos == 0) return false; for (i = pos; i < Count; i++) record[i - 1] = record[i];
  104.             _count--; return true;
  105.         }
  106.         public Person FindByID(string id)
  107.         {
  108.             int pos;
  109.             pos = Locate(id);
  110.             if (pos == 0) return null; else return record[pos - 1];
  111.         }
  112.         public virtual void update(Person p)
  113.         {
  114.             Person p1 = FindByID(p.ID);
  115.             if (p1 != null)
  116.             {
  117.                 p1.ID = p.ID;
  118.                 p1.Name = p.Name;
  119.             }
  120.         }
  121.         
  122.     }
复制代码

主窗体代码:
  1. public partial class FormMain : Form
  2.     {
  3.         PersonSet PsList = new PersonSet();
  4.         public FormMain()
  5.         {
  6.             InitializeComponent();
  7.         }

  8.         private void FormMain_Load(object sender, EventArgs e)
  9.         {

  10.         }
  11.         public void listSaly()
  12.         {
  13.             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"; }
  14.             
  15.         }
  16.         public void listBase()
  17.         {
  18.             tb_list.Text = "编号\t姓名\t类别\t基本\t小时酬金(津贴)\t工时(级别)\r\n";
  19.             for (int i = 0; i < PsList.Count; i++) tb_list.Text += PsList[i] + "\r\n";
  20.         }

  21.         private void bt_addEmploye_Click(object sender, EventArgs e)
  22.         {
  23.             FormEmpAdd f = new FormEmpAdd(this,PsList);
  24.             f.Show();
  25.         }

  26.         private void bt_base_Click(object sender, EventArgs e)
  27.         {
  28.             listBase();
  29.         }

  30.         private void bt_addmange_Click(object sender, EventArgs e)
  31.         {
  32.             FormMangerAdd fm = new FormMangerAdd(this,PsList);
  33.             fm.Show();
  34.         }

  35.         private void bt_listSaly_Click(object sender, EventArgs e)
  36.         {
  37.             listSaly();
  38.         }

  39.       

  40.         private void bt_delete_Click(object sender, EventArgs e)
  41.         {
  42.             if (!PsList.Delete(tb_id.Text.Trim())) MessageBox.Show("不存在!"); else listBase();
  43.         }
  44.     //就是这个修改的两个窗体(FormMangerFind,FormEmpFind)代码写不来
  45.         private void bt_modify_Click(object sender, EventArgs e)
  46.         {
  47.             Person p = PsList.FindByID(tb_id.Text.Trim());
  48.             if (p is Manager)
  49.             {
  50.                 FormMangerFind fm = new FormMangerFind(this, p); fm.Show();
  51.             }
  52.             else
  53.             {
  54.                 FormEmpFind pfm = new FormEmpFind(this, p); pfm.Show();
  55.             }
  56.         }
  57.     }
复制代码
最佳答案
2023-6-20 12:24:26
看起来你需要为 Manager 和 HourlyEmployee 创建两个修改窗体,分别为 FormMangerFind 和 FormEmpFind 。修改窗体应该能够接收当前的员工信息,并允许用户进行修改,最后把修改后的数据返回到主窗体。下面我将给出一个可能的实现方式,代码可能不是完全正确的,但是我相信它会对你有所帮助。我假设你的修改窗体有几个文本框,允许用户输入新的值。

首先是 FormMangerFind 的实现:

  1. public partial class FormMangerFind : Form
  2. {
  3.     private FormMain _parentForm;
  4.     private Manager _currentManager;

  5.     public FormMangerFind(FormMain parentForm, Person person)
  6.     {
  7.         InitializeComponent();
  8.         _parentForm = parentForm;
  9.         _currentManager = person as Manager;

  10.         // 初始化文本框的值
  11.         textBoxID.Text = _currentManager.ID;
  12.         textBoxName.Text = _currentManager.Name;
  13.         textBoxSalary.Text = _currentManager.Salary.ToString();
  14.         textBoxAllowance.Text = _currentManager.Allowance.ToString();
  15.         textBoxLevel.Text = _currentManager.Level.ToString();
  16.     }

  17.     private void bt_submit_Click(object sender, EventArgs e)
  18.     {
  19.         // 将用户输入的新数据写回到_manager对象
  20.         _currentManager.ID = textBoxID.Text;
  21.         _currentManager.Name = textBoxName.Text;
  22.         _currentManager.Salary = float.Parse(textBoxSalary.Text);
  23.         _currentManager.Allowance = float.Parse(textBoxAllowance.Text);
  24.         _currentManager.Level = int.Parse(textBoxLevel.Text);

  25.         // 更新主窗体中的员工信息
  26.         _parentForm.update(_currentManager);

  27.         this.Close();
  28.     }
  29. }
复制代码


接下来是 FormEmpFind 的实现:

  1. public partial class FormEmpFind : Form
  2. {
  3.     private FormMain _parentForm;
  4.     private HourlyEmployee _currentEmployee;

  5.     public FormEmpFind(FormMain parentForm, Person person)
  6.     {
  7.         InitializeComponent();
  8.         _parentForm = parentForm;
  9.         _currentEmployee = person as HourlyEmployee;

  10.         // 初始化文本框的值
  11.         textBoxID.Text = _currentEmployee.ID;
  12.         textBoxName.Text = _currentEmployee.Name;
  13.         textBoxSalary.Text = _currentEmployee.Salary.ToString();
  14.         textBoxHours.Text = _currentEmployee.Hours.ToString();
  15.         textBoxPrice.Text = _currentEmployee.price.ToString();
  16.     }

  17.     private void bt_submit_Click(object sender, EventArgs e)
  18.     {
  19.         // 将用户输入的新数据写回到_employee对象
  20.         _currentEmployee.ID = textBoxID.Text;
  21.         _currentEmployee.Name = textBoxName.Text;
  22.         _currentEmployee.Salary = float.Parse(textBoxSalary.Text);
  23.         _currentEmployee.Hours = int.Parse(textBoxHours.Text);
  24.         _currentEmployee.price = float.Parse(textBoxPrice.Text);

  25.         // 更新主窗体中的员工信息
  26.         _parentForm.update(_currentEmployee);

  27.         this.Close();
  28.     }
  29. }
复制代码


你还需要在 FormMain 中添加一个新的 update 方法来更新员工信息:

  1. public void update(Person person)
  2. {
  3.     PsList.update(person);
  4.     listBase();
  5. }
复制代码


请注意,你可能需要在你的修改窗体设计中添加更多的错误处理代码,例如当用户输入的数据不符合要求时。还需要注意,我

这里的代码并未处理并发问题,如果你的应用需要处理多线程,你可能需要添加更多的代码来处理这个问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-20 12:24:26 | 显示全部楼层    本楼为最佳答案   
看起来你需要为 Manager 和 HourlyEmployee 创建两个修改窗体,分别为 FormMangerFind 和 FormEmpFind 。修改窗体应该能够接收当前的员工信息,并允许用户进行修改,最后把修改后的数据返回到主窗体。下面我将给出一个可能的实现方式,代码可能不是完全正确的,但是我相信它会对你有所帮助。我假设你的修改窗体有几个文本框,允许用户输入新的值。

首先是 FormMangerFind 的实现:

  1. public partial class FormMangerFind : Form
  2. {
  3.     private FormMain _parentForm;
  4.     private Manager _currentManager;

  5.     public FormMangerFind(FormMain parentForm, Person person)
  6.     {
  7.         InitializeComponent();
  8.         _parentForm = parentForm;
  9.         _currentManager = person as Manager;

  10.         // 初始化文本框的值
  11.         textBoxID.Text = _currentManager.ID;
  12.         textBoxName.Text = _currentManager.Name;
  13.         textBoxSalary.Text = _currentManager.Salary.ToString();
  14.         textBoxAllowance.Text = _currentManager.Allowance.ToString();
  15.         textBoxLevel.Text = _currentManager.Level.ToString();
  16.     }

  17.     private void bt_submit_Click(object sender, EventArgs e)
  18.     {
  19.         // 将用户输入的新数据写回到_manager对象
  20.         _currentManager.ID = textBoxID.Text;
  21.         _currentManager.Name = textBoxName.Text;
  22.         _currentManager.Salary = float.Parse(textBoxSalary.Text);
  23.         _currentManager.Allowance = float.Parse(textBoxAllowance.Text);
  24.         _currentManager.Level = int.Parse(textBoxLevel.Text);

  25.         // 更新主窗体中的员工信息
  26.         _parentForm.update(_currentManager);

  27.         this.Close();
  28.     }
  29. }
复制代码


接下来是 FormEmpFind 的实现:

  1. public partial class FormEmpFind : Form
  2. {
  3.     private FormMain _parentForm;
  4.     private HourlyEmployee _currentEmployee;

  5.     public FormEmpFind(FormMain parentForm, Person person)
  6.     {
  7.         InitializeComponent();
  8.         _parentForm = parentForm;
  9.         _currentEmployee = person as HourlyEmployee;

  10.         // 初始化文本框的值
  11.         textBoxID.Text = _currentEmployee.ID;
  12.         textBoxName.Text = _currentEmployee.Name;
  13.         textBoxSalary.Text = _currentEmployee.Salary.ToString();
  14.         textBoxHours.Text = _currentEmployee.Hours.ToString();
  15.         textBoxPrice.Text = _currentEmployee.price.ToString();
  16.     }

  17.     private void bt_submit_Click(object sender, EventArgs e)
  18.     {
  19.         // 将用户输入的新数据写回到_employee对象
  20.         _currentEmployee.ID = textBoxID.Text;
  21.         _currentEmployee.Name = textBoxName.Text;
  22.         _currentEmployee.Salary = float.Parse(textBoxSalary.Text);
  23.         _currentEmployee.Hours = int.Parse(textBoxHours.Text);
  24.         _currentEmployee.price = float.Parse(textBoxPrice.Text);

  25.         // 更新主窗体中的员工信息
  26.         _parentForm.update(_currentEmployee);

  27.         this.Close();
  28.     }
  29. }
复制代码


你还需要在 FormMain 中添加一个新的 update 方法来更新员工信息:

  1. public void update(Person person)
  2. {
  3.     PsList.update(person);
  4.     listBase();
  5. }
复制代码


请注意,你可能需要在你的修改窗体设计中添加更多的错误处理代码,例如当用户输入的数据不符合要求时。还需要注意,我

这里的代码并未处理并发问题,如果你的应用需要处理多线程,你可能需要添加更多的代码来处理这个问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-6-20 12:40:16 | 显示全部楼层
isdkz 发表于 2023-6-20 12:24
看起来你需要为 Manager 和 HourlyEmployee 创建两个修改窗体,分别为 FormMangerFind 和 FormEmpFind 。修 ...

感谢大佬,你就是我干爹,感谢救命之恩
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 12:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表