/home/caleb/ASDV-Java/Semester 2/Assignments/lab5_CalebFontenot/src/main/java/com/calebfontenot/test/Company.java |
package com.calebfontenot.test;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
@author
public class Company {
private ArrayList<Employee> employees = new ArrayList<Employee>();
public boolean addToGroup(Employee e) {
employees.add(e);
return true;
}
public boolean removeFromGroup(Employee e) {
if (employees.contains(e)) {
employees.remove(e);
return true;
} else {
return false;
}
}
public static void sortBySalary (ArrayList<Employee> arr) {
for (int i = 0; i < arr.size() - 1; ++i) {
for (int j = i+1; j < arr.size(); ++j)
if (arr.get(i).computePay() < arr.get(j).computePay()) {
Employee temp = arr.get(i);
arr.set(i, arr.get(j)) ;
arr.set(j, temp);
}
}
}
public static void sortBySalary (Employee arr[]) {
for (int i = 0; i < arr.length - 1; ++i) {
for (int j = i+1; j < arr.length; ++j)
if (arr[i].computePay() < arr[j].computePay()) {
Employee temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
@Override
public String toString()
{
return "Company{" + "employees=" + employees + '}';
}
public static void print(Employee[] arr) {
for(Employee e: arr) {
System.out.println(e);
System.out.println("-----------------------");
}
}
public static void print(Object[] arr) {
for (Object e: arr) {
if (e instanceof Employee)
System.out.println(e + " " + ((Employee) e).computePay());
System.out.println("-----------------------");
}
}
/*
public static void main(String[] args) {
SalaryEmployee se1 = new SalaryEmployee(50000, "john", new Date());
SalaryEmployee se2 = new SalaryEmployee(60000, "mary", new Date());
WageEmployee we1 = new WageEmployee(20, 150, "paul", new Date());
WageEmployee we2 = new WageEmployee(20, 150, "anna", new Date());
ArrayList<Employee> list = new ArrayList<>();
list.add(se1);
list.add(se2);
list.add(we1);
list.add(we2);
System.out.println(list);
Employee[] ar = new Employee[4];
ar[0] = se1;
ar[1] = se2;
ar[2] = we1;
ar[3] = we2;
print(ar);
sortBySalary(ar);
print(ar);
}
*/
public static void main(String[] args)
{
Company c =new Company();
c.addToGroup(new WageEmployee(10, 160, "Mary Poppins", new GregorianCalendar(1990, 12, 18).getTime()));
c.addToGroup(new WageEmployee(12, 160, "John Wayne", new GregorianCalendar(1920, 11, 2).getTime()));
c.addToGroup(new SalaryEmployee(70000, "Marylyn Monroe", new GregorianCalendar(1920, 11, 2).getTime()));
c.addToGroup(new Manager(40000, "Brad Pitt", new GregorianCalendar(1920, 11, 2).getTime()));
System.out.println(c);
System.out.println("------------------------------------");
Company.print(c.employees.toArray());
System.out.println("------------------------------------");
System.out.println("sort by salary");
Company.sortBySalary(c.employees);
Company.print(c.employees.toArray());
}
}