程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#下的學生信息管理程序

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

編輯:關於C語言
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);
  }
 }
}
 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved