题目:

SALES TAXES

Basic 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.49
1 music CD at 14.99
1 chocolcate bar at 0.85 Input 2:
1 imported box of chocolates at 10.00
1 imported bottle of perfume at 47.50 Input 3:
1 imported bottle of perfume at 27.99
1 bottle of perfume at 18.99
1 packet of headache pills at 9.75
1 box of imported chocolates at 11.25 OUTPUT Output 1:
1 book : 12.49
1 music CD: 16.49
1 chocolate bar: 0.85
Sales Taxes: 1.50
Total: 29.83 Output 2:
1 imported box of chocolates: 10.50
1 imported bottle of perfume: 54.65
Sales Taxes: 7.65
Total: 65.15 Output 3:
1 imported bottle of perfume: 32.19
1 bottle of perfume: 20.89
1 packet of headache pills: 9.75
1 imported box of chocolates: 11.85
Sales Taxes: 6.70
Total: 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> goods = new List<Goods>(); //第一批物品
goods.Add(new Goods("book", , false, (int)Enum_GoodType.Book, 12.49m));
goods.Add(new Goods("music CD", , false, (int)Enum_GoodType.Other, 14.99m));
goods.Add(new Goods("chocolcate bar", , false, (int)Enum_GoodType.Food, 0.85m));
return GetTestResult(goods);
} public TestCaseResult GetResultForCasee2()
{
List<Goods> goods = new List<Goods>(); //第二批物品
goods.Add(new Goods("book", , true, (int)Enum_GoodType.Book, 10.0m));
goods.Add(new Goods("perfume", , true, (int)Enum_GoodType.Other, 47.5m));
return GetTestResult(goods);
} public TestCaseResult GetResultForCasee3()
{
List<Goods> goods = new List<Goods>(); //第三批物品
goods.Add(new Goods("perfume", , true, (int)Enum_GoodType.Other, 27.99m));
goods.Add(new Goods("perfume", , false, (int)Enum_GoodType.Other, 18.99m));
goods.Add(new Goods("headache pills", , false, (int)Enum_GoodType.Drug, 9.75m));
goods.Add(new Goods("chocolates", , true, (int)Enum_GoodType.Food, 11.25m));
return GetTestResult(goods);
} /// <summary>
/// 获取测试结果
/// </summary>
/// <param name="goods"></param>
/// <returns></returns>
private TestCaseResult GetTestResult(List<Goods> goods)
{
decimal totalGoods = ; //总价不含税
decimal totalGoodsByTax = ; //总价含税
for (int i = ; 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); } } /// <summary>
/// 商品名称
/// </summary>
class Goods
{
/// <summary>
/// 构造函数,初始化商品名称
/// </summary>
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;
} /// <summary>
/// 商品名称
/// </summary>
public string Name { set; get; } /// <summary>
/// 商品数量
/// </summary>
public int Count { set; get; } /// <summary>
/// 是否进口(true=>进口)
/// </summary>
public bool Import { set; get; } /// <summary>
/// 商品类型 对应枚举 Enum_GoodType
/// </summary>
public int GoodsType { set; get; } /// <summary>
/// 单价
/// </summary>
public decimal Price { set; get; } /// <summary>
/// 基本税
/// </summary>
const decimal BasicDuty = 0.1m; /// <summary>
/// 进口附加税
/// </summary>
const decimal ImportSurtax = 0.05m; /// <summary>
/// 计算商品的价格
/// </summary>
/// <param name="goods">商品</param>
/// <returns>商品最终价格</returns>
public static decimal GetGoodsPriceByTax(Goods goods)
{
decimal result = ;
if (null != goods)
{
if (goods.Import)
{ //进口物品,添加附加税
decimal appTax = goods.Price * goods.Count * ImportSurtax;
result += GetMathResult(appTax);
}
if (goods.GoodsType == )
{//不是书籍、食品、药品 需要征收基础税,上面也可以写成(goods.GoodsType==(int)Enum_GoodType.Book ||...)
decimal baseTax = goods.Price * goods.Count * BasicDuty;
result += GetMathResult(baseTax);
}
result += goods.Price * goods.Count;
}
return result;
} /// <summary>
/// 计算0.05舍弃值,核心代码
/// </summary>
/// <returns></returns>
private static decimal GetMathResult(decimal tax)
{
if ((tax / 0.05m).ToString().IndexOf(".") != -)
{ //rounded up to the nearest 0.05
tax = Math.Round(tax, , MidpointRounding.AwayFromZero);
}
return tax;
} } /// <summary>
/// 商品类型
/// 备注:书籍、食品、药品 可分为一个枚举就是免基本税的,这样细分,考虑后期扩展
/// </summary>
enum Enum_GoodType
{
Book = , //书籍
Food = , //食品
Drug = , //药品
Other = //其他
} }

分析:

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

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

经典面试题SALES TAXES思路分析和源码分享的更多相关文章

  1. Spring源码解析02:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析

    一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...

  2. Spring源码解析 | 第二篇:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析

    一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...

  3. 夯实Java基础系列3:一文搞懂String常见面试题,从基础到实战,更有原理分析和源码解析!

    目录 目录 string基础 Java String 类 创建字符串 StringDemo.java 文件代码: String基本用法 创建String对象的常用方法 String中常用的方法,用法如 ...

  4. Python经典算法-猴子吃桃-思路分析

    问题: 猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾就多吃了一个.第二天早上又将剩下的桃子吃了一半,还是不过瘾又多吃了一个.以后每天都吃前一天剩下的一半再加一个.到第10天刚好剩一个.问猴子第一天 ...

  5. [原创]Android Studio的Instant Run(即时安装)原理分析和源码浅析

    Android Studio升级到2.0之后,新增了Instant Run功能,该功能可以热替换apk中的部分代码,大幅提高测试安装的效率. 但是,由于我的项目中自定义了一些ClassLoader,当 ...

  6. tomcat架构分析和源码解读

    最近在看<深入分析java web技术内幕>,书中讲解了一部分tomcat的相关知识,我也去查看了一些源码,看了大神们写的代码,我才知道自己就像在做加减乘除一样,这是不行的.还有好多包和类 ...

  7. Kinect v2控制鼠标原理分析和源码

    https://blog.csdn.net/baolinq/article/details/54381284 此程序为利用Kinect v2实现用手指隔空控制鼠标,是我另一个项目的一部分,因为在另外那 ...

  8. 李洪强iOS经典面试题156 - Runtime详解(面试必备)

    李洪强iOS经典面试题156 - Runtime详解(面试必备)   一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C ...

  9. 李洪强iOS经典面试题142-第三方框架及其管理

    李洪强iOS经典面试题142-第三方框架及其管理   第三方框架及其管理 使用过CocoaPods吗?它是什么?CocoaPods的原理? CocoaPod是一个第三方库的管理工具,用来管理项目中的第 ...

随机推荐

  1. 记一次python的任务调度模块apscheduler只在首次执行任务的情况

    最近需要写个日更新的程序,用time.sleep()不能很好的控制任务的执行时间 于是,就使用了python的任务调度模块apscheduler,这个模块功能真的是很强大 具体的就不多讲了 将任务程序 ...

  2. [费用流][NOI2008]志愿者招募

    志愿者招募 题目描述 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难 题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完 ...

  3. python基础之Day9

    一.文件操作 1.r+t:可读可写 2.w+t:可写可读 3.a+t:可追加写.可读 4.f.seek(offset,whence) offset代表文件的指针的偏移量,单位是字节byteswhenc ...

  4. java多线程系列12 ConcurrentHashMap CopyOnWriteArrayList 简介

    我们知道 ,hashmap 和 arraylist 是线程不安全的 在多线程环境下有数据安全问题, 当然 我们可以通过Collections的一些方法把他们变成线程安全的, Collections.s ...

  5. c++两个类相互调用

    有可能会碰到两个类之间的相互调用的问题,例如:定义了类A和类B,A中使用了B定义的类型,B中也使用了A定义的类型 class A { B b; }; class B { A a; }; 编译器在声明A ...

  6. Eclipse中一些真正常用的快捷键

    F2 文件重命名(要使用某个过长的文件名或者不好打的文件名时,直接F2再Ctrl+C非常好用,比如:Validform_v5.3.2_min.js) Ctrl+S 保存当前文件 Ctrl+Shift+ ...

  7. 前端vue框架 脚手架

    1.安装node.js最新版本2.cmd下输入 1.node -v得到版本号检测是否安装成功 版本号要在6.9以上 2.npm -v 版本号要在3.10以上3.安装脚手架 1.npm install ...

  8. Request参数值自动去空格

    /// <summary> /// TypeTrimHelper /// </summary> public static class TypeTrimHelper { /// ...

  9. 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容

    [源码下载] 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容 作 ...

  10. 使用FFmpeg如何转发一个RTSP视频流

    版权声明:转载请说明出处:http://www.cnblogs.com/renhui/p/6930221.html   转发RTSP流,这类需求一般出现于转发一些摄像头采集视频,并在摄像头上做RTSP ...