/**
*
* C#
* StudentRecord list ,
* 。 , ,
*/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace studentRecord
{
class Program
{
static void Main(string[] args)
{
Student s1 = new Student();
s1.id = "20081120029";
s1.name = " ";
s1.major = " ";
s1.phone = "12345678";
s1.sex = " ";
s1.email = "[email protected]";
Student s2 = new Student();
s2.id = "20081120030";
s2.name = " ";
s2.major = " ";
s2.phone = "1234567889";
s2.sex = " ";
s2.email = "[email protected]";
string path = "record.dat";
StudentRecord sr = new StudentRecord();
Console.WriteLine("
{0}", s1.ToString());
Console.WriteLine("
{0}", s2.ToString());
sr.Add(s1, path); //
sr.Add(s2, path);
Console.WriteLine(" ");
Console.WriteLine("-------------------------------");
Console.WriteLine(" 20081120029 ");
Student s = sr.Search("20081120029", path);
Console.WriteLine("
{0}", s.ToString());
Console.WriteLine("-------------------------------");
Console.WriteLine(" 20081120029 ");
s.major = " ";
s.email = "[email protected]";
sr.Update(s, path); //
s = sr.Search("20081120029", path);
Console.WriteLine(" 20081120029
{0}", s.ToString());
Console.WriteLine("-------------------------------");
Console.WriteLine(" 20081120029
");
sr.Delete("20081120029", path);
List<Student> list = sr.GetAll(path);
Console.WriteLine("
");
foreach (Student sn in list)
{
Console.WriteLine("{0}", sn.ToString());
}
if (File.Exists(path)) // ,
{
File.Delete(path);
}
Console.ReadKey();
}
}
[Serializable] //
class Student {
public string email { set; get; }
public string name { set; get; }
public string id { set; get; }
public string sex { set; get; }
public string birth { set; get; }
public string phone { set; get; }
public string major { set; get; }
public override string ToString()
{
return " :" + this.id + ",
" + " :" + this.name + ",
" + " :" + this.sex + ",
" + " :" + this.birth + ",
" + " :" + this.major + ",
" + " :" + this.phone + ",
" + "email:" + this.email + ",
";
}
}
class StudentRecord
{
private IFormatter formatter;
private Stream stream;
private List<Student> list;
public StudentRecord()
{
formatter = new BinaryFormatter();
}
public List<Student> GetAll(string path)
{
OpenFileForRead(path);
return list;
}
public void Add(Student st,string path)
{
OpenFileForRead(path);
if (list == null)
{
list = new List<Student>();
list.Add(st);
OpenFileForWrite(path); //
return;
}else {
list.Add(st);
OpenFileForWrite(path); //
return;
}
}
public void Update(Student st,string path)
{
OpenFileForRead(path);
if (list == null)
{
list = new List<Student>();
list.Add(st);
OpenFileForWrite(path); //
}
else
{
for(int i=0;i<list.Count;i++)
{
if (list[i].id.Equals(st.id))
{
list.RemoveAt(i); //
list.Add(st); //
break;
}
}
}
OpenFileForWrite(path); //
}
public void Delete(string id,string path)
{
OpenFileForRead(path);
if (list == null)
{
Console.WriteLine(" ");
return;
}
else
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].id.Equals(id)) //
{
list.RemoveAt(i); //
break;
}
}
OpenFileForWrite(path); //
}
}
public Student Search(string id, string path)
{
Student s= null;
OpenFileForRead(path);
if (list == null || list.Count == 0)
{
Console.WriteLine(" ");
return null;
}else
{
foreach (Student st in list)
{
if (st.id.Equals(id)) // id
{
s = st; //
return s;
}
}
}
return null;
}
private void OpenFileForRead(string path)
{
if (File.Exists(path)) //
{
stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); //
this.list = (List<Student>)formatter.Deserialize(stream); //
stream.Close();
}
else
{
this.list = null;
}
}
private void OpenFileForWrite(string path)
{
stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, this.list);
stream.Close();
}
}
}