interface ILink{
}
class Link implements ILink{
private class Node{
private Object data;
private Node next;
public Node(Object data){
this.data=data;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
//this=当前节点
public void addNode(Node node){
if(this.next==null){//当前节点的下一个为空
this.next=node;
}else{
this.next.addNode(node);
}
}
public void toArrayNode(){
Link.this.reData[Link.this.foot++]=this.data;
if(this.next!=null){
this.next.toArrayNode();
}
}
public boolean containsNode(Object search){
if(this.data.equals(search)){
return true;
}else{
if(this.next!=null){
return this.next.containsNode(search);
}else{
return false;
}
}
}
public Object getNode(int index){
if(Link.this.foot++==index){
return this.data;
}else{
return this.next.getNode(index);
}
}
public void setNode(int index,Object newdata){
if(Link.this.foot++==index){
this.data=newdata;
}else{
this.next.setNode(index, newdata);
}
}
//previous=this.root
//this=root.next
public void removeNode(Object data,Node previous){
if(this.data.equals(data)){
previous.next=this.next ;
}else{
this.next.removeNode(data, this);
}
}
}
//------------------------------
private Object []reData;
private int foot;
private int count=0;
private Node root;//根节点
public void add(Object data){//数据增加
if(data==null){
return;//方法调用结束
}
Node node=new Node(data);
if(this.root==null){
this.root=node;
}else{
this.root.addNode(node);
}
this.count++;
}
public int size(){//取得元素个数
return this.count;
}
public boolean isEmpty(){//检验数组是否为空
return this.root==null&&this.count==0;
}
public Object [] toArray(){
if(this.count==0){
return null;
}
this.reData=new Object[this.count];//声明数组长度
this.foot=0;
this.root.toArrayNode();
return this.reData;
}
public boolean contains(Object search){
if(search==null&&this.root==null){
return false;
}else{
return this.root.containsNode(search);
}
}
public Object get(int index){
if(index>=this.count){
return "wrong";
}else{
this.foot=0;
return this.root.getNode(index);
}
}
public void set(int index,Object newdata){
if(index>=this.count&&newdata==null){
return;
}else{
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(data, this.root);
}this.count--;
}
}
}
interface Pets{
public String getName();
public String getColor();
public int getAge();
}
class PetShop{
private Link pets=new Link();//开辟一个链表,保存多个宠物
public void add(Pets pet){
this.pets.add(pet);
}
public void delet(Pets 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 x=0;x<data.length;x++){
Pets pet=(Pets) data[x];
if(pet.getName().contains(keyWord)||pet.getColor().contains(keyWord)){
result.add(pet);
}
}
return result;
}
}
class Dog implements Pets{
private String name;
private int age;
private String color;
@Override
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
@Override
public String getColor() {
// TODO Auto-generated method stub
return this.color;
}
@Override
public int getAge() {
// TODO Auto-generated method stub
return this.age;
}
public Dog(String name,String color,int age){
this.name=name;
this.color=color;
this.age=age;
}
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.color+this.age;
}
}
class Cat implements Pets{
private String name;
private int age;
private String color;
@Override
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
@Override
public String getColor() {
// TODO Auto-generated method stub
return this.color;
}
@Override
public int getAge() {
// TODO Auto-generated method stub
return this.age;
}
public Cat(String name,String color,int age){
this.name=name;
this.color=color;
this.age=age;
}
public boolean equals(Object obj){
if(obj==null){
return false;
}if(this==obj){
return true;
}if(!(obj instanceof Dog)){
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.color+this.age;
}
}
//==============================================
public class test2 {
public static void main(String args[]){
PetShop ps=new PetShop();
Dog dog1=new Dog("旺旺","witer",1);
ps.add(dog1);
ps.add(new Dog("hei","black",10));
ps.add(new Dog("金毛","black",10));
ps.add(new Dog("波斯猫","black",10));
ps.add(new Dog("加菲","black",10));
ps.delet(new Dog("金毛","black",10));
Link all=ps.search("witer");
Object[]data=all.toArray();
for(int x=0;x<data.length;x++){
System.out.println(data[x]);
}
Link all1=ps.getPets();
Object []result =all1.toArray();
for(int x=0;x<result.length;x++){
System.out.println(result[x]);
}
}
}
简单链表类(宠物商店模型)
最新推荐文章于 2021-10-21 09:23:20 发布