Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.7 kB
3
Indexable
Never
import java.util.ArrayList;
import java.util.List;

abstract class Employee {
    String  firstName;
    String  lastName;
    Employee(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
    abstract double calculatePayroll();

}

class HourlyWorker extends  Employee {
    double wage;
    double hours;
    HourlyWorker(String firstName, String lastName, double wage, double hours){
        super(firstName, lastName);
        this.wage = wage;
        this.hours = hours;
    }
    double calculatePayroll(){
        return wage * hours;
    }
}

class CommissionWorker extends  Employee{
    int quanity;
    double commission;
    double salary;
    CommissionWorker(String firstName, String lastName,int quanity, double commission, double salary){
        super(firstName, lastName);
        this.quanity = quanity;
        this.commission = commission;
        this.salary = salary;
    }
    double calculatePayroll(){
        return  salary + quanity * commission;
    }
}

public class Example {
    static void Payroll(ArrayList<Employee> employees){
        for (Employee employee : employees) {
            System.out.println(employee.firstName +" "+ employee.lastName+ " " + employee.calculatePayroll());
        }
    }

    public static void main(String[] args) throws java.io.IOException {
        ArrayList<Employee> employees = new ArrayList<Employee>();
        HourlyWorker e1 = new HourlyWorker("Hossein", "Khazaei", 10,20);
        CommissionWorker e2 = new CommissionWorker("Pouria", "Jahandideh", 10,30, 40);
        employees.add(e1);
        employees.add(e2);
        Payroll(employees);
    }
}