简体   繁体   English

Java,似乎找不到与我的继承有关的问题

[英]Java, Cannot seem to find issue with my inheritence

I am trying to find out my problem. 我正试图找出我的问题。

For some reason when I try to set the sales of a sales person it only sets the first SalesPerson founds sales to the last Salesperson sales set. 由于某些原因,当我尝试设置销售人员的销售时,它只会将第一个SalesPerson建立的销售设置为最后一个Salesperson销售。

Here EmployeeDatabase holds and EmployeeList which is a container object of types of Employees. 在这里,EmployeeDatabase拥有EmployeeList,这是Employees类型的容器对象。

I have code for the other classes but I think this is all the is needed... Sorry for the lack of comments in the code 我有其他班级的代码,但我认为这是所有需要的...很抱歉,代码中缺少注释

My output: 我的输出:

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

Code: EmployeeDatabase 代码:EmployeeDatabase

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();
}
}

EmployeeList EmployeeList的

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);
}
}

Employee 雇员

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!"); }
 }

WageEmployee WageEmployee

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);
 }
}

SalesPerson 业务员

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);
 }
}

There are two issues in this piece of code. 在这段代码中有两个问题。

First, there is the extra ! 首先,有额外的! in the find method. 在查找方法中。

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

The second issue is the padding created in your wage employee constructor. 第二个问题是您的工资雇员构造函数中创建的填充。

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

This code makes so that the "John" you are looking for is actually "John " . 此代码,以使"John"你正在寻找的其实是"John " Note the spacing. 注意间距。 (Shoot, markdown eliminated most of it, but you get the idea of extra spacing at the end). (射击,降价消除了其中的大部分,但您最终想到了额外的间距)。 This causes the names to never compare equal, resulting in a null pointer exception as you try to work with a non-existent employee. 这将导致名称永远不会相等,从而导致在尝试使用不存在的员工时出现空指针异常。

There are three possible solutions to this. 有三种可能的解决方案。 First, use the String.trim method on em.name before comparing. 首先,在比较之前对em.name使用String.trim方法。 Second, pad your search name the same way as they are stored before comparing. 第二,在比较之前,以与存储搜索名称相同的方式填充搜索名称。 Third, you might be able to just remove the padding altogether(I don't know your use case, but String.format might allow you to just pad only at output time). 第三,您也许可以完全删除填充(我不知道您的用例,但是String.format可能只允许您在输出时填充)。

You have a bug in your find method: 您的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;
}

This results in the first employee being returned for when setSales is invoked for later employees. 这将导致为以后的雇员调用setSales时返回第一个雇员。

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

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