综合接口训练(宠物商店)

class Link {
	private class Node {
		private Object data;
		private Node next;
		public Node(Object data) {
			this.data = data;
		}
		public void addNode(Node newNode) {
			if (this.next == null) {
				this.next = newNode;
			} else {
				this.next.addNode(newNode);
			}
		}
		public boolean containsNode(Object data) {
			if (data.equals(this.data)) {
				return true;
			} else {
				if (this.next != null) {
					return this.next.containsNode(data);
				} else {
					return false;
				}
			}
		}
		public Object getNode(int index) {
			if (Link.this.foot ++ == index) {
				return this.data;
			} else {
				return this.next.getNode(index);
			}
		}
		public void setNode(int index, Object data) {
			if (Link.this.foot ++ == index) {
				this.data = data;
			} else {
				 this.next.setNode(index,data);
			}
		}
		public void removeNode(Node previous,Object data) {
			if (data.equals(this.data)) {
				previous.next = this.next;
			} else {
				 this.next.removeNode(this,data);
			}
		}
		public void toArrayNode() {
			Link.this.retArray[Link.this.foot++] = this.data;
		    if (this.next != null) {
				 this.next.toArrayNode();
			}
		}
	}
	private Node root;
	private int count = 0;
	private int foot = 0;//节点索引
	private Object [] retArray;
	public void add(Object data) {
		if (data == null) {
			return;
		}
		Node newNode = new Node(data);
		if (this.root == null) {
			this.root = newNode;
		} else {
			this.root.addNode(newNode);
		}
		this.count++;
	}
	public int size() {
		return this.count;
	}
	public boolean isEmpty() {
		return this.count == 0;
	}
	public boolean contains(Object data) {
		if ((data == null) ||
			(this.root == null)) {
			return false;
		} else {
			return this.root.containsNode(data);
		}
	}
	public Object get(int index) {
		if (index > this.count) {
			return null;
		}
		this.foot = 0;
		return this.root.getNode(index);
	}
	public void set(int index, Object data) {
		if (index > this.count) {
			return;
		}
		this.foot = 0;
		this.root.setNode(index, data);
	}
	public void remove(Object data) {
		if (this.contains(data)) {
			if (data.equals(this.root.data)) {
				this.root = this.root.next;
			} else {
				this.root.next.removeNode(this.root, data);
			}
			this.count--;
		}
	}
	public Object[] toArray() {
		if (this.root == null) {
			return null;
		}
		this.foot = 0;
		this.retArray = new Object[this.count];
		this.root.toArrayNode();
		return this.retArray;
	}
	public void clear() {
		this.root = null;
		this.count = 0;
	}
}

interface Pet {
	public abstract String getName();//定义宠物商店数据操作的标准
	public abstract int getAge();//定义宠物标准。商店所有进出的事物都是以此作为标准
}
class PetShop {
	Link pets = new Link();//实例化对象一个宠物商店有多个宠物,用链表保存
	public void add(Pet pet) {
		this.pets.add(pet);
	}
	public void delete(Pet pet) {
		this.pets.remove(pet);
	}
	public Link search(String KeyWord) { //模糊查询结果包含多个对象,所以返回值用链表
		Link result = new Link();//保存查询结果
		Object obj[] = this.pets.toArray();
		for (int x = 0; x < obj.length; x++ ) {
			Pet p = (Pet) obj[x];//先向上后向下转换找到具体的宠物对象
			if (p.getName().contains(KeyWord)) {
				result.add(p);
			}
		}
		return result;
	}
}
class Cat implements Pet {
	private String name;
	private int age;
	public Cat(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Cat)) {
			return false;
		}
		Cat c = (Cat) obj;
		if ((this.name.equals(c.name)) &&
			(this.age == c.age)) {
			return true;
		}
		return false;
	}
	public String getName() {
		return this.name;
	}
	public int getAge() {
		return this.age;
	}
	public String toString() {
		return "猫的名字:" + this.name +" 猫的年龄" + this.age;
	}
}
class Dog implements Pet {
	private String name;
	private int age;
	public Dog(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Dog)) {
			return false;
		}
		Dog d = (Dog) obj;
		if ((this.name.equals(d.name)) &&
			(this.age == d.age)) {
			return true;
		}
		return false;
	}
	public String getName() {
		return this.name;
	}
	public int getAge() {
		return this.age;
	}
	public String toString() {
		return "狗的名字:" + this.name +" 狗的年龄" + this.age;
	}
}
public class TestLink {
	public static void main(String args[]) {
		PetShop shop = new PetShop();
		shop.add(new Cat("猫A",1));
		shop.add(new Cat("猫B",2));
		shop.add(new Dog("狗啊A",1));
		shop.add(new Dog("狗啊B",2));
		shop.delete(new Cat("猫A",1));
		Link all = shop.search("啊");
		Object obj[] = all.toArray();
		for (int x = 0; x < obj.length; x++ ) {
			System.out.println(obj[x]);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值