ThoughtWorks公司2014校园招聘作业题

PROBLEM : 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 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) 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 chocolate 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 box of imported chocolates: 11.85 
Sales Taxes: 6.70 
Total: 74.68
类似题目:
基本销售税率是 10%,但是以下商品除外: 书,食物, 医用用品, 这些产品是免销税的。 进口税是一种附加的销售税, 按照进口商品价格的 5%收取,没有任何进口商品能免除进口税。
  当我买商品的时候,我会受到一个列出了所有商品的名称和价格(含税)的收据, 在收据的最下面会提示总的销售税和总的价格。销售税四舍五入的规则是:税率是N%, 那么价格是P的商品所包含的税率是NP/100 再四舍五入到与这个数最接近的0.05, 比如如果是 3.44,那么四舍五入之后就是3.45,3.41四舍五入之后就是3.40
  写一个程序,为以下的购物栏输出上面提到的数据

INPUT:
input1:
1 book at 12.49
1 music CD at 14.99
1 chocolate bar at 0.85

OUTPUT
Output1
1 book : 12.49
1 music CD : 16.49
1 chocolate bar : 0.85
Sales Taxes : 1.50
Total 29.83
java实现如下:
package com.duapp.itfanr;
public class MainTest {
public static void main(String[] args) {
SellTax selltax = new SellTax();
selltax.readin();
selltax.view();
}
}
 
package com.duapp.itfanr;

public class Goods {
//定义   商品名称  数目 单价
private int amount;
private String name;
private double price;
public Goods(){}
public Goods(int amount,String name,double price){
this.amount = amount;
this.name = name;
this.price = price;
}
/**
* @return the id
*/
public int getAmount() {
return amount;
}
/**
* @param id the id to set
*/
public void setAmount(int amount) {
this.amount = amount;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
}
 
package com.duapp.itfanr;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SellTax {

	List<Goods> mlist = new ArrayList<Goods>();// 清单
	String name;// 商品名称
	Goods goods = null;
	int amount = 0;// 商品件数

	double price = 0.0;// 商品单价

	static private double basicTax = 0.1;// 基本税

	static private double importTax = 0.05;// 进口税

	int kind = 0;// 商品种类

	int entranceFlg = 0;// 是否进口

	double totalTax = 0.0;// 总税收

	double totalPrice = 0.0;// 总价格

	String goodsMessage = "";

	public void readin() {

		boolean flag = true; //输入停止标志位
		double tax = 0.0;
		double goodPrice = 0.0;
		while (flag) {
			System.out.println("请输入商品名称:");
			@SuppressWarnings("resource")
			Scanner scan = new Scanner(System.in);
			try {
				name = scan.next();
			} catch (Exception e) {
				System.out.println(e);
			}

			System.out.println("请输入购买数量:");
			@SuppressWarnings("resource")
			Scanner goodsAmount = new Scanner(System.in);
			try {
				amount = goodsAmount.nextInt();
			} catch (Exception e) {
				System.out.println("请输入正整数!");
				break;
			}

			System.out.println("请输入商品单价:");
			@SuppressWarnings("resource")
			Scanner goodsPrice = new Scanner(System.in);
			try {
				price = goodsPrice.nextDouble();
			} catch (Exception e) {

				System.out.println("请输入数字!");
			}
			//根据商品种类和进口与否来确定税额
			System.out.println("请输入商品种类: 1 书;2 食物;3 医疗用品;4 其他");

			@SuppressWarnings("resource")
			Scanner goodsKind = new Scanner(System.in);
			try {
				kind = goodsKind.nextInt();
				if (kind > 4) {
					System.out.println("超出可选范围");
				}
			} catch (Exception e) {
				System.out.println("请输入数字");
			}

			System.out.println("该商品是否进口: 1 进口;0 非进口");
			@SuppressWarnings("resource")
			Scanner goodsEntranceFlg = new Scanner(System.in);
			try {
				entranceFlg = goodsEntranceFlg.nextInt();
			} catch (Exception e) {
				System.out.println(e);
			}
			tax = this.computeTax();
			totalTax += tax;
			goodPrice = price + tax; //加税后的总价格
			goods = new Goods(amount, name, goodPrice);
			mlist.add(goods);

			System.out.println("是否继续添加:1.是,2.否!");
			@SuppressWarnings("resource")
			Scanner exit = new Scanner(System.in);
			String arg = exit.next();
			if (arg.equals("1"))
				flag = true;
			else
				break;//结束输入
		}

	}

	// 计算税收
	public double computeTax() {
		double tax1 = 0.0;
		double tax2  = 0.0 ;
		//除了books food medical products 之外的
		if (kind == 4) {
			tax1 = this.price * basicTax * this.amount;
			int temp = (int) Math.round(tax1 * 100);
			tax1 = (double) temp / 100.00;
		}
		//表示进口
		if (entranceFlg == 1) {
			tax2 = this.price * importTax * this.amount;
			int temp = (int) Math.round(tax2 * 100);
			tax2 = (double) temp / 100.00;

		}
		return tax1+tax2 ;
	}

	// 计算总价格
	public double computePrice() {
		double totalPrice = 0.0;
		for (int i = 0; i < mlist.size(); i++) {
			Goods good = (Goods) mlist.get(i);
			totalPrice += good.getPrice();

		}
		int temp = (int) Math.round(totalPrice * 100);
		totalPrice = (double) temp / 100.00;
		return totalPrice;
	}

	// 显示总税收和总价
	public void view() {
		for (int i = 0; i < mlist.size(); i++) {
			Goods good = (Goods) mlist.get(i);
			goodsMessage = good.getAmount() + " " + good.getName() + " "
					+ Math.round(good.getPrice()) ;
			System.out.println(goodsMessage + "\n");
		}
		System.out.println("Sales Taxes: " + totalTax);
		System.out.println("Sales TotlePrice:" + computePrice());
	}
}
 

转载于:https://my.oschina.net/itfanr/blog/195655

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值