0% found this document useful (0 votes)
19 views11 pages

Prova Av2

The document defines several classes that represent different types of employees. It creates instances of each employee type and processes their pay individually and polymorphically. It demonstrates polymorphism by calling the earnings method on each employee object.

Uploaded by

Giorgi Lucca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Prova Av2

The document defines several classes that represent different types of employees. It creates instances of each employee type and processes their pay individually and polymorphically. It demonstrates polymorphism by calling the earnings method on each employee object.

Uploaded by

Giorgi Lucca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Prova av2: https://replit.com/@gomesjoaovictor/ProvaAV2#src/main/java/Main.

java

Main:

public class Main {

public static void main(String[] args) {

SalariedEmployee salariedEmployee = new SalariedEmployee("joao", "oliveira", "457-


34-7458", 450.00);

HourlyEmployee hourlyEmployee = new HourlyEmployee("Henrique", "gomes", "562-


32-4725", 11.70, 20.0);

CommissionEmployee commissionEmployee = new


CommissionEmployee("Suzana", "castro", "347-26-8597", 40000.0, 0.09);

BasePlusCommissionEmployee basePlusCommissionEmployee = new


BasePlusCommissionEmployee("Bernardo", "andrade",

"894-46-9874", 6000.0, 0.09, 500.00);

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", 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);

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());

Employee:
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();

Comission employee:

public class CommissionEmployee extends Employee {

private double grossSales;

private double commissionRate;

public CommissionEmployee(String firstName, String lastName, String


socialSecurityNumber, double grossSales, double commissionRate) {

super(firstName, lastName, socialSecurityNumber);

if (commissionRate <= 0.0 || commissionRate >= 1.0)

throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");

if (grossSales < 0.0)

throw new IllegalArgumentException("Gross sales must be >= 0.0");


this.grossSales = grossSales;

this.commissionRate = commissionRate;

public void setGrossSales(double grossSales) {

if (grossSales < 0.0)

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)

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());

Base plus comission employee:

public class BasePlusCommissionEmployee extends CommissionEmployee{

private double baseSalary;

public BasePlusCommissionEmployee(String firstName, String lastName, String


socialSecurityNumber, double grossSales, double commissionRate, double
baseSalary){

super(firstName, lastName, socialSecurityNumber, grossSales, commissionRate);

if(baseSalary < 0.0)


throw new IllegalArgumentException("Base salary must be >= 0.0");

this.baseSalary = baseSalary;

public void setBaseSalary(double baseSalary){

if(baseSalary < 0.0)

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%n%s: $%,.2f", "base-salaried", super.toString(), "base


salary", getBaseSalary());

}
}

Hourly employee:

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)

throw new IllegalArgumentException

("Hourly wage must be >= 0.0");

if((hours < 0.0) || (hours > 168.0))

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)


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

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

this.hours = hours;

public double getHours()

return hours;

@Override

public double earnings()

if(getHours() <= 40)

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());

Salaried employee:

public class SalariedEmployee extends Employee{

public 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());

You might also like