How do i calculate a total for all employees pay?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lizBeth
    New Member
    • Jan 2010
    • 1

    How do i calculate a total for all employees pay?

    Hi all,
    i seem to have gotten stuck on this coursework, i am unsure as to how to implement a method in the main class to calculate the sum of all employees monthly salaries.
    Everything else works on the program it does output the employees details and their monthly salaries. I just need to add them up and output a "Total Payroll: xxxxxx.x " in this format. It sounds simple and it probably is simple, i think i can't see the woods for trees on this at the moment and just get confused, any help will be greatly appreciated.


    Code:
     /*Class Main.java
     *
     */
    import Employees.Employee;
    import Employees.StaffList;
    import Employees.FullTimeEmployees;
    import Employees.PartTimeEmployees;
    import Employees.CommisionedEmployees;
    
    
    
    /**
     *
     * @author 
     */
    public class Main
    {
    
        /**
         *
         * @param args
         */
        public static void main(String[] args)
        {
          Employee[]  staff = new Employee[9];
    
          staff[0] =  new FullTimeEmployees("STUART Charles",1002);
          staff[1] =  new PartTimeEmployees("MARPLE Jane",1005,10.5);
          staff[2] =  new CommisionedEmployees("SEYMOUR Thomas",1006,6500.0);
          staff[3] =  new FullTimeEmployees("AYDELJI Paul",1012);
          staff[4] =  new PartTimeEmployees("BROCK Sarah",1001,30.0);
          staff[5] =  new CommisionedEmployees("ALHAMBRA Alia",1003,3450.0);
          staff[6] =  new PartTimeEmployees("WHARTON Edith",1104,20.0);
          staff[7] =  new FullTimeEmployees("TURNER George",1111);
          staff[8] =  new CommisionedEmployees("BERNERS-LEE Tim",1014,7800.0);
         
          printStaffList( staff);
       
          
        }
    
          static void printStaffList(Employee [] s)
           {
             for ( int i =0; i < 9; i++)
               {
    		   s[i].calculatePay() ;   //  THIS LINES COMPUTES PAY
                   System.out.println( s[i].toString());
               }
           }
    
        
        
        
    
         private Main()
         {
            
         }
     }
    Code:
    /*class Employee
     */
    
    package Employees;
    
    
    /**
     *  @author 
     */
    
    public abstract class Employee
    {
        protected String name;    //Fields-containers that hold a value
        protected double hrsWorked;
        protected int empID;
        protected static int lastEmpID = 1000;
        protected double sales;
        protected double hours;
        protected double pay;
        private double TotalPayD;
    
    
        /**
         *
         */
        public Employee()
        {
         name = "";
         hrsWorked = 0.0;
         empID = ++lastEmpID ;
         sales = 0.0;
         hours = 40.00;
         pay = 0.0;
         TotalPayD = 0.0;
    
        }
       
        public Employee(String nm, double hrs, double p)
       {
         name =nm;
         hours=hrs;
         pay= p;
         empID = ++lastEmpID ;
       }
        
        public abstract void calculatePay();
    
        public double setHoursWorked( double hoursWorked)
        {
          return hoursWorked;
        }
    
    
         public double getHoursWorked( double hoursWorked)
        {
         return hoursWorked;
        }
    
    
    
        @Override
    
        public String toString ()
        {
          return (" \n Name:  " + name +
                  " \n WorksID: " + empID +
                  " \n Sales: " + sales +
                  " \n Hours: " + hours +
                  " \n Pay:  " + pay +"");
        }
    
    }
    Code:
    /*class Employee
     */
    
    package Employees;
    
    
    /**
     *  @author 
     */
    
    *class FullTimeEmployees
     *
     */
    package Employees;
    
    /**
     * @author 
     */
    
    public class FullTimeEmployees extends Employee
    {
    
        double hrsWorked = 40.0;  //  ALL Full-Time staff work for 40 hours
    
        /**
         * 
         * @param nm
         * @param id 
         */
        public FullTimeEmployees(String nm, int id)
        {
            name = nm ;
            hours = hrsWorked ;  // i.e. hours = 40.0 
    	empID = id ;
        }
    
    
    
        @Override
        public void calculatePay()
        {
            pay = hrsWorked * 2000.0 / 40.0 ;
        }
    
    }
    Code:
    /*class PartTimeEmployees
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package Employees;
    
    /**
     *
     * @author 
     */
    public class PartTimeEmployees extends Employee
    {
    
    
        /**
         *
         * @param nm
         * @param id
         * @param hr
         */
        public PartTimeEmployees(String nm, int id, double hr)
        {
           name = nm ;
           empID = id ;
           hours = hr ;
        }
    
        @Override
        public void calculatePay()
        {
           pay = hours * 2000.0 / 40.0 ;
        }
     
    }
    Code:
    
    /*class CommisionedEmployees
     */
    
    package Employees;
    
    /**
     *
     * @author 
     */
    public class CommisionedEmployees extends Employee
    {
        private static double commRate = 10.0 ;
        private static double flatAmount = 1200.0 ;
    
        public CommisionedEmployees(String nm, int id, double s)
        {
           name = nm ;
           empID = id ;
           sales = s ;
        }
    
        @Override
        public void calculatePay()
        {
            pay = flatAmount + sales * (commRate / 100.0) ;
        }
     
    }
    Code:
    /*class StaffList
     *
     */
    
    package Employees;
    
    import java.util.Collections;
    
    
    
    
    public class StaffList 
    {
       
    
        public StaffList(String string)
        {
         
        }
    
       
        public void setStaffList()
         {
    
         }
    
        public void getStaffList()
         {
    
         }
    
       
        public void calculatePay()
        {
         
        }
    
        public void printStaffList()
        {
          collections.sort();
        }
         //Collections.sort();
    
    }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I'm not entirely sure what happens to class level variables that have no scope modifiers (like public or private) in Java.

    If the "pay" variable is public (accessible to the calling code), then for each employee use the pay variable to tally up your sum.

    If the "pay" variable is not accessible to the calling code (is private), then either write a method that exposes the pay variable to the calling code or have the "calculateP ay" method return the pay so that you can use it retrieve the pay for each employee and tally up your sum.

    -Frinny

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      I think it defaults to "protected" . In any event you should have get() and set() unless you plan on deriving other classes from these classes.

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        The default rights allow access from within the same package (like protected does) but do not allow access from subclasses in other packages. It is sometimes known as "package-private". See the table in Sun's Tutorial in the chapter Controlling Access to Members of a Class.

        As others have said provide a method by which the pay for an employee is made available: probably make calculatePay() return a double. Then the printStaffList( ) can use the standard idiom for accumulating a result:

        Code:
        double sum = 0;
        for(/*etc*/)
        {
            // other stuff like printing names etc
            sum += ...; // get the pay and add it
        }
        // at this point sum will be the total pay
        Post again if you get stuck.

        Comment

        Working...