简体   繁体   中英

Implementing Scanner to read text file to array

For this program, what i have written so far is in the code below. I am stuck. Any hints/help would be greatly appreciated.

Given the UML Class Diagram and the TestApp class, produce a working system that meets the following requirements:

Implement a Department class with toString and equals overrides. Two department objects are equal if both the name and building numbers match.

Create an abstract Employee class with toString and equals overrides. Two abstract employees are considered to be the same if they are in the same department.

Implement a Consultant class that has an hourlySalary. Two consultants are equal if they have the same rate (in pennies).

Implement a SalariedEmployee class. Salaried employees are equal if they are in the same department and have the same salary (in pennies).

Follow the TODO instructions in the TestApp class to read the departments from the input txt file and test the system.

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);
}

}

* Updated TestApp

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

You're calling get(0) and get(1) on departments before putting anything into the ArrayList. Solution: don't do this! Fill the ArrayList and don't call get(...) ever on it before knowing that something exists at that index.

populateEmployeeArray(company, depts);  // passed into method

And here's how you fill the 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;
}

As you can see, this code does nothing but returns an empty ArrayList, and so it shows that you're going about things a bit backwards. You will instead want to fix this part of your program, get it to read in the text file first , and then fix the displayDepartments(...) method so you can test that the readDepartmentsFromFile works before trying to use the depts ArrayList!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM