要求实现以下需求:
- 上架宠物;
- 下架宠物:按具体对象删除,按关键字删除;
- 搜索:按关键字搜索,按种类搜索;
特别要求:Node中保存的数据data类型为Object。
初版代码如下:
class Link{
//------------------一下为Node类------------------
private class Node{
private Object data;
private Node next;
public Node(){}
public Node(Object data){
this.data = data;
}
public void removeNode(Object keyword){
Node now = Link.this.root.next;
Node bef = Link.this.root;
//第一次:this = this.root.next;
while(now != null){
if(now.data.equals(keyword)){
bef.next = now.next;
Link.this.count--;
}else{
bef = now;
}
now = now.next;
}
return;
}
public void deletNode(Object keyword){
Node now = Link.this.root.next;
IPet n = (IPet)now.data; //强制Object转型为IPet
Node bef = Link.this.root;
IPet b = (IPet)bef.data; //强制Object转型为IPet
//第一次:this = this.root.next;
while(now != null){
if(n.containsPet(keyword)){
bef.next = now.next;
Link.this.count--;
}else{
bef = now;
}
now = now.next;
if(now != null){
n = (IPet)now.data;
}
}
return;
}
public void toArrayNode(){
Node now = Link.this.root;
while(Link.this.foot<Link.this.count){
Link.this.array[Link.this.foot++] = now.data;
now = now.next;
}
}
}
//------------------一下为Link类------------------
private Node root;
private Node last;
int foot;
private int count;
private Object[] array;
public void add(Object data){
if(data == null){
System.out.println("添加失败:添加数据不能为空!");
return;
}
Node newNode = new Node(data);
if(this.root == null){
this.root = newNode;
this.last = newNode;
}else{
this.last.next = newNode;
this.last = newNode;
}
this.count++;
}
public void remove(Object keyword){ //删除该具体对象
if(this.count == 0){
return;
}
while(this.root.data.equals(keyword)){
this.root = this.root.next;
count--;
}
this.root.next.removeNode(keyword);
}
public void delet(Object keyword){ //删除含关键字对象
if(this.count == 0){
return;
}
//强制Object转型为IPet才能调用Ipet的方法containsPet
IPet r = (IPet)this.root.data;
while(r.containsPet(keyword)){
this.root = this.root.next;
//若root更改后为空,则不再强制转型,避免空指针异常
if(this.root != null){
r = (IPet)this.root.data;
}
count--;
}
this.root.next.deletNode(keyword);
}
public Object[] toArray(){
if(this.count == 0){
return null;
}
foot = 0;
array = new Object[this.count];
this.root.toArrayNode();
return array;
}
public void print(){
System.out.println(this.root.next.next.data);
}
}
interface IPet{
public String getName();
public int getAge();
public String getColor();
public String getType();
public boolean containsPet(Object keyword);
}
//宠物商店类
class PetShop{
private Link pets = new Link();
public PetShop(){}
public void add(IPet pet){ //添加宠物(商店只关心是否为宠物,不关心宠物种类)
this.pets.add(pet); //添加到链表中
}
public void remove(Object pet){
this.pets.remove(pet); //从链表删除完全匹配的对象
}
public void delet(Object pet){
this.pets.delet(pet); //从链表删除含关键字的对象
}
public Link search(String keyword){ //查找含关键字的所有宠物
Link result = new Link(); //result为符合标准的所有宠物的链表结果
Object[] data = this.pets.toArray();
for(Object i:data){
IPet pet = (IPet)i; //转型
if(pet.getName().contains(keyword)||pet.getColor().contains(keyword)){
result.add(pet);
}
}
return result;
}
public Link search(int keyword){ //查找含关键字的所有宠物
Link result = new Link(); //result为符合标准的所有宠物的链表结果
Object[] data = this.pets.toArray();
for(Object i:data){
IPet pet = (IPet)i; //转型
if(pet.getAge() == keyword){
result.add(pet);
}
}
return result;
}
public Link searchType(String keyword){ //查找含关键字的所有宠物
Link result = new Link(); //result为符合标准的所有宠物的链表结果
Object[] data = this.pets.toArray();
for(Object i:data){
IPet pet = (IPet)i; //转型
if(pet.getType().contains(keyword)){
result.add(pet);
}
}
return result;
}
public Link getAllPets(){ //取得所有宠物
return this.pets;
}
}
//猫类
class Cat implements IPet{
private String name;
private int age;
private String color;
//设置统一所属物种以便searchType查询归类
public static final String TYPE = "Catcat小猫猫类猫咪";
public Cat(String name,int age,String color){
this.name = name;
this.age = age;
this.color = color;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public void setColor(String color){
this.color = color;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public String getColor(){
return this.color;
}
public String getType(){
return this.TYPE;
}
public String toString(){ //覆写toString()方法
return "【猫】 姓名:"+this.name+"\t"+" | 年龄:"+this.age+"\t"+" | 颜色:"+this.color;
}
public boolean equals(Object obj){ //覆写equals()方法给Link调用
if(this == obj){
return true;
}
if(obj == null){
return false;
}
if(!(obj instanceof Cat)){
return false;
}
Cat pet = (Cat) obj; //转型
return pet.name.equals(this.name)&&pet.age == this.age&&pet.color.equals(this.color);
}
public boolean containsPet(Object keyword){
String obj = String.valueOf(keyword);//强制转型为String
if(obj == null){
return false;
}
//如果包含关键字段,则返回true
return this.name.contains(obj)||this.color.contains(obj)||String.valueOf(this.age).equals(obj);
}
}
//狗类
class Dog implements IPet{
private String name;
private int age;
private String color;
public static final String TYPE = "Dogdog小狗狗类";
public Dog(String name,int age,String color){
this.name = name;
this.age = age;
this.color = color;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public void setColor(String color){
this.color = color;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public String getColor(){
return this.color;
}
public String getType(){
return this.TYPE;
}
public String toString(){ //覆写toString()方法
return "【狗】 姓名:"+this.name+"\t"+" | 年龄:"+this.age+"\t"+" | 颜色:"+this.color;
}
//覆写equals()方法给Link调用
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;
}
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 boolean containsPet(Object keyword){
String obj = String.valueOf(keyword);
if(obj == null){
return false;
}
return this.name.contains(obj)||this.color.contains(obj)||String.valueOf(this.age).equals(obj);
}
}
public class TestDemo{
public static void main(String[] args){
PetShop ps = new PetShop();
ps.add(new Dog("金毛",1,"金色"));
ps.add(new Dog("金毛",1,"黄色"));
ps.add(new Dog("雪纳瑞",2,"白色"));
ps.add(new Dog("拉布拉多",5,"金色"));
ps.add(new Dog("哈士奇",3,"白色"));
ps.add(new Dog("泰迪",1,"黑色"));
ps.add(new Dog("金毛",1,"金色"));
ps.add(new Cat("加菲",2,"金色"));
ps.add(new Cat("田园猫",1,"黄色"));
ps.add(new Cat("暹罗",7,"白色"));
ps.add(new Cat("蓝猫",5,"蓝色"));
System.out.println("**************************************************************");
System.out.println("显示所有类:");
Object[] all = ps.getAllPets().toArray();
if(all != null){
for(Object i: all){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
System.out.println("**************************************************************");
System.out.println("搜索含金字的:");
Object[] gold = ps.search("金").toArray();
if(gold != null){
for(Object i: gold){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
System.out.println("**************************************************************");
System.out.println("删除完全符合:‘金毛’,1,‘金色’的类");
ps.remove(new Dog("金毛",1,"金色"));
all = ps.getAllPets().toArray();
if(all != null){
for(Object i: all){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
System.out.println("**************************************************************");
System.out.println("删除泰迪类");
ps.remove(new Dog("泰迪",1,"黑色"));
all = ps.getAllPets().toArray();
if(all != null){
for(Object i: all){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
System.out.println("**************************************************************");
System.out.println("显示所含白字的:");
ps.delet("白");
all = ps.getAllPets().toArray();
if(all != null){
for(Object i: all){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
System.out.println("**************************************************************");
System.out.println("删除2岁的:");
ps.delet(2);
all = ps.getAllPets().toArray();
if(all != null){
for(Object i: all){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
System.out.println("**************************************************************");
System.out.println("显示所有的猫:");
Object[] cat = ps.searchType("猫咪").toArray();
if(cat != null){
for(Object i: cat){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
System.out.println("**************************************************************");
System.out.println("搜索1岁的:");
Object[] year = ps.search(1).toArray();
if(year != null){
for(Object i: year){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
System.out.println("**************************************************************");
System.out.println("搜索2岁的:");
year = ps.search(2).toArray();
if(year != null){
for(Object i: year){
System.out.println(i);
}
}else{
System.out.println("没有匹配数据!");
}
}
}
其中:
System.out.println("显示所含白字的:");
应为:
System.out.println("删除所含白字的:");
打印信息错误。
运行结果如下: