日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 经典面试题SALES TAXES思路分析和源码分享 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:

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.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.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 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>/// 獲取測試結果/// </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 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>/// 構造函數,初始化商品名稱/// </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.05tax = 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思路分析和源码分享的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。