-1

Here is the part of the code I am having trouble with:

for (i=0; i<=numOfEmployees-1; i++)
     {
        System.out.println("Enter the name of an employee that worked this house:");
        nameOfEmployee = scan.next();
        if(numOfEmployees == 2 && numOfRooms == "6")
           nameOfEmployee.updatePay(30);
        if(numOfEmployees == 2 && numOfRooms == "5")
           nameOfEmployee.updatePay(25);
        if(numOfEmployees == 2 && numOfRooms == "4")
           nameOfEmployee.updatePay(20);
        if(numOfEmployees == 2 && numOfRooms == "3")
           nameOfEmployee.updatePay(15);
        if(numOfEmployees == 3 && numOfRooms == "6")
           nameOfEmployee.updatePay(25);
        if(numOfEmployees == 3 && numOfRooms == "5")
           nameOfEmployee.updatePay(20);
        if(numOfEmployees == 3 && numOfRooms == "4")
           nameOfEmployee.updatePay(15);
        if(numOfEmployees == 3 && numOfRooms == "3")
           nameOfEmployee.updatePay(10);
     }

And here is my method program:

public class Employee
{
String houseNumber, date, numOfRooms, name;
int pay;


public Employee(String name)
{
  this.name = name;
  this.pay = 0;
}

public Employee(String houseNumber, String date, String numOfRooms)
{
  this.houseNumber = houseNumber;
  this.date = date;
  this.numOfRooms = numOfRooms;
}

public void updatePay(int housePay)
{
  pay += housePay;
}

public int getPay()
{
  return pay;
}

public String getName()
{
  return name;
}

public String getHouse()
{
  return "House Number: " + houseNumber + 
     "/nDate Cleaned: " + date + 
     "/nNumber Of Rooms: " + numOfRooms;
}



}

There error is:

Calculation.java:59: error: cannot find symbol
           nameOfEmployee.updatePay(20);
                         ^
symbol:   method updatePay(int)
location: variable nameOfEmployee of type String

I get it for every single one of the updatePay. I'm guessing it's something wrong with my method program but I'm not sure what is wrong with it.

Code where user creates the employees:

  //Prompting user to enter # of employees that worked on Monday
  System.out.println("How many employees worked on Monday?");
  int numOnMon = scan.nextInt();

  Employee[] newEmployeeName = new Employee[numOnMon];

  //Prompting user to enter the employees that worked on Monday
  for (int i=0; i<=numOnMon-1; i++)
  {
     System.out.println("Please enter the name of an employee that worked on Monday:");
     name = scan.next();
     newEmployeeName[i] = new Employee(name);
  }
TGuedes
  • 69
  • 1
  • 6

3 Answers3

1

You are attempting to call updatePay on a String called nameOfEmployee, and updatePay doesn't exist in String. Create an Employee and call updatePay on it instead.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Ok I created the employee's before but I was hoping that since nameOfEmployee would be the same as an employee I already created, it would just do it for me. I'll add that part of the code to the original post. – TGuedes May 28 '14 at 22:05
  • Do you have any ideas on how I can update the pay for an employee? Because there could be more employees on Monday than worked for a specific house. So if 3 employees worked on Monday, but only 2 of them worked on a certain house, then that will pose a problem. I also need to create this for the whole week so how would I go about tracking the employees for the entire week? – TGuedes May 28 '14 at 22:26
1

I believe your method could be greatly simplified,

for (i=0; i<numOfEmployees; i++) {
  System.out.println("Enter the name of an employee that worked this house:");
  nameOfEmployee = scan.next();
  int nr = Integer.parseInt(numOfRooms);    
  if(numOfEmployees == 2) {
    if (nr >= 3 && nr <= 6) nameOfEmployee.updatePay(nr * 5);
  } else if (numOfEmployees == 3) {
    if (nr >= 3 && nr <= 6) nameOfEmployee.updatePay((nr * 5) - 5);
  }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

I noticed you have another mistake..

When you are doing like that it is not good

numOfRooms == "6"

== - Check if the items are the same object, In this case you won't compare the strings but their refernce(pointer)

You need to use equals

Check this out: http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/

Aviad
  • 1,539
  • 1
  • 9
  • 24
  • Thank you for noticing that. I'm sure I would have wondered why I wasn't getting the desired result later. – TGuedes May 28 '14 at 22:09