我正在尝试Java开发并遇到问题。
我创建了两个班级,“员工和合同”。我正在尝试使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);
}
}
Contract.java
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;
}
}
Main.java
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();
}
}
上面的输出如下,
我的预期输出是控制台仅显示分配给员工1的合同,我感觉在我的一个类中,我需要给employee-id作为参数,但是每次尝试进行此设置时,我都会只是造成了越来越多的错误,我尝试了一些解决方案,所以我想提出一个问题。
任何帮助将不胜感激。
预先感谢,
B
更新:
Main.Java
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();
}
}
Employee.java
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);
}
}
对 Contract.java
没有任何更改输出:
答案 0 :(得分:1)
问题是您将Contract
列表保留为静态字段
private static ArrayList<Contract> assignedContracts = new ArrayList<>();
这意味着这些Contract
将在所有Employee
之间共享。
在列表中添加一个实例字段应该足以解决此问题(注意,我已删除static
)
private final ArrayList<Contract> assignedContracts = new ArrayList<>();
您显然需要更新其用法,例如
public void addContract(final Contract contract) {
// Now this list is per-employee, not global!
assignedContracts.add(contract);
}
和createContracts
方法。您需要引用Employee
的特定实例,例如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!
这意味着createContracts
试图访问正在构建的实例 ,因此它将是null
,而NullPointerException
将是抛出。
基本上,您需要完全消除static
字段的Employee
,并将createContracts
方法移到Employee
类本身内。