简体   繁体   English

销售佣金不用Java计算

[英]Sales commision not calculating in Java

I am running into a very simple problem that is driving me insane. 我遇到了一个让我疯狂的非常简单的问题。 I am trying to calculate the commission of sales and add it to the a salary but when I run the code it always gives me a commission of $0 and a compensation of $0. 我正在尝试计算销售佣金并将其添加到工资中,但是当我运行代码时,它总是给我0美元的佣金和0美元的补偿。 Here is my code I have so far: 这是我到目前为止的代码:

package compensationcalculator;

import java.text.NumberFormat;

public class CompensationCalculator {

    private double salary = 75000;
    private double sales;
    private double commission;
    private double compensation;

    public CompensationCalculator() {

        this.salary = 75000.00;

    }

    public void setSales(double sales) {

        this.sales = sales;

    }

    public double getCommission() {

        this.commission = (sales * 0.2);
        return commission;

    }

    public double getCompensation() {

        this.compensation = (salary + commission);
        return compensation;

    }

    public String toString() {

        NumberFormat fmt = NumberFormat.getCurrencyInstance();

        return ("Annual Salary: " + fmt.format(salary) + "\n"
                        + "Total Sales: " + fmt.format(sales) + "\n"
                        + "Total Commission: " + fmt.format(commission) + "\n"
                        + "Total Annual Compensation: " + fmt.format(compensation));

    }

}

Now when I enter 5000 when prompted for the sales I get this as an answer after it runs: 现在当我在提示销售时输入5000时,我会在运行后得到这个答案:

Annual Salary: $75,000.00
Total Sales: $5,000.00
Total Commission: $0.00
Total Annual Compensation: $0.00

I am sure this is a simple error, but I have not used Java in quite some time. 我确信这是一个简单的错误,但我在相当长的一段时间内没有使用过Java。 I have compared it to some of my really old code from years ago and can't see what I am missing. 我把它与几年前的一些旧代码进行了比较,看不出我错过了什么。 Any guidance would be very welcome! 非常欢迎任何指导!

The compensation value is based on the commission , which is a calculated value. 补偿值基于commissioncommission是计算值。

You either need to pre-calculate all the values when you set the values or you need to use the values from these methods, for example... 您需要在设置值时预先计算所有值,或者需要使用这些方法中的值,例如......

Pre-calculated... 预先计算...

public void setSales(double sales) {

    this.sales = sales;
    commission = sales * 0.2;
    compensation = salary + commission;

}

public double getCommission() {

    return commission;

}

public double getCompensation() {

    return compensation;

}

Dependency calculated... 计算依赖性......

public double getCompensation() {

    this.compensation = (salary + getCommission());
    return compensation;

}

public String toString() {

    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    return ("Annual Salary: " + fmt.format(salary) + "\n"
                    + "Total Sales: " + fmt.format(sales) + "\n"
                    + "Total Commission: " + fmt.format(getCommission()) + "\n"
                    + "Total Annual Compensation: " + fmt.format(getCompensation()));

}

In order for a method to work, you have to actually call the method. 为了使方法起作用,您必须实际调用该方法。

try 尝试

return ("Annual Salary: " + fmt.format(salary) + "\n"
                    + "Total Sales: " + fmt.format(sales) + "\n"
                    + "Total Commission: " + fmt.format(getCommission ()) + "\n"
                    + "Total Annual Compensation: " + fmt.format(getCompensation ()));

Calling the variables themselves does not cause the getters methods to be called. 调用变量本身不会导致调用getters方法。

You need to ensure you call getCommission and getCompensation so that the member variables are set. 您需要确保调用 getCommissiongetCompensation以便设置成员变量。

The simplest way to fix it is to probably set those variables within setSales so that they're always consistent. 解决它的最简单方法是在setSales设置这些变量,使它们始终保持一致。

Or ditch them altogether since they can be calculated on demand. 或者完全放弃它们,因为它们可以按需计算。

Simply have getWhatever simply return a calculated variable and make sure toString calls getWhatever to "print" the value. 只需要getWhatever返回一个计算变量并确保toString调用getWhatever来“打印”该值。

The methods calculating the output do not get called. 计算输出的方法不会被调用。 Change the toString() method like this: 像这样更改toString()方法:

    return "Annual Salary: " + fmt.format(this.salary) + "\n" + "Total Sales: " + fmt.format(this.sales) + "\n"
            + "Total Commission: " + fmt.format(getCommission()) + "\n" + "Total Annual Compensation: "
            + fmt.format(getCompensation());

The issue here is that your constructor fails to explicitly initialize several of the fields and thus they are being initialized to their default values of 0, and you never invoke the method that computes them. 这里的问题是你的构造函数无法显式初始化几个字段,因此它们被初始化为默认值0,你永远不会调用计算它们的方法。 (Design note: you should either initialize these in your constructor, lazily compute them without a member variable, or lazily compute and cache the value, but unless you are caching the value, you really shouldn't be doing an assignment like this in a getter. You probably also shouldn't do a caching approach unless there is a good reason to do so. Keep it simple and just always lazily calculate the result without saving the value in a member or always initialize in the constructor for something like this). (设计说明:你应该在构造函数中初始化这些,在没有成员变量的情况下懒惰地计算它们,或者懒惰地计算并缓存值,但除非你缓存这个值,否则你真的不应该在你可能也不应该做一个缓存方法,除非有充分的理由这样做。保持简单,只是总是懒惰地计算结果而不保存成员中的值或总是在构造函数中初始化类似这样的东西) 。

On the subject, however, I feel that it is important to point out that the double and float types are really intended for scientific computing, not monetary usages. 然而,关于这个问题,我觉得重要的是要指出doublefloat类型真正用于科学计算,而不是货币用途。 Double will round according to binary conventions, whereas with currency, you really want to be very stringent in rounding according to the decimal system. Double将根据二进制约定进行舍入,而对于货币,您确实希望根据十进制系统进行舍入非常严格。 In Java, you can use the BigDecimal class for decimal calculations. 在Java中,您可以使用BigDecimal类进行十进制计算。

Here is your toString, method: 这是你的toString方法:

public String toString() {

    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    return ("Annual Salary: " + fmt.format(salary) + "\n"
                    + "Total Sales: " + fmt.format(sales) + "\n"
                    + "Total Commission: " + fmt.format(getCommission()) + "\n"
                    + "Total Annual Compensation: " + fmt.format(getCompensation()));

}

commission and compesantion variables were not set. 佣金和补偿变量没有设定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM