⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 persondao.java

📁 学校的学生和工人管理系统 可以完成增删改查的功能
💻 JAVA
字号:
/*
 * 创建日期 2005-9-21
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package cn.itcareers.lxh.exercise.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import cn.itcareers.lxh.exercise.interfaces.Person;
import cn.itcareers.lxh.exercise.io.FileOperate;
import cn.itcareers.lxh.exercise.io.InputData;
import cn.itcareers.lxh.exercise.main.ShowPerson;
import cn.itcareers.lxh.exercise.person.Student;
import cn.itcareers.lxh.exercise.person.Worker;

/**
 * @author 白涛
 * 
 * 此类用与进行添加、修改、删除、查询的功能
 */
public class PersonDAO {
	/**
	 * 
	 * @param hm 现有的HashMap
	 * @param name  得到查入的类名称
	 * @return
	 */
	public boolean insert(HashMap hm, String name) {
		boolean flag = true;
		InputData in = new InputData();
		Person p = null;
		try {
			p = in.getPerson(name);
		} catch (Exception e) {
			flag = false;
		}
		String key = this.createKey(p);
		hm.put(key, p);
		this.writeFile(hm, p);
		return flag;
	}
	
	/**
	 * 
	 * @param hm 得到现有的HashMap
	 * @param id 操作ID
	 * @return
	 */
	public boolean delete(HashMap hm, String id) {
		boolean flag = true;
		String str = this.getKey(hm, id);
		if (str.equals(""))
			flag = false;
		Person p = (Person) hm.get(str);
		hm.remove(str);
		this.writeFile(hm, p);
		return flag;
	}

	/**
	 * 更新操作
	 * @param hm 得到现有的HashMap
	 * @param id 要更新的ID
	 * @return
	 */
	public boolean update(HashMap hm, String id) {
		boolean flag = true;
		String key = this.getUpdate(hm, id);
		if (key.equals("nothing")) {
			System.out.println("没有此人!!!");
			flag = false;
		} else {
			Person p = (Person) hm.get(key);
			InputData in = new InputData();
			try {
				in.updatePerson(p);
			} catch (Exception e) {
				flag = false;
			}
			this.writeFile(hm, p);
			flag = true;
		}
		return flag;
	}

	/**
	 * 查询方法
	 * @param hm 得到现有的HashMap
	 * @param condition 查询条件
	 * @param type 所选类型
	 */
	public void search(HashMap hm, String condition, String type) {
		ShowPerson.display(this.getSearch(hm, condition), type);
	}
	
	/**
	 * 浏览全部
	 * @param hm 得到现有的HashMap
	 * @param type 所选类型
	 */
	public void select(HashMap hm, String type) {
		ShowPerson.display(hm.values(), type);
	}

	/**
	 * 模糊查找得到要查找的键值
	 * @param hm 得到现有的HashMap
	 * @param id 要查找的内容
	 * @return
	 */
	private String getKey(HashMap hm, String id) {
		Set s = hm.keySet();
		Iterator i = s.iterator();
		String str = "";
		while (i.hasNext()) {
			str = (String) i.next();
			if (str.indexOf(id) != -1) {
				break;
			}
		}
		return str;
	}

	/**
	 * 取得所有检索信息
	 * @param hm 得到现有的HashMap
	 * @param id 要查找的内容
	 * @return
	 */
	private ArrayList getSearch(HashMap hm, String id) {
		Set s = hm.keySet();
		Iterator i = s.iterator();
		String str = "";
		ArrayList al = new ArrayList();
		while (i.hasNext()) {
			str = (String) i.next();
			if (str.indexOf(id) != -1) {
				al.add(hm.get(str));
			}
		}
		return al;
	}

	/**
	 * 
	 * @param hm 得到现有的HashMap
	 * @param id 查找更新的ID号
	 * @return
	 */
	private String getUpdate(HashMap hm, String id) {
		Set s = hm.keySet();
		Iterator i = s.iterator();
		String str = "";
		while (i.hasNext()) {
			str = (String) i.next();
			if (str.indexOf(id) != -1) {
				String st[] = str.split(":");
				if (st[0].equalsIgnoreCase(id)) {
					break;
				}

			} else {
				str = "nothing";
			}
		}
		return str;
	}

	/**
	 * 写入文件
	 * @param hm 得到现有的HashMap
	 * @param p 得到Person实例,用于找到文件名
	 * @return
	 */
	private boolean writeFile(HashMap hm, Person p) {
		String filename = p.getClass().getName();
		File f = new File(subString(filename) + ".info");
		return FileOperate.writeFile(hm, f);
	}

	/**
	 * 截取字符串
	 * @param name 完整的类名
	 * @return
	 */
	private String subString(String name) {
		return name.substring(name.lastIndexOf(".") + 1);
	}

	/**
	 * 生成HashMap的键值
	 * @param p 要操作的Person类型
	 * @return
	 */
	private String createKey(Person p) {
		String temp_key = "";
		if (p instanceof Student) {
			Student s = (Student) p;
			temp_key = s.getId() + ":" + s.getName() + ":" + s.getAge();
		}
		if (p instanceof Worker) {
			Worker w = (Worker) p;
			temp_key = w.getId() + ":" + w.getName() + ":" + w.getAge();
		}
		return temp_key;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -