Java Object综合实战(宠物商店)
最新推荐文章于 2024-11-04 10:51:06 发布

「已注销」 于 2018-03-12 21:10:32 发布
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
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]);
}
}
}

相关知识
链表实战之宠物商店
java宠物商店代码
JAVA—综合练习:宠物商店
Java DTO(data transfer object)的使用
mysql 项目案例宠物商店
Tensorflow Object Detection API 实战教程 宠物与手势识别视频课程【共14课时】
解析宠物商店pet
Java实战项目
12.10综合案例:宠物商店
java宠物商店项目
原文链接: Java Object综合实战(宠物商店) https://www.mcbbbk.com/newsview900921.html
| 上一篇: 《百鬼宠物店[综漫].》镜吉祥 ... | 下一篇: 合并小宠物下载 |





