设计一个Windows应用程序,定义一个Student类,包含学号和姓名、年龄三个字段, 并定义一个班级类classlist,该类包括一个Student集合,使用索引器访问该集合, 实现与实例6-3类

设计一个Windows应用程序,定义一个Student类,包含学号和姓名、年龄三个字段,
并定义一个班级类classlist,该类包括一个Student集合,使用索引器访问该集合,
实现与实例6-3类似的功能。进一步实现按学号升序排序和按年龄降序排序
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProject6_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("请输入学号!");
                }
                else
                {
                    Student stu1 = clist[textBox1.Text];
                    if (stu1 != null)
                    {
                        label3.Text = string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname,stu1.Age);
                    }
                    else
                    {
                        label3.Text = string.Format("没有找到学号是{0}的学生",textBox1.Text );
                    }
                }
            }
            catch
            {
                MessageBox.Show("未找到学生信息!");
            }

        }
        ClassList clist = new ClassList(10);  //创建一个容量为10的班级实例
        int n = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == ""||textBox3.Text.Trim()=="")
            {
                MessageBox.Show("请输入学生的学号或姓名或年龄!");
            }
            else
            {
                Student stu1 = new Student(textBox1.Text, textBox2.Text, textBox3.Text);
                clist[n] = stu1;
                n++;
                label3.Text += string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname,stu1.Age) + "\r\n";
                MessageBox.Show("添加成功!");
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox4.Text == "")
                {
                    MessageBox.Show("请输入索引号!");
                }
                else
                {
                    Student stu1 = clist[Convert.ToInt32(textBox4.Text)];
                    if (stu1 != null)
                    {
                        label3.Text = string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname,stu1.Age);
                    }
                    else
                    {
                        label3.Text = string.Format("没有索引值为a{0}的学生", Convert.ToInt32(textBox4.Text));
                    }
                }
            }
            catch
            {
                MessageBox.Show("未找到学生信息!");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox2.Text == "")
                {
                    MessageBox.Show("请输入姓名!");
                }
                else
                {
                    Student stu1 = clist[textBox2.Text];
                    if (stu1 != null)
                    {
                        label3.Text = string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname, stu1.Age);
                    }
                    else
                    {
                        label3.Text = string.Format("没有找到姓名是{0}的学生", textBox2.Text);
                    }
                }
            }
            catch
            {
                MessageBox.Show("未找到学生信息!");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox3.Text == "")
                {
                    MessageBox.Show("请输入年龄!");
                }
                else
                {
                    Student stu1 = clist[textBox3.Text];
                    if (stu1 != null)
                    {
                        label3.Text = string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname, stu1.Age);
                    }
                    else
                    {
                        label3.Text = string.Format("没有找到姓名是{0}的学生", textBox3.Text);
                    }
                }
            }
            catch
            {
                MessageBox.Show("未找到学生信息!");
            }
            label3.Text = "";
        }
    }
    public class Student
    {
        private string sno;
        private string sname;
        private string age;
        public Student(string sno, string sname,string age)
        {
            this.sno = sno;
            this.sname = sname;
            this.age = age;
        }
        public string Sno
        {
            get
            {
                return sno;
            }
        }
        public string Sname
        {
            get
            {
                return sname;
            }
        }
        public string Age
        {
            get
            {
                return age;
            }
        }
    }
    public class ClassList
    {
        private Student[] students;
        public ClassList(int n)
        {
            students = new Student[n];
        }
        //根据索引查找
        public Student this[int index]
        {
            get
            {
                if (index < 0 || index >= students.Length)
                {
                    return null;
                }
                return students[index];
            }
            set
            {
                if (index < 0 || index >= students.Length)
                {
                    return;
                }
                students[index] = value;
            }
        }
        //按姓名或学号或年龄查找
        public Student this[string infor]
        {
            get
            {
                foreach (Student stu in students)
                {
                    if (stu.Sname == infor || stu.Sno == infor || stu.Age == infor)
                    {
                        return stu;
                    }
                }
                return null;
            }
        }
    }
}

在这里插入图片描述

索引查找
在这里插入图片描述

姓名查找:

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值