Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:24:04+00:00 2026-06-07T15:24:04+00:00

I am trying to find out my problem. For some reason when I try

  • 0

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.

Here EmployeeDatabase holds and EmployeeList which is a container object of types of 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

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

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

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);
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:24:06+00:00Added an answer on June 7, 2026 at 3:24 pm

    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)).

    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 ". 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. 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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to find out the visibility of the Excel and do some activities
I am trying to find out a way to set a time tag to
For some fantastic reason I find myself debugging a problem in a Classic ASP
I am currently trying to send binary data out through pexpect. For some reason,
Trying to find out if it's possible to use SQLAlchemy on Heroku. Thanks.
I am trying to find out how to write an SQL statement that will
i m trying to find out how to convert timestamp date and time (
I am trying to find out if an instance of an application (not vb.net)
I'm trying to find out how to change the icon of a JStree node
I'm trying to find out a little bit more about how the preprocessor works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.