Suppose there is XYZ Company and there are different departments like production, marketing, finance, sales etc. Manager of the company want to know about the detail of the employees who are highly paid in each of the department. Write a program using the concept of classes to implement the same.
import java.util.ArrayList;
class Employee {
private String name;
private double salary;
public Employee() {
}
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the salary
*/
public double getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(double salary) {
this.salary = salary;
}
}
class Department {
private ArrayList<Employee> employees = new ArrayList<Employee>();
public void addEmployee(Employee newEmployee) {
this.employees.add(newEmployee);
}
public Employee getEmployeeHighlyPaid() {
Employee employeeHighlyPaid = null;
if (employees.size() > 0) {
employeeHighlyPaid = employees.get(0);
for (Employee employee : employees) {
if (employeeHighlyPaid.getSalary() < employee.getSalary()) {
employeeHighlyPaid = employee;
}
}
}
return employeeHighlyPaid;
}
}
class ProductionDepartment extends Department {
public void displayEmployeeHighlyPaid() {
Employee employeeHighlyPaid = getEmployeeHighlyPaid();
System.out.println("The employee " + employeeHighlyPaid.getName()
+ " is highly paid in the production department with salary " + employeeHighlyPaid.getSalary());
}
}
class MarketingDepartment extends Department {
public void displayEmployeeHighlyPaid() {
Employee employeeHighlyPaid = getEmployeeHighlyPaid();
System.out.println("The employee " + employeeHighlyPaid.getName()
+ " is highly paid in the marketing department with salary " + employeeHighlyPaid.getSalary());
}
}
class FinanceDepartment extends Department {
public void displayEmployeeHighlyPaid() {
Employee employeeHighlyPaid = getEmployeeHighlyPaid();
System.out.println("The employee " + employeeHighlyPaid.getName()
+ " is highly paid in the finance department with salary " + employeeHighlyPaid.getSalary());
}
}
class SalesDepartment extends Department {
public void displayEmployeeHighlyPaid() {
Employee employeeHighlyPaid = getEmployeeHighlyPaid();
System.out.println("The employee " + employeeHighlyPaid.getName()
+ " is highly paid in the sales department with salary " + employeeHighlyPaid.getSalary());
}
}
class App {
public static void main(String[] args) {
ProductionDepartment productionDepartment = new ProductionDepartment();
productionDepartment.addEmployee(new Employee("Peter", 4500));
productionDepartment.addEmployee(new Employee("Mary", 6500));
productionDepartment.addEmployee(new Employee("Julia", 5500));
productionDepartment.displayEmployeeHighlyPaid();
MarketingDepartment marketingDepartment = new MarketingDepartment();
marketingDepartment.addEmployee(new Employee("John", 5500));
marketingDepartment.addEmployee(new Employee("Mike", 2500));
marketingDepartment.addEmployee(new Employee("Julia", 2500));
marketingDepartment.displayEmployeeHighlyPaid();
FinanceDepartment financeDepartment = new FinanceDepartment();
financeDepartment.addEmployee(new Employee("John", 1500));
financeDepartment.addEmployee(new Employee("Mike", 2500));
financeDepartment.addEmployee(new Employee("Peter", 3500));
financeDepartment.displayEmployeeHighlyPaid();
SalesDepartment salesDepartment = new SalesDepartment();
salesDepartment.addEmployee(new Employee("John", 6500));
salesDepartment.addEmployee(new Employee("Mike", 2500));
salesDepartment.addEmployee(new Employee("Peter", 3500));
salesDepartment.displayEmployeeHighlyPaid();
}
}
Comments
Leave a comment