473,500 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do i calculate a total for all employees pay?

1 New Member
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.


Expand|Select|Wrap|Line Numbers
  1.  
  2.  /*Class Main.java
  3.  *
  4.  */
  5. import Employees.Employee;
  6. import Employees.StaffList;
  7. import Employees.FullTimeEmployees;
  8. import Employees.PartTimeEmployees;
  9. import Employees.CommisionedEmployees;
  10.  
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author 
  16.  */
  17. public class Main
  18. {
  19.  
  20.     /**
  21.      *
  22.      * @param args
  23.      */
  24.     public static void main(String[] args)
  25.     {
  26.       Employee[]  staff = new Employee[9];
  27.  
  28.       staff[0] =  new FullTimeEmployees("STUART Charles",1002);
  29.       staff[1] =  new PartTimeEmployees("MARPLE Jane",1005,10.5);
  30.       staff[2] =  new CommisionedEmployees("SEYMOUR Thomas",1006,6500.0);
  31.       staff[3] =  new FullTimeEmployees("AYDELJI Paul",1012);
  32.       staff[4] =  new PartTimeEmployees("BROCK Sarah",1001,30.0);
  33.       staff[5] =  new CommisionedEmployees("ALHAMBRA Alia",1003,3450.0);
  34.       staff[6] =  new PartTimeEmployees("WHARTON Edith",1104,20.0);
  35.       staff[7] =  new FullTimeEmployees("TURNER George",1111);
  36.       staff[8] =  new CommisionedEmployees("BERNERS-LEE Tim",1014,7800.0);
  37.  
  38.       printStaffList( staff);
  39.  
  40.  
  41.     }
  42.  
  43.       static void printStaffList(Employee [] s)
  44.        {
  45.          for ( int i =0; i < 9; i++)
  46.            {
  47.            s[i].calculatePay() ;   //  THIS LINES COMPUTES PAY
  48.                System.out.println( s[i].toString());
  49.            }
  50.        }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.      private Main()
  57.      {
  58.  
  59.      }
  60.  }
  61.  
  62.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*class Employee
  3.  */
  4.  
  5. package Employees;
  6.  
  7.  
  8. /**
  9.  *  @author 
  10.  */
  11.  
  12. public abstract class Employee
  13. {
  14.     protected String name;    //Fields-containers that hold a value
  15.     protected double hrsWorked;
  16.     protected int empID;
  17.     protected static int lastEmpID = 1000;
  18.     protected double sales;
  19.     protected double hours;
  20.     protected double pay;
  21.     private double TotalPayD;
  22.  
  23.  
  24.     /**
  25.      *
  26.      */
  27.     public Employee()
  28.     {
  29.      name = "";
  30.      hrsWorked = 0.0;
  31.      empID = ++lastEmpID ;
  32.      sales = 0.0;
  33.      hours = 40.00;
  34.      pay = 0.0;
  35.      TotalPayD = 0.0;
  36.  
  37.     }
  38.  
  39.     public Employee(String nm, double hrs, double p)
  40.    {
  41.      name =nm;
  42.      hours=hrs;
  43.      pay= p;
  44.      empID = ++lastEmpID ;
  45.    }
  46.  
  47.     public abstract void calculatePay();
  48.  
  49.     public double setHoursWorked( double hoursWorked)
  50.     {
  51.       return hoursWorked;
  52.     }
  53.  
  54.  
  55.      public double getHoursWorked( double hoursWorked)
  56.     {
  57.      return hoursWorked;
  58.     }
  59.  
  60.  
  61.  
  62.     @Override
  63.  
  64.     public String toString ()
  65.     {
  66.       return (" \n Name:  " + name +
  67.               " \n WorksID: " + empID +
  68.               " \n Sales: " + sales +
  69.               " \n Hours: " + hours +
  70.               " \n Pay:  " + pay +"");
  71.     }
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
Expand|Select|Wrap|Line Numbers
  1. /*class Employee
  2.  */
  3.  
  4. package Employees;
  5.  
  6.  
  7. /**
  8.  *  @author 
  9.  */
  10.  
  11. *class FullTimeEmployees
  12.  *
  13.  */
  14. package Employees;
  15.  
  16. /**
  17.  * @author 
  18.  */
  19.  
  20. public class FullTimeEmployees extends Employee
  21. {
  22.  
  23.     double hrsWorked = 40.0;  //  ALL Full-Time staff work for 40 hours
  24.  
  25.     /**
  26.      * 
  27.      * @param nm
  28.      * @param id 
  29.      */
  30.     public FullTimeEmployees(String nm, int id)
  31.     {
  32.         name = nm ;
  33.         hours = hrsWorked ;  // i.e. hours = 40.0 
  34.     empID = id ;
  35.     }
  36.  
  37.  
  38.  
  39.     @Override
  40.     public void calculatePay()
  41.     {
  42.         pay = hrsWorked * 2000.0 / 40.0 ;
  43.     }
  44.  
  45. }
  46.  
  47.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*class PartTimeEmployees
  3.  * To change this template, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package Employees;
  8.  
  9. /**
  10.  *
  11.  * @author 
  12.  */
  13. public class PartTimeEmployees extends Employee
  14. {
  15.  
  16.  
  17.     /**
  18.      *
  19.      * @param nm
  20.      * @param id
  21.      * @param hr
  22.      */
  23.     public PartTimeEmployees(String nm, int id, double hr)
  24.     {
  25.        name = nm ;
  26.        empID = id ;
  27.        hours = hr ;
  28.     }
  29.  
  30.     @Override
  31.     public void calculatePay()
  32.     {
  33.        pay = hours * 2000.0 / 40.0 ;
  34.     }
  35.  
  36. }
  37.  
  38.  
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. /*class CommisionedEmployees
  4.  */
  5.  
  6. package Employees;
  7.  
  8. /**
  9.  *
  10.  * @author 
  11.  */
  12. public class CommisionedEmployees extends Employee
  13. {
  14.     private static double commRate = 10.0 ;
  15.     private static double flatAmount = 1200.0 ;
  16.  
  17.     public CommisionedEmployees(String nm, int id, double s)
  18.     {
  19.        name = nm ;
  20.        empID = id ;
  21.        sales = s ;
  22.     }
  23.  
  24.     @Override
  25.     public void calculatePay()
  26.     {
  27.         pay = flatAmount + sales * (commRate / 100.0) ;
  28.     }
  29.  
  30. }
  31.  
  32.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. /*class StaffList
  3.  *
  4.  */
  5.  
  6. package Employees;
  7.  
  8. import java.util.Collections;
  9.  
  10.  
  11.  
  12.  
  13. public class StaffList 
  14. {
  15.  
  16.  
  17.     public StaffList(String string)
  18.     {
  19.  
  20.     }
  21.  
  22.  
  23.     public void setStaffList()
  24.      {
  25.  
  26.      }
  27.  
  28.     public void getStaffList()
  29.      {
  30.  
  31.      }
  32.  
  33.  
  34.     public void calculatePay()
  35.     {
  36.  
  37.     }
  38.  
  39.     public void printStaffList()
  40.     {
  41.       collections.sort();
  42.     }
  43.      //Collections.sort();
  44.  
  45. }
  46.  
  47.  
Jan 6 '10 #1
3 8391
Frinavale
9,735 Recognized Expert Moderator Expert
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 "calculatePay" method return the pay so that you can use it retrieve the pay for each employee and tally up your sum.

-Frinny
Jan 6 '10 #2
RedSon
5,000 Recognized Expert Expert
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.
Jan 6 '10 #3
pbrockway2
151 Recognized Expert New Member
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:

Expand|Select|Wrap|Line Numbers
  1. double sum = 0;
  2. for(/*etc*/)
  3. {
  4.     // other stuff like printing names etc
  5.     sum += ...; // get the pay and add it
  6. }
  7. // at this point sum will be the total pay
  8.  
Post again if you get stuck.
Jan 6 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9969
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a...
53
5655
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
7
2814
by: rick | last post by:
Can anyone help, I am try to create a simple form using a table, where a user can fill out quanty and price and have a total automatically calculated and inserted in another field. I stuck trying...
7
4308
by: Jurek | last post by:
I have 10+ experience in C/C++ - mostly automation and graphics. I have never written any business apps though. Recently I've been asked to write a simple report that would calculate sales...
4
6350
by: Rich_C | last post by:
I'm sure this is very simple, but I have very little experience with javascript -- and what I do know isn't helping me here. I have a simple form where users can enter a quantity (qty) and cost...
1
4117
by: jaswmil | last post by:
I have a query (see SQL below) that essentially grabs a list of all employees terminated during a specific period. What I am needing to do is to be able to take this number and then divide it by...
3
11769
by: gator6688 | last post by:
I have to write a program that asks for a cost-per-item, number of items purchased, and a discount rate. Then it should calculate the total cost, tax due, and amount due. I have to use the...
4
2762
by: trogenone | last post by:
Hi i have already asked what to use for calculating an age and that works fine. The problem is it dosnt leave a value in the desired table. Tables: Employees & Holidays I asked for a formula...
4
11189
by: shilpareddy2787 | last post by:
Hello, I have some total values, I want to calculate percenatge of these Total Values. I want to divide the total with No. Of working Days Excluding Saturdays and Sundays in a given period. ...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.