zoukankan      html  css  js  c++  java
  • 基于 控制台 简易 学生信息管理系统 (增、删、改)

    1、场景

          创建一个学生信息管理系统,具体要求如下:

    1.1  数据需求

          学生信息包括:学号、姓名、年龄、性别、专业、班级。

    1.2 基本功能需求(每组可以自行完善)

    •   学生信息添加(学生信息从键盘上输入,保存到磁盘文件)
    •   学生信息删除(按学号删除)
    •  学生信息查询(提供两种查询方式:按学号查询、按姓名查询,要求使用方法的重载实现)

    2、系统设计

    2.1 在新建控制台应用程序,项目名为StudentManagement

    2.2 添加一个文件读写类FileAccessOperate,至少包括2个方法,文件的读方法和文件写方法。读方法设计时,需要用到集合类,将读取到的所有学生添加到该集合,例如arraylist;写方法设计时,注意每次向文件写入一行,即每个学生占去一行。        

    using System;
    
    using System.Linq;
    using System.Text;
    //using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace StudentManagement
    {
        /*添加一个文件读写类FileAccess,至少包括2个方法,文件的读方法和文件写方法。
         * 读方法设计时,需要用到集合类,将读取到的所有学生添加到该集合,
         * 例如arraylist;写方法设计时,注意每次向文件写入一行,即每个学生占去一行。*/
    
        class FileAccessOperate
        {             
            static string txtDictionary=@"e:1111104";
            //写方法
            public void StuWrite(List<Student> stuList)
            {            
                string path = txtDictionary + "stu.dat";    //学生信息存储路径
    
                FileInfo fi = new FileInfo(path);
    
                //判断文件是否存在
                if (fi.Exists == false)
                {
                    FileStream fs = new FileStream(path,FileMode.Create);
                    fs.Close();
                    fi = new FileInfo(path);
                }
                //判断文件是否为空,即是否是第一次序列化存储
                if (fi.Length > 0)
                {
                    //如果不为空,即已经进行过序列化存储,需将之前存的信息,反序列化读取,添加到要存储的对象集,覆盖存储
                    List<Student> StuListRead = StuRead();
    
                    foreach (Student stu in StuListRead)
                    {
                        stuList.Add(stu);
                    }
                }
    
                Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
                BinaryFormatter bf = new BinaryFormatter(); //创建序列化对象 
    
                //序列化
                bf.Serialize(stream, stuList);
                stream.Close();
            }
    
            //删除学生
            public void DeleteStu(int num)
            {           
                List<Student> stuList = StuRead();
    
                for (int i = 0; i < stuList.Count; i++)
                {
                    if (stuList[i].StuNum == num)
                        stuList.Remove(stuList[i]);
                }
    
                string path = txtDictionary + "stu.dat";    //学生信息存储路径
                Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
                BinaryFormatter bf = new BinaryFormatter(); //创建序列化对象 
    
                bf.Serialize(stream, stuList);
                stream.Close();
            }
    
            //读方法
            public List<Student> StuRead()
            {
                string path=txtDictionary+"stu.dat";    //学生信息存储路径
               
                FileStream stream = new FileStream(path,FileMode.Open,FileAccess.Read);
                BinaryFormatter bf = new BinaryFormatter(); //创建序列化对象
                List<Student> stuList;
                          
                stuList = bf.Deserialize(stream) as List<Student>;
                
                stream.Close();
                return stuList;
            }
    
        }
    } 

    2.3 添加一个学生类Student,该类封装学生的一些基本信息(学号、姓名、年龄等字段),重载一个带参数的构造函数,为学生的基本信息字段初始化。

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace StudentManagement
    {
        /*
           添加一个学生类Student,该类封装学生的一些基本信息(学号、姓名、年龄等字段),
         * 重载一个带参数的构造函数,为学生的基本信息字段初始化。*/
        [Serializable]
        class Student
        {
            private int stuNum;  //学号
    
            public int StuNum
            {
                get { return stuNum; }
                set { stuNum = value; }
            }
    
            private string stuName;  //姓名
    
            public string StuName
            {
                get { return stuName; }
                set { stuName = value; }
            }
    
            private string stuSex;     //性别
    
            public string StuSex
            {
                get { return stuSex; }
                set { stuSex = value; }
            }
    
            private string stuMajor;    //专业
    
            public string StuMajor
            {
                get { return stuMajor; }
                set { stuMajor = value; }
            }
    
            private string stuClass;    //班级
    
            public string StuClass
            {
                get { return stuClass; }
                set { stuClass = value; }
            }
            
            //构造函数初始化
            public Student(int stuNum, string stuName, string stuSex, string stuMajor, string stuClass)
            {
                this.stuNum = stuNum;
                this.stuName = stuName;
                this.stuSex = stuSex;
                this.stuMajor = stuMajor;
                this.stuClass = stuClass;
            }
        }
    }


    2.4添加一个学生管理类StudentManger,主要包含以下几个方法:第一,添加学生信息方法;第二,显示所有学生信息方法;第三,计算身体质量指数BMI方法(计算方法:BMI=w/h2 ,w是以千克为单位的体重值,h是以米为单位的身高值。BMI的值在20至25之间是正常的)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace StudentManagement
    {
        /*4.	添加一个学生管理类StudentManger,主要包含以下几个方法:
         * 第一,添加学生信息方法;
         * 第二,显示所有学生信息方法;
         * 第三,计算身体质量指数BMI方法
            (计算方法:BMI=w/h2,w是以千克为单位的体重值,h是以米为单位的身高值。BMI的值在20至25之间是正常的)*/
        class StudentManger
        {
            FileAccessOperate faOper = new FileAccessOperate();
    
            //添加学生信息方法
            public void AddStudent()
            {
                List<Student> stuList=new List<Student>();
    
                Console.Write("请输入要添加学生的人数:");
                int totalAdd = 0; //每次输入的学生数
                int count=0;  //记录一个输入的学生数
                //Student[] stuArr=new Student[100];  //记录学生的数组
                int stuNum; //学生属性,初始化
                string stuName,stuSex,stuMajor,stuClass;
             
                //判断输入是否正确           
                try
                {
                    totalAdd = int.Parse(Console.ReadLine());
                    if (totalAdd < 1)
                        throw new Exception();
                }
                catch
                {
                    Console.WriteLine("请输入一个正整数");
                }
    
                while(totalAdd>0)
                {
                    int countSort = count + 1;
                    Console.Write("请输入第{0}学生的学号:", countSort);
                    stuNum=int.Parse(Console.ReadLine());
    
                    Console.Write("请输入第{0}学生的姓名:", countSort);
                    stuName=Console.ReadLine();
    
                    Console.Write("请输入第{0}学生的性别:", countSort);
                    stuSex=Console.ReadLine();
    
                    Console.Write("请输入第{0}学生的专业:", countSort);
                    stuMajor=Console.ReadLine();
    
                    Console.Write("请输入第{0}学生的班级:", countSort);
                    stuClass=Console.ReadLine();
    
                    List<Student> stuListQuery = new List<Student>();
                    int isExist = 0;
                    foreach (Student stu in stuListQuery)
                    {
                        if (stu.StuNum == stuNum)
                        {
                            Console.WriteLine("学生的学号不可相同!");
                            isExist++;
                            break;
                        }
                    }
                    if (isExist == 1)
                        continue;
                    stuList.Add(new Student(stuNum, stuName, stuSex, stuMajor, stuClass)); //创建一个学生对象
                    count++;
                    totalAdd--;
                   
                #region
                    if(totalAdd==0)   //输入学生到最后一个时提醒是否继续输入
                    {
                        Console.Write("你已经输入{0}个学生,是否继续<y> OR <n>?",count);
                        string isContunue=Console.ReadLine();
    
                        if(isContunue=="y"||isContunue=="Y")
                        {
                            try
                            {
                                totalAdd = int.Parse(Console.ReadLine());
                                if (totalAdd < 1)
                                    throw new Exception();
                            }
                            catch
                            {
                                Console.WriteLine("请输入一个正整数");
                            }
                        }
    
                    }
                #endregion               
                }
                faOper.StuWrite(stuList);  //将数组中的学生信息写到文件中
            }
    
            //读取学生信息
            public void ReadAllStu()
            {
                List<Student> stuList = new List<Student>();
    
                stuList = faOper.StuRead();
                Console.WriteLine("	学号	姓名	性别	专业	班级");
    
                if (stuList.Count == 0)
                {
                    Console.WriteLine("数据库中没有学生!");
                    return;
                }
    
               foreach(Student stu in stuList)
               {
                    Console.WriteLine("	{0}	{1}	{2}	{3}	{4}", stu.StuNum, stu.StuName, stu.StuSex, stu.StuMajor, stu.StuClass);    
               }
            }
    
            //删除指定学生信息
            public void DeleteNum(int num)
            {
                faOper.DeleteStu(num);
    
            }
    
            //按学号查询学生信息
            public void QueryStu(int num)
            {
                List<Student> stuList = faOper.StuRead();
                int count = 0;
    
                Console.WriteLine("	学号	姓名	性别	专业	班级");
                foreach (Student stu in stuList)
                {
                    if (stu.StuNum == num)
                    {
                        Console.WriteLine("	{0}	{1}	{2}	{3}	{4}", stu.StuNum, stu.StuName, stu.StuSex, stu.StuMajor, stu.StuClass);
                        count++;
                    }
                }
                if(count==0)
                {
                    Console.WriteLine("	数据库中没该学生!");
                }
            }
            //按姓名查询学生信息
            public void QueryStu(string name)
            {
                List<Student> stuList = faOper.StuRead();
                int count = 0;
    
                Console.WriteLine("	学号	姓名	性别	专业	班级");
                foreach (Student stu in stuList)
                {
                    if (stu.StuName==name)
                    {
                        Console.WriteLine("	{0}	{1}	{2}	{3}	{4}", stu.StuNum, stu.StuName, stu.StuSex, stu.StuMajor, stu.StuClass);
                        count++;
                    }
                }
                if (count == 0)
                {
                    Console.WriteLine("	数据库中没该学生!");
                }
            }
            // 第三,计算身体质量指数BMI方法
            //计算方法:BMI=w/h2,w是以千克为单位的体重值,h是以米为单位的身高值。BMI的值在20至25之间是正常的)*/
            public void Health()
            {
                Console.Write("请输入你的体重(单位:千克):");
                int weight = int.Parse(Console.ReadLine());
    
                Console.Write("请输入你的身高(单位:米):");
                double high = double.Parse(Console.ReadLine());
    
                double BMI = weight / high;
    
                if (BMI >= 20 && BMI <= 25)
                {
                    Console.WriteLine("你的身体健康指数正常!");
                    return;            
                }
                else if (BMI < 20)
                {
                    Console.WriteLine("你的身体健康指数偏瘦!");
                    return;
                }
                else
                {
                    Console.WriteLine("你的身体健康指数偏胖!");
                }
            }
        }
    }

    2.5添加用户操作类。至少包含2个方法:一个用于显示登录菜单;一个用于显示学生信息管理主操作菜单。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace StudentManagement
    {
        
        /*
            添加用户操作类。至少包含2个方法:一个用于显示登录菜单;一个用于显示学生信息管理主操作菜单。
         */
        class UserOperate
        {
            //登录判断
            public static  int ShowLogin()
            {
                Console.WriteLine("╭⌒╮┅~ ¤  ╭⌒╮ ╭⌒╮ ");
                Console.WriteLine("╭⌒╭⌒╮╭⌒╮~╭⌒╮︶︶, ︶︶ ");
                Console.WriteLine(",︶︶︶︶,''︶~~ ,''~︶︶  ,'' ");
                Console.WriteLine("╬ ╱◥███◣╬╬╬╬╬╬╬╬╬╬╬"); 
                Console.WriteLine("╬ ︱田︱田 田 ︱          ╬ ");
                Console.WriteLine("╬                 ╬");
                Console.WriteLine("╬ ╭○╮●                 ╬");
               Console.WriteLine(@"╬  /■/■                       ╬");
                Console.WriteLine("╬  <| ||                   ╬");
    
                
    
                int flag = 0;  // 登陆成功标志位
                for (int i = 0; i < 3; i++)
                {
                    Console.Write("请输入登录名:");
                    string userName = Console.ReadLine();
    
                    Console.Write("请输入密码:");
                    string password = Console.ReadLine();
    
                    if (userName == "sa" && password == "123")
                    {
                        Console.WriteLine("现在是{0},恭喜你登陆成功!",DateTime.Now);
                        flag = 1;
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Sorry,用户名或密码错误,你还有{0}次机会",2-i);
                    }
                }
    
                return flag;            
            }
    
            //程序操作主菜单
            public static int MainMenu()
            {
                string flag = "y"; //结束标志位
                int select = 0; //选择菜单项
                StudentManger sm = new StudentManger();//创建一个学生管理类
    
                while (flag == "y" || flag == "Y")
                {
                    Console.WriteLine("	1.添加学生信息
    	2.删除学生信息
    	3.查询所有学生信息
    	4.按学号查询学生信息
    	5.按姓名查询学生信息
    	6.个人健康指数查询
    	7.退出系统");
                    Console.Write("请选择:");
                    try
                    {
                        select = int.Parse(Console.ReadLine());
                        if (select < 1 || select > 7)
                            throw new Exception();
                    }
                    catch
                    {
                        Console.WriteLine("请输入1~7的整数");
                        continue;
                    }
    
                    switch (select)
                    { 
                        case 1:
                            sm.AddStudent();//添加学生信息
                            break;
                        case 2:
                            Console.Write("请输入要删除学生的学号:");
                            int num;
                            while (true)
                            {
                                try
                                {
                                    num = int.Parse(Console.ReadLine());
                                    break;
                                }
                                catch (Exception)
                                {
                                    Console.Write("学号为整数请重新输入:");
                                }             
                            }                                  
                            sm.DeleteNum(num);
                            break;
                            //删除学生信息
                        case 3:
                            sm.ReadAllStu();
                            break;
                            //查询所有学生信息
                        case 4:
                            Console.Write("请输入要查询学生的学号:");
                            int num1;
                            while (true)
                            {
                                try
                                {
                                    num1 = int.Parse(Console.ReadLine());
                                    break;
                                }
                                catch (Exception)
                                {
                                    Console.Write("学号为整数请重新输入:");
                                }             
                            }
                            sm.QueryStu(num1);                        
                            break;
                            //按学号查询学生信息
                        case 5:
                            Console.Write("请输入要查询学生的姓名:");
                            string name = string.Format(Console.ReadLine());
                            sm.QueryStu(name);
                            //按姓名查询学生信息
                             break;
                        case 6:
                             sm.Health();
                            //个人健康指数查询
                             break;
                        case 7:
                             return 0;
                            //退出系统
                        default:
                            Console.WriteLine("你的输入有误");
                            break;
                    }
                   
                }
                return 1;
            }
        }
    }

    2.6.程序入口类 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace StudentManagement
    {
        class Program
        {
            static void Main(string[] args)
            {           
                int flagLogin= UserOperate.ShowLogin();//三次登录机会全失败,返回0
                int flagMenu=UserOperate.MainMenu();//退出程序返回0
    
                if (flagLogin==0||flagMenu==0)
                {
                    return;
                }
                Console.ReadKey();
            }
        }
    }

     

    3、系统运行界面截图

    图1:项目类分布情况(StudentList用不到)

    图2:程序登陆界面、程序主菜单

    图3:添加学生模块

    图4:查询所有学生

    图5:按学号查询学生

    图6:按姓名查询

    图7:按学号删除学生

    图8:BMI测试

  • 相关阅读:
    快速幂(Fast Pow)
    半小时写完替罪羊重构点分树做动态动态点分治之紫荆花之恋的wyy贴心指导
    POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
    二分图判定
    Tarjan求点双连通分量
    POJ1523 SPF 单点故障
    OI比赛常数优化
    Tarjan求割点
    NOIP2015 D1T1 神奇的幻方
    NOIP2016 D2T2 蚯蚓
  • 原文地址:https://www.cnblogs.com/xiangyangzhu/p/4239802.html
Copyright © 2011-2022 走看看