class Link {
class Node {
private Node next;
private Object data;
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 void setNode(int index, Object data) {
if (Link.this.foot++ == index) {
this.data = data;
}
this.next.setNode(index, data);
}
public Object getData(int index) {
if (Link.this.foot++ == index) {
return this.data;
}
return this.next.getData(index);
}
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;
private int foot;
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;
}
return this.root.containsNode(data);
}
public void set(int index, Object data) {
if (index > this.count) {
return;
}
this.foot = 0;
this.root.setNode(index, data);
}
public Object get(int index) {
if (index > this.count) {
return null;
}
this.foot = 0;
return this.root.getData(index);
}
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.retArray = new Object[this.count];
this.root.toArrayNode();
return this.retArray;
}
}
interface Pet {
public abstract String getName();
public abstract int getAge();
}
class PetShop {
private Link pets = new Link();
public void add(Pet pet) {
this.pets.add(pet);
}
public void remove(Pet pet) {
this.pets.remove(pet);
}
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]);
}
}
}