Program | Information Management
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,
//Otherwi Se 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 are want to 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 the 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 for your 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 ' 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 ' 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 (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;
Todo
{
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);
}
}
}