2015-02-11 27 views
0

該程序假設將員工添加到數組,然後爲每個員工/經理分配一個分配。取決於他們是經理/員工的分配是不同的數量,並且最終所有分配都被添加。構造函數java試圖做一個簡單的程序

我在哪裏試圖找出如何計算所有員工加入後的總數。

package pay; 

import java.util.*; 



public class manager 
{ 
    static double allocation =0; 
    public manager(String string, int i, String type) { 
     // 
    } 

    public static void main(String[] args) 
    { 
     // construct a Manager object 
     manager boss = new manager("Carl Cracker", 80000, "QA"); 
     int total = 0; 

     Employee[] staff = new Employee[3]; 

     // fill the staff array with Manager and Employee objects 

    // staff[0] = boss; 
     staff[1] = new Employee("Harry Hacker", 0,"Dev"); 
     staff[2] = new Employee("Tommy Tester", 0,"QA"); 

     // print out information about all Employee objects 
     int i; 
    for (i=0; i<3; i++) 
     { 

     total += allocation; 
     } 
    } 
} 



class Employee 
{ 

public Employee(String string, int i, String string2) { 
     // TODO Auto-generated constructor stub 
    } 
public double Employee(String n, int s, String type) 
    { 
    if (type.equals("Dev")) 
      allocation = 1000; 
     else if (type.equals("QA")) 
      allocation = 500; 
     else 
      allocation = 250; 
    return allocation; 
    } 
    private double getallocation() 
    { 
    return allocation; 
    } 

    private double allocation; 
    public String getName() 
    { 
     return name; 
    } 
    private String name; 

} 

class Manager extends Employee 
{ 

    public Manager(String string, int i, String string2) { 
     super(string, i, string2); 
     // TODO Auto-generated constructor stub 
    } 
    /* 
    n the employee's name 
    s the salary 

     */ 
    public double manager(String n, double s) 
    { 
    n= getName(); 
    double getAllocation = 0; 
    s = getAllocation; 
     s=s+300; 
    return s; 
    } 
} 
+3

而問題是什麼? ... – alfasin 2015-02-11 03:44:52

+2

爲什麼你有一個'經理級**和**經理級?另外,爲什麼**? – 2015-02-11 03:48:04

+0

@ElliottFrisch也許記得java區分大寫和小寫zz – ikh 2015-02-11 03:54:12

回答

0

如果使用的Java 8則分配可以概括使用下面的表達式:

Arrays.stream(staff).mapToDouble(Employee::getAllocation).sum(); 

作爲題外話(或有用的提示)這些類型的類結構的通常使用複合材料的設計圖案。它應該是這樣的:

interface Employee { 
    double getAllocation(); 
    EmployeeType getType(); 
} 

class IndividualContributor implements Employee { 
    private Manager boss; 
    public double getAllocation() { 
     return getType().getAllocation(); 
    } 
} 

class Manager implements Employee { 
    private final List<Employee> staff; 
    public double getAllocation() { 
     return getType().getAllocation() + 300; 
    } 
} 

而你的員工類型,目前是String一般會被實現爲enum

enum EmployeeType { 
    QA(500), 
    DEV(100), 
    OTHER(250); 

    private final double allocation; 

    EmployeeType(double allocation) { 
     this.allocation = allocation; 
    } 

    public double getAllocation() { 
     return allocation; 
    } 
}