【Java基础】【用链表实现一个宠物商店】

/*
综合案例,宠物商店

要求:
实现要销售宠物的上架,下架,关键字查询

要求:
描述出程序的关系即可

宠物信息:
名字,年龄,颜色

一个宠物商店 -- 多种宠物(一对多关系映射)

*/
class Link {
	private Node root; //根节点,增加数据函数添加
	private int count; //统计元素个数
	private Object[] retData; //返回对象数组
	private int index = 0; //操作游标
	// 定义Node内部类,表示Node只为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 void toArrayNode() {
			Link.this.retData[Link.this.index ++] = this.data;//先把root节点的数据取出,然后游标加一
			if (this.next != null){ //如果下个节点还有数据,则递归获取
				this.next.toArrayNode();
			}
		}
		//查找数据
		public boolean containsNode(Object search) {
			if (search.equals(this.data)) {
				return true; //找到数据
			} else {
				if (this.next != null) { //还有后续节点
					return this.next.containsNode(search);//递归查找
				} else {
					return false;
				}
			}
		}
		//根据索引取得数据
		public Object getNode(int searchIndex) {
			if (Link.this.index ++ == searchIndex) {
				return this.data;
			} else {
				this.next.getNode(searchIndex);
			}
			return null;
		}
		//修改指定索引的数据
		public void setNode(int searchIndex, Object newData) {
			if (Link.this.index ++ == searchIndex) {
				this.data = newData;
			} else {
				if (this.next != null) {
					this.next.setNode(searchIndex,newData);
				}
			}
		}
		//删除元素
		public void removeDataNode(Node previous, Object data) {
			if (this.data.equals(data)) {
				previous.next = this.next; //中间是this,如果this的data是所要删除的data,那么把this之前的一个node指向this之后的一个node
			} else {
				this.next.removeDataNode(this, data);
			}
		}
	}
	// ---------以下是Link类定义-----------
	//增加元素
	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() {
		if (this.root == null && this.count == 0 ) {
			return false;
		} else {
			return true;
		}
	}
	//链表转换成对象数组
	public Object[] toArray() {
		if (this.count == 0) { //如果链表没有数据,那么就返回null
			return null;
		}
		this.retData = new Object[this.count];//如果count不为零,那么开辟指定空间的对象数组
		this.index = 0;//游标初始化为0
		this.root.toArrayNode();//交给Node类进行数据的取出
		return this.retData;//返回对象数组
	}
	//查找数据
	public boolean contains(Object search) {
		if (search == null || this.root == null) {
			return false;
		}
		return this.root.containsNode(search);
	}
	//根据索引取得数据
	public Object get(int searchIndex) {
		if (searchIndex >= this.count) {
			return null;
		}
		this.index = 0;
		return this.root.getNode(searchIndex);
	}
	//修改指定索引的数据
	public void setData(int searchIndex, Object newData) {
		if (searchIndex >= this.count) {
			return ;
		} else {
			this.index = 0;
			this.root.setNode(searchIndex, newData);
		}
	}
	//删除数据
	public void removeData(Object data) {
		if (this.contains(data)) {
			if (this.root.data.equals(data)) {
				this.root = this.root.next;
			} else {
				this.root.next.removeDataNode(this.root, data);
			}
			this.count --;
		}
	}
			
}

class Factory {
	public static Link getInstance() {
		return new Link();
	}
	public static PetShop getPetShopInstance() {
		return new PetShop();
	}
}
//以下为宠物案例

//宠物接口标准
interface IPet {
	public String getName() ;
	public String getColor() ;
	public int getAge() ;
}
//宠物商店类
class PetShop {
	private Link pets = Factory.getInstance() ;
	public void add(IPet pet) {
		this.pets.add(pet) ;
	}
	public void delete(IPet pet) {
		this.pets.removeData(pet) ;
	}
	public Link search(String keyWord) {
		Link result = Factory.getInstance() ; //保存查询结果
		Object[] data = this.pets.toArray() ;
		for (Object x : data) {
			IPet pet = (IPet) x; //向上转型
			if (pet.getName().contains(keyWord) || pet.getColor().contains(keyWord)) {
				result.add(pet) ;
			}
		}
		return result ;
	}
	public Link getAllPets() {
		return this.pets;
	}
}
//定义宠物狗
class Dog implements IPet {
	private String name;
	private String color;
	private int age;
	public Dog(String name, String color, int age) {
		this.name = name;
		this.color = color;
		this.age = age;
	}
	public String getName() {
		return this.name;
	}
	public String getColor() {
		return this.color;
	}
	public int getAge() {
		return this.age;
	}
	public boolean equals(Object obj) {//这个方法给Link链表调用
		if (obj == null) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Dog)) {
			return false;
		}
		Dog pet = (Dog) obj;
		return this.name.equals(pet.name) &&
		this.color.equals(pet.color) &&
		this.age == pet.age;
	}
	public String toString() {
		return "Dog name: " + this.name 
		+ " color : " + this.color
		+ " age : " + this.age;
	}
}
//测试
public class PetDemo {
	public static void main(String[] args) {
		System.out.println("宠物的开张: ");
		PetShop ps = Factory.getPetShopInstance();
		System.out.println("上架宠物狗");
		ps.add(new Dog("A狗","黑色",1));
		ps.add(new Dog("B狗","金色",2));
		ps.add(new Dog("C狗","黑色",3));
		Dog Ddog = new Dog("D狗","黑色",4);
		ps.add(Ddog);
		System.out.println("现在有宠物如下: ");
		Link all1 = ps.getAllPets();
		Object[] data1 = all1.toArray();
		for (Object x : data1) {
			System.out.println(x) ;
		}
		System.out.println("下架D狗");
		ps.delete(Ddog);
		System.out.println("现在有宠物如下: ");
		Link all2 = ps.getAllPets();
		Object[] data2 = all1.toArray();
		for (Object x : data2) {
			System.out.println(x) ;
		}
		//查找功能
		System.out.println("查找黑色宠物如下: ");
		Link all = ps.search("黑色");
		Object[] data = all.toArray();
		for (Object x : data) {
			System.out.println(x) ;
		}
	}
}
			

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值