using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace RelationModel
{
class Program
{
static void Main(string[] args)
{
//创建数据表对象
Table rocketsRoster = new Table();
//为数据表定义字段
rocketsRoster.AddField(new Field("球衣号", "Number"));
rocketsRoster.AddField(new Field("球员姓名", "Varchar(20)"));
rocketsRoster.AddField(new Field("场上位置", "Varchar(30)"));
//在表中插入记录
rocketsRoster.AddRecord(new Record(new Cell[]{ new Cell(11), new Cell("Yao Ming"), new Cell("Center")}));
rocketsRoster.AddRecord(new Record(new Cell[] { new Cell(1), new Cell("Tracy Macgrady"), new Cell("Shooting Guard") }));
rocketsRoster.AddRecord(new Record(new Cell[] { new Cell(12), new Cell("Rafer Alston"), new Cell("Point Guard") }));
rocketsRoster.AddRecord(new Record(new Cell[] { new Cell(99), new Cell("Ron Artest"), new Cell("Small Forword") }));
rocketsRoster.AddRecord(new Record(new Cell[] { new Cell(4), new Cell("Luis Scolar"), new Cell("Power Forword") }));
//输出表中的数据
rocketsRoster.PrintTable();
//记录查询
string userInput = "";
while (true)
{
Console.WriteLine("请输入球衣号来查询该球员,按/'E/'退出程序:");
userInput = Console.ReadLine().Trim();
if (userInput.ToUpper() == "E")
{
break;
}
if (!rocketsRoster.SearchRecord(userInput))
{
Console.WriteLine("该球员不存在,请重新查询!");
}
}
}
}
/// <summary>
/// 数据单元 实体
/// </summary>
public class Cell
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="value"></param>
public Cell(object cellValue)
{
CellValue = cellValue;
}
/// <summary>
/// 数据单元值
/// </summary>
private object cellValue;
/// <summary>
/// 所属记录
/// </summary>
private Record parentRecord;
/// <summary>
/// 所属字段
/// </summary>
private Field parentField;
/// <summary>
/// 获取或设置 数据单元值
/// </summary>
public object CellValue
{
get
{
return cellValue;
}
set
{
cellValue = value;
}
}
/// <summary>
/// 获取或设置 所属记录
/// </summary>
public Record ParentRecord
{
get
{
return parentRecord;
}
set
{
parentRecord = value;
}
}
/// <summary>
/// 获取或设置 所属字段
/// </summary>
public Field ParentField
{
get
{
return parentField;
}
set
{
parentField = value;
}
}
/// <summary>
/// 打印数据单元
/// </summary>
/// <returns></returns>
public override string ToString()
{
return CellValue.ToString();
}
}
/// <summary>
/// 记录 实体
/// </summary>
public class Record
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="cells">初始化的数据单元集合</param>
public Record(Cell[] cells)
{
ContainCells.AddRange(cells);
}
/// <summary>
/// 包含的数据单元
/// </summary>
private ArrayList containCells = new ArrayList();
/// <summary>
/// 所属数据表
/// </summary>
private Table parentTable;
/// <summary>
/// 获取或设置 包含的数据单元
/// </summary>
public ArrayList ContainCells
{
get
{
return containCells;
}
set
{
containCells = value;
}
}
/// <summary>
/// 获取或设置 所属数据表
/// </summary>
public Table ParentTable
{
get
{
return parentTable;
}
set
{
parentTable = value;
}
}
/// <summary>
/// 添加数据单元
/// </summary>
/// <param name="cell"></param>
public void AddCell(Cell cell)
{
ContainCells.Add(cell);
}
/// <summary>
/// 删除数据单元
/// </summary>
/// <param name="cell"></param>
public void DropCell(Cell cell)
{
ContainCells.Remove(cell);
}
/// <summary>
/// 打印记录
/// </summary>
/// <returns></returns>
public override string ToString()
{
string output = "";
foreach (Cell cell in ContainCells)
{
output = output + cell.ToString() + "/t";
}
return output;
}
}
/// <summary>
/// 字段 实体
/// </summary>
public class Field
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name">字段名陈</param>
/// <param name="type">字段类型</param>
public Field(string name, string type)
{
Name = name;
Type = type;
}
/// <summary>
/// 字段名称
/// </summary>
private string name;
/// <summary>
/// 字段类型
/// </summary>
private string type;
/// <summary>
/// 所属数据表
/// </summary>
private Table parentTable;
/// <summary>
/// 获取或设置 字段名称
/// </summary>
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
/// <summary>
/// 获取或设置 字段类型
/// </summary>
public string Type
{
get
{
return type;
}
set
{
type = value;
}
}
/// <summary>
/// 获取或设置 所属数据表
/// </summary>
public Table ParentTable
{
get
{
return parentTable;
}
set
{
parentTable = value;
}
}
/// <summary>
/// 打印字段
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Name.ToString();
}
}
/// <summary>
/// 数据表 实体
/// </summary>
public class Table
{
/// <summary>
/// 表名称
/// </summary>
private string name;
/// <summary>
/// 包含的字段
/// </summary>
private ArrayList containFields = new ArrayList();
/// <summary>
/// 包含的记录
/// </summary>
private ArrayList containRecords = new ArrayList();
/// <summary>
/// 获取或设置 名称
/// </summary>
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
/// <summary>
/// 获取或设置 包含的字段
/// </summary>
public ArrayList ContainFields
{
get
{
return containFields;
}
set
{
containFields = value;
}
}
/// <summary>
/// 获取或设置 包含的记录
/// </summary>
public ArrayList ContainRecords
{
get
{
return containRecords;
}
set
{
containRecords = value;
}
}
/// <summary>
/// 添加字段
/// </summary>
/// <param name="field">字段</param>
public void AddField(Field field)
{
ContainFields.Add(field);
}
/// <summary>
/// 删除字段
/// </summary>
/// <param name="field">字段</param>
public void DropField(Field field)
{
ContainFields.Remove(field);
}
/// <summary>
/// 添加记录
/// </summary>
/// <param name="record">记录</param>
public void AddRecord(Record record)
{
ContainRecords.Add(record);
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="record">记录</param>
public void DropRecord(Record record)
{
ContainFields.Remove(record);
}
/// <summary>
/// 打印数据表
/// </summary>
/// <returns></returns>
public override string ToString()
{
string output = "";
foreach (Record record in ContainRecords)
{
output = output + record.ToString() + "/n/r";
}
return output;
}
/// <summary>
/// 打印标题行
/// </summary>
/// <returns></returns>
public string PrintFieldsRow()
{
string output = "";
foreach (Field field in ContainFields)
{
output = output + field.ToString() + "/t";
}
output = output + "/n";
return output;
}
/// <summary>
/// 打印表
/// </summary>
/// <returns></returns>
public void PrintTable()
{
Console.WriteLine(PrintFieldsRow() + ToString());
}
/// <summary>
/// 根据记录
/// </summary>
public bool SearchRecord(string keyword)
{
bool result = false;
foreach (Record record in ContainRecords)
{
if (record.ContainCells[0].ToString() == keyword)
{
Console.WriteLine(record.ToString());
result = true;
}
}
return result;
}
}
}