题目:

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", 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> goods = new List<Goods>(); //第二批物品
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> goods = new List<Goods>(); //第三批物品
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);
}
/// <summary>
/// 获取测试结果
/// </summary>
/// <param name="goods"></param>
/// <returns></returns>
private TestCaseResult GetTestResult(List<Goods> 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);
}
}
/// <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 = 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;
}
/// <summary>
/// 计算0.05舍弃值,核心代码
/// </summary>
/// <returns></returns>
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;
}
}
/// <summary>
/// 商品类型
/// 备注:书籍、食品、药品 可分为一个枚举就是免基本税的,这样细分,考虑后期扩展
/// </summary>
enum Enum_GoodType
{
Book = 1,   //书籍
Food = 2,   //食品
Drug = 3,   //药品
Other = 4   //其他
    }
}

View Code

分析:

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

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

经典面试题SALES TAXES思路分析和源码分享相关推荐

  1. 解析并符号 读取dll_Spring IOC容器之XmlBeanFactory启动流程分析和源码解析

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

  2. python删除链表中重复的节点_Java编程删除链表中重复的节点问题解决思路及源码分享...

    一. 题目 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 二. 例子 输入链表:1->2->3->3->4->4-&g ...

  3. 【面试】968- 66 道前端常见算法面试题(附思路分析)

    本部分主要是 CavsZhouyou 在练习<剑指 Offer>时所做的笔记,主要涉及算法相关知识和一些相关面试题时所做的笔记,分享这份总结给大家,帮助大家对算法的可以来一次全方位的检漏和 ...

  4. Java 版植物大战僵尸思路和源码分享!

    点击上方"逆锋起笔",公众号回复 PDF 领取大佬们推荐的学习资料 来源 | https://urlify.cn/byeEjy 有谁没玩过植物大战僵尸吗?用Java语言开发了自己的 ...

  5. [文档和源码分享]C++实现的基于α-β剪枝算法的井字棋游戏

    "井字棋"游戏(又叫"三子棋"),是一款十分经典的益智小游戏,操作简单,娱乐性强.两个玩家,一个打圈(O),一个打叉(X),轮流在3乘3的格上打自己的符号,最先 ...

  6. [文档和源码分享] 基于JAVA实现的塔防游戏

    塔防游戏主要代表一类通过在游戏地图上装置炮塔,阻止敌人进攻的策略型游戏.本游戏是在地图上的特定地点装置多种能力不同的炮台以抵御多种怪兽的入侵.同时玩家每场战斗将拥有多种道具让玩家防守更加轻松.游戏原型 ...

  7. [文档和源码分享] 基于WIN32 API界面编程实现的百战天虫小游戏

    在游戏编写的过程中,我一直在思考我自己制作的游戏的主旨是什么,想来想去,结合"百战天虫"游戏的特点,我想到了"战争"这个主旨.游戏中阵营的相互厮杀不正如国家之间 ...

  8. DenseNet重点介绍和源码分享

    1 背景 1.1 引入原因 研究表明,非常深的ResNet中,越往后,其层数的增加对效果的提升越少,因此超深ResNet中的层数可以随机丢掉,类似于循环神经网络.因此DenseNet基于这一特性,不在 ...

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

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

最新文章

  1. python页面跳转中_python web页面跳转
  2. SOCKS代理工具EarthWorm、sSoks
  3. 210317阶段三opencv
  4. iphone开发JSON库之BSJSONAdditions
  5. 区间数多属性决策matlab,区间数多属性决策的改进理想解法
  6. LeetCode 1305. 两棵二叉搜索树中的所有元素(二叉树迭代器)
  7. 布线规划要点-开始设计前必须考虑的几个问题
  8. php指纹登录原理,指纹锁的应用原理竟然是这样,你知道吗?
  9. 【React Native 安卓开发】----(mac下开发环境配置)【第一篇】
  10. angularJs内置指令63个
  11. win7 exfat补丁_大神面对 win7系统安装补丁提示安装程序出错的操作方案 -win7系统使用教程...
  12. JDBC在jsp中的使用
  13. 光纤接口类型及光纤收发器指示灯图解
  14. 4000 字详解「用户反馈」的收集与分析
  15. python实现简单的求矩阵的逆
  16. P7961 [NOIP2021] 数列
  17. 协同过滤算法(基于用户)
  18. 逻辑越权——垂直、水平越权
  19. 要知道宇宙有多少星球,比数清地球上的沙子数量还要困难!
  20. 信息无障碍的发展和技术实践

热门文章

  1. redhat系统双网卡绑定
  2. linux下安装phantomjs
  3. 日常问题——Mac下新建目录报Read-only file system
  4. ubuntu下wps不能输入中文
  5. 蓝桥杯 出现次数最多的整数
  6. 分块编码(Transfer-Encoding: chunked)VS Content-length
  7. 解决outlook2013设置错误无法启动
  8. ios页面间跳转方式总结
  9. 软件外包业的崛起,掀起电脑培训热潮
  10. Linux设置ssh免密码登录