对于这个程序,到目前为止我所写的内容在下面的代码中。我被卡住了。任何提示/帮助将不胜感激。
给定UML类图和TestApp类,生成满足以下要求的工作系统:
使用toString实现Department类并等于覆盖。如果名称和建筑物编号都匹配,则两个部门对象相等。
使用toString和equals覆盖创建一个抽象的Employee类。如果两个抽象员工在同一个部门,则被认为是相同的。
实现具有hourlySalary的Consultant类。如果两个顾问的费率相同(用便士),则两个顾问是平等的。
实施SalariedEmployee类。如果薪水员工在同一个部门并且薪水相同(以便士为单位),那么他们是平等的。
按照TestApp类中的TODO说明从输入txt文件中读取部门并测试系统。
Ouput should be:
Executive 100
Research 112
Engineering 115
Production 120
Accounting 122
Executive 100(100) Steve Jobs $1000000.00
Research 112(200) Bill Joy $300000.00
Engineering 115(300) James Gosling $300000.00
Production 120(1100) Craig McClanahan $250.00
Accounting 122(400) Kevin Malone $51567.00
Research 112
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class TestApp {
public static void main(String[] args) {
ArrayList<Department> depts =
readDepartmentsFromFile("depts.txt");
displayDepartments(depts);
ArrayList<Employee> company = new ArrayList<>();
populateEmployeeArray(company, depts);
// TODO: Display everyone's pay w/o referring to the
// subclasses (i.e., using Employee only)
// TODO: Display all departments that are equal to this one:
Department d1 = new Department("Research", 112);
// TODO: Display all departments that are equal to this one:
Department d2 = new Department("Accounting", 50);
}
private static void displayDepartments(ArrayList<Department> list) {
// TODO: Display the list to std error (NOT standard output)
}
private static ArrayList<Department> readDepartmentsFromFile(String filename) {
ArrayList<Department> list = new ArrayList<>();
// TODO: Use a Scanner to read the input file depts.txt
// and create one Department object per line in the file.
// Add all objects to the list.
// Be sure to close the scanner object using a finally block.
// Handle possible execptions by producing a nice error
// along with the execption's message to standard error
// (NOT standard output).
return list;
}
public static void populateEmployeeArray(ArrayList<Employee> array,
ArrayList<Department> departments) {
SalariedEmployee exec = new SalariedEmployee(100, "Steve",
"Jobs", departments.get(0), 100000000);
SalariedEmployee architect = new SalariedEmployee(200, "Bill",
"Joy", departments.get(1), 30000000);
SalariedEmployee engineer = new SalariedEmployee(300, "James",
"Gosling", departments.get(2), 30000000);
Consultant consultant = new Consultant(1100, "Craig",
"McClanahan", departments.get(3), 25000);
SalariedEmployee accountant = new SalariedEmployee(400,
"Kevin", "Malone", departments.get(4), 5156700);
array.add(exec);
array.add(architect);
array.add(engineer);
array.add(consultant);
array.add(accountant);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class TestApp {
public static void main(String[] args) {
ArrayList<Department> depts =
readDepartmentsFromFile("depts.txt");
displayDepartments(depts);
ArrayList<Employee> company = new ArrayList<>();
populateEmployeeArray(company, depts);
// TODO: Display everyone's pay w/o referring to the
// subclasses (i.e., using Employee only)
for (Employee employee : company){
System.out.println(employee);
}
// TODO: Display all departments that are equal to this one:
Department d1 = new Department("Research", 112);
for (Department department : depts)
if(department.equals(d1)){
System.out.println(department);
}
// TODO: Display all departments that are equal to this one:
Department d2 = new Department("Accounting", 50);
for(Department department : depts){
if(department.equals(d2)){
System.out.println(department);
}
}
}
private static void displayDepartments(ArrayList<Department> list) {
// TODO: Display the list to std error (NOT standard output)
System.err.println(department.toString());
}
private static ArrayList<Department> readDepartmentsFromFile(String filename) {
ArrayList<Department> list = new ArrayList<>();
// TODO: Use a Scanner to read the input file depts.txt
// and create one Department object per line in the file.
// Add all objects to the list.
// Be sure to close the scanner object using a finally block.
// Handle possible execptions by producing a nice error
// along with the execption's message to standard error
// (NOT standard output).
File aFile = new File(filename);
System.out.println(aFile.exists());
Scanner sc = null;
if (aFile.exists())
try{
sc = new Scanner(aFile);
while(sc.hasNext()) {
String deptName = sc.next();
int buildNum = sc.nextInt();
Department mallory = new Department(deptName, buildNum);
list.add(mallory);
}
}
catch(FileNotFoundException exec) {
System.out.println(exec.getMessage());
}
finally {
sc.close();
}
return list;
}
public static void populateEmployeeArray(ArrayList<Employee> array,
ArrayList<Department> departments) {
SalariedEmployee exec = new SalariedEmployee(100, "Steve",
"Jobs", departments.get(0), 100000000);
SalariedEmployee architect = new SalariedEmployee(200, "Bill",
"Joy", departments.get(1), 30000000);
SalariedEmployee engineer = new SalariedEmployee(300, "James",
"Gosling", departments.get(2), 30000000);
Consultant consultant = new Consultant(1100, "Craig",
"McClanahan", departments.get(3), 25000);
SalariedEmployee accountant = new SalariedEmployee(400,
"Kevin", "Malone", departments.get(4), 5156700);
array.add(exec);
array.add(architect);
array.add(engineer);
array.add(consultant);
array.add(accountant);
}
}
TEXT FILE -
depts.txt -
Executive 100
Research 112
Engineering 115
Production 120
Accounting 122
答案 0 :(得分:2)
在将任何内容放入ArrayList之前,您已在部门上调用get(0)
和get(1)
。解决方案:不要这样做!填写ArrayList,在知道该索引存在某些内容之前,不要再调用get(...)
。
populateEmployeeArray(company, depts); // passed into method
以下是填充depts ArrayList:
的方法// you might need a bit more code in here!!!! The // TODO: means do it :)
private static ArrayList<Department> readDepartmentsFromFile(String filename) {
ArrayList<Department> list = new ArrayList<>();
// TODO: Use a Scanner to read the input file depts.txt
// and create one Department object per line in the file.
// Add all objects to the list.
// Be sure to close the scanner object using a finally block.
// Handle possible execptions by producing a nice error
// along with the execption's message to standard error
// (NOT standard output).
return list;
}
正如你所看到的,这段代码什么都不做,只返回一个空 ArrayList,所以它表明你有点倒退。您将需要修复程序的这一部分,让它在文本文件首先中阅读,然后修复displayDepartments(...)
方法,以便您可以测试readDepartmentsFromFile是否正常工作之前尝试使用depts ArrayList!