销售税问题基本销售税对所有商品征收,税率是10%,但是书籍、食品和药品可以免征。进口税是向所有进口商品征收的额外的税,税率5%,没有进口商品可以免征进口税。 
当一个顾客采购物品时,他会收到一个收据,上面列出所有物品的总价,以及全部应付的税费。税金需要上舍入到5分(0.05元)。比如,3.14应该上舍入成3.15, 2.48应该上舍入成2.50, 3.01上舍入成3.05。 
需求:
需要你实现计算出一份订单中所有物品的总税金和总价的功能。.
 
• 请实现Program.cs的calculateResult()方法。 
• 请创建需要的类来实现这个功能。你的实现应该符合面向对象设计的原则。 
• 你的代码必须通过全部测试用例。  测试用例已经包含在文档中,请不要修改。
• 编程时请留意代码的可扩展性。业务要求可能会有改变,•  比如免税的商品品种可能增加,• 或者有新的税种。我们希• 望你的实现对已有代码做尽可能小的改变就能支持这些扩展。2.   case1.xml :
<?xml version="1.0" encoding="utf-8" ?>
<testCase name="testCase1">
    <items>
      <item name="book" count="1" unitPrice="12.49" type="book" imported="false"></item>
      <item name="music CD" count="1" unitPrice="14.99" type="av" imported="false"></item>
      <item name="chocolate bar" count="1" unitPrice="0.85" type="food" imported="false"></item>
    </items>
    <result taxes="1.50" total="29.83"></result>
</testCase>3.  case2.xml:<?xml version="1.0" encoding="utf-8" ?>
<testCase name="testCase2">
  <items>
    <item name="box of chocolates" count="1" unitPrice="10.00" type="food" imported="true"></item>
    <item name="bottle of perfume" count="1" unitPrice="47.50" type="makeup" imported="true"></item>
  </items>
  <result taxes="7.65" total="65.15"></result>
</testCase>4.    case3.xml:<?xml version="1.0" encoding="utf-8" ?>
<testCase name="testCase3">
  <items>
    <item name="bottle of perfume" count="1" unitPrice="27.99" type="makeup" imported="true"></item>
    <item name="bottle of perfume" count="1" unitPrice="18.99" type="makeup" imported="false"></item>
    <item name="packet of headache pills" count="1" unitPrice="9.75" type="medical" imported="false"></item>
    <item name="box of chocolates" count="1" unitPrice="11.25" type="food" imported="true"></item>
  </items>
  <result taxes="6.70" total="74.68"></result>
</testCase>
5.  
    class MyResult
    {
        private decimal taxes = 0;
        public decimal Taxes
        {
            get { return taxes; }
            set { taxes = value; }
        }        private decimal total = 0;
        public decimal Total
        {
            get { return total; }
            set { total = value; }
        }
    }  6.class Program
    {
        static void Main(string[] args)
        {
                test();
                Console.ReadLine();
        }        static MyResult calculateResult(XmlDocument testCase)
        {
                //Please implement this function; feel free to add classes if need.
                return new MyResult();
        }        static void test()
        {
            try
            {
                string[] testCases = Directory.GetFiles("Test/Cases");
                foreach (string testCase in testCases)
                {
                    XmlDocument caseDocument = new XmlDocument();
                    caseDocument.Load(testCase);
                    if (null != caseDocument)
                    {
                        MyResult result;
                        XmlNode caseNode = caseDocument.SelectSingleNode("testCase");
                        string caseName = caseNode.Attributes["name"].Value;
                        XmlNode resultNode = caseNode.SelectSingleNode("result");
                        decimal expectedTaxes = decimal.Parse(resultNode.Attributes["taxes"].Value);
                        decimal expectedTotal = decimal.Parse(resultNode.Attributes["total"].Value);
                        try
                        {
                            result = calculateResult(caseDocument);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Exception occurs in calculateResult() while runing " + caseName + ": " + e.Message);
                            continue;
                        }
                        bool isCorrect = expectedTaxes == result.Taxes && expectedTotal == result.Total;
                        Console.WriteLine(caseName + ":" + (isCorrect ? "passed" : "failed"));
                    }
                }
            }
            catch
            {
                Console.WriteLine("Exception occurs in test application. Test data might be damaged. Please ask HR for help.");
                return;
            }
        }
    }
谢谢
  

解决方案 »

  1.   

    看到XML就头疼
    用DATASET载入XML后分析
    使用工厂模式实现扩展性,读取<testCase name="testCase1">这个节点反射后找到实际处理的工厂类来处理
      

  2.   

    using System;
    using System.IO;
    using System.Collections;
    using System.Xml;
    using System.Collections.Generic;
    using System.Text;class MyResult 
        { 
            private decimal taxes = 0; 
            public decimal Taxes 
            { 
                get { return taxes; } 
                set { taxes = value; } 
            }         private decimal total = 0; 
            public decimal Total 
            { 
                get { return total; } 
                set { total = value; } 
            } 
        } class Program 
        { 
            static void Main(string[] args) 
            { 
                    test(); 
                    Console.ReadLine(); 
            }         static MyResult calculateResult(XmlDocument testCase) 
            { 
                    //Please implement this function; feel free to add classes if need. 
                    calculatefee calculate=new calculatefee();
                    MyResult result=new MyResult();
                    result.Taxes=calculate.calcutotaltax(testCase);
                    result.Total = calculate.calcutotalvalue(testCase);
                    return result; 
            }         static void test() 
            { 
                try 
                { 
                    string[] testCases = Directory.GetFiles(@"F:\test"); 
                    foreach (string testCase in testCases) 
                    { 
                        XmlDocument caseDocument = new XmlDocument(); 
                        caseDocument.Load(testCase); 
                        if (null != caseDocument) 
                        { 
                            MyResult result; 
                            XmlNode caseNode = caseDocument.SelectSingleNode("testCase"); 
                            string caseName = caseNode.Attributes["name"].Value; 
                            XmlNode resultNode = caseNode.SelectSingleNode("result"); 
                            decimal expectedTaxes = decimal.Parse(resultNode.Attributes["taxes"].Value); 
                            decimal expectedTotal = decimal.Parse(resultNode.Attributes["total"].Value); 
                            try 
                            { 
                                result = calculateResult(caseDocument); 
                            } 
                            catch (Exception e) 
                            { 
                                Console.WriteLine("Exception occurs in calculateResult() while runing " + caseName + ": " + e.Message); 
                                continue; 
                            } 
                            bool isCorrect = expectedTaxes == result.Taxes && expectedTotal == result.Total; 
                            Console.WriteLine(caseName + ":" + (isCorrect ? "passed" : "failed")); 
                        } 
                    } 
                } 
                catch 
                { 
                    Console.WriteLine("Exception occurs in test application. Test data might be damaged. Please ask HR for help."); 
                    return; 
                } 
            } 
        } 
    public class calculatefee
    {
    static decimal totalvalue=0;
    static decimal basictax=0;
    static decimal importtax = 0;
    static decimal totaltax = 0; 
    static decimal importtaxfee=0.05M;
    static decimal normaltaxfee=0.1M;
    private   List<string> books;
    private   List<string> foods ;
    private   List<string> medicines;
    public calculatefee()
    {
     books = new List<string>();
     foods = new List<string>();
     medicines = new List<string>();
    books.Add("book");
    foods.Add("chocolate bar");
    foods.Add("box of chocolates");
    medicines.Add("packet of headache pills");
    }
    public List<string>  Books
    {
    get
    {
    return this.books;
    }
    }
    public List<string>  Foods
    {
    get
    {
    return this.foods;
    }
    }
    public List<string>  Medicines
    {
    get
    {
    return this.medicines;
    }
    }
    public  decimal calcubasictax(XmlDocument xd)
    {
    XmlNodeList xn=xd.GetElementsByTagName("item");
    basictax = 0;
    for(int i=0;i<xn.Count;i++)
    {    
            if (this.books.Contains(xn[i].Attributes["name"].Value) || this.foods.Contains(xn[i].Attributes["name"].Value) || this.medicines.Contains(xn[i].Attributes["name"].Value))
            {
               
            }
            else
            {
                basictax = basictax + decimal.Parse(xn[i].Attributes["unitPrice"].Value) * normaltaxfee;
            }
        
    }
    return basictax;
    }public  decimal calcuimporttax(XmlDocument xd)
    {
        XmlNodeList xn = xd.GetElementsByTagName("item");
        importtax = 0;
        for (int i = 0; i < xn.Count; i++)
        {
            if (xn[i].Attributes["imported"].Value == "true")
            {
                
                    importtax = importtax + decimal.Parse(xn[i].Attributes["unitPrice"].Value) * importtaxfee;
               
            }
        }
        return importtax;
    }
    public decimal calcutotaltax(XmlDocument xd)
    {
         totaltax = 0;
         totaltax = this.calcubasictax(xd) + this.calcuimporttax(xd);
         return changedecimal.change(totaltax);
    }
    public decimal calcutotalvalue(XmlDocument xd)
    {
        XmlNodeList xn = xd.GetElementsByTagName("item");
        totalvalue = 0;
        for (int i = 0; i < xn.Count; i++)
        {
                       
                    totalvalue = totalvalue + decimal.Parse(xn[i].Attributes["unitPrice"].Value);
                      
        }
        return changedecimal.change(totalvalue);
    }
       
    }
    class changedecimal
    {
    public static decimal change(decimal m)
    {
    m=decimal.Round(m,2);
    string mm=Convert.ToString(m);
    Char[] n=mm.ToCharArray();
    string s="";
    for(int i=0;i<n.Length;i++)
    {
    s=s+n[i].ToString();
    }
    StringBuilder sd=new StringBuilder(s);
    int ll=n.Length-1;
    string ln=n[ll].ToString();
    int inm=Convert.ToInt32(ln);
    if(inm<5 && inm!=0)
    {
    sd.Remove(ll,1);
    Char[] nem=Convert.ToString(5).ToCharArray();
    sd.Append(nem[0]);
    string newsd=sd.ToString();
    decimal newde=decimal.Parse(newsd);
    return newde;
    }
    if(inm>5)
    {
    sd.Remove(ll,1);
    string sub=sd.ToString();
    decimal d=decimal.Parse(sub);
    d=d+0.1M;
    string ns=d.ToString();
    StringBuilder sdd=new StringBuilder(ns);
    Char[] neu=Convert.ToString(0).ToCharArray();
    sdd.Append(neu[0]);
    string newsdd=sdd.ToString();
    decimal newdec=decimal.Parse(newsdd);
    return newdec;
    }
    return m;
    }
    }
      

  3.   

    CSDN告诉我..
    每天回帖即可获得10分可用分!
      

  4.   

    这里发言,表示您接受了CSDN社区的用户行为准则