How to items from a different class to an ArrayList?

Discussion in 'Java' started by anzo31, Nov 14, 2010.

  1. anzo31

    anzo31 New Member

    Joined:
    Nov 14, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I want to Create an Employees class. It will contain 1 arraylist which holds Payable objects (Manager, HourlyWorker, all implement Payable). It will have a method to addEmployee, calculateWeeklyTotal, retrieve number of managers, hourly workers. and the toString method will print out the employee number and weekly pay for all employees. It will have a generateWeeklyPayReport method that returns a string with the entire report.
    Also the class Manager contain variable to hold paycode1, the salary and the ID
    and the class HourlyWorker contain variable to hold paycode2, hourly salary, and hours worked (for this class, employeees recieve fixed hourly wage for up to the first 40 hours they work and "time-and-a-half," i.e., 1.5 times their hourly wage, for overtime hours worked),
    payable interface has the methode public double calculateWeeklyPay();
     
  2. anzo31

    anzo31 New Member

    Joined:
    Nov 14, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    public class Employees
    {
    private payable Manager;
    
    private payable HourlyWorker;
    
    
    private ArrayList<String>EmployeesBorad;
    
    private int paycode;
    
    public Employees() 
    {	
    	EmployeesBorad= new ArrayList<String>();
    	paycode = 0;
    	
    	Manager = null;
    	HourlyWorker = null;
    	
    }
    public void addEmployees (String Manager, String HourlyWorker)
    {
    	if paycode = 1;
    		
    	EmployeesBorad.add(Manager);
    
                if paycode = 2;
    
    	EmployeesBorad.add(HourlyWorker);
    	}
    
    ......
    }
     
    Last edited by a moderator: Nov 14, 2010
  3. ihatec

    ihatec New Member

    Joined:
    Sep 1, 2010
    Messages:
    20
    Likes Received:
    3
    Trophy Points:
    0
    If I understood your problem correctly, I would recommend slightly different solution. I don't know what is your level, but I assume you've heard about polymorphism.
    I haven't read the full code, but I will do it in such a way:
    1. Employee class would superclass(can be abstract)
    2. Other classes will be subclasses of Employee class
    3. Use polymorphism for example like that:
    Code:
    ArrayList<Employee> list = new ArrayList<Employee>();
    list.add(new Manager());
    
    Your description looks strange for me. Because you want to have objects of Payable, but as I understood it is an interface.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice