NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

1.
public abstract class Employee {

private final String firstName;
private final String lastName;
private final String socialSecurityNumber;

public Employee(String firstName, String lastName, String socialSecurityNumber)
{
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;

}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;

}
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
}
@Override
public String toString()
{
return String.format("%s %s%nsocial security number : %s",
getFirstName(),getLastName(),getSocialSecurityNumber());
}
public abstract double earnings();
}
2.
public class SalariedEmployee extends Employee {

private double weeklySalary;

public SalariedEmployee(String firstName, String lastName, String socialSecurityNumber, double weeklySalary) {
super(firstName, lastName, socialSecurityNumber);
if (weeklySalary < 0.0) {
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0");
}

this.weeklySalary = weeklySalary;
}

public void setWeeklySalary(double weeklySalary) {
if (weeklySalary < 0.0) {
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0");
}

this.weeklySalary = weeklySalary;
}

public double getWeeklySalary() {
return weeklySalary;
}

@Override
public double earnings() {
return getWeeklySalary();
}

@Override
public String toString() {
return String.format("salaried employee: %s%n%s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary());
}

}
3.
public class HourlyEmployee extends Employee {

private double wage;
private double hours;

public HourlyEmployee(String firstName, String lastName,
String socialSecurityNumber, double wage, double hours) {
super(firstName, lastName, socialSecurityNumber);
if (wage < 0.0) // validate wage
{
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0");
}

if ((hours < 0.0) || (hours > 168.0)) // validate hours
{
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0");
}

this.wage = wage;
this.hours = hours;
}

public void setWage(double wage) {
if (wage < 0.0) // validate wage
{
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0");
}

this.wage = wage;
}

public double getWage() {
return wage;
}

public void setHours(double hours) {
if ((hours < 0.0) || (hours > 168.0)) // validate hours

throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0");

this.hours = hours;
}

// return hours worked
public double getHours() {
return hours;
}

@Override
public double earnings() {
if (getHours() <= 40) // no overtime
{
return getWage() * getHours();
} else {
return 40 * getWage() + (getHours() - 40) * getWage() * 1.5;
}
}

@Override
public String toString() {
return String.format("hourly employee: %s%n%s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage", getWage(),
"hours worked", getHours());
}
}
4.
public class CommissionEmployee extends Employee {

private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage

public CommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate) {
super(firstName, lastName, socialSecurityNumber);

if (commissionRate <= 0.0 || commissionRate >= 1.0) // validate
{
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
}

if (grossSales < 0.0) // validate
{
throw new IllegalArgumentException("Gross sales must be >= 0.0");
}
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}

public void setGrossSales(double grossSales) {
if (grossSales < 0.0) // validate
{
throw new IllegalArgumentException("Gross sales must be >= 0.0");
}

this.grossSales = grossSales;
}

public double getGrossSales() {
return grossSales;
}

public void setCommissionRate(double commissionRate) {
if (commissionRate <= 0.0 || commissionRate >= 1.0) // validate
{
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
}

this.commissionRate = commissionRate;
}

public double getCommissionRate() {
return commissionRate;
}

@Override
public double earnings() {
return getCommissionRate() * getGrossSales();
}

@Override
public String toString() {
return String.format("%s: %s%n%s: $%,.2f; %s: %.2f",
"commission employee", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate());
}
}
5.
public class BasePlusCommissionEmployee extends CommissionEmployee {

private double baseSalary; // base salary per week

public BasePlusCommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate, double baseSalary) {
super(firstName, lastName, socialSecurityNumber,
grossSales, commissionRate);

if (baseSalary < 0.0) // validate baseSalary
{
throw new IllegalArgumentException("Base salary must be >= 0.0");
}

this.baseSalary = baseSalary;
}

public void setBaseSalary(double baseSalary) {
if (baseSalary < 0.0) // validate baseSalary
{
throw new IllegalArgumentException("Base salary must be >= 0.0");
}
this.baseSalary = baseSalary;
}

public double getBaseSalary() {
return baseSalary;
}

@Override
public double earnings() {
return getBaseSalary() + super.earnings();
}

@Override
public String toString() {
return String.format("%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary());

}
}

6.
public class PayrollSystemTest {

public static void main(String[] args) {
SalariedEmployee salariedEmployee
= new SalariedEmployee("John", "Smith", "111-11-1111", 800.00);
HourlyEmployee hourlyEmployee
= new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75, 40);
CommissionEmployee commissionEmployee
= new CommissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06);
BasePlusCommissionEmployee basePlusCommissionEmployee
= new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300);
System.out.println("Employees processed individually:");

System.out.printf("%n%s%n%s: $%,.2f%n%n",
salariedEmployee, "earned", salariedEmployee.earnings());
System.out.printf("%s%n%s: $%,.2f%n%n",
hourlyEmployee, "earned", hourlyEmployee.earnings());
System.out.printf("%s%n%s: $%,.2f%n%n",
commissionEmployee, "earned", commissionEmployee.earnings());
System.out.printf("%s%n%s: $%,.2f%n%n",
basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings());
Employee[] employees = new Employee[4];

employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
employees[2] = commissionEmployee;
employees[3] = basePlusCommissionEmployee;
System.out.printf("Employees processed polymorphically:%n%n");

for (Employee currentEmployee : employees) {
System.out.println(currentEmployee); // invokes toString

if (currentEmployee instanceof BasePlusCommissionEmployee){

BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee) currentEmployee ;

employee.setBaseSalary(1.10 * employee.getBaseSalary());
System.out.printf(
"new base salary with 10%% increase is: $%,.2f%n",
employee.getBaseSalary());
}

System.out.printf(
"earned $%,.2f%n%n", currentEmployee.earnings());
}
for (int j = 0; j < employees.length; j++)
System.out.printf("Employee %d is a %s%n", j,
employees[j].getClass().getName());
}


}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.