平台为了方便做税务核对,对每一种商品进行税金的统计。
(1)平台上线之初,商品具有以下特性:
普通商品需要征收13%增值税 嗜好品/奢侈品需要在普通商品基础上,加收20%消费税 生鲜类农产品、二手商品不收税
请根据商品特性进行类图设计与代码实现(不需要提供接口和持久化功能)。
(2)随着规模扩大,平台开展了全球购业务,商品新增特性:
进口商品需要征收9%关税
请根据新增需求,调整类图设计与代码实现。设计需要考虑扩展性,以应对后续引入更多的税种。
(3)全球购业务运行一段时间,销量不是理想,原因是和海外的免税店相比,价格上没有竞争优势,于是开始周期性对当时的热门商品进行免税:
热门商品免除增值税、消费税、关税
java 实现如下,请参考:
public class MainProgram{
public static void main(String args){
Good g1 = new CommonGood(100.00);
Good g2 = new LuxuryGood(100.00);
Good g3 = new SeaAndSecondGood(100.00);
Good g4 = new InGood (100.00);
Good g5 = new HotGood (100.00);
double g1Value = g1.calcTaxForPrice();
double g2Value = g2.calcTaxForPrice();
double g3Value = g3.calcTaxForPrice();
double g4Value = g4.calcTaxForPrice();
double g5Value = g5.calcTaxForPrice();
}
}
//商品类
abstarct class Good {
public static double ValueAddedTax = 13.0 /100; //增值税率
public static double ComsumerTax = 20.0 /100;//消费税率
public static double TariffTax = 9.0 /100;//关税税率
public Good(double _price){
setGoodPrice(_price);
}
private double goodPrice;
public double getGoodPrice(){
return goodPrice;
}
public void setGoodPrice(double _goodPrice){
goodPrice = _goodPrice;
}
protected abstract double calcTaxForPrice();
}
//普通商品类
class CommonGood extends Good {
public double calcTaxForPrice(){
return getGoodPrice() * Good.ValueAddedTax;
}
}
//奢侈品商品类
class LuxuryGood extends CommonGood {
public double calcTaxForPrice(){
return supper.CalcTaxForPrice() + getGoodPrice() * Good.ComsumerTax ;
}
}
//生鲜二手
class SeaAndSecondGood extends Good {
public double calcTaxForPrice(){
return getGoodPrice() * 0.0;
}
}
//进口商品类
class InGood extends Good {
public double calcTaxForPrice(){
return getGoodPrice() * Good.TariffTax ;
}
}
//热门商品类
class HotGood extends Good {
public double calcTaxForPrice(){
return getGoodPrice() * 0.0 ;
}
}
该回答引用GPTᴼᴾᴱᴺᴬᴵ
// 商品类
public class Product {
private String name; // 商品名称
private double price; // 商品价格
private String type; // 商品类型,如普通商品、嗜好品、奢侈品、生鲜类农产品、二手商品、进口商品等
public Product(String name, double price, String type) {
this.name = name;
this.price = price;
this.type = type;
}
// 计算税金的方法
public double calculateTax() {
double tax = 0;
switch (type) {
case "普通商品":
tax = price * 0.13;
break;
case "嗜好品":
case "奢侈品":
tax = price * 0.33; // 20%消费税 + 13%增值税
break;
case "生鲜类农产品":
case "二手商品":
break; // 不收税
case "进口商品":
tax = price * 0.09; // 9%关税
break;
case "热门商品":
break; // 免税
default:
System.out.println("未知商品类型");
}
return tax;
}
// 计算商品总价的方法
public double getTotalPrice() {
return price + calculateTax();
}
// getter 和 setter 方法
// ...
}
针对第二个需求,需要新增一个“进口商品”的类,其继承自“商品”类,新增“calculateTax()”方法,计算9%的关税。同时,“Product”类中的“calculateTax()”方法需要做相应的调整,将进口商品的计算税金逻辑转移到“进口商品”类中。
// 进口商品类
public class ImportedProduct extends Product {
public ImportedProduct(String name, double price, String type) {
super(name, price, type);
}
// 计算关税的方法
public double calculateTax() {
return getPrice() * 0.09;
}
}
修改后的“Product”类:
// 商品类
public class Product {
private String name; // 商品名称
private double price; // 商品价格
private String type; // 商品类型,如普通商品、嗜好品、奢侈品、生鲜类农产品、二手商品、进口商品等
public Product(String name, double price, String type) {
this.name = name;
this.price = price;
this.type = type;
}
// 计算税金的方法
public double calculateTax() {
double tax = 0;
switch (type) {
case "普通商品":
tax = price * 0.13;
break;
case "嗜好品":
case "奢侈品":
tax = price * 0.33; // 20%消费税 + 13%增值税
break;
case "生鲜类农产品":
case "二手商品":
break; // 不收税
case "热门商品":
break; // 免税
default:
System.out.println("未知商品类型");
}
return tax;
}
// 计算商品总价的方法
public double getTotalPrice() {
return price + calculateTax();
}
// getter 和 setter 方法
// ...
}
针对第三个需求,可以新增一个“热门商品”的类,其继承自“商品”类,重写“calculateTax()”方法,使其返回0。同时,“Product”类中的“calculateTax()”方法需要做相应的调整,将热门商品的计算税金逻辑转移到“热门商品”类中。
// 热门商品类
public class PopularProduct extends Product {
public PopularProduct(String name, double price, String type) {
super(name, price, type);
}
// 计算税金的方法
public double calculateTax() {
return 0;
}
}
修改后的“Product”类:
// 商品类
public class Product {
private String name; // 商品名称
private double price; // 商品价格
private String type; // 商品类型,如普通商品、嗜好品、奢侈品、生鲜类农产品、二手商品、进口商品、热门商品等
public Product(String name, double price, String type) {
this.name = name;
this.price = price;
this.type = type;
}
// 计算税金的方法
public double calculateTax() {
double tax = 0;
switch (type) {
case "普通商品":
tax = price * 0.13;
break;
case "嗜好品":
case "奢侈品":
tax = price * 0.33; // 20%消费税 + 13%增值税
break;
case "生鲜类农产品":
case "二手商品":
break; // 不收税
case "进口商品":
tax = price * 0.09; // 9%关税
break;
case "热门商品":
tax = 0; // 免税
break;
default:
System.out.println("未知商品类型");
}
return tax;
}
// 计算商品总价的方法
public double getTotalPrice() {
return price + calculateTax();
}
// getter 和 setter 方法
// ...
}
这样设计的好处是,可以根据商品的特性,设计不同的子类,使代码更加清晰易懂,同时也具备扩展性,以应对后续引入更多的税种和商品类型。
该回答引用ChatGPT
如有疑问,可以回复我!
以下是 Java 实现。我们可以定义一个商品类 Commodity,包含商品名、价格和税金等信息,并通过继承实现不同种类商品的特性。
// 商品类
public class Commodity {
private String name; // 商品名
private double price; // 商品价格
private double tax; // 商品税金
public Commodity(String name, double price, double tax) {
this.name = name;
this.price = price;
this.tax = tax;
}
public double getPrice() {
return price;
}
public double getTax() {
return tax;
}
}
// 普通商品类
public class OrdinaryCommodity extends Commodity {
private static final double VAT_RATE = 0.13; // 增值税率
public OrdinaryCommodity(String name, double price) {
super(name, price, price * VAT_RATE);
}
}
// 嗜好品/奢侈品类
public class LuxuryCommodity extends Commodity {
private static final double CONSUMPTION_TAX_RATE = 0.2; // 消费税率
public LuxuryCommodity(String name, double price) {
super(name, price, price * (1 + VAT_RATE + CONSUMPTION_TAX_RATE));
}
}
// 生鲜类农产品类
public class FreshCommodity extends Commodity {
public FreshCommodity(String name, double price) {
super(name, price, 0);
}
}
// 二手商品类
public class SecondHandCommodity extends Commodity {
public SecondHandCommodity(String name, double price) {
super(name, price, 0);
}
}
// 进口商品类
public class ImportedCommodity extends Commodity {
private static final double IMPORT_TAX_RATE = 0.09; // 关税率
public ImportedCommodity(String name, double price) {
super(name, price, price * IMPORT_TAX_RATE);
}
}
// 热门商品类
public class HotCommodity extends Commodity {
public HotCommodity(String name, double price) {
super(name, price, 0);
}
@Override
public double getTax() {
return 0;
}
}
通过上述实现,我们可以对不同种类的商品进行实例化,计算其税金等信息,并进行统计分析。在后续引入更多的税种时,只需要添加新的商品类并继承商品类即可。
该回答引用CHATGPT
商品税金统计需求
为了方便做税务核对,平台需要对每一种商品进行税金的统计。根据商品的特性进行类图设计与代码实现。
商品特性
平台上线之初,商品具有以下特性:
普通商品需要征收13%增值税
嗜好品/奢侈品需要在普通商品基础上,加收20%消费税
生鲜类农产品、二手商品不收税
类图设计
根据商品特性,设计如下类图:
+-----------------------+
| Commodity |
+-----------------------+
| + name : String |
| + price : double |
| + category : Category |
+-----------------------+
| + calculateTax() : double |
+-----------------------+
+-----------------------+
| Category |
+-----------------------+
| + name : String |
| + taxRate : double |
+-----------------------+
| + calculateTax() : double |
+-----------------------+
+-----------------------+
| OrdinaryCategory |
+-----------------------+
| + calculateTax() : double |
+-----------------------+
+-----------------------+
| LuxuryCategory |
+-----------------------+
| + calculateTax() : double |
+-----------------------+
+-----------------------+
| FreshCategory |
+-----------------------+
| + calculateTax() : double |
+-----------------------+
代码实现
根据类图设计,实现如下代码:
public abstract class Commodity {
private String name;
private double price;
private Category category;
public Commodity(String name, double price, Category category) {
this.name = name;
this.price = price;
this.category = category;
}
public double calculateTax() {
return price * category.calculateTax();
}
}
public abstract class Category {
private String name;
private double taxRate;
public Category(String name, double taxRate) {
this.name = name;
this.taxRate = taxRate;
}
public double calculateTax() {
return taxRate;
}
}
public class OrdinaryCategory extends Category {
public OrdinaryCategory() {
super("Ordinary", 0.13);
}
public double calculateTax() {
return super.calculateTax();
}
}
public class LuxuryCategory extends Category {
public LuxuryCategory() {
super("Luxury", 0.33);
}
public double calculateTax() {
return super.calculateTax();
}
}
public class FreshCategory extends Category {
public FreshCategory() {
super("Fresh", 0.0);
}
public double calculateTax() {
return super.calculateTax();
}
}
新增需求
随着规模扩大,平台开展了全球购业务,商品新增特性:
进口商品需要征收9%关税
调整类图设计与代码实现
为了支持新增需求,需要调整类图设计和代码实现,设计如下类图:
+-----------------------+
| Commodity |
+-----------------------+
| + name : String |
| + price : double |
| + category : Category |
+-----------------------+
| + calculateTax() : double |
+-----------------------+
+-----------------------+
| Category |
+-----------------------+
| + name : String |
| + taxRates : Map<String, Double> |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
+-----------------------+
| OrdinaryCategory |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
+-----------------------+
| LuxuryCategory |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
+-----------------------+
| FreshCategory |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
代码实现如下:
public abstract class Commodity {
private String name;
private double price;
private Category category;
public Commodity(String name, double price, Category category) {
this.name = name;
this.price = price;
this.category = category;
}
public double calculateTax(String region) {
return price * category.calculateTax(region);
}
}
public abstract class Category {
private String name;
private Map<String, Double> taxRates;
public Category(String name, Map<String, Double> taxRates) {
this.name = name;
this.taxRates = taxRates;
}
public double calculateTax(String region) {
if (taxRates.containsKey(region)) {
return taxRates.get(region);
}
return 0.0;
}
}
public class OrdinaryCategory extends Category {
public OrdinaryCategory() {
super("Ordinary", Map.of("domestic", 0.13, "import", 0.22));
}
public double calculateTax(String region) {
return super.calculateTax(region);
}
}
public class LuxuryCategory extends Category {
public LuxuryCategory() {
super("Luxury", Map.of("domestic", 0.33, "import", 0.42));
}
public double calculateTax(String region) {
return super.calculateTax(region);
}
}
public class FreshCategory extends Category {
public FreshCategory() {
super("Fresh", Map.of("domestic", 0.0, "import", 0.09));
}
public double calculateTax(String region) {
return super.calculateTax(region);
}
}
免税商品
全球购业务运行一段时间,销量不如理想,原因是和海外的免税店相比,价格上没有竞争优势,于是开始周期性对当时的热门商品进行免税:
热门商品免除增值税、消费税、关税
为了支持免税商品,需要调整类图设计和代码实现,设计如下类图:
+-----------------------+
| Commodity |
+-----------------------+
| + name : String |
| + price : double |
| + category : Category |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
+-----------------------+
| Category |
+-----------------------+
| + name : String |
| + taxRates : Map<String, Double> |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
+-----------------------+
| OrdinaryCategory |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
+-----------------------+
| LuxuryCategory |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
+-----------------------+
| FreshCategory |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
+-----------------------+
| ExemptCategory |
+-----------------------+
| + calculateTax(String) : double |
+-----------------------+
代码实现如下:
public abstract class Commodity {
private String name;
private double price;
private Category category;
public Commodity(String name, double price, Category category) {
this.name = name;
this.price = price;
this.category = category;
}
public double calculateTax(String region) {
if (category instanceof ExemptCategory) {
return 0.0;
}
return price * category.calculateTax(region);
}
}
public abstract class Category {
private String name;
private Map<String, Double> taxRates;
public Category(String name, Map<String, Double> taxRates) {
this.name = name;
this.taxRates = taxRates;
}
public double calculateTax(String region) {
if (taxRates.containsKey(region)) {
return taxRates.get(region);
}
return 0.0;
}
}
public class OrdinaryCategory extends Category {
public OrdinaryCategory() {
super("Ordinary", Map.of("domestic", 0.13, "import", 0.22));
}
public double calculateTax(String region) {
return super.calculateTax(region);
}
}
public class LuxuryCategory extends Category {
public LuxuryCategory() {
super("Luxury", Map.of("domestic", 0.33, "import", 0.42));
}
public double calculateTax(String region) {
return super.calculateTax(region);
}
}
public class FreshCategory extends Category {
public FreshCategory() {
super("Fresh", Map.of("domestic", 0.0, "import", 0.09));
}
public double calculateTax(String region) {
return super.calculateTax(region);
}
}
public class ExemptCategory extends Category {
public ExemptCategory() {
super("Exempt", Map.of());
}
public double calculateTax(String region) {
return 0.0;
}
}
参考GPT和自己的思路:针对以上需求,可以进行如下设计:
首先,设计一个商品类(Product),其中包含以下属性和方法:
属性:
name:商品名称
price:商品价格
type:商品类型(普通商品、嗜好品/奢侈品、生鲜类农产品、二手商品、进口商品、热门商品等)
taxRate:税率
方法:
getPriceWithTax():返回加上税金后的商品价格
针对第一部分需求,我们可以在商品类中添加一个静态代码块,用于初始化普通商品、嗜好品/奢侈品、生鲜类农产品和二手商品的税率。
针对第二部分需求,我们可以在商品类中添加一个静态方法,用于设置进口商品的税率,并且考虑到扩展性,可以将不同的税种作为不同的子类,让商品类继承不同的子类即可。
针对第三部分需求,我们可以在商品类中添加一个静态方法,用于设置热门商品的税率,同时考虑到扩展性,可以采用装饰器模式,让商品类实现一个基本税金计算方法,再让不同的免税装饰器作为其装饰品即可。
下面是Java类图和代码示例:
// 商品类
public abstract class Product {
private String name;
private double price;
private double taxRate;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
// 获取商品名称
public String getName() {
return name;
}
// 获取商品价格
public double getPrice() {
return price;
}
// 获取商品类型
public abstract String getType();
// 获取税率
public double getTaxRate() {
return taxRate;
}
// 设置税率
public void setTaxRate(double taxRate) {
this.taxRate = taxRate;
}
// 获取加上税金后的商品价格
public double getPriceWithTax() {
return price * (1 + taxRate);
}
}
// 普通商品类
public class OrdinaryProduct extends Product {
private static double TAX_RATE = 0.13;
public OrdinaryProduct(String name, double price) {
super(name, price);
setTaxRate(TAX_RATE);
}
@Override
public String getType() {
return "普通商品";
}
}
// 嗜好品/奢侈品类
public class LuxuryProduct extends Product {
private static double TAX_RATE = 0.33;
public LuxuryProduct(String name, double price) {
super(name, price);
setTaxRate(TAX_RATE);
}
@Override
public String getType() {
return "嗜好品/奢侈品";
}
}
// 生鲜类农产品类
public class FreshProduct extends Product {
public FreshProduct(String name, double price) {
super(name, price);
setTaxRate(0);
}
@Override
public String getType() {
return "生鲜类农产品";
}
}
参考GPT的回答和自己的思路,针对这个需求,可以进行如下的类图设计和代码实现:
class Commodity {
private String name; // 商品名称
private double price; // 商品价格
private double taxRate; // 税率
public Commodity(String name, double price, double taxRate) {
this.name = name;
this.price = price;
this.taxRate = taxRate;
}
public double getPriceWithTax() {
return price * (1 + taxRate);
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public double getTaxRate() {
return taxRate;
}
public void setTaxRate(double taxRate) {
this.taxRate = taxRate;
}
}
class GeneralCommodity extends Commodity {
public GeneralCommodity(String name, double price) {
super(name, price, 0.13);
}
}
class LuxuryCommodity extends Commodity {
public LuxuryCommodity(String name, double price) {
super(name, price, 0.33);
}
}
class FreshCommodity extends Commodity {
public FreshCommodity(String name, double price) {
super(name, price, 0);
}
}
class SecondHandCommodity extends Commodity {
public SecondHandCommodity(String name, double price) {
super(name, price, 0);
}
}
class ImportedCommodity extends Commodity {
public ImportedCommodity(String name, double price) {
super(name, price, 0.09);
}
}
class PopularCommodity extends Commodity {
public PopularCommodity(String name, double price) {
super(name, price, 0);
}
@Override
public double getPriceWithTax() {
return price;
}
@Override
public void setTaxRate(double taxRate) {
// do nothing, popular commodity is tax free
}
}
在这个设计中,Commodity 是商品的基类,包含了商品的名称、价格和税率等属性。各种类型的商品继承自 Commodity 类,并通过构造函数初始化自己的税率。PopularCommodity 类覆盖了 getPriceWithTax 和 setTaxRate 方法,实现了热门商品的免税功能。
这个设计具有良好的扩展性,如果需要引入新的税种,只需要定义一个新的商品类型,并在构造函数中初始化自己的税率即可。另外,如果需要对已有的商品进行调整,也可以通过 setTaxRate 方法来修改其税率。
classDiagram
class Product{
- int id
- String name
- double price
- int type
+ double calculateTax()
}
class OrdinaryProduct{
+ double calculateTax()
}
class LuxuryProduct{
+ double calculateTax()
}
class FreshProduct{
+ double calculateTax()
}
class ImportedProduct{
+ double calculateTax()
}
class HotProduct{
+ double calculateTax()
}
Product <|-- OrdinaryProduct
Product <|-- LuxuryProduct
Product <|-- FreshProduct
Product <|-- ImportedProduct
Product <|-- HotProduct
public abstract class Product {
private int id;
private String name;
private double price;
private int type; // 0 - OrdinaryProduct, 1 - LuxuryProduct, 2 - FreshProduct, 3 - ImportedProduct, 4 - HotProduct
public abstract double calculateTax(); // calculate tax based on product type
// getters and setters
}
public class OrdinaryProduct extends Product {
@Override
public double calculateTax() {
return getPrice() * 0.13;
}
}
public class LuxuryProduct extends Product {
@Override
public double calculateTax() {
return getPrice() * 1.2 * 0.13;
}
}
public class FreshProduct extends Product {
@Override
public double calculateTax() {
return 0;
}
}
public class ImportedProduct extends Product {
@Override
public double calculateTax() {
return getPrice() * 0.09;
}
}
public class HotProduct extends Product {
@Override
public double calculateTax() {
return 0;
}
}
classDiagram
class Product{
- int id
- String name
- double price
- int type
+ double calculateTax()
}
class OrdinaryProduct{
+ double calculateTax()
}
class LuxuryProduct{
+ double calculateTax()
}
class FreshProduct{
+ double calculateTax()
}
class ImportedProduct{
+ double calculateTax()
}
class HotProduct{
+ double calculateTax()
}
Product <|-- OrdinaryProduct
Product <|-- LuxuryProduct
Product <|-- FreshProduct
Product <|-- ImportedProduct
Product <|-- HotProduct
public abstract class Product {
private int id;
private String name;
private double price;
private int type; // 0 - OrdinaryProduct, 1 - LuxuryProduct, 2 - FreshProduct, 3 - ImportedProduct, 4 - HotProduct
public abstract double calculateTax(); // calculate tax based on product type
// getters and setters
}
public class OrdinaryProduct extends Product {
@Override
public double calculateTax() {
return getPrice() * 0.13;
}
}
public class LuxuryProduct extends Product {
@Override
public double calculateTax() {
return getPrice() * 1.2 * 0.13;
}
}
public class FreshProduct extends Product {
@Override
public double calculateTax() {
return 0;
}
}
public class ImportedProduct extends Product {
@Override
public double calculateTax() {
return getPrice() * 0.09;
}
}
public class HotProduct extends Product {
@Override
public double calculateTax() {
return 0;
}
}
public class HotProduct extends Product {
@Override
public double calculateTax() {
return 0;
}
}
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
类图设计:
 {
this.name = name;
this.price = price;
}
// 获取不同税种的税金
public abstract double getTax();
// 获取商品价格
public double getPrice() {
return price;
}
// 获取带税金的总价
public double getTotalPrice() {
return price + getTax();
}
}
// 普通商品类
public class OrdinaryCommodity extends Commodity {
public OrdinaryCommodity(String name, double price) {
super(name, price);
}
// 征收13%增值税
public double getTax() {
return getPrice() * 0.13;
}
}
// 奢侈品/嗜好品类
public class LuxuryCommodity extends Commodity {
public LuxuryCommodity(String name, double price) {
super(name, price);
}
// 在普通商品基础上,加收20%消费税
public double getTax() {
return getPrice() * 0.33;
}
}
// 生鲜类农产品类
public class FreshCommodity extends Commodity {
public FreshCommodity(String name, double price) {
super(name, price);
}
// 不收税
public double getTax() {
return 0;
}
}
// 二手商品类
public class SecondHandCommodity extends Commodity {
public SecondHandCommodity(String name, double price) {
super(name, price);
}
// 不收税
public double getTax() {
return 0;
}
}
// 进口商品类
public class ImportedCommodity extends Commodity {
public ImportedCommodity(String name, double price) {
super(name, price);
}
// 征收9%关税
public double getTax() {
return getPrice() * 0.09;
}
}
// 热门商品类
public class HotCommodity extends Commodity {
public HotCommodity(String name, double price) {
super(name, price);
}
// 免除所有税金
public double getTax() {
return 0;
}
}
这样设计具有良好的扩展性,如果后续有新的税种引入,只需要新增一个类继承自Commodity
即可。