我已经完成了两个课程,这些课程从基础"员工"类。我在下面展示了Employee和Salesman类。我需要配置Salesman类来显示年薪总额。员工类打印没问题,但是,我无法让Salesman类打印年薪。我确定它很简单,但我被卡住了。任何帮助表示赞赏!
以下是我目前获得的输出:
2014年员工:
员工姓名:Smuck,Jenna,月工资= $ 3,000.00,年薪= $ 36,000.00
推销员姓名:Joans,Barbara,月薪= $ 3,200.00,年销售额= $ 100,000.00
推销员姓名:McMains,Ashley,月薪= $ 2,900.00,年销售额= $ 400,000.00
执行名称:Seibert,Greg,月工资= $ 5,000.00,股价= $ 49.00
2014年所有员工的平均薪资= 25,891.00美元
2015年员工:
员工姓名:Kallinen,Brion,月薪= $ 3,100.00,年薪= $ 37,200.00
推销员姓名:Mentzer,Dickey,月薪= $ 4,100.00,年销售额= $ 75,000.00
推销员姓名:Thomas,Carol,月工资= $ 3,800.00,年销售额= $ 90,000.00
执行姓名:Tofte,Abby,月工资= $ 4,800.00,股价= $ 190.00
执行名称:Kuehn,Jennifer,月工资= $ 6,000.00,股价= $ 79.00
执行姓名:Brown,Eric,月薪= $ 6,500.00,股价= $ 51.00
2015年所有员工的平均薪资= $ 57,432.67
建立成功(总时间:0秒)
我需要打印出销售员和高管的年薪。
package AverageSalary;
public class Employee {
private String employeeName;
private double monthlySalary;
// Constructor that initiailizes name and monthly salary
public Employee(String EmployeeName, double mnthSalary) {
employeeName = EmployeeName;
monthlySalary = mnthSalary;
}
//set Employee Name function
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
//get Employee Name Function
public String getEmployeeName() {
return employeeName;
}
//monthlySalary
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
// Method named annualSalary returns the salary for a whole year.
public double annualSalary() {
return getMonthlySalary() * 12;
}
// toString method returns string containing name and monthly salary
public String toString() {
return String.format("\nEmployee Name: %s, Monthly Salary=$%,.2f,"
+ "Annual Salary=$%,.2f", getEmployeeName(), getMonthlySalary(),
annualSalary());
}
}
________________________________________________________________________
package AverageSalary;
import java.util.HashSet;
public class Salesman extends Employee{
private double annualSales;
public double getAnnualSales() {
return annualSales;
}
// Constructor that initializes name, monthly salary and annual sales
Salesman(String Name, double mnthSalary, double dblAnnualSales)
{
super(Name, mnthSalary);
this.annualSales=dblAnnualSales;
}
//An overridden method annualSalary that returns the salary for year
//Salary of salesman consists of base salary computed from the monthly
salary plus a commission.
//The commission is computed as 2% of that salesman's annual sales.
//The maximum commission a salesman can earn is $20,000.
@Override
public double annualSalary()
{
double dblSalesCommission=0;
dblSalesCommission=(getMonthlySalary()*12)*0.02;
if ( dblSalesCommission > 20000)
dblSalesCommission=20000;
return getMonthlySalary()+dblSalesCommission;
}
@Override
public String toString()
{
return String.format("\nSalesman Name: %s, Monthly
Salary=$%,.2f,Annual
Sales=$%,.2f",
getEmployeeName(), getMonthlySalary(),
getAnnualSales() );
}
}
答案 0 :(得分:1)
我有两个问题,我可以找到。
您的推销员toString()
实际上没有打印出年薪。我改为:
@Override
public String toString() {
return String
.format("\nSalesman Name: %s, Monthly Salary=$%,.2f,Annual Sales=$%,.2f,Annual Salary=$%,.2f",
getEmployeeName(), getMonthlySalary(), getAnnualSales(), annualSalary());
}
此外,您的annualSalary()
计算不正确,您需要将月薪乘以12然后再加上佣金。
@Override
public double annualSalary() {
double dblSalesCommission = 0;
dblSalesCommission = getAnnualSales() * 0.02;
if (dblSalesCommission > 20000)
dblSalesCommission = 20000;
return (12.0 * getMonthlySalary()) + dblSalesCommission;
}
进行这些更改为我产生了正确的输出:
Employee Name: Test Guy, Monthly Salary=$5,000.00,Annual Salary=$60,000.00
Salesman Name: Sales Guy, Monthly Salary=$5,000.00,Annual Sales=$100,000.00,Annual Salary=$61,200.00
答案 1 :(得分:1)
您似乎错误地计算了年度薪水。
不清楚年度销售额是销售员在一年内从销售中赚取的钱,还是一年内销售的销售数量。我假设它的销售数量。
// Salary of salesman consists of base salary computed
// from the monthly salary plus a commission.
// The commission is computed as 2% of that salesman's annual sales.
// The maximum commission a salesman can earn is $20,000.
@Override
public double annualSalary() {
double moneyFromSalary = getMonthlySalary * 12;
double moneyMadePerSale = // Unknown
double dblSalesCommission = moneyMadePerSale * annualSales * 0.02;
if (dblSalesCommission > 20000)
dblSalesCommission = 20000;
return moneyFromSalary + dblSalesCommission;
}
答案 2 :(得分:0)
您的问题是您的String.format参数是乱码。而不是
String.format("\nSalesman Name: %s, Monthly Salary=$%,.2f,Annual Sales=$%,.2f"
尝试
String.format("\nSalesman Name: %s, Monthly Salary=$%.2f, Annual Sales=$%.2f",