Java Object综合实战(宠物商店)

/*
 * 链表结构
 */
class Link {
	class Node {
		private Node next;// 下一个节点
		private Object data;// 数据

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

		// 1.添加节点
		public void addNode(Node newNode) {
			if (this.next == null) {
				this.next = newNode;
			} else {
				this.next.addNode(newNode);
			}
		}

		// 2.数据查询
		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;
				}

			}

		}

		// 3.修改数据
		public void setNode(int index, Object data) {
			if (Link.this.foot++ == index) {
				this.data = data;
			}
			this.next.setNode(index, data);
		}

		// 4.获取数据
		public Object getData(int index) {
			if (Link.this.foot++ == index) {
				return this.data;
			}
			return this.next.getData(index);
		}

		// 5.删除数据
		public void removeNode(Node previous, Object data) {
			if (data.equals(this.data)) {
				previous.next = this.next;
			} else {
				this.next.removeNode(this, data);
			}
		}

		// 6.对象数组
		public void toArrayNode() {
			Link.this.retArray[Link.this.foot++] = this.data;
			if (this.next != null) {
				this.next.toArrayNode();
			}
		}

	}

	// ===================以上是内部类=========================
	private Node root;// 根节点
	private int count;// 计数器
	private int foot;// 脚标
	private Object[] retArray;// 对象数组
	// 1.添加数据

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

	// 2.链表长度
	public int size() {
		return this.count;
	}

	// 3.链表是否为空
	public boolean isEmpty() {
		return this.count == 0;
	}

	// 4.链表查询
	public boolean contains(Object data) {
		if (data == null || this.root == null) {
			return false;
		}
		return this.root.containsNode(data);
	}

	// 5.修改数据
	public void set(int index, Object data) {
		if (index > this.count) {
			return;
		}
		this.foot = 0;
		this.root.setNode(index, data);
	}

	// 6.获取数据
	public Object get(int index) {
		if (index > this.count) {
			return null;
		}
		this.foot = 0;
		return this.root.getData(index);
	}

	// 7.删除数据
	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--;
	}

	// 8.对象数组
	public Object[] toArray() {
		if (this.root == null) {
			return null;
		}
		this.retArray = new Object[this.count];
		this.root.toArrayNode();
		return this.retArray;
	}

}

/*
 * 宠物接口
 */
interface Pet {
	public abstract String getName();

	public abstract int getAge();
}

/*
 * 宠物商店类 功能: 1.宠物上架 ;2.删除宠物信息;3.模糊查询
 */
class PetShop {
	private Link pets = new Link();

	// 1.上架
	public void add(Pet pet) {
		this.pets.add(pet);
	}

	// 2.删除
	public void remove(Pet pet) {
		this.pets.remove(pet);
	}

	// 3.模糊查询
	public Link search(String keyword) {
		Link result = new Link();// 存放查询结果
		Object[] obj = this.pets.toArray();// 对象数组
		for (int i = 0; i < obj.length; i++) {
			Pet p = (Pet) obj[i];// 向下转型
			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;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public int getAge() {
		return this.age;
	}

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

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

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public int getAge() {
		return this.age;
	}

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

	@Override
	public String toString() {
		return "狗的名字:" + this.name + ",狗的年龄:" + this.age;
	}
}

public class Demo {
	public static void main(String[] args) {
		PetShop all=new PetShop();
		all.add(new Cat("加菲猫", 5));
		all.add(new Cat("秋山猫", 5));
		all.add(new Cat("无毛猫", 7));
		all.add(new Dog("柴犬", 4));
		all.add(new Dog("秋田犬", 3));
		all.add(new Dog("猎犬", 6));
		Link link=all.search("秋");
		Object[]obj=link.toArray();
		for (int i = 0; i < obj.length; i++) {
			System.out.println(obj[i]);
		}
	}

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值