我目前正在尝试编写一个跟踪公司员工的程序。我有几节课。我目前的问题是打印出NewWorker
类型的数组。我的课程如下:
NewWorker.java
public class NewWorker extends Person {
private static int howManyWorkers = 0;
public int workerNumber;
private MyDate dateJoiningCompany;
private float Salary = 0.0f;
public Manager Supervisor;
public NewWorker(String name, String date, double salary) {
super(name);
++howManyWorkers; //Add one to total workers
dateJoiningCompany = new MyDate(date);
Salary = (float)salary;
workerNumber = howManyWorkers;
}
public NewWorker(String name, String date) {
super(name);
++howManyWorkers;
dateJoiningCompany = new MyDate(date);
workerNumber = howManyWorkers;
}
public void setSalary(double salary) {
Salary = (float)salary;
}
public void setSupervisor(Manager supervisor) {
if(supervisor != null) {
this.Supervisor = supervisor;
}
else {
System.out.println("The person you are trying to assign a Supervisor to has no supervisor");
}
}
public static int getHowManyWorkers() {
return howManyWorkers;
}
public Name getSupervisorName() {
return Supervisor.getPersonName();
}
public float getSalary() {
return Salary;
}
public Name toString(NewWorker[] nw) {
return super.getPersonName();
}
public String toString() {
return ("Worker Number " + workerNumber + " " + super.toString() + " " + "Date Joined Company = " + dateJoiningCompany.toString()
+ " " + "Salary = " + Salary);
}
public String getDate() {
return dateJoiningCompany.toString();
}
}
Person.java
public class Person {
private Name personName;
private Person spouse = null;
public Person(String fullName) {
personName = new Name(fullName);
}
public void setSpouse(Person spouse) {
this.spouse = spouse;
}
public void setSpouse(NewWorker spouse) {
this.spouse = spouse;
}
public String toString() {
if(spouse != null) {
return ("Name is " + getPersonName().toString() + " Married to " + spouse.getPersonName().toString());
}
else {
return ("Name is " + getPersonName().toString());
}
}
public float getFamilyIncome() {
if(this instanceof NewWorker) {
if(this.spouse == null){
return ((NewWorker)this).getSalary();
}
else {
return ((NewWorker)this).getSalary() + ((NewWorker)this.spouse).getSalary();
}
}
return 0.0f;
}
public Name getPersonName() {
return personName;
}
public void setPersonName(Name personName) {
this.personName = personName;
}
}
Manager.java
import java.util.Arrays;
public class Manager extends NewWorker {
NewWorker[] employeesSupervised = new NewWorker[10];
private int numWorkersSupervised = 0;
public Manager(String name, String date, double salary) {
super(name, date, salary);
}
public Manager(String name, String date) {
super(name, date);
}
public double getSalary(double salary) {
return this.getSalary() + (100 * numWorkersSupervised);
}
public void addWorker(NewWorker w) {
employeesSupervised[numWorkersSupervised] = w;
numWorkersSupervised++;
}
public void deleteWorker(NewWorker w) {
for(int a = 0; a < numWorkersSupervised; ++a) {
if(employeesSupervised[a].equals(w)) {
int numElements = employeesSupervised.length - ( a + 1 );
System.arraycopy( employeesSupervised, a + 1, employeesSupervised, a, numElements);
}
}
}
public int equals(NewWorker w) {
if (this == w) {
return 1;
}
else {
return 0;
}
}
public String toString() {
if(Supervisor != null) {
return "Employee Number = " + this.workerNumber + " Name is " + this.getPersonName() + " Date joined company = "
+ this.getDate() + " Supervisor is " + this.getSupervisorName() + " Salary = " + getSalary((double)getSalary())
+ " Workers supervised: " + Arrays.toString(employeesSupervised);
}
else {
return "Employee Number = " + this.workerNumber + " Name is " + this.getPersonName() + " Date joined company = "
+ this.getDate() + " Salary = " + getSalary((double)getSalary());
}
}
}
所以我的阵列试图抓住经理正在观看的所有人/老板。所以当我使用文件
运行我的程序时 test.java
public class test {
public static void main(String[] args) {
NewWorker w1, w2, w3, w4;
Manager m1, m2;
w1 = new NewWorker("Jack Vanier", "01/01/11", 50000.00);
w2 = new NewWorker("Robert Perry Fletcher", "02/02/02");
w3 = new NewWorker("Hannah Burdett", "05/05/05");
w4 = new NewWorker("Mitchell John McDonagh", "12/12/12", 25000.0);
m1 = new Manager("Sydney Chapados", "11/11/11", 75000);
m2 = new Manager("Steph Scane", "10/10/01", 80000);
m1.addWorker(w1);
m1.addWorker(w2);
m1.addWorker(w3);
m2.addWorker(w4);
m2.addWorker(w1);
w1.setSpouse(w3);
w3.setSpouse(w1);
w2.setSupervisor(m1);
w1.setSupervisor(m1);
w4.setSupervisor(m2);
m2.setSupervisor(m1);
System.out.println(w1);
System.out.println(w2);
System.out.println(w3);
System.out.println(w4);
System.out.println(m1);
System.out.println(m2);
}
}
我得到了前四个(所有工人)的正确输出,但对于我的经理,我无法正确地打印出那些工人。这两个问题是它打印出整个员工信息(姓名,日期,加入工人号码等),我只希望它打印员工姓名,但无法弄清楚如何做到这一点。
另一个问题是,由于我的数组不完整,因此在调用Array.toString()时会得到一堆空值。我怎样才能摆脱这些空值?
答案 0 :(得分:0)
因为这一行:
NewWorker[] employeesSupervised = new NewWorker[10];
数组中没有足够的元素,这就是输出null
答案 1 :(得分:0)
如果您只想在打印管理器时使用员工姓名,那么请使用to string方法的Person实现。 然而,访问超类的超级并不是一件简单的事情,你需要将每个相对基类中的引用存储到超类中,然后调用super.superClass.super.SuperClass.toString()来获取Person toString()
答案 2 :(得分:0)
Arrays.toString
在这里不合适,也不是任何开箱即用的解决方案。例如,您似乎不想在任何意义上实际使用toString
,因为您只需要名称。这是Java 8的解决方案:
+ " Workers supervised: " +
Arrays.stream(employeesSupervised)
.filter(Objects::nonNull)
.map(NewWorker::getPersonName)
.collect(Collectors.joining(", "));