博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
经典面试题SALES TAXES思路分析和源码分享
阅读量:7286 次
发布时间:2019-06-30

本文共 5793 字,大约阅读时间需要 19 分钟。

题目:

SALES TAXESBasic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax 除书籍 食品 药品外其他商品基本税为10%。进口税附加5%,不免税。applicable on all imported goods at a rate of 5%, with no exemptions.When I purchase items, I receive a receipt which lists the name of all the items and their price (including tax), finishing with the total cost of the items, and the total amounts of sales taxes paid.  The rounding rules for sales tax are that for a tax rate of n%, a shelf price of p contains (np/100 rounded up to the nearest 0.05, exp:7.125 -> 7.15; 6.66 -> 6.7) amount of sales tax.Write an application that prints out the receipt details for these shopping baskets...INPUT:Input 1:1 book at 12.491 music CD at 14.991 chocolcate bar at 0.85Input 2:1 imported box of chocolates at 10.001 imported bottle of perfume at 47.50Input 3:1 imported bottle of perfume at 27.991 bottle of perfume at 18.991 packet of headache pills at 9.751 box of imported chocolates at 11.25 OUTPUTOutput 1:1 book : 12.491 music CD: 16.491 chocolate bar: 0.85Sales Taxes: 1.50Total: 29.83Output 2:1 imported box of chocolates: 10.501 imported bottle of perfume: 54.65Sales Taxes: 7.65Total: 65.15Output 3:1 imported bottle of perfume: 32.191 bottle of perfume: 20.891 packet of headache pills: 9.751 imported box of chocolates: 11.85Sales Taxes: 6.70Total: 74.68

C#实现代码如下:

using System;using System.Collections.Generic;using System.Text;using System.Threading.Tasks;using Microsoft.VisualStudio.TestTools.UnitTesting;namespace SalesTaxes{    public class TestCaseResult    {        public decimal Taxes { get; set; } //税合计        public decimal TotalPrice { get; set; } //总计含税        public TestCaseResult(decimal Taxes, decimal TotalPrice)        {            this.Taxes = Taxes;            this.TotalPrice = TotalPrice;        }    }    public class Test    {        //Test case1        public TestCaseResult GetResultForCasee1()        {            List
goods = new List
(); //第一批物品 goods.Add(new Goods("book", 1, false, (int)Enum_GoodType.Book, 12.49m)); goods.Add(new Goods("music CD", 1, false, (int)Enum_GoodType.Other, 14.99m)); goods.Add(new Goods("chocolcate bar", 1, false, (int)Enum_GoodType.Food, 0.85m)); return GetTestResult(goods); } public TestCaseResult GetResultForCasee2() { List
goods = new List
(); //第二批物品 goods.Add(new Goods("book", 1, true, (int)Enum_GoodType.Book, 10.0m)); goods.Add(new Goods("perfume", 1, true, (int)Enum_GoodType.Other, 47.5m)); return GetTestResult(goods); } public TestCaseResult GetResultForCasee3() { List
goods = new List
(); //第三批物品 goods.Add(new Goods("perfume", 1, true, (int)Enum_GoodType.Other, 27.99m)); goods.Add(new Goods("perfume", 1, false, (int)Enum_GoodType.Other, 18.99m)); goods.Add(new Goods("headache pills", 1, false, (int)Enum_GoodType.Drug, 9.75m)); goods.Add(new Goods("chocolates", 1, true, (int)Enum_GoodType.Food, 11.25m)); return GetTestResult(goods); } ///
/// 获取测试结果 /// ///
///
private TestCaseResult GetTestResult(List
goods) { decimal totalGoods = 0; //总价不含税 decimal totalGoodsByTax = 0; //总价含税 for (int i = 0; i < goods.Count; i++) { totalGoods += goods[i].Price * goods[i].Count; totalGoodsByTax += Goods.GetGoodsPriceByTax(goods[i]); } return new TestCaseResult(totalGoodsByTax - totalGoods, totalGoodsByTax); } } class Program { static void Main(string[] args) { //Test case 1 var test = new Test(); var result = test.GetResultForCasee1(); Assert.AreEqual(1.50m, result.Taxes); Assert.AreEqual(29.83m, result.TotalPrice); //Test case 2 result = test.GetResultForCasee2(); Assert.AreEqual(7.65m, result.Taxes); Assert.AreEqual(65.15m, result.TotalPrice); //Test case 3 result = test.GetResultForCasee3(); Assert.AreEqual(6.70m, result.Taxes); Assert.AreEqual(74.68m, result.TotalPrice); } } ///
/// 商品名称 /// class Goods { ///
/// 构造函数,初始化商品名称 /// public Goods(string Name, int Count, bool Import, int GoodsType, decimal Price) { this.Name = Name; this.Count = Count; this.Import = Import; this.GoodsType = GoodsType; this.Price = Price; } ///
/// 商品名称 /// public string Name { set; get; } ///
/// 商品数量 /// public int Count { set; get; } ///
/// 是否进口(true=>进口) /// public bool Import { set; get; } ///
/// 商品类型 对应枚举 Enum_GoodType /// public int GoodsType { set; get; } ///
/// 单价 /// public decimal Price { set; get; } ///
/// 基本税 /// const decimal BasicDuty = 0.1m; ///
/// 进口附加税 /// const decimal ImportSurtax = 0.05m; ///
/// 计算商品的价格 /// ///
商品 ///
商品最终价格
public static decimal GetGoodsPriceByTax(Goods goods) { decimal result = 0; if (null != goods) { if (goods.Import) { //进口物品,添加附加税 decimal appTax = goods.Price * goods.Count * ImportSurtax; result += GetMathResult(appTax); } if (goods.GoodsType == 4) { //不是书籍、食品、药品 需要征收基础税,上面也可以写成(goods.GoodsType==(int)Enum_GoodType.Book ||...) decimal baseTax = goods.Price * goods.Count * BasicDuty; result += GetMathResult(baseTax); } result += goods.Price * goods.Count; } return result; } ///
/// 计算0.05舍弃值,核心代码 /// ///
private static decimal GetMathResult(decimal tax) { if ((tax / 0.05m).ToString().IndexOf(".") != -1) { //rounded up to the nearest 0.05 tax = Math.Round(tax, 1, MidpointRounding.AwayFromZero); } return tax; } } ///
/// 商品类型 /// 备注:书籍、食品、药品 可分为一个枚举就是免基本税的,这样细分,考虑后期扩展 /// enum Enum_GoodType { Book = 1, //书籍 Food = 2, //食品 Drug = 3, //药品 Other = 4 //其他 }}
View Code

分析:

这道题考察的点有两个,一个是对业务的理解能力;二是编码能力的考量,封装继承以及写代码功底如何;

编码能力每个人有不同的风格和书写方式,尽量遵循代码设计模式轻耦合,模块化是最好的,而这道题的业务中有一个核心点,就是对税收不能被0.05整除要四舍五入到小数点后一位x.x,详见代码。

 

转载地址:http://topjm.baihongyu.com/

你可能感兴趣的文章
linux自学笔记--bash特性
查看>>
Linux平台中设置文件的执行、写权限
查看>>
CentOS7-虚拟网卡的删除
查看>>
Ruby中的include和extend
查看>>
Sencha的Eclipse插件提示和技巧
查看>>
超全前端面试题及答案
查看>>
使用纯真版IP地址库,根据IP判断所在地
查看>>
转:SQL注入攻击的原理
查看>>
DATA VISUALIZATION – PART 2
查看>>
如何用几何画板把圆奇数等分
查看>>
数据结构-线性表操作
查看>>
5Python全栈之路系列之算法
查看>>
一个效果不错的Java Swing模拟屏幕截图工具类
查看>>
MySQL 的主从复制
查看>>
把合同中红色印章实现打印不显示方法
查看>>
linux调优工具使用
查看>>
php.ini中开启段标签
查看>>
php-扩展编译安装扩展(通用版)
查看>>
信号槽的实现实例—— Qt 和 Boost
查看>>
一段简单的php翻页代码
查看>>