// 定义税率区间tax_bracket 1range: 0 - 15000rate: 5%tax_bracket 2range: 15001 - 45000rate: 10%tax_bracket 3range: 45001 - 120000rate: 20%tax_bracket 4range: 120001 - 200000rate: 30%tax_bracket 5//注:就是这个不同寻常的格式引发了Bugrange: 200001 and aboverate: 40%// 定义抵免额deduction Standardamount: 5000deduction Health_Insuranceamount: 2000deduction Retirement_Savingsamount: 3000
// 计算应纳税额income: 80000deductions: Standard, Health_Insurance, Retirement_Savingscalculate_tax
import java.util.ArrayList;import java.util.Arrays;import java.util.List;class TaxBracket {int id;int lowerBound;int upperBound;double rate;public TaxBracket(int id, int lowerBound, int upperBound, double rate) {this.id = id;this.lowerBound = lowerBound;this.upperBound = upperBound;this.rate = rate;}}class Deduction {String name;int amount;public Deduction(String name, int amount) {this.name = name;this.amount = amount;}}class TaxCalculator {List<TaxBracket> taxBrackets;List<Deduction> deductions;int income;public TaxCalculator(int income, List<TaxBracket> taxBrackets, List<Deduction> deductions) {this.income = income;this.taxBrackets = taxBrackets;this.deductions = deductions;}public double calculateTax() {int taxableIncome = income;for (Deduction deduction : deductions) {taxableIncome -= deduction.amount;}double tax = 0;for (TaxBracket bracket : taxBrackets) {if (taxableIncome <= bracket.upperBound) {tax += (taxableIncome - bracket.lowerBound ) * bracket.rate;break;} else {tax += (bracket.upperBound - bracket.lowerBound ) * bracket.rate;}}return tax;}}public class Main {public static void main(String[] args) {List<TaxBracket> taxBrackets = new ArrayList<>(Arrays.asList(new TaxBracket(1, 0, 15000, 0.05),new TaxBracket(2, 15001, 45000, 0.10),new TaxBracket(3, 45001, 120000, 0.20),new TaxBracket(4, 120001, 200000, 0.30),new TaxBracket(5, 200001, Integer.MAX_VALUE, 0.40)));List<Deduction> deductions = new ArrayList<>(Arrays.asList(new Deduction("Standard", 5000),new Deduction("Health_Insurance", 2000),new Deduction("Retirement_Savings", 3000)));int income = 80000;TaxCalculator calculator = new TaxCalculator(income, taxBrackets, deductions);double tax = calculator.calculateTax();System.out.println("应纳税额: " + tax);}}
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;class DSLParser {private final List<TaxBracket> taxBrackets = new ArrayList<>();private final List<Deduction> deductions = new ArrayList<>();private int income;public DSLParser(String filePath) {try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {String line;while ((line = reader.readLine()) != null) {parseLine(line.trim());}} catch (IOException e) {System.err.println("Error reading file: " + e.getMessage());}}private void parseLine(String line) {if (line.startsWith("tax_bracket")) {int id = Integer.parseInt(line.split(" ")[1]);int lowerBound = 0;int upperBound = 0;double rate = 0;for (int i = 0; i < 2; i++) {line = getNextLine();if (line.startsWith("range:")) {String[] range = line.substring(6).trim().split(" - ");lowerBound = Integer.parseInt(range[0]);if (range[1].equals("and above")) {upperBound = Integer.MAX_VALUE;} else {upperBound = Integer.parseInt(range[1]);}} else if (line.startsWith("rate:")) {rate = Double.parseDouble(line.substring(5).trim().replace("%", "")) 100;}}taxBrackets.add(new TaxBracket(id, lowerBound, upperBound, rate));} else if (line.startsWith("deduction")) {String name = line.split(" ")[1];int amount = 0;line = getNextLine();if (line.startsWith("amount:")) {amount = Integer.parseInt(line.substring(7).trim());}deductions.add(new Deduction(name, amount));} else if (line.startsWith("income:")) {income = Integer.parseInt(line.substring(7).trim());} else if (line.equals("calculate_tax")) {// Do nothing, just a marker to calculate tax}}private String getNextLine() {// Read the next line from the file, or return null if end of file is reached}public List<TaxBracket> getTaxBrackets() {return taxBrackets;}public List<Deduction> getDeductions() {return deductions;}public int getIncome() {return income;}}
public class Main {public static void main(String[] args) {if (args.length != 1) {System.out.println("Usage: java Main <DSL file path>");return;}DSLParser parser = new DSLParser(args[0]);List<TaxBracket> taxBrackets = parser.getTaxBrackets();List<Deduction> deductions = parser.getDeductions();int income = parser.getIncome();TaxCalculator calculator = new TaxCalculator(income, taxBrackets, deductions);double tax = calculator.calculateTax();System.out.println("应纳
注:这里没输出完,就断掉了。
刘欣:继续
GPT-4 :
税额: " + tax);}}
现在,这个实现已经包括了解析DSL文件的功能。你可以将上述代码保存为Java源文件(例如`TaxCalculation.java`),并将你的DSL文件保存为一个文本文件(例如`tax.dsl`)。然后使用以下命令编译并运行代码:
javac TaxCalculation.javajava Main tax.dsl
class DSLParser {private final List<TaxBracket> taxBrackets = new ArrayList<>();private final List<Deduction> deductions = new ArrayList<>();private int income;private BufferedReader reader;public DSLParser(String filePath) {try {this.reader = new BufferedReader(new FileReader(filePath));String line;while ((line = reader.readLine()) != null) {parseLine(line.trim());}reader.close();} catch (IOException e) {System.err.println("Error reading file: " + e.getMessage());}}private void parseLine(String line) {// ... (same as before)}private String getNextLine() {try {return reader.readLine();} catch (IOException e) {System.err.println("Error reading next line: " + e.getMessage());return null;}}// ... (getTaxBrackets, getDeductions, and getIncome methods)}
private void parseLine(String line) {line = line.trim();if (line.startsWith("tax_bracket")) {// ... (same as before)} else if (line.startsWith("deduction")) {// ... (same as before)} else if (line.startsWith("income:")) {income = Integer.parseInt(line.substring(7).trim());} else if (line.equals("calculate_tax")) {// Do nothing, just a marker to calculate tax}}
private String getNextLine() {try {String line = reader.readLine();if (line != null) {return line.trim();}} catch (IOException e) {System.err.println("Error reading next line: " + e.getMessage());}return null;}
Exception in thread "main" java.lang.NumberFormatException: For input string: "200001 and above"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:580)at java.lang.Integer.parseInt(Integer.java:615)at DSLParser.parseLine(App.java:94)at DSLParser.<init>(App.java:73)at App.main(App.java:150)
private void parseLine(String line) {if (line.startsWith("tax_bracket")) {int id = Integer.parseInt(line.split(" ")[1]);int lowerBound = 0;int upperBound = 0;double rate = 0;for (int i = 0; i < 2; i++) {line = getNextLine();if (line.startsWith("range:")) {String rangeString = line.substring(6).trim();if (rangeString.contains("and above")) {String[] range = rangeString.split(" and above");lowerBound = Integer.parseInt(range[0]);upperBound = Integer.MAX_VALUE;} else {String[] range = rangeString.split(" - ");lowerBound = Integer.parseInt(range[0]);upperBound = Integer.parseInt(range[1]);}} else if (line.startsWith("rate:")) {rate = Double.parseDouble(line.substring(5).trim().replace("%", "")) 100;}}taxBrackets.add(new TaxBracket(id, lowerBound, upperBound, rate));} else if (line.startsWith("deduction")) {// ... (same as before)} else if (line.startsWith("income:")) {income = Integer.parseInt(line.substring(7).trim());} else if (line.equals("calculate_tax")) {// Do nothing, just a marker to calculate tax}}
本文转自作者刘欣,著有畅销书《码农翻身》,《半小时漫画计算机》,前IBM架构师,领导过多个企业应用架构设计和开发工作
文章转载自云原生数据库,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




