宠物商店

interface ILink {
public void add(Object data) ; // 这个方法向链表之中保存对象
public int size() ; // 取得链表的长度
public boolean isEmpty() ; // 判断该链表是否有数据
public Object get(int index) ; // 根据索引取得链表中保存的数据
public boolean contains(Object obj) ;// 查询某个数据是否存在
public void remove(Object obj) ; // 删除不需要的数据
public void set(int index,Object data) ; // 根据索引修改指定数据
public void clean() ; // 清空链表
public Object [] toArray() ; // 变为对象数组后输出
}
class LinkImpl implements ILink {
// Node类可以描述出对象保存的先后顺序
private class Node { // 这个类用户不需要知道,只有本类使用
private Object data ; // 保存数据
private Node next ; // 保存下一个节点
// 保证每一个Node创建的时候都有要保存的数据
public Node(Object data) {
this.data = data ;
}
// 第1次执行:this = LinkImpl.root
// 第2次执行:this = LinkImpl.root.next
// 第3次执行:this = LinkImpl.root.next.next
public void addNode(Node newNode) { // 设置节点关系
if (this.next == null) { // 当前节点之后有位置
this.next = newNode ; // 保存新节点
} else { // 如果没有位置
this.next.addNode(newNode) ;
}
}
// 第1次执行:this = LinkImp.root
// 第2次执行:this = LinkImp.root.next
public Object getNode(int index) {
if (LinkImpl.this.foot ++ == index) {
return this.data ;
} else {
return this.next.getNode(index) ;
}
}
// 第1次:this = LinkImpl.root.next,previous = LinkImp.root
public void removeNode(Node previous,Object obj) {
if (this.data.equals(obj)) {
previous.next = this.next ;
} else { // 之前已经判断过要删除的数据存在
this.next.removeNode(this,obj) ;
}
}
public boolean containsNode(Object data) {
if (this.data.equals(data)) {
return true ;
} else {
if (this.next != null) {
return this.next.containsNode(data) ;
} else {
return false ;
}
}
}
public void setNode(Object obj,int index) {
if (LinkImpl.this.foot ++ == index) {
this.data = obj ;
} else {
this.next.setNode(obj,index) ;
}
}
public void toArrayNode() {
LinkImpl.this.retObj [LinkImpl.this.foot ++] = this.data ;
if (this.next != null) {
this.next.toArrayNode() ;
}
}
}
// *******************************
private Node root ; // 表示根节点
private int count = 0 ;
private int foot = 0 ; // 查找使用
private Object retObj [] ; // 返回的对象数组
// ************* 覆写接口中的方法 **********
public void add(Object data) {
// 1、数据本身不包含有先后的顺序
// 需要将数据包装在Node类对象之中,因为Node可以描述先后关系
if (data == null) { // 数据为null
return ; // 结束调用
}
// 2、将数据包装在Node类对象之中
Node newNode = new Node(data) ;
// 3、处理根元素
if (this.root == null) { // 现在没有根元素
this.root = newNode ; // 将第一个节点保存为根元素
} else {
this.root.addNode(newNode) ;// 交由Node处理
}
this.count ++ ;
}
public int size() {
return this.count ;
}
public boolean isEmpty() {
return this.count == 0 && this.root == null ;
}
public Object get(int index) {
if (this.isEmpty()) { // 为空元素
return null ;
}
if (index >= this.count) { // 超过了保存数量
return null ;
}
this.foot = 0 ;
return this.root.getNode(index) ;
}
public boolean contains(Object obj) {
if (this.isEmpty()) {
return false ;
}
return this.root.containsNode(obj) ;
}
public void remove(Object obj) {
if (!this.contains(obj)) { // 数据不存在
return ; // 结束方法
}
// 链表里面最关注的是根节点
if (this.root.data.equals(obj)) { // 根节点要删除
this.root = this.root.next ; // 下一个作为根节点
} else { // 根节点判断完了
this.root.next.removeNode(this.root,obj) ;
}
this.count -- ;
}
public void set(int index,Object data){
this.foot = 0 ;
if (this.isEmpty()) { // 为空元素
return ;
}
if (index >= this.count) { // 超过了保存数量
return ;
}
this.root.setNode(data,index) ;
}
public void clean() {
this.root = null ;
this.count = 0 ;
}
public Object [] toArray() {
if (this.isEmpty()) {
return null ;
}
this.foot = 0 ;
this.retObj = new Object [this.count] ;
this.root.toArrayNode() ;
return this.retObj ;
}
}
interface Pet{//定义宠物标准
public String getName();
public int getAge();
}
class PetShop
{
private ILink allPets;
public PetShop(){
this.allPets = new LinkImpl();
}
public void add(Pet pet){
this.allPets.add(pet);
}
public void delete(Pet pet){
this.allPets.remove(pet);
}
public ILink search(String keyWord){
ILink result=new LinkImpl();
Object obj[]=this.allPets.toArray();
for (int x=0;x<obj.length ;x++ )
{
Object o = obj[x];
Pet p = (Pet)o;
if(p.getName().contains(keyWord)){
result.add(p);
}
}
return result;
}
}
class Dog implements Pet
{
private String name;
private int age;
public Dog(String name,int age){
this.name=name;
this.age=age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof Dog)){
return false;
}
Dog p = (Dog)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
public String toString(){
return"狗名字:"+this.name+",年龄:"+this.age;
}
}
class Cat implements Pet
{
private String name;
private int age;
public Cat(String name,int age){
this.name=name;
this.age=age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.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;
return this.name.equals(c.name)&&this.age==c.age;
}
public String toString(){
return"猫名字:"+this.name+",年龄:"+this.age;
}
}
public class Test
{
public static void main(String args[]){
PetShop shop=new PetShop();
shop.add(new Dog("黑毛",1));
shop.add(new Dog("小毛",1));
shop.add(new Dog("大毛",1));
shop.add(new Dog("毛毛", 1));
shop.add(new Cat("黑小猫",1));
shop.add(new Cat("黑大猫",1));
ILink result = shop.search("黑");
Object obj[]=result.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、付费专栏及课程。

余额充值