簡體   English   中英

構造函數java,試圖制作一個簡單的程序

[英]constructors java trying to make a simple program

該程序假設將雇員添加到陣列中,然后為每個雇員/經理分配一個分配。 取決於他們是否是經理/雇員的分配金額不同,最后將所有分配相加。

我現在的位置是試圖弄清楚添加所有員工后如何計算總數。

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;
   }
 }

如果您使用的是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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM