d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > New To Java Help > Creating Table In Java With Netbeans
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 22 2014 07:28pm
Well it looks good so far. Now if you want to have annualSalary aswell, you should create an Employee class, with the fields employeeName and annualSalary. 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.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 22 2014 08:27pm
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")));



}
}


Member
Posts: 10
Joined: Feb 17 2014
Gold: 0.00
Feb 23 2014 12:14am
Great code Just a couple of questions if you don't mind me asking you. I have not seen "_employees.add (sp)" all this action is doing is adding employees to salesPerson correct? Also with the public classes I can create those in the same application or should I create each of these class separately from one another? Again I thank you for your help.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 23 2014 12:32am
Id suggest you ask questions regarding my design, and not try to take it as your own. I overly engineered it specifically so you couldnt copy it.

To answer your question, add is a method on the ArrayList class. It adds an object to the collection. It is adding a SalesPerson to the ArrayList of SalesPerson objects.
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 23 2014 01:56am
Omg Minko lol, and here you were the one telling me not to confuse beginners :rofl:

This post was edited by m0hawk on Feb 23 2014 02:08am
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 23 2014 02:52am
Quote (m0hawk @ Feb 23 2014 02:56am)
Omg Minko lol, and here you were the one telling me not to confuse beginners  :rofl:


There is a difference between beginners asking for answers to specific questions, and those asking for answers.
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 23 2014 03:33am
Quote (Minkomonster @ Feb 23 2014 10:52am)
There is a difference between beginners asking for answers to specific questions, and those asking for answers.


I see ur point :)
Member
Posts: 10
Joined: Feb 17 2014
Gold: 0.00
Feb 23 2014 02:06pm
Copying your code does me no good. That defeats the purpose. I am here asking for help to understanding myself.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 23 2014 02:19pm
Quote (Hammertime1979 @ Feb 23 2014 03:06pm)
Copying your code does me no good. That defeats the purpose. I am here asking for help to understanding myself.


Then ask a specific question. What is it specifically that you don't understand about your assignment? What have you tried that doesn't work? Pasting code you have written and saying you are stuck doesn't give us any insight into what you are thinking. What exactly are you stuck on?
Member
Posts: 10
Joined: Feb 17 2014
Gold: 0.00
Feb 24 2014 03:13pm
Ok I will try explaining what I am having problems with in more detail. I know what classes are and sub classes I am jst not sure when to create another class. When I do create another class is doesn't run because it doesn't have a main method. What am I forgetting? I try to compile it but the option is not even available. I am using net beans if that helps any. I know that I am making this a lot harder on myself than need be but I am getting frustrated and I need to turn this in today. I still have plenty of time with 8 hours to go. I am also confused on the for loops and return methods. So when I wrote to make a comparsion do I create a boolean variable first. For example if employee x does not reach 80% of

{while
(double sales < (double salesTarget * 0.80))

salesTarget System.out.println

("You did not make enough sales for a commission");
}

else

I don't have any more to include as I erased what I had and started over. I hope this helps so you can explain it better. Thanks again.
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll