程序|信息管理 using System; namespace SEI.DL88250.SourceCodes.CSharp string Name string Gender string ClassNum string Specialty string BDay public interface IStudentIMP class Student public string Name public string Gender public string ClassNum public string Specialty public string BDay } class StudentIMP : IStudentIMP Console.WriteLine(_saveInfo[0]); // Add a new student's info _stdInfo.Add(tmpStd.ID, tmpStd); // store } // Search a student's info. Time complexity: O(n) // Display all students' info IEnumerator e = _stdInfo.GetEnumerator(); // Delete a student's info [STAThread] // single-thread model
using System.Collections;
using System.IO;
{
public interface IStudent
{
// Properties
string ID
{
get;
set;
}
{
get;
set;
}
{
get;
set;
}
{
get;
set;
}
{
get;
set;
}
{
get;
set;
}
}
{
// 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);
}
{
// 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;
}
}
{
get
{
return _name;
}
set
{
_name = value;
}
}
{
get
{
return _gender;
}
set
{
_gender = value;
}
}
{
get
{
return _classNum;
}
set
{
_classNum = value;
}
}
{
get
{
return _specialty;
}
set
{
_specialty = value.ToString();
}
}
{
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);
}
{
// 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;
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();
}
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);
// 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();
}
}
public void DisplayInfo()
{
if (0 == _stdInfo.Count)
{
Console.WriteLine("No student info!");
return;
}
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);
}
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);
}
}
// 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#下的学生信息管理程序
80酷酷网 80kuku.com