Quote (m0hawk @ Feb 22 2014 08:28pm)
And maybe encapsulate the startsWith call in the Employee class for the sake of proper OOP. Then you would just create a List<Employee> employees = new ArrayList<Employee>() to handle your employees.
Proper OOP you say? You mean like this overly engineered solution:
Code
public class SalesPerson
{
private String _name;
private double _annualSales, _salary, _salesTarget;
public SalesPerson(String name)
{
this(name,0.0,0.0,0.0);
}
public SalesPerson(String name, double annualSales, double salary, double salesTarget)
{
_name = name;
_annualSales = annualSales;
_salary = salary;
_salesTarget = _salesTarget;
}
public String getName()
{
return _name;
}
public double getAnnualSales()
{
return _annualSales;
}
public double getSalary()
{
return _salary;
}
public double getSalesTarget()
{
return _salesTarget;
}
public String toString()
{
return getName();
}
public boolean equals(Object other)
{
boolean same;
SalesPerson that;
same = other instanceof SalesPerson;
if(same)
{
that = (SalesPerson)other;
same = this._name.equals(that._name);
}
return same;
}
}
Code
public class CommissionCalculator
{
private final double ACCELERATION = 1.5;
private final double INCENTIVE = 0.80;
private final double COMMISSION = 0.12;
private double _annualSales, _salary, _salesTarget;
public CommissionCalculator(double annualSales, double salary, double salesTarget)
{
_annualSales = annualSales;
_salary = salary;
_salesTarget = salesTarget;
}
public boolean isIncentiveMet()
{
return isIncentiveMet(_annualSales);
}
public boolean isIncentiveMet(double annualSales)
{
return annualSales >= (_salesTarget * INCENTIVE);
}
public boolean isSalesTargetExceeded()
{
return isSalesTargetExceeded(_annualSales);
}
public boolean isSalesTargetExceeded(double annualSales)
{
return annualSales > _salesTarget;
}
public double calculateCommission()
{
return calculateCommission(_annualSales);
}
public double calculateCommission(double annualSales)
{
double total = 0.0;
if(isIncentiveMet(annualSales))
{
total = annualSales * COMMISSION;
if(isSalesTargetExceeded(annualSales))
{
total *= ACCELERATION;
}
}
return total;
}
public double calculateTotalAnnualCompensation()
{
return calculateTotalAnnualCompensation(_annualSales);
}
public double calculateTotalAnnualCompensation(double annualSales)
{
return _salary + calculateCommission(annualSales);
}
public double calculateNeededSalesForCompensation(double comp)
{
double need;
comp -= _salary;
need = 0.0;
//if salary alone doesn't meet this requirement then sales need to be adjusted
if(comp > 0)
{
//find the minimum sales needed to meet this requirement with acceleration
if(comp-calculateCommission(_salesTarget) > 0)
{
need = comp / (COMMISSION * ACCELERATION);
}
//find the minimum sales needed to meet this requirement with just commission
else if(comp-calculateCommission(_salesTarget) < 0)
{
need = comp / COMMISSION;
}
//otherwise the minimum sales needed to meet this requirement is the sales target
else
{
need = _salesTarget;
}
}
return need;
}
}
Code
import java.util.ArrayList;
public class Store
{
private ArrayList<SalesPerson> _employees;
public Store()
{
_employees = new ArrayList<SalesPerson>();
}
public void hireEmployee(SalesPerson sp)
{
_employees.add(sp);
}
public void fireEmployee(SalesPerson sp)
{
_employees.remove(sp);
}
public SalesPerson getEmployeeByName(String name)
{
int i;
SalesPerson sp, dummy;
dummy = new SalesPerson(name);
i = _employees.indexOf(dummy);
sp = null;
if(i != -1) sp = _employees.get(i);
return sp;
}
public String consultExpertOnEmployees(SalesPerson him, SalesPerson her)
{
return EfficiencyExpert.compareEmployees(him,her);
}
}
Code
public class EfficiencyExpert
{
public static String compareEmployees(SalesPerson him, SalesPerson her)
{
String report;
CommissionCalculator his = new CommissionCalculator(him.getAnnualSales(),him.getSalary(), him.getSalesTarget());
CommissionCalculator hers = new CommissionCalculator(her.getAnnualSales(),her.getSalary(),her.getSalesTarget());
double hisComp, herComp, neededSales;
hisComp = his.calculateTotalAnnualCompensation();
herComp = hers.calculateTotalAnnualCompensation();
report = "";
if(hisComp == herComp) report = "Both employees made the same amount";
else
{
if(hisComp > herComp)
{
neededSales = hers.calculateNeededSalesForCompensation(hisComp);
report = String.format("%s needs %.2f more sales to match %s",her.toString(),neededSales-her.getAnnualSales(),him.toString());
}
else
{
neededSales = his.calculateNeededSalesForCompensation(herComp);
report = String.format("%s needs %.2f more sales to match %s",him.toString(),neededSales-him.getAnnualSales(),her.toString());
}
}
return report;
}
}
Code
public class StoreTest extends Store
{
public static void main(String[] args)
{
Store store = new Store();
SalesPerson sp;
//alice is hired with a salary of $50,000.00. She makes $50,000.00 in total sales with a target of $60,000.00
sp = new SalesPerson("Alice",50000.0, 50000.0,60000.0);
store.hireEmployee(sp);
//bob is hired with a salary of $50,000.00. He makes $60,000.00 in total sales with a target of $60,000.00
sp = new SalesPerson("Bob",60000.0,50000.0,60000.0);
store.hireEmployee(sp);
//comparing these two, since Bob made more in sales, this should say Alice needs to make $10,000 more in sales
System.out.println(store.consultExpertOnEmployees(store.getEmployeeByName("Alice"),store.getEmployeeByName("Bob")));
//Clay is hired with a salary of $80,000.00. He makes $50,000.00 in total sales with a target of $60,000.00
sp = new SalesPerson("Clay",50000.0,80000.0,60000.0);
store.hireEmployee(sp);
//comparing these two, since Clay has a larger salary, this should say Alice needs to make more in sales
System.out.println(store.consultExpertOnEmployees(store.getEmployeeByName("Alice"),store.getEmployeeByName("Clay")));
//Diane is hired on with a salary of $50,000.00. She makes $120,000.00 in sales with a target of $60,000.00
sp = new SalesPerson("Diane",120000.0,50000.0,60000.0);
store.hireEmployee(sp);
//comparing these two, even though Diane doubled her sales target, because Clay makes more in salary, this will say Diane needs more sales
System.out.println(store.consultExpertOnEmployees(store.getEmployeeByName("Clay"),store.getEmployeeByName("Diane")));
}
}