标题:大家能帮我改改着程序吗?
只看楼主
heqinwu8
Rank: 2
来 自:黄土高坡
等 级:论坛游民
帖 子:65
专家分:58
注 册:2009-7-11
结帖率:100%
已结贴  问题点数:20 回复次数:4 
大家能帮我改改着程序吗?
学生管理系统的,实现功能添加、删除、修改、查询功能,但是这个程序没应用OOP思想,都是些比较低级编程思想,现在想使用接口、继承、多态的一些特性修改这个程序,让这个程序更简洁明了
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    public class Student
    {
        public Student(int _number,string _name)
        {
            number = _number;
            name = _name;
        }
        public int number = 0;
        public string name = "";
    }
    class Program
    {
        public static List<Student> stu = new List<Student>();
        public static void Create()   //添加函数
        {
            bool flag=true;
            char a;
            int m;
            string n;
                while (flag)
                {
                    Console.WriteLine("请输入您要添加的数据(按s开始输入数据,按e结束输入数据,学号和姓名用回车键隔开):");
                    a = char.Parse(Console.ReadLine());
                    switch (a)
                    {
                        case 's':
                            bool t=true;
                            while (t)
                            {
                                Console.WriteLine("开始输入:");
                                m = int.Parse(Console.ReadLine());
                                n = Console.ReadLine();
                                bool c = false;
                                if (m == 0 && n == "")
                                {
                                    Console.WriteLine("您输入的格式有误,请重新输入!");
                                }
                                else
                                {
                                foreach (Student s in stu)
                                {
                                    if (s.number == m && s.name == n)
                                    {
                                    Console.WriteLine("您添加的数据已存在!");
                                    c = true;
                                    break;
                                    }
                                 }
                                    if (!c)
                                    {
                                    stu.Add(new Student(m, n));
                                    t = false;
                                    }
                                }
                            }
                            break;
                        case 'e':
                            flag = false; Console.WriteLine("输入结束!"); break;
                        default:
                            Console.WriteLine("您输入的格式有误,请重新输入!"); break;
                    }
                }
                return;
        }
        public static void Retrieve()   //查询函数
        {
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("请输入s查找数据,g返回:");
                int num;
                string na;
                char a;
                a = char.Parse(Console.ReadLine());
                switch (a)
                {
                    case 's':
                        {
                            num = int.Parse(Console.ReadLine());
                            na = Console.ReadLine();
                            bool fl = false;
                            if (num == 0 && na == "")
                            {
                                Console.WriteLine("您输入的格式不正确,请重新输入!");
                                break;
                            }
                            else
                            {
                                    foreach (Student s in stu)
                                    {
                                        if (s.number==num && s.name==na)
                                        {
                                            Console.WriteLine("您查找的数据已存在!");
                                            fl = true;
                                            break;
                                        }
                                    }
                                    if (!fl)
                                    {
                                        Console.WriteLine("您查找的数据不存在!");
                                    }
                            }
                        } break;
                    case 'g':
                        flag = false; return;
                    default:
                        Console.WriteLine("输入错误!"); break;
                }
                }
        }
        public static void Delete()      //删除函数
        {
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("请输入您要删除的数据:");
                int num;
                num = int.Parse(Console.ReadLine());
                string na;
                na = Console.ReadLine();
                bool t = false;
                if (num == 0 && na == "")
                {
                    Console.WriteLine("输入的数据有误!");
                }
                else
                {
                    foreach (Student s in stu)
                    {
                        if (s.number == num && s.name == na)
                        {
                            stu.Remove(s);
                            Console.WriteLine("数据已删除!");
                            t = true;
                            return;
                        }
                    }
                    if (!t)
                    {
                        Console.WriteLine("输入的数据不存在!");
                    }
                }
            }
        }
        public static void Update()         //修改函数
        {
            char a;
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("输入n修改姓名,输入m修改学号,其他键退出");
                a = char.Parse(Console.ReadLine());
                switch (a)
                {
                    case 'n':
                        {
                            Console.WriteLine("请输入要修改数据的学号:");
                            int num;
                            num = int.Parse(Console.ReadLine());
                            if (num == 0)
                            {
                                Console.WriteLine("您输入的格式有误!");
                            }
                            else
                            {
                                bool fl = false;
                                foreach (Student s in stu)
                                {
                                    if (s.number == num)
                                    {
                                        Console.WriteLine("请输入修改后的姓名:");
                                        fl = true;
                                        s.name = Console.ReadLine();
                                        break;
                                    }
                                }
                                    if (!fl)
                                    {
                                        Console.WriteLine("该学号不存在!");
                                    }
                            }
                        } break;
                    case 'm':
                        {
                            bool t = false;
                            Console.WriteLine("请输入要修改数据的姓名:");
                            string na;
                            int num;
                            na = Console.ReadLine();
                            if (na == "")
                            {
                                Console.WriteLine("您输入的格式有误!");
                            }
                            else
                            {
                                foreach (Student s in stu)
                                {
                                    if (s.name == na)
                                    {
                                        Console.WriteLine("请输入修改后的学号:");
                                        num = int.Parse(Console.ReadLine());
                                        bool e = false;
                                        foreach (Student w in stu)
                                        {
                                            if (w.number == num)
                                            {
                                                Console.WriteLine("学号已重复,请重新输入!");
                                                e = true;
                                                break;
                                            }
                                        }
                                        if (!e)
                                        {
                                            s.number = num;
                                        }
                                        t = true;
                                        break;
                                    }
                                }
                                if (!t)
                                {
                                    Console.WriteLine("该姓名不存在!");
                                }
                            }
                        } break;
                    default:
                        flag = false; break;
                }
            }
            return;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("已存在的数据如下:");
            stu.Add(new Student(160801, "honey"));
            stu.Add(new Student(160802, "love"));
            foreach (Student s in stu)
            {
                Console.WriteLine("{0},{1}", s.number, s.name);
            }
            bool flag=true;
            char a;
            while (flag)
            {
                Console.WriteLine("学生信息管理:输入c添加数据,输入r查找数据,输入d删除数据,输入u更改数据,按其他键退出");
                a=char.Parse(Console.ReadLine());
                switch (a)
                {
                    case 'c':
                        Create(); break;
                    case 'r':
                        Retrieve(); break;
                    case 'd':
                        Delete(); break;
                    case 'u':
                        Update(); break;
                    default:
                        Console.WriteLine("感谢您的使用!"); flag = false; break;
                }
            }
        }
    }
}
搜索更多相关主题的帖子: 改改 
2009-09-24 12:34
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
得分:0 
怎么还用控制台程序啊

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-09-24 13:37
heqinwu8
Rank: 2
来 自:黄土高坡
等 级:论坛游民
帖 子:65
专家分:58
注 册:2009-7-11
得分:0 
恩,就是想从简单的控制台来了解这个OOP思想

戒烟真难啊,去楼顶抽个,没人发现
2009-09-24 21:02
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
得分:0 
可以做成类似linux命令的形式
代码我正在尝试

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-09-24 22:02
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
得分:20 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApplication3
{
    public class Student
    {
        public string number = "0";
        public string name = "";
 
        public Student(string _number, string _name)
        {
            number = _number;
            name = _name;
        }
         
    }
    class Program
    {
        public static List<Student> stu = new List<Student>();
 
        public static Student studenttemp ;
 
        private static bool ParseCommand(string str)
        {
            string[] strtemp = str.Split(' ');
 
            if (strtemp.Length != 3)
            {
                return false;
            }
 
            studenttemp = new Student(strtemp[1], strtemp[2]);
 
            switch (strtemp[0])
            {
                case "ADD": ADD(); break;
                case "SELECT": SELECT(); break;
                case "DELETE": DELETE(); break;
                case "MODIFYNUMBER": MODIFYNUMBER(); break;
                case "MODIFYNAME": MODIFYNAME(); break;
                default:   return false;
            }
           
            return true;
        }
 
        public static void ADD()
        {
            stu.Add(studenttemp);
 
            Console.WriteLine("添加成功");
        }
 
        public static void SELECT()
        {
            bool exist = false;
            foreach (Student s in stu)
            {
                if (s.name==studenttemp.name&&s.number==studenttemp.number)
                {
                    Console.WriteLine("该学生已存在表中!");
                    exist = true;
                    break;
                }
            }
            if (!exist)
            {
                Console.WriteLine("该学生没有在表中!");
            }
        }
 
        public static void DELETE()
        {
            bool hasdelete = false;
            foreach (Student s in stu)
            {
                if (s.name == studenttemp.name && s.number == studenttemp.number)
                {
                    stu.Remove(s);
                    Console.WriteLine("删除成功!");
                    hasdelete = true;
                    break;
                }
            }
            if (!hasdelete)
            {
                Console.WriteLine("删除失败!");
            }
        }
 
        public static void MODIFYNUMBER()
        {
            bool changed = false;
            foreach (Student s in stu)
            {
                if (s.name==studenttemp.name)
                {
                    s.number = studenttemp.number;
                    Console.WriteLine("学号修改成功!");
                    changed = true;
                    break;
                }
            }
            if (!changed)
            {
                Console.WriteLine("学号修改失败!");
            }
        }
 
        public static void MODIFYNAME()
        {
            bool changed = false;
            foreach (Student s in stu)
            {
                if (s.number == studenttemp.number)
                {
                    s.name = studenttemp.name;
                    Console.WriteLine("姓名修改成功!");
                    changed = true;
                    break;
                }
            }
            if (!changed)
            {
                Console.WriteLine("姓名修改失败!");
            }
        }
 
 
 
        static void Main(string[] args)
        {
            
            stu.Add(new Student("160801", "honey"));
            stu.Add(new Student("160802", "love"));
            stu.Add(new Student("160803", "love1"));
            stu.Add(new Student("160804", "love2"));
 
            Console.WriteLine("已存在的数据如下:");
            Console.WriteLine("######################################");
            foreach (Student s in stu)
            {
                Console.WriteLine("{0} {1}", s.number, s.name);
            }
            Console.WriteLine("######################################\n\n");
 
            Console.WriteLine("学生信息管理:\n添加数据:ADD 学号 姓名\n查找数据:SELECT 学号 姓名\n删除数据:DELETE 学号 姓名\n更改学号:MODIFYNUMBER 学号 姓名\n更改姓名:MODIFYNAME 学号 姓名\n");
            Console.WriteLine("请输入命令:\n");
 
            while (true)
            {
                while (!ParseCommand(Console.ReadLine()))
                {
                    Console.WriteLine("输入有误");                    
                }
 
                Console.WriteLine("######################################");
                foreach (Student s in stu)
                {
                    Console.WriteLine("{0} {1}", s.number, s.name);
                }
                Console.WriteLine("######################################\n\n");
            }
        }
    }
}

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-09-24 23:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-286649-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.215732 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved