0

我试图找出我的问题。

出于某种原因,当我尝试设置销售人员的销售额时,它只将第一个 SalesPerson 创建的销售额设置为最后一个 Salesperson 销售集。

这里 EmployeeDatabase 包含和 EmployeeList ,它是员工类型的容器对象。

我有其他类的代码,但我认为这就是所有需要的......抱歉代码中缺少注释

我的输出:

John 12000.0
Set sales to: 12000.0
Joan 10000.0
Set sales to: 10000.0
Jack 5000.0
Set sales to: 5000.0
Name: John           Commision: 0.03 Sales: 5000.0
Name: Joan           Commision: 0.04 Sales: 0.0
Name: Jack           Commision: 0.02 Sales: 0.0

Payroll: 150.0

代码:员工数据库

public class EmployeeDatabase 
{

public static void main(String[] args) 
{
    EmployeeList emp = new EmployeeList();
    /*emp.enqueue(new SalesManager("Gee", 1000));
    emp.enqueue(new SalesManager("Gal", 1000));
    emp.enqueue(new SalesManager("Gem", 1000));*/
    emp.enqueue(new SalesPerson("John", 0.03));
    emp.enqueue(new SalesPerson("Joan", 0.04));
    emp.enqueue(new SalesPerson("Jack", 0.02));
    /*emp.enqueue(new Manager("Fred", 10000));
    emp.enqueue(new Manager("Frank", 5000));
    emp.enqueue(new Manager("Florence", 3000));
    emp.enqueue(new Programmer("Linda", 7));
    emp.enqueue(new Programmer("Larry", 5));
    emp.enqueue(new Programmer("Lewis", 3));*/

    /*emp.setHours("Linda", 35);
    emp.setHours("Larry", 23);
    emp.setHours("Lewis", 3);*/
    emp.setSales("John", 12000);
    emp.setSales("Joan", 10000);
    emp.setSales("Jack", 5000);
    /*emp.setSales("Gee", 4000);
    emp.setSales("Gal", 3000);
    emp.setSales("Gem", 2000);
    emp.setSalary("Gee", 1000);
    emp.setSalary("Gal", 2000);
    emp.setSalary("Gem", 3000);*/
    emp.display();
}
}

员工名单

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;


public class EmployeeList 
{
Queue<Employee> empList = new LinkedList<Employee>();

Employee find(String nm)
{
    Iterator<Employee> it = empList.iterator();
    while(it.hasNext())
    {
        Employee em = (Employee)it.next();
        if(!em.name.equals(nm))
        {
            return em;
        }   
    }
    return null;
}

double payroll()
{
    double payroll = 0.0;
    Iterator<Employee> it = empList.iterator();
    while(it.hasNext())
    {
        Employee em = (Employee)it.next();
        payroll += em.computePay(); 
    }
    return payroll;
}

void display()
{
    Iterator<Employee> it = empList.iterator();
    while(it.hasNext())
    {
        Employee em = (Employee)it.next();
        em.display();   
    }
    System.out.println("\nPayroll: " + payroll());
}

void enqueue(Employee e)
{
    empList.add(e);
}

void setHours(String nm, int hrs)
{
    Employee em = find(nm);
    /*if(em == null)
        return;*/
    em.setHours(hrs);
}

void setSalary(String nm, float salary)
{
    Employee em = find(nm);
    /*if(em == null)
        return;*/
    em.setSalary(salary);
}

void setSales(String nm, double sales)
{
    System.out.println(nm + " " + sales);
    Employee em = find(nm);
    /*if(em == null)
        return;*/
    em.setSales(sales);
}
}

员工

abstract class Employee 
{
String name;

Employee() {}
Employee (String nm) { name = nm; }
abstract double computePay();
void display () {}
void setHours(double hrs) {}
void setSales(double sales) {}
void setSalary(double salary) { System.out.println("NO!"); }
 }

工资雇员

public class WageEmployee extends Employee 
{
double rate;
double hours;

WageEmployee(String name)
{
    this.name = name;
    if(this.name.length() < 14)
    {
        while(this.name.length() < 14)
        {
            this.name += " ";
        }
    }
}
WageEmployee(String name, double rate)
{
    this.name = name;
    if(this.name.length() < 14)
    {
        while(this.name.length() < 14)
        {
            this.name += " ";
        }
    }
    this.rate = rate;
}

double computePay()
{
    return rate * hours;
}

void setHours(double hrs) 
{
    hours = hrs;
    System.out.println("Set Hours to: " + hours);
}

 void display () 
 {
     System.out.println("Name: " + name + " Hours: " + hours + " Rate: " + rate);
 }
}

销售员

public class SalesPerson extends WageEmployee 
{
double comission;
double salesMade;

SalesPerson(String name, double commision) 
{
    super(name);
    this.comission = commision;
}

double computePay()
{
    return comission * salesMade;
}

void setSales(double sales) 
{
    salesMade = sales;
    System.out.println("Set sales to: " + salesMade);
}

void display () 
 {
     System.out.println("Name: " + name + " Commision: " + comission + " Sales: " + salesMade);
 }
}
4

2 回答 2

3

这段代码有两个问题。

首先,有额外的!在查找方法中。

if(!em.name.equals(nm))应该是if(em.name.equals(nm))

第二个问题是在您的工资员工构造函数中创建的填充。

if(this.name.length() < 14)
{
    while(this.name.length() < 14)
    {
        this.name += " ";
    }
}`

此代码使"John"您正在寻找的实际上是"John ". 注意间距。(拍摄,markdown 消除了大部分内容,但你会在最后得到额外间距的想法)。这会导致名称永远不会比较相等,从而在您尝试与不存在的员工一起工作时导致空指针异常。

对此有三种可能的解决方案。em.name首先,在比较之前使用 String.trim 方法。其次,在比较之前以与存储搜索名称相同的方式填充您的搜索名称。第三,您也许可以完全删除填充(我不知道您的用例,但 String.format 可能只允许您仅在输出时填充)。

于 2012-07-16T04:05:46.650 回答
1

您的find方法中有一个错误:

Employee find(String nm)
{
    Iterator<Employee> it = empList.iterator();
    while(it.hasNext())
    {
        Employee em = (Employee)it.next();
        if(!em.name.equals(nm)) // should be if(em.name.equals(nm))
        {
            return em;
        }   
    }
    return null;
}

这导致第一个员工在setSales为后来的员工调用时返回。

于 2012-07-16T03:57:07.190 回答