经典面试题SALES TAXES思路分析和源码分享
生活随笔
收集整理的這篇文章主要介紹了
经典面试题SALES TAXES思路分析和源码分享
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目:
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%。進(jìn)口稅附加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.85Input 2: 1 imported box of chocolates at 10.00 1 imported bottle of perfume at 47.50Input 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.25OUTPUTOutput 1: 1 book : 12.49 1 music CD: 16.49 1 chocolate bar: 0.85 Sales Taxes: 1.50 Total: 29.83Output 2: 1 imported box of chocolates: 10.50 1 imported bottle of perfume: 54.65 Sales Taxes: 7.65 Total: 65.15Output 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.68C#實(shí)現(xiàn)代碼如下:
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; } //稅合計(jì)public decimal TotalPrice { get; set; } //總計(jì)含稅public TestCaseResult(decimal Taxes, decimal TotalPrice){this.Taxes = Taxes;this.TotalPrice = TotalPrice;}}public class Test{//Test case1public 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>/// 獲取測試結(jié)果/// </summary>/// <param name="goods"></param>/// <returns></returns>private TestCaseResult GetTestResult(List<Goods> goods){decimal totalGoods = 0; //總價(jià)不含稅decimal totalGoodsByTax = 0; //總價(jià)含稅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 1var test = new Test();var result = test.GetResultForCasee1();Assert.AreEqual(1.50m, result.Taxes);Assert.AreEqual(29.83m, result.TotalPrice);//Test case 2result = test.GetResultForCasee2();Assert.AreEqual(7.65m, result.Taxes);Assert.AreEqual(65.15m, result.TotalPrice);//Test case 3result = test.GetResultForCasee3();Assert.AreEqual(6.70m, result.Taxes);Assert.AreEqual(74.68m, result.TotalPrice);}}/// <summary>/// 商品名稱/// </summary>class Goods{/// <summary>/// 構(gòu)造函數(shù),初始化商品名稱/// </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>/// 商品數(shù)量/// </summary>public int Count { set; get; }/// <summary>/// 是否進(jìn)口(true=>進(jìn)口)/// </summary>public bool Import { set; get; }/// <summary>/// 商品類型 對應(yīng)枚舉 Enum_GoodType/// </summary>public int GoodsType { set; get; }/// <summary>/// 單價(jià)/// </summary>public decimal Price { set; get; }/// <summary>/// 基本稅/// </summary>const decimal BasicDuty = 0.1m;/// <summary>/// 進(jìn)口附加稅/// </summary>const decimal ImportSurtax = 0.05m;/// <summary>/// 計(jì)算商品的價(jià)格/// </summary>/// <param name="goods">商品</param>/// <returns>商品最終價(jià)格</returns>public static decimal GetGoodsPriceByTax(Goods goods){decimal result = 0;if (null != goods){if (goods.Import){ //進(jìn)口物品,添加附加稅decimal appTax = goods.Price * goods.Count * ImportSurtax; result += GetMathResult(appTax);}if (goods.GoodsType == 4){//不是書籍、食品、藥品 需要征收基礎(chǔ)稅,上面也可以寫成(goods.GoodsType==(int)Enum_GoodType.Book ||...)decimal baseTax = goods.Price * goods.Count * BasicDuty;result += GetMathResult(baseTax);}result += goods.Price * goods.Count;}return result;}/// <summary>/// 計(jì)算0.05舍棄值,核心代碼/// </summary>/// <returns></returns>private static decimal GetMathResult(decimal tax){if ((tax / 0.05m).ToString().IndexOf(".") != -1){ //rounded up to the nearest 0.05tax = Math.Round(tax, 1, MidpointRounding.AwayFromZero);}return tax;}}/// <summary>/// 商品類型/// 備注:書籍、食品、藥品 可分為一個(gè)枚舉就是免基本稅的,這樣細(xì)分,考慮后期擴(kuò)展/// </summary>enum Enum_GoodType{Book = 1, //書籍Food = 2, //食品Drug = 3, //藥品Other = 4 //其他 }} View Code分析:
這道題考察的點(diǎn)有兩個(gè),一個(gè)是對業(yè)務(wù)的理解能力;二是編碼能力的考量,封裝繼承以及寫代碼功底如何;
編碼能力每個(gè)人有不同的風(fēng)格和書寫方式,盡量遵循代碼設(shè)計(jì)模式輕耦合,模塊化是最好的,而這道題的業(yè)務(wù)中有一個(gè)核心點(diǎn),就是對稅收不能被0.05整除要四舍五入到小數(shù)點(diǎn)后一位x.x,詳見代碼。
?
總結(jié)
以上是生活随笔為你收集整理的经典面试题SALES TAXES思路分析和源码分享的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js节流函数和js防止重复提交的N种方法
- 下一篇: 一个好的技术团队应该怎么选择开发语言