您的位置:首页 > 其它

Table是怎样炼成的:SaleReport Table的继承者

2007-02-13 23:40 253 查看
有了Table,我们对其继承,实现更符合业务要求的类

1



/**//// <summary>
2

/// 描述一个销售报表
3

/// </summary>
4

public class SaleReport : Table
5





{
6


7

public SaleReport(string reportName)
8

: base(reportName, new ColumnCollection())
9





{
10


11

this.Columns.Add("序号", typeof(int), 0);
12

this.Columns.Add("姓名", typeof(string), null);
13

this.Columns.Add("商品名称", typeof(string), null);
14

this.Columns.Add("日期", typeof(DateTime), null);
15

this.Columns.Add("数量", typeof(double), null);
16

}
17


18



/**//// <summary>
19

/// 报表的名称
20

/// </summary>
21

public string Name
22





{
23

get
24





{
25

return this.Name;
26

}
27

}
28


29



/**//// <summary>
30

/// 加入数据
31

/// </summary>
32

/// <param name="staff"></param>
33

/// <param name="commodity"></param>
34

/// <param name="cash"></param>
35

public void AddRecord(string staff, string commodity, double cash)
36





{
37

Row row = this.NewRow();
38

row["序号"] = this.Rows.Count + 1;
39

row["姓名"] = staff;
40

row["商品名称"] = commodity;
41

row["日期"] = DateTime.Now;
42

row["数量"] = cash;
43


44

this.Rows.Add(row);
45

}
46


47



/**//// <summary>
48

/// 移除数据
49

/// </summary>
50

/// <param name="index"></param>
51

public void RemoveAt(int index)
52





{
53

this.Rows.RemoveAt(index);
54

}
55


56



/**//// <summary>
57

/// 返回销售报表的销售金额
58

/// </summary>
59

/// <returns></returns>
60

public double GetTotal()
61





{
62

double cash = 0;
63

foreach (Row row in this.Rows)
64





{
65

cash += (double)row["数量"];
66

}
67

return cash;
68

}
69


70



/**//// <summary>
71

/// 返回销售报表的销售金额
72

/// </summary>
73

/// <param name="staff">销售员</param>
74

/// <returns></returns>
75

public double GetTotal(string staff)
76





{
77

double cash = 0;
78

foreach (Row row in this.Rows)
79





{
80

if ((string)row["姓名"] == staff)
81





{
82

cash += (double)row["数量"];
83

}
84

}
85

return cash;
86

}
87


88

}

SaleReport对外封装了对Table的处理细节,使用SaleReport的用户不会感觉到在使用Table类

1

SaleReport report = new SaleReport("销售台帐");
2

report.AddRecord("Alex", "Phone", 2600);
3

report.AddRecord("Alex", "PC", 4560);
4

report.AddRecord("Alex", "Table", 234);
5

report.AddRecord("Sidney", "Phone", 2100);
6

report.AddRecord("Sidney", "TV", 4500);
7

report.AddRecord("Tom", "oven", 300);
8

report.AddRecord("Leo", "oven", 240);
9


10

report.Print();

运行的结果是
序号 姓名 商品名称 日期 数量

1 Alex Phone 2007-2-13 23:19:27 2600
2 Alex PC 2007-2-13 23:19:27 4560
3 Alex Table 2007-2-13 23:19:27 234
4 Sidney Phone 2007-2-13 23:19:27 2100
5 Sidney TV 2007-2-13 23:19:27 4500
6 Tom oven 2007-2-13 23:19:27 300
7 Leo oven 2007-2-13 23:19:27 240
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: