任务:已知职工文件中包括职工号、职工姓名、职务和职称等若干数据项(见下表)。职务有校长、系主任、室主任和教员;校长领导所有系主任,系主任领导他所在系的所有室主任,室主任领导他所在室的全体教员;职称有教授、副教授和讲师3种。
请给该文件建立索引,通过该索引文件,要求:
(1)能够检索出全体职工间领导与被领导的情况;
(2)能够分别检索出全体教授、全体副教授、全体讲师。要求指针数量尽可能少,给出各指针项索引的名称及含义即可。
职工号 职工姓名 职务 职称
1 张军 教员 讲师
2 沈灵 系主任 教授
3 叶明 校长 教授
staff.java:`public class staff {
private int ID;
private String name;
private String post;
private String title;
staff(int ID, String name,String post, String title){
this.ID = ID ;
this.post = post ;
this.title = title ;
this.name = name;
}
int getID(){
return ID;
}
String getName(){
return name;
}
String getPost(){
return post;
}
String getTitle(){
return title;
}
}
import java.util.ArrayList;
createIdx.java:
public class createIdx {
//staff sf0 = new staff(0,”“,”“,”“);
staff sf1 = new staff(1,”张军”,”教员”,”讲师”);
staff sf2 = new staff(2,”沈灵”,”系主任”,”教授”);
staff sf3 = new staff(3,”叶明”,”校长”,”教授”);
//LinkedHashMap<Integer , String> lhm1 = new LinkedHashMap<Integer , String>();
/*public LinkedHashMap<Integer, String> staffIdx(){
lhm1.put(1, sf1.getName());
lhm1.put(2, sf2.getName());
lhm1.put(3, sf3.getName());
return lhm1;
}*/
ArrayList<staff> list = new ArrayList<staff>();
public ArrayList<staff> staffIdx(){
//list.add(0,sf0);
list.add(sf1);
list.add(sf2);
list.add(sf3);
staff aa = (staff)list.get(1);
System.out.println(aa.getName());
for(staff sf : list){
System.out.println(sf.getName());
}
return list;
}
public String searchName(int i){
staff sf = (staff)list.get(i);
return sf.getName();
}
public String searchPost(int i){
staff sf = (staff)list.get(i);
return sf.getPost();
}
public String searchTitle(int i){
staff sf = (staff)list.get(i);
return sf.getTitle();
}
public StringBuffer searchStaff(int id){
StringBuffer sr = new StringBuffer();
if(id>list.size() || id<0){
sr.append("不存在此人");
}
else{
staff sa = (staff)list.get(id);
String s1 = sa.getPost().toString();
if(s1.equals("教员"))
for(staff sf : list){
String s2 = sf.getPost().toString();
//String s3 = sf.getName().toString();
if(s2.equals("系主任")){
sr.append(sa.getName()+"是教员"+"被"+sf.getName()+"领导");
}
}
else if(s1.equals("系主任"))
for(staff sf : list){
String s2 = sf.getPost().toString();
if(s2.equals("校长"))
sr.append("被"+sf.getName()+"领导,"+"。");
if(s2.equals("教员")){
sr.append(sa.getName()+"是系主任"+"领导教员"+sf.getName()+",");
}
}
else{
for(staff sf : list){
String s2 = sf.getPost().toString();
if(s2.equals("系主任")){
sr.append(sa.getName()+"是校长,"+"领导"+sf.getName());
}
}
}
}
return sr;
}
public StringBuffer searchLuc(){
String[] luc = new String[10];
StringBuffer sb = new StringBuffer();
for(staff sf : list){
String s1 = sf.getTitle().toString();
if(s1.equals("讲师")){
sb.append(sf.getName());
for(int i=0; i<list.size(); i++){
String s2 = sf.getName().toString();
luc[i]=(s2);
}
}
}
return sb;
}
public StringBuffer searchPro(){
StringBuffer su = new StringBuffer();
for(staff sf : list){
String s1 = sf.getTitle().toString();
if(s1.equals("教授") )
su.append(sf.getName()+"\t");
}
return su;
}
public StringBuffer searchAssPro(){
StringBuffer sa = new StringBuffer();
int flag = 0;
for(staff sf : list){
String s1 = sf.getTitle().toString();
if(s1.equals("副教授") ){
sa.append(sf.getName());
}else{
if(flag == 0){
sa.append("不存在");
flag = 1;
}
}
}
return sa;
}
}`
view.java:`import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class view {
public static void main(String[] args) {
// 创建 JFrame 实例
JFrame frame = new JFrame("search");
// Setting the width and height of frame
frame.setSize(350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
/*
* 调用用户定义的方法并添加组件到面板
*/
placeComponents(panel);
// 设置界面可见
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
/*
* 这边设置布局为 null
*/
panel.setLayout(null);
// 创建 JLabel
//JLabel userLabel = new JLabel("查询员工:");
/* 这个方法定义了组件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
//userLabel.setBounds(5,20,80,25);
//panel.add(userLabel);
/*
* 创建文本域用于用户输入
*/
JTextField idText = new JTextField(20);
idText.setBounds(150,250,50,25);
panel.add(idText);
JButton IdButton = new JButton("查询员工");
IdButton.setBounds(10, 100, 100, 25);
IdButton.setLocation(50,250);
panel.add(IdButton);
IdButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
createIdx cd = new createIdx();
cd.staffIdx();
String si = idText.getText();
int i = Integer.parseInt(si);
String ss = cd.searchStaff(i).toString();
JOptionPane.showMessageDialog(IdButton,ss);
}
}
);
// 创建按钮
JButton LucButton = new JButton("查询讲师");
LucButton.setBounds(10, 100, 100, 25);
LucButton.setLocation(50,200);
LucButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
createIdx cd = new createIdx();
cd.staffIdx();
String ss = cd.searchLuc().toString();
JOptionPane.showMessageDialog(LucButton,ss);
}
}
);
panel.add(LucButton);
JButton ProButton = new JButton("查询教授");
ProButton.setBounds(10, 100, 100, 25);
ProButton.setLocation(50,150);
ProButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
createIdx cd = new createIdx();
cd.staffIdx();
String ss = cd.searchPro().toString();
JOptionPane.showMessageDialog(ProButton,ss);
}
}
);
panel.add(ProButton);
JButton AssProButton = new JButton("查询副教授");
AssProButton.setBounds(10, 100, 100, 25);
AssProButton.setLocation(50, 100);
AssProButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
createIdx cd = new createIdx();
cd.staffIdx();
String ss = cd.searchAssPro().toString();
JOptionPane.showMessageDialog(ProButton,ss);
}
}
);
panel.add(AssProButton);
}
}`