Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My zoo #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .gitignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
out/

### Maven ###
log/
target/
!.mvn/wrapper/maven-wrapper.jar

### STS / Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea/
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# jv-homework-template
jv-zoo
Цель: прокачать навыки в ООП и работе с Generics

Ваша задача заключается в том чтобы создать свой зоопарк, разместить животных по вольерам и накромить их. Создание инстансов животных и кормление можно проводить в классе App(или Main или любом другом). В данном темплейте есть класс HelloWorld и класс HelloWorldTest. Feel free to remove them)

Создать зоопарк
В зоопарке должны быть вольер для птиц, для всех животных и аквариум
Создать классы тигр, пингвин, воробей и акула и добавить в соответствующие среды обитания (см. п.2)
Птицы должны уметь летать, рыбы должны уметь плавать. Обратите внимание на пингвина, там не все так просто: он может плавать, но не может летать, при єтом является птицей
Должна быть возможность накормить всех животных
Для решения задачи воспользоваться Дженериками
Binary file added out/production/main/core/basesyntax/Animal.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Bird.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Cage.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Fish.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Flyable.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Mammal.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Pingvin.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Shark.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Sparrow.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Swimable.class
Binary file not shown.
Binary file added out/production/main/core/basesyntax/Tiger.class
Binary file not shown.
230 changes: 230 additions & 0 deletions src/main/java/core/basesyntax/Cage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package core.basesyntax;

import java.util.Arrays;
import java.util.List;

/**
* Feel free to remove this class and create your own.
*/
public class Cage {
public static void printAll(List<?> list) {
for (Object item : list)
System.out.println(item + " ");
}



public static void main(String[] args) {
Animal animal1 = new Shark("Masanya_1");
Animal animal2 = new Pingvin("Kovalski_1");
Animal animal3 = new Tiger("Tigrula_1");
Animal animal4 = new Sparrow("Chijik_1");
Mammal mammal1 = new Tiger("Tigera_2");
Tiger tiger1 = new Tiger("Tigrula_3");
Swimable swimable1 = new Pingvin("Kovalski_4");
Flyable flyableable1 = new Sparrow("Chijik_4");

List<Object> animalList = Arrays.asList(animal1, animal2, animal3, animal4,mammal1,tiger1,swimable1,flyableable1);
System.out.println("Cage for all animals " );
printAll(animalList);
System.out.println("-----------------------");

Bird bird1 = new Pingvin("Kovalski_2");
Pingvin pingvin1 = new Pingvin("Kovalski_3");
Sparrow sparrow1 = new Sparrow("Chijik_3");
List<Bird>birdList = Arrays.asList(bird1,pingvin1,sparrow1);
System.out.println("Cage for all birds");
printAll(birdList);
System.out.println("-----------------------");


Fish fish1 = new Shark("Masanya_1");
Shark shark2 = new Shark("Masanya_2");
List<Fish>akvarium = Arrays.asList(fish1,shark2);
System.out.println("Acvarium for all fishes");
printAll(akvarium);
System.out.println("-----------------------");



for (Object o : animalList) {
if (o instanceof Fish) {
System.out.println(((Animal)o).getName());
((Animal)o).eat();
((Fish) o).swim();
} else if (o instanceof Pingvin) {
Pingvin p = (Pingvin) o;
System.out.println(p.getName());
p.eat();
p.swimable();
} else if (o instanceof Tiger) {
Tiger t = (Tiger) o;
System.out.println(t.getName());
t.eat();
t.run();
} else if (o instanceof Sparrow) {
Sparrow s = (Sparrow) o;
System.out.println(s.getName());
s.eat();
s.flyable();
}
System.out.println("-----------------------");
}
for (Bird b : birdList) {
if (b instanceof Pingvin) {
Pingvin p = (Pingvin) b;
System.out.println(p.getName());
p.eat();
p.swimable();
} else if (b instanceof Sparrow) {
Sparrow s = (Sparrow) b;
System.out.println(s.getName());
s.eat();
s.flyable();
}
System.out.println("-----------------------");
}
}
}

abstract class Animal {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


Animal(String name) {
this.name = name;
}

abstract void eat();
public String toString(){
return "Animal name is - " + getName();
}
}

abstract class Fish extends Animal {
//protected String name;
public Fish(String name) {
super(name);
this.setName(name);
}

abstract void swim();
}

abstract class Bird extends Animal implements Flyable {
//private String name;
public Bird(String name) {
super(name);
this.setName(name);
}

//abstract void fly();
@Override
public void flyable() {
System.out.println(getName() + "fly");
}
}

abstract class Mammal extends Animal {
//String name;
Mammal(String name) {
super(name);
this.setName(name);
}

abstract void run();

}

interface Swimable {
void swimable();
}

interface Flyable {
void flyable();
}

class Shark extends Fish {
//protected String name;
Shark(String name) {
super(name);
this.setName(name);
}

@Override
protected void eat() {
System.out.println("Shark is predator and eat fish");
}

@Override
public void swim() {
System.out.println("Shark is a great swimer");
}
}

class Sparrow extends Bird {
Sparrow(String name) {
super(name);
this.setName(name);
}

@Override
protected void eat() {
System.out.println("Sparrow eat grain");
}

@Override
public void flyable() {
System.out.println(getName() + "Sparrow fly very well");
}
}

class Pingvin extends Bird implements Swimable {
//private String name;
Pingvin(String name) {
super(name);
this.setName(name);
}

@Override
protected void eat() {
System.out.println("Pinguin lubit est ribu!");
}

@Override
public void swimable() {
System.out.println("Pinguin can swim but not fly");
}

@Override
public void flyable() {
System.out.println(getName() + "Can't fly");
}
}

class Tiger extends Mammal {
//private String name;
Tiger(String name) {
super(name);
this.setName(name);
}

@Override
protected void eat() {
System.out.println("Tiger like meet");
}

@Override
void run() {
System.out.println("Tiger - can run!");
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.