Reputation:
0
I have an assignment which asks for everything I have in the code below. That all works fine - I just need to calculate any monthly hours over 160 hours to be paid at 1.5 times the normal hourly rate. My math seems sound and calculates fine:
((hours - 160) * overtime) + (160 * hourlyRate)
But I dont know if I'm putting this if statement in the right method or if it even should be an if statement. My increase/decreasePay methods are working prior to this and they need to stay. I removed some things so it's easier to read.
HourlyWorker Class:
public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;
private double overtime = (1.5 * hourlyRate);
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)
{
this.hourlyRate = rate;
}
public double getHourlyRate()
{
return hourlyRate;
}
public double getMonthlyPay()
{
if (hours > 160)
{
monthlyPay = ((hours - 160) * overtime) + (160 * hourlyRate);
}
else
{
monthlyPay = hourlyRate * hours;
}
return monthlyPay;
}
public void increasePay(double percentage)
{
hourlyRate *= 1 + percentage / 100;
}
public void decreasePay(double percentage)
{
hourlyRate *= 1 - percentage / 100;
}
}
What I'm testing with:
public class TestEmployee2
{
public static void main(String[] args)
{
Employee [] staff = new Employee[3];
HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 10);
hw1.setHours(200);
staff[0] = hw1;
System.out.println(staff[0].getMonthlyPay());
staff[0].increasePay(10);
System.out.println(staff[0].getMonthlyPay());
}
}
Output is: 1600 (initial monthly rate, with 40 overtime hours and 160 regular hours) 1760 (10% increase to the monthlyPay)
Should be: 2006 2206.6
Upvotes: 0
Views: 965
Reputation: 43078
Assuming you have added the following artists to your list:
new Artist("Rolling Stones", "Rock");
new Artist("Abba", "Rock");
Now you want your addArtists
method to actually update an artist's genre if they were added again, you can do it like this:
(I'm also assuming this.artists
is a Collection
.)
public void addArtist(String artistName, String genre) {
this.artists.removeIf((artist) -> artist.name.equals(artistName)));
this.artists.add(new Artist(artistName, genre));
}
The method first removes (using removeIf
) any artist with that name from the list, then it adds the new artist.
Now when you do:
artists.addArtist("Rolling Stones", "Rock");
artists.addArtist("Abba", "Pop");
The previous artists you had added will be replaced by these
Upvotes: 0
Reputation: 18245
You're almost there. Fort set()
method, you have to add position as first argument public E set(int index, E element)
:
this.artists.set(0, new Artist(artist, genre)); // this should work
Upvotes: 1
Reputation: 44824
To update an Object in an ArrayList use the set
method
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#set-int-E-
This takes two arguments, the index
and the new Object.
To get the index
see https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#indexOf-java.lang.Object-
Upvotes: 0