实验要求
1. 实现一个学生信息管理系统,包含信息为:学号、姓名、性别、出生年月、专业、备注等信息。
2. 要求为学生信息中的每个字段选择适当的数据类型或自定义类型;
3. 设计的系统中要求用面向对象的方式抽象和封装你所分析出的类;
4. 为你抽象出来的每个类成员定义适当的访问存取权限
5. 为你抽象出来的每个类定义适当的公共接口(属性及索引器)
6. 要求可以对该系统中的学生信息实现:增加、删除、修改及查询的功能;
7. 要求学生信息可保存到一个指定的文本文件中,即支持文件的读写功能。
{
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, 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 inforeturn (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(
" Students 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 itreturn _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(
" Students 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 findConsole.WriteLine("No find!");
return;
}
else
{
// find a studentConsole.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 findConsole.WriteLine("No find!");
return;
}
else
{
// find a studentStudent 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 modifyreturn;
}
// 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