import java.security.PublicKey;
import java.util.Date;
import javax.naming.directory.SearchControls;
import org.omg.CORBA.PRIVATE_MEMBER;
interface Pet{
public String getName();
public String getColor();
public int getAge();
}
class Link{
private class Node{
private Object data;//保存节点数据
private Node next;//保存节点关系
public Node (Object data) {
this.data = data;
}
//第一次调用:this = Link.root
//第二次调用:this = Link.root.next
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.foot++] = this.data;//数据丢数组,脚标加1
if (this.next!=null) {
this.next.toArrayNode();
}
}
public boolean containNode(Object search){
if (search.equals(this.data)) {//找到了
return true;
}else {
if (this.next!=null){
return this.next.containNode(search);
}else{
return false;
}
}
}
public Object getNode(int index){
if (Link.this.foot++ == index) {
return this.data;
}else {
System.out.println("=");
this.next.getNode(index);
}
return null;
}
public void setNode(int index,Object newData){
if (Link.this.foot ++ == index) {
this.data = newData;
return;
}else {
if(this.next != null){
this.next.setNode(index, newData);
}
}
}
public void removeNode(Node previos,Object data) {
//第一次调用:Link.root.next;
if (this.data.equals(data)) {//当前节点为删除节点
previos.next = this.next;
System.out.println("删除成功啦");//删除当前,对象失去引用,GC处理
}else {
this.next.removeNode(this,data);
}
}
}
//--------------以下为Link类定义-------------
private Node root;//定义根节点
private int count = 0;//保存计数 ,当前的保存个数
private Object[] retData;
private int foot = 0;
public void add(Object data){
if (data == null) {
return;
}
Node newNode = new Node(data);
if(this.root == null){
this.root = newNode;
}else{//根节点已经存在
//应该把此时的节点顺序的处理交给Node类
this.root.addNode(newNode);
}
this.count++;
}
public int size(){
return this.count;
}
public boolean isEmpty(){
return this.root==null;
}
public Object[] toArray(){//转换成为数组的方法
if (this.count == 0 ) {//链表为空,返回null
return null;
}
this.retData = new Object[this.count];//创建数组retData用来装数据
this.foot=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.containNode(search);
}
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 newData){
if(index>=this.count){//超过了保存的个数
return; //结束
}
this.foot=0;
this.root.setNode(index,newData);
}
public void remove(Object data){
if(this.contains(data)){//如果存在则删除处理
if(this.root.data.equals(data)){//先判断是不是根节点
this.root = this.root.next;//根节点变为下个节点
}else {//不是根节点则
this.root.next.removeNode(this.root,data);
}
this.count --;
System.out.println("删除成功");
}
}
}
/*
* 猫
*/
class Cat implements Pet{
private String name;
private String color;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Cat(String name,int age,String color){
this.name = name;
this.age = age;
this.color = color;
}
public boolean equals(Object obj){
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (!(obj instanceof Cat)) {
return false;
}
Cat pet = (Cat) obj;
return this.name.equals(pet.name)&&this.age == pet.age&&this.color.equals(pet.color);
}
public String toString(){
return "【猫】名字:"+this.name+" 年龄:"+this.age+" 颜色:"+this.color;
}
}
/*
* 狗
*/
class Dog implements Pet{
private String name;
private String color;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Dog(String name,int age,String color){
this.name = name;
this.age = age;
this.color = color;
}
public boolean equals(Object obj){
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.age == pet.age&&this.color.equals(pet.color);
}
public String toString(){
return "【狗】名字:"+this.name+" 年龄:"+this.age+" 颜色:"+this.color;
}
}
/*
* 宠物店
*/
class PetShop{
private Link pets = new Link();
public void add(Pet pet){
this.pets.add(pet);
}
public void delete(Pet pet){
this.pets.remove(pet);
}
public Link getPets(){
return this.pets;
}
public Link Search(String keyword){
Link result = new Link();
Object [] data = this.pets.toArray();
for (int i = 0; i < data.length; i++) {
Pet pet = (Pet) data[i];
if (pet.getName().contains(keyword) || pet.getColor().contains(keyword)) {
result.add(pet);
}
}return result;
}
}
public class PetStore {
public static void main(String[] args) {
PetShop ps = new PetShop();
ps.add(new Dog("黑狗", 1, "黑色"));
ps.add(new Dog("金毛", 3, "金色"));
ps.add(new Dog("泰迪", 2, "棕色"));
ps.add(new Dog("腊肠", 2, "灰色"));
ps.add(new Cat("加菲猫", 1, "金色"));
ps.add(new Cat("加菲猫", 1, "金色"));
ps.delete(new Dog("金毛", 3, "金色"));
Link all = ps.Search("金");
Object [] data = all.toArray();
for (int i = 0; i < data.length; i++) {
System.out.println(data[i]);
}
}
}