链表加雇员部门模型

class Link { // 外部类
	private int length; // 链表长度
	private Node root; // 根节点
	private int foot; // 操作数组的角标
	private Emp[] retData; // 返回数组

	private class Node { // 私有内部类

		private Emp data;
		private Node next; // 下一个节点

		public Node(Emp data) {
			this.data = data;
		}

		public void addNode(Node newNode) {
			if (this.next == null) {
				this.next = newNode;
			} else {
				this.next.addNode(newNode);
			}
		}

		public boolean containNode(Emp data) {
			if (data.compare(this.data)) { // 如果与当前节点数据相符
				return true;
			} else {
				if (this.next != null) { // 不相符时
					return this.next.containNode(data);
				} else {
					return false;
				}
			}

		}

		public void removeNode(Node previous, Emp data) {
			if (this.data.compare(data)) {
				previous.next = this.next;
			} else {
				this.next.removeNode(this.next, data);
			}

		}

		public void toArrayNode() {
			Link.this.retData[Link.this.foot++] = this.data;
			if (this.next != null) {
				this.next.toArrayNode();
			}

		}

		public Emp getNode(int index) {
			if (Link.this.foot++ == index) {
				return this.data;
			} else {
				return this.next.getNode(index);
			}
		}
	} // end class Node

	public boolean add(Emp data) {
		if (data == null) {
			return false;
		}
		Node newNode = new Node(data);
		if (this.root == null) {
			this.root = newNode;
		} else {
			this.root.addNode(newNode);
		}
		length++;

		return true;

	}

	public boolean addAll(Emp[] data) {

		for (int index = 0; index < data.length; index++) {
			if (!this.add(data[index])) {
				return false;
			}
		}

		return true;
	}

	public int size() {
		return length;
	}

	public boolean isEmpty() {
		if (this.root == null) {
			return false;
		}
		return true;

		// 最简单的一句 return this.length==0;
	}

	public boolean contains(Emp data) {
		if (this.root == null || data == null) {
			return false;
		}
		return this.root.containNode(data);

	}

	public void remove(Emp data) {
		if (!contains(data)) {
			return;
		}
		if (data.equals(this.root.data)) {
			this.root = this.root.next;

		} else { // 如果要删除的不是根节点
			this.root.next.removeNode(this.root, data);
		}

		this.length--;

	}

	public Emp[] toArray() {
		if (this.length == 0) {
			return null;
		}
		this.foot = 0;
		this.retData = new Emp[this.length];
		this.root.toArrayNode();

		return this.retData;

	}

	public void print() {
		Emp[] str = toArray();
		for (int i = 0; i < str.length; i++) {
			System.out.println(str[i].getEmpInfo());
		}

	}

	public Emp get(int index) {
		if (index > this.length) {
			return null;
		}
		this.foot = 0;
		return this.root.getNode(index);
	}

	public void clear() {
		this.root = null;
		this.length = 0;
	}

}

class Dept { // 部门模型
	private Link emps = new Link();

	private int deptno;
	private String dname;
	private String loc;

	public Dept() {
	}

	public Dept(int deptno, String dname, String loc) {
		this.deptno = deptno;
		this.dname = dname;
		this.loc = loc;
	}

	public void setEmps(Link emps) {
		this.emps = emps;
	}

	public Link getEmps() {
		return this.emps;
	}

	public boolean compare(Dept dept) {
		if (this == dept) {
			return true;
		}
		if (dept == null) {
			return false;
		}
		if (this.deptno == dept.deptno && this.dname.equals(dept.dname)
				&& this.loc.equals(dept.loc)) {
			return true;
		}
		return false;
	}

	public String getDeptInfo() {
		return "部门编号:" + this.deptno + ",名称:" + this.dname + ",位置:" + this.loc;
	}

}

class Emp { 	//雇员模型
	private int empno;
	private String ename;
	private String job;
	private double sal;
	private double comm;

	private Emp mgr;
	private Dept dept;

	public Emp(int empno, String ename, String job, double sal, double comm) {
		this.empno = empno;
		this.ename = ename;
		this.job = job;
		this.sal = sal;
		this.comm = comm;
	}

	public boolean compare(Emp emp) {

		if (this == emp) {
			return true;
		}
		if (emp == null) {
			return false;
		}

		if (this.empno == emp.empno 
				&& this.ename.equals(emp.ename)
				&& this.job.equals(emp.job) 
				&& this.sal == emp.sal
				&& this.comm == comm) {
			return true;
		}
		return false;

	}

	public void setMgr(Emp mgr) {
		this.mgr = mgr;
	}

	public Emp getMgr() {
		return this.mgr;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Dept getDept() {
		return this.dept;
	}

	public String getEmpInfo() {
		return "雇员编号:" + this.empno + ",姓名:" + this.ename + ",职位:" + this.job
				+ ",工资:" + this.sal + ",佣金:" + this.comm;
	}
}

public class Test2 {
	public static void main(String[] args) {
		Emp ea = new Emp(9527,"TIM","CLEAR",800.0,0.0);  //设置雇员
		Emp eb = new Emp(9528,"RED","MANAGER",2450.0,0.0);
		Emp ec = new Emp(9529,"SMALLLIGHT","PERSIDENT",5000.0,300.0);
		Dept dept = new Dept(10, "TEST_DEPT", "SHENZHEN");  //设置部门
		ea.setMgr(eb);  //设置领导
		eb.setMgr(ec);
		ea.setDept(dept);
		eb.setDept(dept);
		ec.setDept(dept);
		
		dept.getEmps().add(ea);
		dept.getEmps().add(eb);
		dept.getEmps().add(ec);
		
	System.out.println(dept.getDeptInfo());
	dept.getEmps().print();
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值