java - 使用特定值计算税金

标签 java

我无法理解如何让我的程序正确计算这些税号
如果单例和应税收入结束 但还没有结束 税是 金额超过

$0
$8,000  10% $0
$8,000  $32,000 $800 + 15%  $8,000
$32,000 
$4,400 + 25%    $32,000
If married and taxable income is over   but not over    the tax is  of the amount over
$0  $16,000 10% $0
$16,000 $64,000 $1,600 + 15%    $16,000
$64,000 
$8,800 + 25%    $64,000
You should prompt the user for their filing status and their taxable income. The user should enter 'S' for single filers and 'M' for married filers.

例如,如果纳税人是单例且收入为 10,000 美元。他们将支付 800 美元加上 2000 美元的 15% (10,000 - 8,000)。 这就是我到目前为止所拥有的

public class Income_tax

{  



 public static final int SINGLE = 1;

   public static final int MARRIED = 2;


   private static final double RATE1 = 0.10;

   private static final double RATE1_SINGLE_LIMIT = 8000;

   private static final double RATE1_MARRIED_LIMIT = 16000;

   private static final double RATE2_SINGLE_LIMIT = 32000;

   private static final double RATE2_MARRIED_LIMIT = 64000;

   private static final double RATE3_SINGLE_LIMIT = 60000;

   private static final double RATE3_MARRIED_LIMIT = 150000;

private static final double RATE2 = 0.15;

private static final double RATE3 = 0.25;




private double income;

   private int status;


   public Income_tax(double anIncome, int aStatus)

   {  

      income = anIncome;

      status = aStatus;

   }



  public double getTax()

   {  

  double tax1 = 0;

  double tax2 = 0;

  if (status == SINGLE)

  {  

     if (income <= RATE1_SINGLE_LIMIT)

     {

        tax1 = RATE1 * income;

     }

     else

     {

        tax1 = RATE1 * RATE1_SINGLE_LIMIT;

        tax2 = RATE2 * (income - RATE1_SINGLE_LIMIT);

     }

  }

   if (income <= RATE3_SINGLE_LIMIT)

  {  
      tax1 = RATE3 * income;


     if (income <= RATE1_MARRIED_LIMIT)

     {
        tax1 = RATE1 * income;

     }
     if (income <= RATE3_MARRIED_LIMIT)
     {
         tax1 = RATE3 * income;
     }

     else 

     {

        tax1 = RATE1 * RATE1_MARRIED_LIMIT;

        tax2 = RATE2 * (income - RATE1_MARRIED_LIMIT);

     }
     if (status == SINGLE)

     {  

        if (income <= RATE2_SINGLE_LIMIT)

        {

           tax1 = RATE2 * income;

        }

        else

        {

           tax1 = RATE1 * RATE2_SINGLE_LIMIT;

           tax2 = RATE1 * (income - RATE2_SINGLE_LIMIT);

        }

     }

     else

     {  

        if (income <= RATE2_MARRIED_LIMIT)

        {
           tax1 = RATE2 * income;

        }

        else 

        {

           tax1 = RATE2 * RATE2_MARRIED_LIMIT;

           tax2 = RATE1 * (income - RATE2_MARRIED_LIMIT);

        }
  }
  return tax1 + tax2; 



 }
    return tax2 + tax1;

}
}

我的计算器类(class)是

    import java.util.Scanner;

public class TaxCalculator {
     @SuppressWarnings("resource")
    public static void main(String[] args)

        {  

           Scanner in = new Scanner(System.in);


           System.out.print("Please enter your anual income [per year]: ");

           double income = in.nextDouble();


           System.out.print("Please enter S for Single and M for Married (S/M) ");

           String input = in.next();

           int status;

           if (input.equals("M")) 

           {

              status = Income_tax.MARRIED;

           }

           else  

           {

              status = Income_tax.SINGLE;

           }

           Income_tax aTaxReturn = new Income_tax(income, status);

           System.out.println(" Your tax is: "

                 + aTaxReturn.getTax());

        }

     }

最佳答案

我不太确定为什么您在主方法中将“aTaxReturn”作为对象调用,这似乎有点不必要。您可以将其作为常规方法调用。

我会做的一种方法是有两种不同的方法,一种用于单例,一种用于已婚。 然后,在每个单独的方法中,您可以分割税收计算,例如:

public class Income_tax {

    private static final double RATE1 = 0.10;

    private static final double RATE1_SINGLE_LIMIT = 8000;

    private static final double RATE1_MARRIED_LIMIT = 16000;

    private static final double RATE2_SINGLE_LIMIT = 32000;

    private static final double RATE2_MARRIED_LIMIT = 64000;

    private static final double RATE3_SINGLE_LIMIT = 60000;

    private static final double RATE3_MARRIED_LIMIT = 150000;

    private static final double RATE2 = 0.15;

    private static final double RATE3 = 0.25;

    //private double income; Don't need these since you're not making an object

    //private int status; Don't need these since you're not making an object

//Default constructor since you don't need to make an object
    public Income_tax() {

    }

    public double getTaxSingle(double income) {
        //Different if statements for tax calculations
    }

    public double getTaxMarried(double income) {
        //Different if statements for tax calculations
    }
}

然后在 main 方法中你可以这样调用它:

public static double getTaxRateSingle(double income) {
        return Income_tax.getTaxSingle(income);
    }

    public static double getTaxRateMarried(double income) {
        return Income_tax.getTaxMarried(income);
    }

这样您就不需要创建不必要的对象,并且可以在保持简单性的同时最大限度地减少代码。

还有一个提示:您应该温习 Java naming conventions

关于java - 使用特定值计算税金,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58610441/

相关文章:

java - @QueryParam 总是显示 null

java - 如何避免连接器在 Open Office/Libre Office Draw 中移动形状

java - 设置土耳其语和英语语言环境 : translate Turkish characters to Latin equivalents

java - 获取 Windows 注销时间?

java - 如何覆盖策略 GenerationType.TABLE 的 Hibernate Id 生成

java - 在Java中查找GenericArrayList参数的长度

java - 如何选择Antlr4运行时的启动规则?

java - 将 JSON 反序列化为 Java 枚举

java - 将快捷方式添加到插件中定义的快捷方式 Eclipse 操作

java - 我应该把 JUnit 测试放在哪里?