英文:
How to calculate sales commission in java
问题
我想知道如何进行销售提成的计算,我需要编写这个方法,但我无法弄清楚。首先,销售额与目标之间存在关系,最初销售额低于目标的50%,销售员只能获得1%的提成;销售额与目标之间的值在50%至70%之间,销售员获得2.5%的提成。在 if 的空白处,我不知道应该放什么,因为我不知道如何将销售额设定为不到目标一半的值,甚至是从一个值到另一个值,即在这种情况下是50%至70%,然后在71%至100%之间,提成为3.5%。
我认为销售目标属性需要在方法中,因为程序必须理解销售额少于目标的一半。
public class Salesman {
// 定义属性
private String name;
private double salesGoal;
private double totalSold;
public double calculateCommission() {
double soldValue;
if (totalSold < salesGoal * 0.5) {
soldValue = (totalSold * 1.01);
} else if (totalSold >= salesGoal * 0.5 && totalSold < salesGoal * 0.7) {
soldValue = (totalSold * 1.025);
} else if (totalSold >= salesGoal * 0.7 && totalSold <= salesGoal) {
soldValue = (totalSold * 1.035);
} else {
soldValue = 0;
}
return soldValue;
}
}
英文:
I would like to know how I perform this calculation of the sales commission, I need to do the method and I'm not getting it. There is the value sold in relation to the goal, at first it would be less than 50% the seller earns only 1% and has the value sold in relation to the goal of 50 to 70% and the seller earns 2.5%. Where is the blank space of the if, is where I don’t know what to put, because I don’t know how to put less than half a value or even one value until the other, which in this case is 50 to 70%, e and from 71 to 100% you get 3.5%.
I believe that the Sales Goal attribute needs to be in the method, because the program has to understand that the value sold was less than half of the goal.
public class Salesman {
//defining attributes
private String name;
private double salesGoal;
private double totalSold;
public double calculateCommission() {
double soldValue;
if (totalSold < ) {
soldValue =(totalSold * 1.01);
}else if(totalSold >= ){
soldValue =(totalSold * 1.025);
}else if(totalSold >= ){
soldValue =(totalSold * 1.035);
}else{
soldValue = 0;
}
return soldValue;
}
}
答案1
得分: 2
也许这正是你想要的:
private String name;
private double salesGoal;
private double totalSold;
public double calculateCommission() {
double seventyPercentOfSalesGoal = salesGoal * 0.70;
double fiftyPercentOfSalesGoal = salesGoal * 0.50;
if (totalSold >= seventyPercentOfSalesGoal) {
return totalSold * 0.025;
} else if (totalSold >= fiftyPercentOfSalesGoal) {
return totalSold * 0.01;
}
return 0;
}
在你检查较低阈值之前,你需要检查是否通过了较高阈值。我还删除了变量,因为一旦你知道需要计算什么,就可以直接返回一个值。
英文:
Perhaps this is what you're after:
private String name;
private double salesGoal;
private double totalSold;
public double calculateCommission() {
double seventyPercentOfSalesGoal = salesGoal * 0.70;
double fiftyPercentOfSalesGoal = salesGoal * 0.50;
if (totalSold >= seventyPercentOfSalesGoal) {
return totalSold * 0.025;
} else if (totalSold >= fiftyPercentOfSalesGoal) {
return totalSold * 0.01;
}
return 0;
}
you need to check whether the higher threshold is passed before you check the lower one. I've also removed the variable as you can just return a value the moment you know what you need to calculate.
答案2
得分: 2
它有助于制作一些图表
输入和输出决定了你的方法签名。
public double calculateCommission(double soldValue, double goal) {
return 0d;
}
然后创建某种流程图
public double calculateCommissionRate(double soldValue, double goal) {
double ratio = soldValue / goal;
if (ratio < 0.5) return 0.01;
if (ratio < 0.7) return 0.025;
return 0.035;
}
尝试将你的目标或销售目标想象成一个蛋糕。你需要知道的是,已经卖掉了蛋糕的多少份(ratio = sold/cake
)。这是要计算的第一件事。
实际上,百分比也是一种比率。“pro-cent” 意味着 x/100。因此,50% 实际上是 50/100,也就是 0.5。
因此,我们可以直接将这两个比率进行比较。
我刚意识到你可能也想要应用你的佣金率。
在这种情况下,你必须将其与 soldValue
相乘。
public double calculateCommission(double soldValue, double goal) {
double commissionRate = calculateCommissionRate(soldValue, goal);
return soldValue * commissionRate;
}
最后的备注(超出范围):double
适合存储比率,但对于货币值(即你方法的输入)来说可能不太好,因为会涉及各种舍入误差。你可能想要使用 BigDecimal
,或者作为替代,也可以使用 Integer
并将你的数字乘以 100,以存储小数部分。然后,将值显示给最终用户只是使用适当的格式化工具的问题。
英文:
It helps to make some diagrams
What goes in, and what goes out determines your method signature.
public double calculateCommission(double soldValue, double goal) {
return 0d;
}
And then create some kind of flow diagram
public double calculateCommissionRate(double soldValue, double goal) {
double ratio = soldValue / goal;
if (ratio < 0.5) return 0.01;
if (ratio < 0.7) return 0.025;
return 0.035;
}
Try to think of your goal or sales target as a cake. What you need to know, is how much of the cake has been sold (ratio = sold/cake
). That's the first thing to calculate.
And a percentage is actually a ratio as well. "pro-cent" means x/100. so 50% is actually 50/100 , which is actually 0.5.
So, we can just compare those 2 ratios directly with each other.
I just realized that you may want to apply your commission-rate as well.
In that case, you have to multiply it with the soldValue
.
public double calculateCommission(double soldValue, double goal) {
double commissionRate = calculateCommissionRate(soldValue, goal);
return soldValue * commissionRate;
}
Final remark (out of scope): double
is fine to store a ratio, but aren't that good for monetary values (i.e. the inputs of your method), due to all kind of rounding errors. You may want to use BigDecimal
or alternatively, perhaps Integer
and multiply your numbers by 100 to also store the decimals. Showing your values to the end user is then just a matter of using a decent formatter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论