经典面试题SALES TAXES思路分析和源码分享

简介: 题目: 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%。

题目:

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,详见代码。

 

img_fa0be433d68c8212b2b0b3b1a564ccb1.png
如果本文对你有所帮助,请打赏——1元就足够感动我:)
支付宝打赏 微信打赏
联系邮箱:intdb@qq.com
我的GitHub: https://github.com/vipstone
关注公众号: img_9bde0f31ac4a0eca10b1bd7414b78faf.png


作者: 王磊
出处: http://vipstone.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,请标明出处。

相关文章
|
23天前
|
JavaScript 前端开发
【Vue面试题二十五】、你了解axios的原理吗?有看过它的源码吗?
这篇文章主要讨论了axios的使用、原理以及源码分析。 文章中首先回顾了axios的基本用法,包括发送请求、请求拦截器和响应拦截器的使用,以及如何取消请求。接着,作者实现了一个简易版的axios,包括构造函数、请求方法、拦截器的实现等。最后,文章对axios的源码进行了分析,包括目录结构、核心文件axios.js的内容,以及axios实例化过程中的配置合并、拦截器的使用等。
【Vue面试题二十五】、你了解axios的原理吗?有看过它的源码吗?
|
23天前
|
JavaScript 前端开发
【Vue面试题二十七】、你了解axios的原理吗?有看过它的源码吗?
文章讨论了Vue项目目录结构的设计原则和实践,强调了项目结构清晰的重要性,提出了包括语义一致性、单一入口/出口、就近原则、公共文件的绝对路径引用等原则,并展示了单页面和多页面Vue项目的目录结构示例。
|
1月前
|
机器学习/深度学习 算法 数据中心
【机器学习】面试问答:PCA算法介绍?PCA算法过程?PCA为什么要中心化处理?PCA为什么要做正交变化?PCA与线性判别分析LDA降维的区别?
本文介绍了主成分分析(PCA)算法,包括PCA的基本概念、算法过程、中心化处理的必要性、正交变换的目的,以及PCA与线性判别分析(LDA)在降维上的区别。
41 4
面试官: 请你手写一份 Call()源码,看完此篇不用担心!
面试官: 请你手写一份 Call()源码,看完此篇不用担心!
|
1月前
|
存储 JavaScript 前端开发
JS浅拷贝及面试时手写源码
JS浅拷贝及面试时手写源码
|
2月前
|
监控 Java 开发者
Java面试题:如何使用JVM工具(如jconsole, jstack, jmap)来分析内存使用情况?
Java面试题:如何使用JVM工具(如jconsole, jstack, jmap)来分析内存使用情况?
112 2
|
2月前
|
存储 安全 Java
Android面试题之ArrayList源码详解
ArrayList是Java中基于数组实现的列表,提供O(1)的索引访问,但插入和删除操作平均时间复杂度为O(n)。默认容量为10,当需要时会通过System.arraycopy扩容。允许存储null,非线程安全。面试常问:List是接口,ArrayList是其实现之一,推荐使用List接口编程以实现更好的灵活性。更多详情见[ArrayList源码](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/ArrayList.java#ArrayList.Node)。
23 2
|
2月前
|
算法 Java API
Android性能优化面试题经典之ANR的分析和优化
Android ANR发生于应用无法在限定时间内响应用户输入或完成操作。主要条件包括:输入超时(5秒)、广播超时(前台10秒/后台60秒)、服务超时及ContentProvider超时。常见原因有网络、数据库、文件操作、计算任务、UI渲染、锁等待、ContentProvider和BroadcastReceiver的不当使用。分析ANR可借助logcat和traces.txt。主线程执行生命周期回调、Service、BroadcastReceiver等,避免主线程耗时操作
45 3
|
2月前
|
设计模式 安全 NoSQL
Java面试题:结合单例模式与Java内存管理,设计一个线程安全的单例类?分析Java多线程工具类ExecutorService与Java并发工具包中的工具类,设计一个Java并发框架的分布式锁实现
Java面试题:结合单例模式与Java内存管理,设计一个线程安全的单例类?分析Java多线程工具类ExecutorService与Java并发工具包中的工具类,设计一个Java并发框架的分布式锁实现
40 0
|
2月前
|
设计模式 安全 Java
Java面试题:请列举三种常用的设计模式,并分别给出在Java中的应用场景?请分析Java内存管理中的主要问题,并提出相应的优化策略?请简述Java多线程编程中的常见问题,并给出解决方案
Java面试题:请列举三种常用的设计模式,并分别给出在Java中的应用场景?请分析Java内存管理中的主要问题,并提出相应的优化策略?请简述Java多线程编程中的常见问题,并给出解决方案
67 0