这是一个简单的Java应用程序,通过汇总该部门所有员工的月工资来计算部门的月度开支。部门的工作人员安排在层次结构中 (测试人员支付 500.0 ,&amp ;,开发人员支付 1000.0 ,由经理支付 300.0 ,经理支付 300.0 。
通过这种方式,ManagerA的费用总值应 2100.0 。这笔费用总额是部门每月费用。
我打算建立一个能够做到这一点的应用程序。然后通过测试 getTotalExpenses()的返回来证明它,它应该是 2100.0 。
我构建了测试,测试失败(pic):
所以我用 println()构建了一个 main(),看看发生了什么&得到了一些非常意外的输出(pic):
monDepExpensesApp.QATester@15db9742
monDepExpensesApp.Developer@6d06d69c
monDepExpensesApp.Manager@7852e922
monDepExpensesApp.Manager@7852e922
monDepExpensesApp.Manager@4e25154f
monDepExpensesApp.Manager@4e25154f
所有记录的源代码都在下面,它解释得很多。
但这些是我的问题:
为什么我的考试没有过去?我没有正确测试或我的应用程序存在缺陷吗?
为什么我的main()显示上面的输出?它是像指针,无符号数据,字节?我需要换行,更改数据类型吗?
我从来没有见过这样的Java。
注意:我只构建主要部分,告诉我更多关于我的测试失败的原因。 注意:第二个问题已经回答
感谢您一眼,我有兴趣了解您的最新动态。
员工
package monDepExpensesApp;
/**
* Employee declares expenses, & getExpenses().
*
* Employee is the superclass of Manager, QATester, and Developer.
*
* @author Reed
*/
public class Employee {
protected double expenses;
/**
* getExpenses() returns the monthly allocation amount of a Manager, Developer, or QATester object.
* @return a double values representing what the specified Employee is paid each month.
*/
public double getExpenses() {
return expenses;
}
}
开发
package monDepExpensesApp;
/**
* Developers warrant a monthly allocation of $1000.00, per Developer.
*
* Developer extends Employee.
*
* @author Reed
*/
public class Developer extends Employee {
/**
* Developer() creates a Developer, who is paid $1000.0 a month.
*/
public Developer() {
expenses = 1000.0;
}
}
QATester
package monDepExpensesApp;
/**
* QA Testers warrant a monthly allocation of $500.00, per QA Tester.
*
* QATester extends Employee.
*
* @author Reed
*/
public class QATester extends Employee {
/**
* QATester() creates a QA Tester, who is paid $500.0 a month.
*/
public QATester() {
expenses = 500.0;
}
}
管理器
package monDepExpensesApp;
import java.util.ArrayList;
/**
* Managers warrant a monthly allocation of $300.00, per Manager.
*
* A manager is at the top of a hierarchical relationship,
* in which one manager oversees employees such as developers,
* QA testers, & other managers. A hierarchy is constituted of all the
* employees of a department. An employee's title is associated with
* an amount paid monthly. A departments monthly expenses can be determined
* by adding the amounts paid to the employees in a hierarchy.
*
* Manager extends Employee.
*
* @author Reed
*/
public class Manager extends Employee {
private ArrayList<Employee> employeeList = new ArrayList<>();
/**
* Manager() creates a Manager, who is paid $300.0 a month.
*/
public Manager() {
expenses = 300.0;
}
/**
* Add() adds employees to a list.
* @param employee
*/
public void add(Employee employee) {
employeeList.add(employee);
}
/**
* getTotalExpenses() adds the values in employeeList,
* calculating a departments total monthly expenses.
* @return the value of totalExpenses.
*/
public double getTotalExpenses() {
double totalExpenses = 0.00;
for(Employee employee : employeeList) {
totalExpenses += employee.getExpenses();
}
return totalExpenses;
}
}
DepOneExpTest
package monDepExpensesApp.UnitTest;
import static org.junit.Assert.*;
import monDepExpensesApp.Developer;
import monDepExpensesApp.Manager;
import monDepExpensesApp.QATester;
import org.junit.Test;
/**
* DepOneExpTest evaluates a departments actual total expenses,
* by comparing them to a projected total.
*
* @author Reed
*/
public class DepOneExpTest {
/**
* testOne() compares a departments actual total expenses with a projected total of $2100.00.
* This departments employees create the following hierarchy:
* managerA <-- managerB <-- tester1 & dev1.
*/
@Test
public void test() {
QATester tester1 = new QATester();
Developer dev1 = new Developer();
Manager managerB = new Manager();
managerB.add(tester1);
managerB.add(dev1);
Manager managerA = new Manager();
managerA.add(managerB);
assertEquals(2100.0, managerA.getTotalExpenses(), 0.001);
}
}
MAIN2
package monDepExpensesApp;
public class Main2 {
public static void main(String[] args) {
QATester tester1 = new QATester();
System.out.println(tester1);
Developer dev1 = new Developer();
System.out.println(dev1);
Manager managerB = new Manager();
System.out.println(managerB);
managerB.add(tester1);
managerB.add(dev1);
System.out.println(managerB);
Manager managerA = new Manager();
System.out.println(managerA);
managerA.add(managerB);
System.out.println(managerA);
}
}
答案 0 :(得分:0)
简单地将以下内容添加到Employee类将解决您的问题
@Override
public String toString() {
//return something sensible like the Employee Name
return getEmployeeName();
}
答案 1 :(得分:0)
当您打印对象时,它只会打印调用toString()
的结果。默认的toString()
实现将返回一个字符串,其中包含类名和哈希码的表示形式(有关详细信息,请参阅toString()
Javadoc)。正如其他人所指出的那样,您可以通过覆盖toString()
:
@Override
public String toString() {
return expenses + " " + employeeList;
}
你的问题在这里:
totalExpenses += employee.getExpenses();
这将增加孩子的费用,但不包括孙子女的费用。你想要:
totalExpenses += employee.getTotalExpenses();
您可能还希望getTotalExpenses()
依赖于expenses
字段。