2014-06-20 31 views
0

我刚刚进入Java,我很快就要为我的课程准备一个项目,并继续运行相同的错误。我要创建一个学生,教师,员工目录,可以添加新条目,更新它们,并使用String()方法打印它们。我被推荐使用一个treeMap集合。错误涉及.getKey()方法,无法找到它。我将包括我的代码,并将非常感谢任何意见,以帮助我弄清楚这一点。先谢谢你。Java中的学生,教师,职员目录

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

} 
+0

你得到的* exact *错误信息是什么? – awksp

+0

无法找到符号 - 方法getKey() – user3758442

+1

您已经张贴了太多的代码。请阅读[如何创建一个最小,完整和可验证的示例](http://stackoverflow.com/help/mcve)指出发生此错误的位置也很方便。 – PakkuDon

回答

0

首先变化:

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

只是:

import java.util.* 

(这只是为了保持整洁)

那么我会建议使用地图或HashMap而不是treeMap(但是我不熟悉treeMap)

编辑:只是改变Persons.getKey()newPersons.getKey()

你试图静态调用它。

希望这会有所帮助!

+1

这将使密钥不唯一,这意味着无论您添加到目录中,该密钥都将被覆盖 – MadProgrammer

相关问题