繁体   English   中英

Java中的学生,教职员工目录

[英]Student, Faculty, Staff Directory in Java

我刚接触Java,很快就有一个班级要交的项目,并继续遇到相同的错误。 我要创建一个学生,教职员工目录,可以添加新条目,更新它们并使用to String()方法打印它们。 建议我也使用treeMap集合。 该错误涉及.getKey()方法,无法找到它。 我将包含我的代码,非常感谢您提供任何帮助我解决这个问题的输入。 先感谢您。

import java.util.TreeMap;
import java.util.*;
import java.util.Map.Entry;
import java.util.Iterator;
import java.util.Map;

/**
 * Write a description of class Persons here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Directory {

    private String firstName, lastName;

    private TreeMap<String, Persons> schoolDirectory;
    private int numberInDirectory;

    public Directory() {
        schoolDirectory = new TreeMap<String, Persons>();

    }

    public String getKey() {
        return lastName + firstName;
    }

    public void addPerson(Persons newPersons) {
        schoolDirectory.put(Persons.getKey(), newPersons);
        numberInDirectory++;
    }

    public void addPerson(Staff newStaff) {
        schoolDirectory.put(newStaff.getKey(), newStaff);
        numberInDirectory++;
    }

    public int getNumber() {
        return numberInDirectory;
    }

    public class Persons {

        // instance variables - replace the example below with your own

        private String firstName;
        private String lastName;
        private String email;

    }
}

/**
 * Write a description of class People here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class People extends Directory {

    private String firstName, lastName, emailAddress, phoneNumber, ID;

    public People(String firstName, String lastName, String emailAddress, String phoneNumber, String ID) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.emailAddress = emailAddress;
        this.phoneNumber = phoneNumber;
        this.ID = ID;
    }

    public String getKey() {
        return lastName + firstName;
    }
}

/**
 * Write a description of class Students here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Students extends People {

    // instance variables - replace the example below with your own

    private String classlevel;
    private int SudentID;

}

/**
 * Write a description of class Faculty here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Faculty extends People {

    private String RoomNumber;
    private String EmployeeStatus;
    private String ProgramOfInstruction;

    public Faculty(String firstName, String lastName, String emailAddress, String phoneNumber, String ID, String room, String status, String instruction) {
        super(firstName, lastName, emailAddress, phoneNumber, ID);
        RoomNumber = room;
        EmployeeStatus = status;
        ProgramOfInstruction = instruction;
    }
}

/**
 * Write a description of class Staff here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Staff extends People {

    private String OfficeNumber;
    private String JobTitle;

    public Staff(String firstName, String lastName, String emailAddress, String phoneNumber, String ID, String office, String position) {
        super(firstName, lastName, emailAddress, phoneNumber, ID);
        OfficeNumber = office;
        JobTitle = position;
    }

}

首先更改:

import java.util.TreeMap;
import java.util.*;
import java.util.Map.Entry;
import java.util.Iterator;
import java.util.Map;

只是:

import java.util.*

(这只是为了保持整洁)

那么我建议您使用Map或HashMap而不是treeMap(但是我不熟悉treeMap)

编辑:只需将Persons.getKey()更改为newPersons.getKey()

您试图静态地调用它。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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