简体   繁体   中英

How do I create an boolean equals method compares objects in an array

My programming assignment tasked me with writing an increase/decreasePay abstract method that must be put in my abstract employee class. I can't seem to get the the method correct in HourlyWorker so that it will take increase or decrease the pay by a "percentage". My math is sound (monthly pay - or + (monthly pay * the percentage), but my output in my test class is coming out the same after increasing/decreasing pay. Any help?

Employee class:

abstract public class Employee
{

private String lastName;
private String firstName;
private String ID;


public abstract void increasePay(double percentage);
public abstract void decreasePay(double percentage);
public abstract double getMonthlyPay();

public Employee(String last, String first, String ID)
   {
   lastName = last;
   firstName = first;
   this.ID = ID;
   } 

public void setLast(String last)
   {
      lastName = last;
   }
   
public void setFirst(String first)
   {
      firstName = first;
   }
   
public void setIdNumber(String ID)
   {
      this.ID = ID;
   }

      
public String getLastName()
{
   return lastName;
}

public String getFirstName()
{
   return firstName;
}

public String getName()
{
   return firstName + lastName;
}

public String getIdNumber()
{
   return ID;
}
}

HourlyWorkerClass

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

public void setHours(int hours)
{
   this.hours = hours;
}

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   if ( hours > 160 )
       this.hourlyRate = hourlyRate * 1.5;
    else 
       this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public void setMonthlyPay(double monthlyPay)
{
   monthlyPay = hourlyRate * hours;
}

public double getMonthlyPay()
{
   return hourlyRate * hours;
}

public void increasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public void decreasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public String toString() 
   {
        String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
                         + getIdNumber() + " \nHourly Rate: " + hourlyRate;
                        return result;
   }

}

Testing class (currently testing increase

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      Supervisor sup = new Supervisor("Boss", "Jim", "JB7865", 54000);
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 11.95);

      hw1.setHours(200);
            
      staff[0] = sup;
      staff[1] = hw1;        
      
   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(5);
   System.out.println(staff[0].getMonthlyPay());

   System.out.println(staff[1].getMonthlyPay());
   staff[1].increasePay(10);
   System.out.println(staff[1].getMonthlyPay());
   
}
}
Supervisor class:

public class Supervisor extends Employee
{
private double annualSalary;
private double monthlyPay;

public Supervisor(String last, String first, String ID, double salary)
{
   super(last, first, ID);
   annualSalary = salary;
}

public void setAnnualSalary(double salary)
{
   annualSalary = salary;
}

public double getAnnualSalary()
{
   return annualSalary;
}

public double getMonthlyPay()
{
   return ((annualSalary + (annualSalary * .02)) / 12);
}

public void increasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public void decreasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public String toString() 
   {
        String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
                         + getIdNumber() + "\nAnnual Salary: " + annualSalary;
         return result;
   }
}

Output is:

4590.0 4590.0 2390.0 2390.0

Doesn't appear to be modifying getMonthlyPay()

Should be:

4590.00 4819.50 2390.00 2629.00

When you do list.indexOf(temp) , what that does, right now, is look for a Stock that is equals to the argument passed to it -- so it looks for a Stock with price zero, not caring about the symbol at all. That's what the code does right now.

Honestly, using indexOf and equals is not really appropriate for this problem. indexOf is really only useful when you have something that's totally equal to the target you're looking for.

The best way to do something like this is

Optional<Stock> foundStock = list.stream().filter(stock -> stock.getName().equals(symbol)).findAny();
if (foundStock.isPresent()) {
  // do something with foundStock.get()
} else {
  // no found stock
}

indexOf() is a method return the index of the first occurrence of the specified element in the returned list. If the list does not contain this element, value -1 is returned. More formally, return the lowest index i that meets the following conditions:

if(o==null? get(i)==null :o.equals(get(i))){
    return i;
}
return -1;

If there is no such index, return -1.

And you have override the equals method, I guess you just want to focus on the same price Stock?:

@Override
public boolean equals(Object obj){
    if (obj instanceof Stock){   
        Stock other = (Stock) obj;
        return getPrice() == other.getPrice();
   }
   return false;
}  

As my opinion, you have use List<Stock> list so the Object in the list is all Stock . Maybe it could be simplifed:

@Override
public boolean equals(Object obj){
    Stock other = (Stock) obj;
    return getPrice() == other.getPrice();
}

Generally, when implementing equals() , you compare “key” fields whose values don't change for the entity, and don't compare “state” fields whose values change from time to time.

You are comparing sharePrice , when I believe you should be comparing symbol .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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