首页 > 开发 > 综合 > 正文

C#下的学生信息管理程序

2024-07-21 02:26:34
字体:
来源:转载
供稿:网友


using system;
using system.collections;
using system.io;

namespace sei.dl88250.sourcecodes.csharp
{
 public interface istudent
 {
 // properties
  string id
  {
   get;
   set;
  }

  string name
  {
   get;
   set;
  }

  string gender
  {
   get;
   set;
  }

  string classnum
  {
   get;
   set;
  }

  string specialty
  {
   get;
   set;
  }

  string bday
  {
   get;
   set;
  }
 }

 public interface istudentimp
 {
 // methods
  // add a new student's info into arraylist
  void addinfo();
  // print out all students' info to console
  void displayinfo();
  // delete a student's info
  void dropinfo();
  // save all students' info into disk
  void savefile();
  // search a student's info by id
  // if find info return the student's id hash code,
  // otherwise return reserved id(20051120000) hash code
  int searchinfo(string id);
 }


 class student
 {
 // fields
  private string _id;
  private string _name;
  private string _gender;
  private string _classnum;
  private string _specialty;
  private system.datetime _bday;
 
 // properties
  public string id
  {
   get
   {
    return _id;
   }
   set
   {
    _id = value;
   }
  }

  public string name
  {
   get
   {
    return _name;
   }
   set
   {
    _name = value;
   }
  }

  public string gender
  {
   get
   {
    return _gender;
   }
   set
   {
    _gender = value;
   }
  }

  public string classnum
  {
   get
   {
    return _classnum;
   }
   set
   {
    _classnum = value;
   }
  }

  public string specialty
  {
   get
   {
    return _specialty;
   }
   set
   {
    _specialty = value.tostring();
   }
  }

  public string bday
  {
   get
   {
    return _bday.toshortdatestring();
   }
   set
   {
    _bday = system.datetime.parse(value);
   }
  }
 
 // constructors
  public student(string id, string name, string gender,
          string classnum, string specialty, string bday)
  {
   _id = id;
   _name = name;
   _gender = gender;
   _classnum = classnum;
   _specialty = specialty;
   _bday = system.datetime.parse(bday);
  }
 

 }

 class studentimp : istudentimp
 {
 // fileds
  private static string[] _mainmenu; // function description
  private static string[] _addinfo; // add info prompt
  private static string[] _saveinfo; // save file prompt
  private static string[] _modifyinfo; // modify menu prompt
  private static string[] _helpinfo; // help infomation
  private hashtable _stdinfo;  // store student info
 // consturctors
  studentimp()
  {
   // initial some prompt info
   _mainmenu = new string[]{
    "1. add",
    "2. modify",
    "3. drop",
    "4. save file",
    "5. display info",
    "6. help",
    "7. exit"
   };
   _helpinfo = new string[]{
    "1. id must unique.",
    "2. modify function can't change id. if you want to change the id, /n   please drop this record and new one again.",
    "3. birthday format is keep to iso standard."};
   _addinfo = new string[]{
    "please type in some infomation of this student.",
    "id: ",
    "name: ",
    "gender: ",
    "classnum: ",
    "specialty: ",
    "birthday(198x/xx/xx): "
   };
   _saveinfo = new string[]{
    "please input save file name: "
   };
   _modifyinfo = new string[]{
    "please choose the attribute you want to modify.",
    "1. name",
    "2. gender",
    "3. classnum",
    "4. specialty",
    "5. birthday"
   };
   // initial storage space
   this._stdinfo = new hashtable();
  }
 // indexers
  public student this[string id]
  {
   get
   {
    if (_stdinfo.contains(id))
    {// exists the student's info
     return (student)_stdinfo[id];
    }
    else
    {
     return null;
    }
   }
  }
 // methods
  // store student info into disk file
  public void savefile()
  {
   string filename;

   console.writeline(_saveinfo[0]);
   filename = console.readline();
   streamwriter fwriter = file.createtext(filename);
   ienumerator e = _stdinfo.getenumerator();
   student std;
   fwriter.writeline("id           name             gender  class   specialty   birthday");
   while (e.movenext())
   {
    dictionaryentry de = (dictionaryentry)(e.current);
    std = (student)de.value;
    fwriter.writeline("{0, -13}{1, -17}{2, -10}{3, -6}{4, -12}{5, -10}",
        std.id, std.name,
        std.gender, std.classnum,
        std.specialty, std.bday);
   }
   fwriter.writeline("/t/t/t/t/t/tstudents total: {0}", _stdinfo.count);
   fwriter.close();
  }

  // add a new student's info
  public void addinfo()
  {
   string id, name, gender, classnum, specialty, bday;
  
   console.writeline(_addinfo[0]);
   console.writeline(_addinfo[1]);
   id = console.readline();
   console.writeline(_addinfo[2]);
   name = console.readline();
   console.writeline(_addinfo[3]);
   gender = console.readline();
   console.writeline(_addinfo[4]);
   classnum = console.readline();
   console.writeline(_addinfo[5]);
   specialty = console.readline();
   console.writeline(_addinfo[6]);
   bday = console.readline();
   student tmpstd = new student(id, name, gender, classnum, specialty, bday);

   _stdinfo.add(tmpstd.id, tmpstd); // store

  }

  // search a student's info. time complexity: o(n)
  // if find info return id hash code,
  // otherwise return reserved hash code
  public int searchinfo(string id)
  {
   if ((null != _stdinfo[id])
       && (id == ((student)_stdinfo[id]).id))
   {// find it
    return _stdinfo[id].gethashcode();
   }
   else
   {// no find
    // return reserved id hash code
    return "20051120000".gethashcode();
   }
  
  }

  // display all students' info
  public void displayinfo()
  {
   if (0 == _stdinfo.count)
   {
    console.writeline("no student info!");
    return;
   }

   ienumerator e = _stdinfo.getenumerator();
   student std;
   console.writeline("id           name             gender  class   specialty   birthday");
   while (e.movenext())
   {
    dictionaryentry de = (dictionaryentry)(e.current);
    std = (student)de.value;
    console.writeline( "{0, -13}{1, -17}{2, -10}{3, -6}{4, -12}{5, -10}",
        std.id, std.name,
        std.gender, std.classnum,
        std.specialty, std.bday);
   }
   console.writeline("/t/t/t/t/t/tstudents total: {0}", _stdinfo.count);
  }

  // delete a student's info
  public void dropinfo()
  {
   console.writeline("please input the student's id: ");
   string id = console.readline();
   if ("20051120000".gethashcode() == searchinfo(id))
   {// no find
    console.writeline("no find!");
    return;
   }
   else
   {// find a student
    console.writeline("confirm delete it? (y/n)");
    string confirm = console.readline();
    if ("y" == confirm.tolower())
    {// delete it
     _stdinfo.remove(_stdinfo[id]);
    }
   }
   return;
  }
 
  // modify a student's info
  public void modifyinfo()
  {
   console.writeline("please input the student's id: ");
   string id = console.readline();
   if ("20051120000".gethashcode() == searchinfo(id))
   {// no find
    console.writeline("no find!");
    return;
   }
   else
   {// find a student
    student std = (student)_stdinfo[id];
    console.writeline("id           name             gender  class   specialty   birthday");
    console.writeline("{0, -13}{1, -17}{2, -10}{3, -6}{4, -12}{5, -10}",
        std.id, std.name, std.gender,
        std.classnum, std.specialty, std.bday);
    console.writeline("do you want to modify the info? (y/n)");
    string choice = console.readline();
    if (choice.tolower() == "n")
    {// do not modify
     return;
    }
   
    // display student's attributes
    foreach (string att in _modifyinfo)
    {
     console.writeline(att);
    }
   
    // select a attribute to modify
    string attchoice = console.readline();
    switch (attchoice)
    {
     case "1":
     {
      console.writeline(_addinfo[2]);
      std.name = console.readline();
      break;
     }
     case "2":
     {
      console.writeline(_addinfo[3]);
      std.gender = console.readline();
      break;
     }
     case "3":
     {
      console.writeline(_addinfo[4]);
      std.classnum = console.readline();
      break;
     }
     case "4":
     {
      console.writeline(_addinfo[5]);
      std.specialty = console.readline();
      break;
     }
     case "5":
     {
      console.writeline(_addinfo[6]);
      std.bday = console.readline();
      break;
     }
     default:
     {
      console.writeline("functioin undefine!");
      break;
     }
    }
   }
  }
 
  // main menu, display program functions
  private static void mainmenu()
  {
   foreach (string func in _mainmenu)
   {
    console.writeline(func);
   }
  }

  [stathread] // single-thread model
  // program entry point
  static void main()
  {
   console.foregroundcolor = consolecolor.darkgreen;
   console.title = "student imp -lab3   by 88250";
 
   studentimp stdimp = new studentimp();
   string funcselect;
   do
   {
    console.clear();
    mainmenu();
    funcselect = console.readline();
    switch (funcselect)
    {
     case "1":
     {
      stdimp.addinfo();
      break;
     }
     case "2":
     {
      stdimp.modifyinfo();
      break;
     }
     case "3":
     {
      stdimp.dropinfo();
      break;
     }
     case "4":
     {
      stdimp.savefile();
      break;
     }
     case "5":
     {
      stdimp.displayinfo();
      break;
     }
     case "6":
     {
      foreach (string hlpinfo in _helpinfo)
      {
       console.writeline(hlpinfo);
      }
      break;
     }
     case "7":
     {
      return;
     }
     default:
     {
      console.writeline("function undefine!");
      break;
     }
    }
    console.writeline("press any key to continue....");
    console.readline();
   }
   while (true);
  }
 }
}
 


 



上一篇:C#的双缓存技术

下一篇:C#与RSS亲密接触

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表