package com.company;
class Book {
private String name;
private int price;
public Book(String name, int price) {
this.name = name;
this.price = price;
}
public String toString() {
return this.name + " " + this.price;
}
public boolean equals(Object object) {
if (object instanceof Book) {
if (this == object) {
return true;
} else if (object == null) {
return false;
} else if (this.name.equals(((Book) object).name) && this.price == ((Book) object).price) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
public class Main {
public static void main(String[] args) {
Book bookA = new Book("JAVA",100);
Book bookB = new Book("Python",100);
System.out.println(bookA);
System.out.println(bookB);
System.out.println(bookA.equals(bookB));
}
}
package com.company;
class Link {
private class Node {
private Object data;
private Node next;
public Node(Object data) {
this.data = data;
}
public void relation(Node newNode) {
if (this.next == null) {
this.next = newNode;
} else {
this.next.relation(newNode);
}
}
public boolean contains(Object data) {
if (data.equals(this.data)) {
return true;
} else {
if (this.next == null) {
return false;
} else {
return this.next.contains(data);
}
}
}
public Object getNode(int index){
if(Link.this.foot == index){
return this.data;
}
else{
foot ++;
return this.next.getNode(index);
}
}
public void setNode(int index, Object data){
if(Link.this.foot == index){
this.data = data;
}
else{
foot ++;
this.next.setNode(index,data);
}
}
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 = 0;
private int foot;
private Object [] retArray;
public int getCount() {
return this.count;
}
public boolean isEmpty() {
if (this.count == 0) {
return true;
} else {
return false;
}
}
public void add(Object data) {
if (data == null) {
return;
}
Node newNode = new Node(data);
if (this.root == null) {
this.root = newNode;
} else {
this.root.relation(newNode);
}
this.count++;
}
public boolean contains(Object data) {
if (this.root == null || data == null) {
return false;
}
return this.root.contains(data);
}
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 data){
if(index > this.count){
return;
}
this.foot = 0;
this.root.setNode(index, data);
}
public void remove(Object data){
if(Link.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[] getRetArray() {
if(this.root == null){
return null;
}
this.foot = 0;
this.retArray = new Object[this.count];
this.root.toArrayNode();
return retArray;
}
}
interface Animals{
public String getName();
public int getAge();
}
class Zoo{
private Link animals = new Link();
public Link getAnimals(){
return this.animals;
}
public void add(Animals animals){
this.animals.add(animals);
}
public void remove(Animals animals){
this.animals.remove(animals);
}
public Link search(String keyWord){
Link result = new Link();
Object object [] = this.animals.getRetArray();
for(int i = 0; i < object.length; i ++){
Animals animals = (Animals)object[i];
if(animals.getName().contains(keyWord)){
result.add(animals);
}
}
return result;
}
}
class Tiger implements Animals{
private String name;
private int age;
public Tiger(String name, int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public String toString(){
return "name:" + " " + this.name + " " + "age:" + " " + this.age;
}
public boolean equals(Object object){
Tiger tiger = (Tiger) object;
if(this == object){
return true;
}
else if(object == null){
return false;
}
else if(!(object instanceof Tiger)){
return false;
}
else if(this.name.equals(tiger.name) && this.age == tiger.age){
return true;
}
return true;
}
}
class Panda implements Animals {
private String name;
private int age;
public Panda(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String toString() {
return "name:" + " " + this.name + " " + "age:" + " " + this.age;
}
public boolean equals(Object object) {
Panda panda = (Panda) object;
if (this == object) {
return true;
} else if (object == null) {
return false;
} else if (!(object instanceof Panda)) {
return false;
} else if (this.name.equals(panda.name) && this.age == panda.age) {
return true;
}
return true;
}
}
public class Main {
public static void main(String[] args) {
Zoo zoo = new Zoo();
zoo.add(new Panda("Beibei",4));
zoo.add(new Panda("JingJing",5));
zoo.add(new Panda("HuanHuan",3));
zoo.add(new Panda("YingYing",6));
zoo.add(new Panda("NINi",3));
zoo.add(new Tiger("DaDa",4));
zoo.add(new Tiger("SISi",8));
zoo.add(new Tiger("MaMa",2));
zoo.add(new Tiger("PiPi",5));
Link link = zoo.search("i");
Object object [] = link.getRetArray();
for(int i = 0; i < object.length; i ++){
System.out.println(object[i]);
}
}
}