Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使这个类基于特定变量(ID)工作?_Java_Oop - Fatal编程技术网

Java 如何使这个类基于特定变量(ID)工作?

Java 如何使这个类基于特定变量(ID)工作?,java,oop,Java,Oop,我正在尝试java开发,但遇到了一个问题 我创建了两个类,Employee和Contract。我试图让main方法中的代码为特定员工执行。每个员工都有一个id和一个姓名。我所做的代码如下 Employee.java package com.company; import java.util.ArrayList; public class Employee { // Employee Data public int employeeId; public String employeeName

我正在尝试java开发,但遇到了一个问题

我创建了两个类,Employee和Contract。我试图让main方法中的代码为特定员工执行。每个员工都有一个id和一个姓名。我所做的代码如下

Employee.java

package com.company;

import java.util.ArrayList;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private static ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public static void addContract(Contract contract) {
    assignedContracts.add(contract);
}
}
package com.company;

public class Contract {

// Contract Data
private int contractId;
private String contractName;
private int contractCost;
private int maxAssignedEmployees;

public Contract(String contractName, int contractId, int contractCost, int maxAssignedEmployees) {
    this.contractId = contractId;
    this.contractName = contractName;
    this.contractCost = contractCost;
    this.maxAssignedEmployees = maxAssignedEmployees;
}

// Return contract id
public int getContractId() {
    return contractId;
}

// Return contract name
public String getContractName() {
    return contractName;
}

// Return contract cost
public int getContractCost() {
    return contractCost;
}

// Return number of max employees for a contract
public int getmaxAssignedEmployees() {
    return maxAssignedEmployees;
}
}
package com.company;

public class Main {

// Create employee 1
private static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
private static Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

public class Main {

// Create employee 1
public static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
public Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee1.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee1.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee1.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee1.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

import java.util.ArrayList;

import static com.company.Main.*;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private final ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public void addContract(final Contract contract) {
    assignedContracts.add(contract);
}
}
Main.java

package com.company;

import java.util.ArrayList;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private static ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public static void addContract(Contract contract) {
    assignedContracts.add(contract);
}
}
package com.company;

public class Contract {

// Contract Data
private int contractId;
private String contractName;
private int contractCost;
private int maxAssignedEmployees;

public Contract(String contractName, int contractId, int contractCost, int maxAssignedEmployees) {
    this.contractId = contractId;
    this.contractName = contractName;
    this.contractCost = contractCost;
    this.maxAssignedEmployees = maxAssignedEmployees;
}

// Return contract id
public int getContractId() {
    return contractId;
}

// Return contract name
public String getContractName() {
    return contractName;
}

// Return contract cost
public int getContractCost() {
    return contractCost;
}

// Return number of max employees for a contract
public int getmaxAssignedEmployees() {
    return maxAssignedEmployees;
}
}
package com.company;

public class Main {

// Create employee 1
private static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
private static Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

public class Main {

// Create employee 1
public static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
public Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee1.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee1.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee1.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee1.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

import java.util.ArrayList;

import static com.company.Main.*;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private final ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public void addContract(final Contract contract) {
    assignedContracts.add(contract);
}
}
上述输出如下:

我的预期输出是控制台只显示分配给employee 1的合同,我觉得在我的一个类中,我需要将employee id作为一个参数,但每次我尝试进行此操作时,都会导致越来越多的错误,我尝试了一些解决方案,所以我想我应该发布一个问题

任何帮助都将不胜感激

提前感谢,

B

更新:

Main.Java

package com.company;

import java.util.ArrayList;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private static ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public static void addContract(Contract contract) {
    assignedContracts.add(contract);
}
}
package com.company;

public class Contract {

// Contract Data
private int contractId;
private String contractName;
private int contractCost;
private int maxAssignedEmployees;

public Contract(String contractName, int contractId, int contractCost, int maxAssignedEmployees) {
    this.contractId = contractId;
    this.contractName = contractName;
    this.contractCost = contractCost;
    this.maxAssignedEmployees = maxAssignedEmployees;
}

// Return contract id
public int getContractId() {
    return contractId;
}

// Return contract name
public String getContractName() {
    return contractName;
}

// Return contract cost
public int getContractCost() {
    return contractCost;
}

// Return number of max employees for a contract
public int getmaxAssignedEmployees() {
    return maxAssignedEmployees;
}
}
package com.company;

public class Main {

// Create employee 1
private static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
private static Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

public class Main {

// Create employee 1
public static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
public Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee1.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee1.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee1.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee1.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

import java.util.ArrayList;

import static com.company.Main.*;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private final ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public void addContract(final Contract contract) {
    assignedContracts.add(contract);
}
}
Employee.java

package com.company;

import java.util.ArrayList;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private static ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public static void addContract(Contract contract) {
    assignedContracts.add(contract);
}
}
package com.company;

public class Contract {

// Contract Data
private int contractId;
private String contractName;
private int contractCost;
private int maxAssignedEmployees;

public Contract(String contractName, int contractId, int contractCost, int maxAssignedEmployees) {
    this.contractId = contractId;
    this.contractName = contractName;
    this.contractCost = contractCost;
    this.maxAssignedEmployees = maxAssignedEmployees;
}

// Return contract id
public int getContractId() {
    return contractId;
}

// Return contract name
public String getContractName() {
    return contractName;
}

// Return contract cost
public int getContractCost() {
    return contractCost;
}

// Return number of max employees for a contract
public int getmaxAssignedEmployees() {
    return maxAssignedEmployees;
}
}
package com.company;

public class Main {

// Create employee 1
private static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
private static Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

public class Main {

// Create employee 1
public static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
public Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee1.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee1.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee1.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee1.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

import java.util.ArrayList;

import static com.company.Main.*;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private final ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public void addContract(final Contract contract) {
    assignedContracts.add(contract);
}
}
package.com公司;
导入java.util.ArrayList;
导入static com.company.Main.*;
公营雇员{
//员工数据
公共国际雇员ID;
公共字符串employeeName;
//分配给员工的所有合同的数组
私有最终ArrayList assignedContracts=新ArrayList();
//分配的合同数组的大小
私人分配;
公职人员(int-employeeId、String-employeeName、String-ContractName1、String-ContractName2、String-ContractName3、String-ContractName4、int-ContractD1、int-ContractD2、int-ContractD3、int-ContractCost1、int-ContractCost2、int-ContractCost3、int-ContractCost4、int-MaxAssignedEmployees1、int-MaxAssignedEmployees2、int-MaxAssignedEmployees3),int MaxAssignedEmployees4){
this.employeeId=employeeId;
this.employeeName=employeeName;
Main.createContracts(ContractName1、ContractName2、ContractName3、ContractName4、ContractD1、ContractD2、ContractD3、ContractD4、ContractCost1、ContractCost2、ContractCost3、ContractCost4、MaxAssignedEmployees1、MaxAssignedEmployees2、MaxAssignedEmployees3、MaxAssignedEmployees4);
contractAllocation=assignedContracts.size();
}
//将所有分配的合同打印到控制台(如表所示)
公共void ProviderAssignedControl(){
System.out.println(“------------------------------------------------------------------------------------------------”;
System.out.printf(“%10s%20s%20s%20s%20s”、“合同ID”、“合同名称”、“合同成本”、“最大员工数”);
System.out.println();
System.out.println(“------------------------------------------------------------------------------------------------”;
对于(int i=0;i
没有对Contract.java的更改

package com.company;

import java.util.ArrayList;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private static ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public static void addContract(Contract contract) {
    assignedContracts.add(contract);
}
}
package com.company;

public class Contract {

// Contract Data
private int contractId;
private String contractName;
private int contractCost;
private int maxAssignedEmployees;

public Contract(String contractName, int contractId, int contractCost, int maxAssignedEmployees) {
    this.contractId = contractId;
    this.contractName = contractName;
    this.contractCost = contractCost;
    this.maxAssignedEmployees = maxAssignedEmployees;
}

// Return contract id
public int getContractId() {
    return contractId;
}

// Return contract name
public String getContractName() {
    return contractName;
}

// Return contract cost
public int getContractCost() {
    return contractCost;
}

// Return number of max employees for a contract
public int getmaxAssignedEmployees() {
    return maxAssignedEmployees;
}
}
package com.company;

public class Main {

// Create employee 1
private static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
private static Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

public class Main {

// Create employee 1
public static Employee Employee1 = new Employee(1, "Bradley", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create employee 2
public Employee Employee2 = new Employee(2, "Patrick", "Contract 1", "Contract 2", "Contract 3", "Contract 4", 1, 2,3,4,300,1200,500,900, 5,10,5,2);

// Create contracts
public static void createContracts(String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int maxAssignedEmployees1, int maxAssignedEmployees2, int maxAssignedEmployees3, int maxAssignedEmployees4) {
    Employee1.addContract(new Contract(ContractName1, ContractId1, ContractCost1, maxAssignedEmployees1));
    Employee1.addContract(new Contract(ContractName2, ContractId2, ContractCost2, maxAssignedEmployees2));
    Employee1.addContract(new Contract(ContractName3, ContractId3, ContractCost3, maxAssignedEmployees3));
    Employee1.addContract(new Contract(ContractName4, ContractId4, ContractCost4, maxAssignedEmployees4));
}

// Show assigned contracts for employee
public static void displayAssignedContracts() {
    Employee1.provideAssignedContracts();
}

public static void main(String[] args) {
    System.out.println("Employee ID: " + Employee1.employeeId);
    System.out.println("Employee Name: " + Employee1.employeeName);
    System.out.println("Assigned Contracts: " + Employee1.provideNumOfAssignedContracts());
    System.out.println("Total cost of all contracts assigned: £" + Employee1.provideTotalCostOfAssignedContracts() + "\n");
    displayAssignedContracts();
}
}
package com.company;

import java.util.ArrayList;

import static com.company.Main.*;

public class Employee {

// Employee Data
public int employeeId;
public String employeeName;

// Array of all contracts assigned to an employee
private final ArrayList<Contract> assignedContracts = new ArrayList<>();

// Size of assigned contracts array
private int contractAllocation;

public Employee(int employeeId, String employeeName, String ContractName1, String ContractName2, String ContractName3, String ContractName4, int ContractId1, int ContractId2, int ContractId3, int ContractId4, int ContractCost1, int ContractCost2, int ContractCost3, int ContractCost4, int MaxAssignedEmployees1, int MaxAssignedEmployees2, int MaxAssignedEmployees3, int MaxAssignedEmployees4)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(ContractName1, ContractName2, ContractName3, ContractName4, ContractId1, ContractId2, ContractId3, ContractId4, ContractCost1, ContractCost2, ContractCost3, ContractCost4, MaxAssignedEmployees1, MaxAssignedEmployees2, MaxAssignedEmployees3, MaxAssignedEmployees4);
    contractAllocation = assignedContracts.size();
}

// Print all assigned contracts to console (as table)
public void provideAssignedContracts() {
    System.out.println("-----------------------------------------------------------------------------");
    System.out.printf("%10s %20s %20s %20s", "CONTRACT ID", "CONTRACT NAME", "CONTRACT COST", "MAX EMPLOYEES");
    System.out.println();
    System.out.println("-----------------------------------------------------------------------------");
    for(int i = 0; i < assignedContracts.size(); i++) {
        System.out.format("%10s %20s %20s %22s",
                assignedContracts.get(i).getContractId(), assignedContracts.get(i).getContractName(), "£" + assignedContracts.get(i).getContractCost(), assignedContracts.get(i).getmaxAssignedEmployees());
        System.out.println();
    }
    System.out.println("-----------------------------------------------------------------------------");
}

// Number of assigned contracts
public int provideNumOfAssignedContracts() {
    // Count for number of contracts
    int count = 0 ;
    // Increment count for every contract
    for(int i = 0; i < assignedContracts.size(); i++) {
        count++;
    }
    // Return int for number of assigned contracts
    return count;
}

// Total cost of all assigned contracts
public int provideTotalCostOfAssignedContracts() {
    // variable to store cost in
    int sum = 0;
    // add each cost iteration to sum
    for(int i = 0; i < assignedContracts.size(); i++) {
        sum += assignedContracts.get(i).getContractCost();
    }
    // Return int for total cost
    return sum;
}

// Add a contract to the employee
public void addContract(final Contract contract) {
    assignedContracts.add(contract);
}
}
输出:


问题是您将
合同
列表维护为一个静态字段

private static ArrayList<Contract> assignedContracts = new ArrayList<>();
您显然需要更新它的用法,例如

public void addContract(final Contract contract) {
   // Now this list is per-employee, not global!
   assignedContracts.add(contract);
}
createContracts
方法。您需要引用
员工的特定实例,如
employee1
employee2


当前代码有几个问题

public static Employee Employee1 = new Employee(...)
此代码所做的是调用
Employee
的构造函数,但正如您所看到的,构造函数调用
createContracts

public Employee(...)  {
    this.employeeId = employeeId;
    this.employeeName = employeeName;
    Main.createContracts(...); 
    contractAllocation = assignedContracts.size();
}
它使用静态字段
Employee1

public static void createContracts(...) {
    Employee1.addContract(...) <-- Here!
publicstaticvoidcreatecontracts(…){

Employee1.addContract(…)您好@LppEdd,我真的很感谢您的帮助,我有点理解其中的逻辑..请允许我先想一想..当更改addContract中的用法时(如上所示)我收到一个错误“非静态字段不能从静态上下文引用”。你理解这个错误吗?你能解释一下它的意思吗?再一次,非常感谢。无论如何,我将它更改为非静态,它删除了错误并创建了3个。我将继续尝试:)@bbowesbo因为我不能更改您所有的代码,所以我让您来做无聊的部分;)重要的是,合同列表不再像我在回答中所写的那样以静态形式存储。@bbowesbo如果您需要澄清,请告诉我。谢谢。在线程“main”中获取以下-Exceptionjava.lang.ExceptionInInitializerError由com.company.Main.createContracts(Main.java:13)com.company.Employee.(Employee.java:22)com.company.Main.(Main.java:6)的java.lang.NullPointerException引起。我不是在寻求解决方案,我想也许我应该回到画板(教科书)上,我已经不知所措了