using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.OleDb;
namespace ConsoleApplication作业
{
//class student : IComparable
//{
//
public int nID;
//
public string szName;
//
public int nAge;
//
public string szAddress;
//
public string szPhoto;
//
public virtual int CompareTo(object obj)
//
{
//
student stu = (student)obj;
//
if (this.nID > stu.nID)
//
return -1;
//
else if (this.nID == stu.nID)
//
return 0;
//
else
//
return 1;
//
}
//}
class student : IComparable
{
public int nID;
public string szName;
public string szSex;
public string szNation;
public string szParty;
public string szPhone;
public string szAddress;
public string szPhoto;
public virtual int CompareTo(object obj)
{
student gradeclass = (student)obj;
if (this.(gradeclass.nID) < 0)
return -1;
else if (this.(gradeclass.nID) == 0)
return 0;
else
return 1;
}
}
class Manager
{
private ArrayList stulist = new ArrayList();
//存放学生的集合
public Manager()
{
GetConnect("");
}
public int GetNumber()
{
return stulist.Count;
}
public void Input(student s, int i)
{
//
int nLengh = stus.Length;
//
stus[i] = s;
}
private void GetConnect(String szPath)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\2009级地信九班具体信息3.xls; Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = "SELECT* FROM[Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
System.Data.DataSet myDataSet;
//读取excel的数据集
myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[Sheet1$]");
myConn.Close();
//对dataSet中的数据进行处理
for (int i = 0; i < myDataSet.Tables.Count; i++)
{
for (int j = 0; j < myDataSet.Tables[i].Rows.Count - 1; j++)
{
student stu = new student();
stu.nID = Convert.ToInt32(myDataSet.Tables[i].Rows[j][0]);
stu.szName = myDataSet.Tables[i].Rows[j][1].ToString();
stu.szSex = myDataSet.Tables[i].Rows[j][2].ToString();
stu.szNation = myDataSet.Tables[i].Rows[j][3].ToString();
stu.szParty = myDataSet.Tables[i].Rows[j][4].ToString();
stu.szPhone = myDataSet.Tables[i].Rows[j][5].ToString();
stu.szAddress = myDataSet.Tables[i].Rows[j][6].ToString();
//
stu.szPhoto = myDataSet.Tables[i].Rows[j][7].ToString();
stu.szPhoto = Convert.ToString(stu.nID) + ".jpg";
stulist.Add(stu);
}
}
}
public void Sort()
{
stulist.Sort();
//for (int i = 0; i < stulist.Count; i++)
//{
//
for (int j = i + 1; j < stulist.Count; j++)
//
{
//
if (((student)stulist[j]).nID > ((student)stulist[i]).nID)
//
{
//
student temp = (student)stulist[i];
//
stulist[i] = stulist[j];
//
stulist[j] = temp;
//
}
//
}
//}
}
public void Add()
{
student stu = new student();
Console.WriteLine("请输入要添加几个学生信息:");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.WriteLine("请输入第{0}个学生的信息:", i + 1);
Console.WriteLine("请输入学号:");
stu.nID =Convert .ToInt32
(Console.ReadLine());
if (stu.nID < 0)
{
Console.WriteLine("输入有误,学号应为正值!");
}
Console.WriteLine("请输入姓名:");
stu.szName = Console.ReadLine();
Console.WriteLine("请输入性别:");
stu.szSex = Console.ReadLine();
Console.WriteLine("请输入地址:");
stu.szAddress = Console.ReadLine();
Console.WriteLine("请输入电话号码:");
stu.szPhone = Console.ReadLine();
Console.WriteLine("请输入民族:");
stu.szNation = Console.ReadLine();
Console.WriteLine("请输入政治面貌:");
stu.szParty = Console.ReadLine();
stulist.Add(stu);
Console.WriteLine("添加成功!");
}
}
public void Remove(int num)
{
int j = 0;
int index = 0;
foreach (student
i in stulist )
{
j++;
if (i.nID ==num)
{
index = j;
}
}
if (index == 0)
{
Console.WriteLine("对不起,没有该学员");
}
else
{
stulist .RemoveAt(index);
Console.WriteLine("删除成功");
}
}
public void Contains(int no)
{
int j = 0;
int index = 0;
foreach (student i in stulist)
{
j++;
if (i.nID == no)
{
index = j;
Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7}",i.nID
, i.szName, i.szSex,i.szNation, i.szParty, i.szPhone, i.szAddress, i.szPhoto);
}
}
if (index == 0)
{
Console.WriteLine("对不起,没有该学员");
}
}
public void select(int i)
{
student stu = (student)stulist[i];
Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7}", stu.nID, stu.szName, stu.szSex, stu.szNation, stu.szParty, stu.szPhone, stu.szAddress, stu.szPhoto);
}
public void Print(int i)
{
student stu = (student)stulist[i];
Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7}", stu.nID, stu.szName, stu.szSex, stu.szNation, stu.szParty, stu.szPhone, stu.szAddress, stu.szPhoto);
}
}
class Program
{
static void Main(string[] args)
{
Manager manger = new Manager();
manger.Sort();
for (int i = 0; i < manger.GetNumber(); i++)
{
manger.Print(i);
}
String yesOrNo; ;
do
{
Console.WriteLine("添加按1,删除按2,查询按3,查找按4");
switch (Console.ReadLine())
{
case "1":
manger.Add();
break;
case "2":
Console.WriteLine("请输入要删除学生的学号");
int num = int.Parse(Console.ReadLine());
manger.Remove(num);
break;
case "3":
for (int i = 0; i < manger.GetNumber(); i++)
{
manger.select(i);
}
break;
case "4":
Console.WriteLine("请输入要查找学生的学号");
int no = int.Parse(Console.ReadLine());
manger.Contains(no);
break;
}
Console.WriteLine("是否继续(Y/N)");
yesOrNo = Console.ReadLine();
} while (yesOrNo == "Y" || yesOrNo == "y");
}
}
}
删除那个函数有些问题啊