首页 新闻 会员 周边 捐助

求高手优化面向对象设计问题

0
悬赏园豆:60 [待解决问题]
  1 最近有高手给我和朋友出题,题目听上去不难,不过我和朋友作出的答案不能完全符合高手的要求,高手还嘲笑我们的设计非常地不面向对象,但又不给解释。百思不得其解,来此求救。以下是高手的问题,为抛砖引玉,将我们的答案一并列出。
  2 
  3 要求如下:
  4 。设计一个简化的财务系统,要求根据公司毛收入,税收,捐献,确定其净收入。要求提供本月份的毛收入,税收,捐献,净收入,及年度至今的毛收入,税收,捐献,净收入。 
  5 。净收入 = 毛收入 - 税收 - 捐献。 税收依收入分四档,自行确定税率。 捐献也依收入分四档,自行确定比率,计算方法类似税收。如果本年度捐献超过一个数额(最大捐献额),停止捐献。
  6 。使用C#和面向对象设计。
  7 。至少提供两个界面:财务界面,和收入计算界面。财务界面只提供一种方法: 计算财务; 收入计算提供三种方法:计算税收,计算捐献,计算净收入。计算财务可以提供本月份的毛收入,税收,捐献,净收入,及年度至今的毛收入,税收,捐献,净收入。计算财务的输入参数是一个公司的list.
  8 。收入计算界面和基于它的类会被其它系统使用,设计时必须注意软件重用问题。
  9 。税率和捐献比率及最大捐献额有可能改变。
 10 。尽量减少使用条件判断(if/else, switch/case)来计算税收和捐献。
 11 。为简化起见,不用考虑数据库和数据存储。假定数据已经存在。
 12 9。其余自行决定。但系统必须简单易懂。程序中使用英文注释,非关键点可以不注释。
 13 
 14 以下是我们的解答,共五个文件:
 15 1. Company.cs  
 16 using System.Diagnostics;
 17 
 18 namespace XCompany.Accounting.Service
 19 {
 20     public class Company
 21     {
 22         //In general, OOP prefers private and protected fields than public fields
 23         private decimal grossIncomeThisMonth = 0.00m; //Optional: provide a default value
 24 
 25         //Constrcutor
 26         public Company() 
 27         { 
 28         }
 29 
 30         //By implementing property, it will be easier to modify this class in the future.
 31         //For example, if we decide to use different data type, or add more constraints etc.
 32 
 33         //Property Name, auto implement for get/set
 34         public string Name { get; set; }
 35 
 36         //Property GrossIncomeThisMonth. 
 37         public decimal GrossIncomeThisMonth
 38         {
 39             get { return grossIncomeThisMonth; }
 40             set
 41             {
 42                 if (value < 0.00m)
 43                     Trace.WriteLine("Invalid value for Income.");
 44                 else
 45                 {
 46                     grossIncomeThisMonth = value;
 47 
 48                 }
 49             }
 50         }
 51         //Property NetIncomeThisMonth, auto implement for get/set
 52         public decimal NetIncomeThisMonth { get; set; }
 53         //Property FundSupportThisMonth, auto implement for get/set
 54         public decimal FundSupportThisMonth { get; set; }
 55         //Property TaxThisMonth, auto implement for get/set
 56         public decimal TaxThisMonth { get; set; }
 57 
 58         // year to date 
 59         //Property GrossIncomeYearToDate, auto implement for get/set
 60         public decimal GrossIncomeYearToDate { get; set; }
 61         //Property NetIncomeYearToDate, auto implement for get/set
 62         public decimal NetIncomeYearToDate { get; set; }
 63         //Property FundSupportYearToDate, auto implement for get/set
 64         public decimal FundSupportYearToDate { get; set; }
 65         //Property TaxYearToDate, auto implement for get/set
 66         public decimal TaxYearToDate { get; set; }
 67 
 68     }
 69 }
 70 
 71 2. IAccounting.cs
 72 using System.Collections.Generic;
 73 
 74 namespace XCompany.Accounting.Service
 75 {
 76     // USE THIS INTERFACE
 77     public interface IAccounting
 78     {
 79         List<Company> DoAccounting(List<Company> companyList); 
 80     }
 81 }
 82 
 83 3. Accounting.cs
 84 using System.Collections.Generic;
 85 using System.Diagnostics;
 86 
 87 
 88 namespace XCompany.Accounting.Service
 89 {
 90     public class Accounting: IAccounting
 91     {
 92         private readonly ICalc accountingCalculator;
 93         private List<Company> companies;
 94 
 95         public Accounting()
 96         { 
 97             //In case the tax rates change, modify the following array accordingly.
 98             //Define an array of various Income Levels and tax rates, and fund contribution rates accordingly
 99             decimal[,] incomeTaxNew = new decimal[3,4]{ {0.00m, 2000.00m, 3000.00m, 4000.00m}, {5.00m, 6.00m, 8.00m, 10.00m}, {0.00m, 1.00m, 2.00m, 3.00m} };
100             decimal maxFund = 2000.00m;
101 
102             accountingCalculator = new Calculator(incomeTaxNew, maxFund);
103             
104            
105             //If the tax rates and fund contribution rates keep intact, OK to use the default constructor. 
106             //accountingCalculator = new Calculator();    
107         }
108 
109         //Property Companies
110         public List<Company> Companies
111         {
112             get { return companies; }
113             set { companies = value; }
114         }
115 
116         public List<Company> DoAccounting(List<Company> companyList)
117         {
118             try
119             {
120                 Companies = companyList;
121 
122                 foreach (Company oneCompany in Companies)
123                 {
124                     // calcualte the company taxes for accounting
125                     oneCompany.TaxThisMonth = accountingCalculator.CalcTax(oneCompany.GrossIncomeThisMonth);
126                     // add company taxes to year to date for company
127                     oneCompany.TaxYearToDate = oneCompany.TaxYearToDate + oneCompany.TaxThisMonth;
128 
129                     // calculate company fund contributions 
130                     oneCompany.FundSupportThisMonth = accountingCalculator.CalcFundSupport(oneCompany.GrossIncomeThisMonth, oneCompany.FundSupportYearToDate);
131                     // add company contributions to year to date for company
132                     oneCompany.FundSupportYearToDate = oneCompany.FundSupportYearToDate + oneCompany.FundSupportThisMonth;
133 
134                     // calculate company net income for period
135                     oneCompany.NetIncomeThisMonth = accountingCalculator.CalcNetIncome(oneCompany.GrossIncomeThisMonth, oneCompany.FundSupportYearToDate);
136                     // add company income for period to year to date for company
137                     oneCompany.NetIncomeYearToDate = oneCompany.NetIncomeYearToDate + oneCompany.NetIncomeThisMonth;
138 
139                 }
140             }
141             catch (System.ApplicationException ex)
142             {
143                 Trace.WriteLine(ex.ToString() );
144             }
145             catch (System.Exception ex)
146             {
147                 Trace.WriteLine(ex.ToString());
148             }
149 
150             return Companies;
151         }        
152     }
153 }
154 
155 4. ICalc.cs
156 namespace XCompany.Accounting.Service
157 {
158     public interface ICalc
159     {
160         decimal CalcTax(decimal grossIncomeThisMonth);
161         decimal CalcNetIncome(decimal grossIncomeThisMonth, decimal fundSupportYearToDate);
162         decimal CalcFundSupport(decimal grossIncomeThisMonth, decimal fundSupportYearToDate);
163     }
164 }
165 
166 5. Calculator.cs
167 using System.Diagnostics;
168 
169 namespace XCompany.Accounting.Service
170 {
171     // This class determines the amount of deductions to be made for  
172     // company based on their income.  Depending on the amount of 
173     // income will determine the tax rate. 
174     // The company will donate to a fund based on the amount of income,
175     // similar to the tax rate determination. However, once a maximum 
176     // amount has been reached, the company will stop donation.
177     public class Calculator : ICalc
178     {
179         // local variables
180 
181         //In case the tax rates change in the new year, modify the following array accordingly.
182         //Define an array of various Income Levels and tax rates, and fund contribution rates accordingly
183         public readonly decimal[,] IncomeTax = new decimal[3, 4] { { 0.00m, 2000.00m, 3000.00m, 4000.00m }, { 5.00m, 6.00m, 8.00m, 10.00m }, { 0.00m, 1.00m, 2.00m, 3.00m } };
184         public readonly decimal MaxFund = 2000.00m;
185 
186         // constructor. If no parameters are provided in the calling function, the constructor will use default tax rates.
187         public Calculator(decimal[,] _incomeTax = null, decimal _maxFund = 2000.00m)
188         { 
189            if (_incomeTax != null)
190                IncomeTax = _incomeTax;
191 
192            MaxFund = _maxFund; 
193         }
194 
195         //Create a custom exception class based on System.ApplicationException.
196         //It is a good practice to define a custom exception class for a specific application exception.
197         public class InvalidInputException : System.ApplicationException
198         {
199             // The default constructor needs to be defined
200             // explicitly now since it would be gone otherwise.
201             public InvalidInputException()
202             {
203             }
204 
205             public InvalidInputException(string message)
206                 : base(message)
207             {
208             }
209         }
210 
211 
212         /*
213          * class methods 
214          */
215         // The gross income determines the tax rate
216         public decimal GetTaxRate(decimal grossIncomeThisMonth)
217         {
218             decimal taxRate = 0.00m;
219 
220             try
221             {
222                 //It is not the way that CRA calculates our tax. However, since it is a demo, let's follow the way of the sample.
223                 if (grossIncomeThisMonth >= IncomeTax[0, 3])
224                 {
225                     taxRate = IncomeTax[1, 3];
226                 }
227                 else if (grossIncomeThisMonth >= IncomeTax[0, 2])
228                 {
229                     taxRate = IncomeTax[1, 2];
230                 }
231                 else if (grossIncomeThisMonth >= IncomeTax[0, 1])
232                 {
233                     taxRate = IncomeTax[1, 1];
234                 }
235                 else if (grossIncomeThisMonth >= IncomeTax[0, 0])
236                 {
237                     taxRate = IncomeTax[1, 0];
238                 }
239                 else
240                 {
241                     //Report Error: Invalid Income amount.
242                     throw new InvalidInputException("Error: Invalid gross income amount.");
243 
244                 }
245             }
246             catch (InvalidInputException ex)
247             {
248                 Trace.WriteLine(ex.ToString());
249             }
250             catch (System.ApplicationException ex)
251             {
252                 Trace.WriteLine(ex.ToString());
253             }
254             catch (System.Exception ex)
255             {
256                 Trace.WriteLine(ex.ToString());
257             }
258 
259            return taxRate;
260         }
261 
262         // The gross income determines the fund rate. 
263         public decimal GetFundRate(decimal grossIncomeThisMonth, decimal fundSupportYearToDate)
264         {
265            decimal fundRate = 0.00m;
266 
267            try
268            {   //If it exceeds the maximum amount, stop contribution
269                if (fundSupportYearToDate >= MaxFund)
270                    return 0.00m;
271 
272                //Calculates rates based on income
273                if (grossIncomeThisMonth >= IncomeTax[0, 3])
274                {
275                    fundRate = IncomeTax[2, 3];
276                }
277                else if (grossIncomeThisMonth >= IncomeTax[0, 2])
278                {
279                    fundRate = IncomeTax[2, 2];
280                }
281                else if (grossIncomeThisMonth >= IncomeTax[0, 1])
282                {
283                    fundRate = IncomeTax[2, 1];
284                }
285                else if (grossIncomeThisMonth >= IncomeTax[0, 0])
286                {
287                    fundRate = IncomeTax[2, 0];
288                }
289                else
290                {
291                    //Report Error: Invalid Income amount.
292                    throw new InvalidInputException("Error: Invalid gross income amount.");
293 
294                }
295            }
296            catch (InvalidInputException ex)
297            {
298                Trace.WriteLine(ex.ToString());
299            }
300            catch (System.ApplicationException ex)
301            {
302                Trace.WriteLine(ex.ToString());
303            }
304            catch (System.Exception ex)
305            {
306                Trace.WriteLine(ex.ToString());
307            }
308 
309            return fundRate;
310         }
311 
312 
313         // This method takes gross income, and returns tax.
314         virtual public decimal CalcTax(decimal grossIncomeThisMonth)
315         {
316             // declare and initialize variables
317             decimal taxRate = 0.00m;
318             decimal taxAmount = 0.00m; 
319 
320             // determine the Tax Rate to use
321             taxRate = GetTaxRate(grossIncomeThisMonth);
322             // calculate the tax amounts
323             taxAmount = (grossIncomeThisMonth * taxRate) / 100.00m;
324          
325             // return the tax amount
326             return taxAmount;
327         }
328 
329         // This method takes gross income, and returns fund contribution.
330         // Once the fund reaches the maximum amount, stop contribution.
331         virtual public decimal CalcFundSupport(decimal grossIncomeThisMonth, decimal fundSupportYearToDate)
332         {
333             // declare and initialize variables
334             decimal fundRate = 0.00m;
335             decimal fundAmount = 0.00m;
336 
337             // determine the Tax Rate to use
338             fundRate = GetFundRate(grossIncomeThisMonth, fundSupportYearToDate);
339             // calculate the tax amounts
340             fundAmount = (grossIncomeThisMonth * fundRate) / 100.00m;
341 
342             //If it exceeds the maximum amount, stop contribution
343             if ( (fundAmount+ fundSupportYearToDate) > MaxFund)
344                 fundAmount = MaxFund - fundSupportYearToDate;
345 
346             // return the contributed fund amount
347             return fundAmount;
348         }
349 
350 
351         // This method takes a Income, and return the net income
352         virtual public decimal CalcNetIncome(decimal grossIncomeThisMonth, decimal fundSupportYearToDate)
353         {
354             // declare and initialize variables
355             decimal tax = CalcTax(grossIncomeThisMonth);
356 
357             // calculate the fund support
358             decimal fund = CalcFundSupport(grossIncomeThisMonth, fundSupportYearToDate);
359 
360             // return the Income 
361             return (grossIncomeThisMonth - tax - fund);
362         }       
363     }
364 }
365 
366 在计算税率时,还是使用了if/else.

 

最近有高手给我和朋友出题,题目听上去不难,不过我和朋友作出的答案不能完全符合高手的要求,高手还嘲笑我们的设计非常地不面向对象,但又不给解释。百思不得其解,来此求救。以下是高手的问题,为抛砖引玉,将我们的答案一并列出。 要求如下: 1。设计一个简化的财务系统,要求根据公司毛收入,税收,捐献,确定其净收入。要求提供本月份的毛收入,税收,捐献,净收入,及年度至今的毛收入,税收,捐献,净收入。  2。净收入 = 毛收入 - 税收 - 捐献。 税收依收入分四档,自行确定税率。 捐献也依收入分四档,自行确定比率,计算方法类似税收。如果本年度捐献超过一个数额(最大捐献额),停止捐献。 3。使用C#和面向对象设计。 4。至少提供两个界面:财务界面,和收入计算界面。财务界面只提供一种方法: 计算财务; 收入计算提供三种方法:计算税收,计算捐献,计算净收入。计算财务可以提供本月份的毛收入,税收,捐献,净收入,及年度至今的毛收入,税收,捐献,净收入。计算财务的输入参数是一个公司的list. 5。收入计算界面和基于它的类会被其它系统使用,设计时必须注意软件重用问题。 6。税率和捐献比率及最大捐献额有可能改变。 7。尽量减少使用条件判断(if/else, switch/case)来计算税收和捐献。 8。为简化起见,不用考虑数据库和数据存储。假定数据已经存在。 9。其余自行决定。但系统必须简单易懂。程序中使用英文注释,非关键点可以不注释。 以下是我们的解答,共五个文件: 1. Company.cs using System.Diagnostics; namespace XCompany.Accounting.Service { public class Company { //In general, OOP prefers private and protected fields than public fields private decimal grossIncomeThisMonth = 0.00m; //Optional: provide a default value //Constrcutor public Company() { } //By implementing property, it will be easier to modify this class in the future. //For example, if we decide to use different data type, or add more constraints etc. //Property Name, auto implement for get/set public string Name { get; set; } //Property GrossIncomeThisMonth. public decimal GrossIncomeThisMonth { get { return grossIncomeThisMonth; } set { if (value < 0.00m) Trace.WriteLine("Invalid value for Income."); else { grossIncomeThisMonth = value; } } } //Property NetIncomeThisMonth, auto implement for get/set public decimal NetIncomeThisMonth { get; set; } //Property FundSupportThisMonth, auto implement for get/set public decimal FundSupportThisMonth { get; set; } //Property TaxThisMonth, auto implement for get/set public decimal TaxThisMonth { get; set; } // year to date //Property GrossIncomeYearToDate, auto implement for get/set public decimal GrossIncomeYearToDate { get; set; } //Property NetIncomeYearToDate, auto implement for get/set public decimal NetIncomeYearToDate { get; set; } //Property FundSupportYearToDate, auto implement for get/set public decimal FundSupportYearToDate { get; set; } //Property TaxYearToDate, auto implement for get/set public decimal TaxYearToDate { get; set; } } } 2. IAccounting.cs using System.Collections.Generic; namespace XCompany.Accounting.Service { // USE THIS INTERFACE public interface IAccounting { List DoAccounting(List companyList); } } 3. Accounting.cs using System.Collections.Generic; using System.Diagnostics; namespace XCompany.Accounting.Service { public class Accounting: IAccounting { private readonly ICalc accountingCalculator; private List companies; public Accounting() { //In case the tax rates change, modify the following array accordingly. //Define an array of various Income Levels and tax rates, and fund contribution rates accordingly decimal[,] incomeTaxNew = new decimal[3,4]{ {0.00m, 2000.00m, 3000.00m, 4000.00m}, {5.00m, 6.00m, 8.00m, 10.00m}, {0.00m, 1.00m, 2.00m, 3.00m} }; decimal maxFund = 2000.00m; accountingCalculator = new Calculator(incomeTaxNew, maxFund); //If the tax rates and fund contribution rates keep intact, OK to use the default constructor. //accountingCalculator = new Calculator(); } //Property Companies public List Companies { get { return companies; } set { companies = value; } } public List DoAccounting(List companyList) { try { Companies = companyList; foreach (Company oneCompany in Companies) { // calcualte the company taxes for accounting oneCompany.TaxThisMonth = accountingCalculator.CalcTax(oneCompany.GrossIncomeThisMonth); // add company taxes to year to date for company oneCompany.TaxYearToDate = oneCompany.TaxYearToDate + oneCompany.TaxThisMonth; // calculate company fund contributions oneCompany.FundSupportThisMonth = accountingCalculator.CalcFundSupport(oneCompany.GrossIncomeThisMonth, oneCompany.FundSupportYearToDate); // add company contributions to year to date for company oneCompany.FundSupportYearToDate = oneCompany.FundSupportYearToDate + oneCompany.FundSupportThisMonth; // calculate company net income for period oneCompany.NetIncomeThisMonth = accountingCalculator.CalcNetIncome(oneCompany.GrossIncomeThisMonth, oneCompany.FundSupportYearToDate); // add company income for period to year to date for company oneCompany.NetIncomeYearToDate = oneCompany.NetIncomeYearToDate + oneCompany.NetIncomeThisMonth; } } catch (System.ApplicationException ex) { Trace.WriteLine(ex.ToString() ); } catch (System.Exception ex) { Trace.WriteLine(ex.ToString()); } return Companies; } } } 4. ICalc.cs namespace XCompany.Accounting.Service { public interface ICalc { decimal CalcTax(decimal grossIncomeThisMonth); decimal CalcNetIncome(decimal grossIncomeThisMonth, decimal fundSupportYearToDate); decimal CalcFundSupport(decimal grossIncomeThisMonth, decimal fundSupportYearToDate); } } 5. Calculator.cs using System.Diagnostics; namespace XCompany.Accounting.Service { // This class determines the amount of deductions to be made for // company based on their income. Depending on the amount of // income will determine the tax rate. // The company will donate to a fund based on the amount of income, // similar to the tax rate determination. However, once a maximum // amount has been reached, the company will stop donation. public class Calculator : ICalc { // local variables //In case the tax rates change in the new year, modify the following array accordingly. //Define an array of various Income Levels and tax rates, and fund contribution rates accordingly public readonly decimal[,] IncomeTax = new decimal[3, 4] { { 0.00m, 2000.00m, 3000.00m, 4000.00m }, { 5.00m, 6.00m, 8.00m, 10.00m }, { 0.00m, 1.00m, 2.00m, 3.00m } }; public readonly decimal MaxFund = 2000.00m; // constructor. If no parameters are provided in the calling function, the constructor will use default tax rates. public Calculator(decimal[,] _incomeTax = null, decimal _maxFund = 2000.00m) { if (_incomeTax != null) IncomeTax = _incomeTax; MaxFund = _maxFund; } //Create a custom exception class based on System.ApplicationException. //It is a good practice to define a custom exception class for a specific application exception. public class InvalidInputException : System.ApplicationException { // The default constructor needs to be defined // explicitly now since it would be gone otherwise. public InvalidInputException() { } public InvalidInputException(string message) : base(message) { } } /* * class methods */ // The gross income determines the tax rate public decimal GetTaxRate(decimal grossIncomeThisMonth) { decimal taxRate = 0.00m; try { //It is not the way that CRA calculates our tax. However, since it is a demo, let's follow the way of the sample. if (grossIncomeThisMonth >= IncomeTax[0, 3]) { taxRate = IncomeTax[1, 3]; } else if (grossIncomeThisMonth >= IncomeTax[0, 2]) { taxRate = IncomeTax[1, 2]; } else if (grossIncomeThisMonth >= IncomeTax[0, 1]) { taxRate = IncomeTax[1, 1]; } else if (grossIncomeThisMonth >= IncomeTax[0, 0]) { taxRate = IncomeTax[1, 0]; } else { //Report Error: Invalid Income amount. throw new InvalidInputException("Error: Invalid gross income amount."); } } catch (InvalidInputException ex) { Trace.WriteLine(ex.ToString()); } catch (System.ApplicationException ex) { Trace.WriteLine(ex.ToString()); } catch (System.Exception ex) { Trace.WriteLine(ex.ToString()); } return taxRate; } // The gross income determines the fund rate. public decimal GetFundRate(decimal grossIncomeThisMonth, decimal fundSupportYearToDate) { decimal fundRate = 0.00m; try { //If it exceeds the maximum amount, stop contribution if (fundSupportYearToDate >= MaxFund) return 0.00m; //Calculates rates based on income if (grossIncomeThisMonth >= IncomeTax[0, 3]) { fundRate = IncomeTax[2, 3]; } else if (grossIncomeThisMonth >= IncomeTax[0, 2]) { fundRate = IncomeTax[2, 2]; } else if (grossIncomeThisMonth >= IncomeTax[0, 1]) { fundRate = IncomeTax[2, 1]; } else if (grossIncomeThisMonth >= IncomeTax[0, 0]) { fundRate = IncomeTax[2, 0]; } else { //Report Error: Invalid Income amount. throw new InvalidInputException("Error: Invalid gross income amount."); } } catch (InvalidInputException ex) { Trace.WriteLine(ex.ToString()); } catch (System.ApplicationException ex) { Trace.WriteLine(ex.ToString()); } catch (System.Exception ex) { Trace.WriteLine(ex.ToString()); } return fundRate; } // This method takes gross income, and returns tax. virtual public decimal CalcTax(decimal grossIncomeThisMonth) { // declare and initialize variables decimal taxRate = 0.00m; decimal taxAmount = 0.00m; // determine the Tax Rate to use taxRate = GetTaxRate(grossIncomeThisMonth); // calculate the tax amounts taxAmount = (grossIncomeThisMonth * taxRate) / 100.00m; // return the tax amount return taxAmount; } // This method takes gross income, and returns fund contribution. // Once the fund reaches the maximum amount, stop contribution. virtual public decimal CalcFundSupport(decimal grossIncomeThisMonth, decimal fundSupportYearToDate) { // declare and initialize variables decimal fundRate = 0.00m; decimal fundAmount = 0.00m; // determine the Tax Rate to use fundRate = GetFundRate(grossIncomeThisMonth, fundSupportYearToDate); // calculate the tax amounts fundAmount = (grossIncomeThisMonth * fundRate) / 100.00m; //If it exceeds the maximum amount, stop contribution if ( (fundAmount+ fundSupportYearToDate) > MaxFund) fundAmount = MaxFund - fundSupportYearToDate; // return the contributed fund amount return fundAmount; } // This method takes a Income, and return the net income virtual public decimal CalcNetIncome(decimal grossIncomeThisMonth, decimal fundSupportYearToDate) { // declare and initialize variables decimal tax = CalcTax(grossIncomeThisMonth); // calculate the fund support decimal fund = CalcFundSupport(grossIncomeThisMonth, fundSupportYearToDate); // return the Income return (grossIncomeThisMonth - tax - fund); } } } 在计算税率时,还是使用了if/else.

LikeLearn的主页 LikeLearn | 初学一级 | 园豆:142
提问于:2013-02-04 00:34
< >
分享
所有回答(5)
0

你指的这个高手,需要的是你要加入设计模式的思想

Rich.T | 园豆:3440 (老鸟四级) | 2013-02-04 12:58

我还是不太明白,请再多指教一下。

支持(0) 反对(0) LikeLearn | 园豆:142 (初学一级) | 2013-02-04 13:20

@LikeLearn: 

你去看看这往篇文章,你应该能有收获

http://www.cnblogs.com/cj723/archive/2007/04/02/697431.html

支持(0) 反对(0) Rich.T | 园豆:3440 (老鸟四级) | 2013-02-04 13:21
0

既然是问设计,为啥不把代码换成类图呢。看着一堆代码排版也不好,眼花,这样回答的人就少了

Martin Stallman | 园豆:202 (菜鸟二级) | 2013-02-04 13:44
0
。尽量减少使用条件判断(if/else, switch/case)来计算税收和捐献。
你这里的确是不够面向对象。
GetTaxRate
GetFundRate
这2个实现重写
函数里不要有if,else语句
每种条件写成一个子类实现
将共同的计算步骤放到基类中实现。
xchsp | 园豆:181 (初学一级) | 2013-02-05 09:55
0
  1 public class TaxEntity{
  2         public decimal Value{get;set;}
  3         public double Rate{get;set;}
  4     }
  5    
  6     public class FundEntity
  7     {
  8         public decimal MaxValue { get; set; }
  9         public decimal Value { get; set; }
 10     }
 11     
 12     public interface ICapture
 13     {
 14         decimal CaptureNet(Company company);
 15         decimal CaptureTax(Company company);
 16         decimal CaptureFund(Company company);
 17     }
 18   
 19     public class CapturerMonth : ICapture
 20     {
 21         #region ICapture 成员
 22 
 23         public decimal CaptureNet(Company company)
 24         {
 25             throw new NotImplementedException();
 26         }
 27 
 28         public decimal CaptureTax(Company company)
 29         {
 30             throw new NotImplementedException();
 31         }
 32 
 33         public decimal CaptureFund(Company company)
 34         {
 35             throw new NotImplementedException();
 36         }
 37 
 38         #endregion
 39     }
 40     public class CapturerYear : ICapture
 41     {
 42        
 43         #region ICapture 成员
 44 
 45         public decimal CaptureNet(Company company)
 46         {
 47             throw new NotImplementedException();
 48         }
 49 
 50         public decimal CaptureTax(Company company)
 51         {
 52             throw new NotImplementedException();
 53         }
 54 
 55         public decimal CaptureFund(Company company)
 56         {
 57             throw new NotImplementedException();
 58         }
 59 
 60         #endregion
 61     }
 62     public class Company {
 63 
 64         FundEntity fund_;
 65         public FundEntity GetFundEntity
 66         {
 67             get
 68             {
 69                 return fund_;
 70             }
 71         }
 72         TaxEntity tax_;
 73         public TaxEntity GetTaxEntity
 74         {
 75             get
 76             {
 77                 return tax_;
 78             }
 79         }
 80 
 81         public decimal FundYear { get; set; }
 82         public decimal GrosstMonth { get; set; }
 83         public decimal GrossYear { get; set; }
 84         public decimal NetMonth { get; set; }
 85         public decimal NetYear { get; set; }
 86 
 87         public Company(TaxEntity tax,FundEntity fund)
 88         {
 89             fund_ = fund;
 90             tax_ = tax;
 91             InitData();
 92         }
 93         void InitData()
 94         {
 95             this.GrossYear = 90000000;
 96             this.GrosstMonth = 1000000;
 97             this.FundYear = 1200;
 98         }
 99     }
100     public class AccountContext
101     {
102         public void Start()
103         {
104             List<Company> list = new List<Company>();
105 
106             list.Add(new Company(new TaxEntity { Value = 2000, Rate = 0.2 }, new FundEntity { MaxValue = 2000, Value = 100 }));
107             list.Add(new Company(new TaxEntity { Value = 3000, Rate = 0.3 }, new FundEntity { MaxValue = 2000, Value = 100 }));
108             list.Add(new Company(new TaxEntity { Value = 4000, Rate = 0.4 }, new FundEntity { MaxValue = 2000, Value = 100 }));
109             list.Add(new Company(new TaxEntity { Value = 5000, Rate = 0.5 }, new FundEntity { MaxValue = 2000, Value = 100 }));
110 
111 
112             list.ForEach(company => Do(company, new CapturerMonth()));
113             list.ForEach(company => Do(company, new CapturerYear()));
114         }
115         void Do(Company company, ICapture capture)
116         {
117             capture.CaptureNet(company);
118             capture.CaptureTax(company);
119             capture.CaptureFund(company);
120         }
121     }
122 
123 不知道行不行, 请你的高手赐教,  capture 具体没实现的.
Yu | 园豆:12990 (专家六级) | 2013-02-06 16:59
0

希望这篇文章能给你有所指引:http://www.cnblogs.com/smartbooks/archive/2013/01/12/2857613.html

SmartBooks | 园豆:264 (菜鸟二级) | 2013-02-06 18:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册