diff --git a/.gitignore.txt b/.gitignore.txt new file mode 100644 index 0000000..193ab13 --- /dev/null +++ b/.gitignore.txt @@ -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/ \ No newline at end of file diff --git a/README.md b/README.md index 0078a88..4f8fd65 100644 --- a/README.md +++ b/README.md @@ -1 +1,12 @@ # jv-homework-template +jv-zoo +Цель: прокачать навыки в ООП и работе с Generics + +Ваша задача заключается в том чтобы создать свой зоопарк, разместить животных по вольерам и накромить их. Создание инстансов животных и кормление можно проводить в классе App(или Main или любом другом). В данном темплейте есть класс HelloWorld и класс HelloWorldTest. Feel free to remove them) + +Создать зоопарк +В зоопарке должны быть вольер для птиц, для всех животных и аквариум +Создать классы тигр, пингвин, воробей и акула и добавить в соответствующие среды обитания (см. п.2) +Птицы должны уметь летать, рыбы должны уметь плавать. Обратите внимание на пингвина, там не все так просто: он может плавать, но не может летать, при єтом является птицей +Должна быть возможность накормить всех животных +Для решения задачи воспользоваться Дженериками diff --git a/out/production/main/core/basesyntax/Animal.class b/out/production/main/core/basesyntax/Animal.class new file mode 100644 index 0000000..1ac587e Binary files /dev/null and b/out/production/main/core/basesyntax/Animal.class differ diff --git a/out/production/main/core/basesyntax/Bird.class b/out/production/main/core/basesyntax/Bird.class new file mode 100644 index 0000000..c167f65 Binary files /dev/null and b/out/production/main/core/basesyntax/Bird.class differ diff --git a/out/production/main/core/basesyntax/Cage.class b/out/production/main/core/basesyntax/Cage.class new file mode 100644 index 0000000..9ce9206 Binary files /dev/null and b/out/production/main/core/basesyntax/Cage.class differ diff --git a/out/production/main/core/basesyntax/Fish.class b/out/production/main/core/basesyntax/Fish.class new file mode 100644 index 0000000..924a650 Binary files /dev/null and b/out/production/main/core/basesyntax/Fish.class differ diff --git a/out/production/main/core/basesyntax/Flyable.class b/out/production/main/core/basesyntax/Flyable.class new file mode 100644 index 0000000..8ccc020 Binary files /dev/null and b/out/production/main/core/basesyntax/Flyable.class differ diff --git a/out/production/main/core/basesyntax/Mammal.class b/out/production/main/core/basesyntax/Mammal.class new file mode 100644 index 0000000..a4cf382 Binary files /dev/null and b/out/production/main/core/basesyntax/Mammal.class differ diff --git a/out/production/main/core/basesyntax/Pingvin.class b/out/production/main/core/basesyntax/Pingvin.class new file mode 100644 index 0000000..dcac24b Binary files /dev/null and b/out/production/main/core/basesyntax/Pingvin.class differ diff --git a/out/production/main/core/basesyntax/Shark.class b/out/production/main/core/basesyntax/Shark.class new file mode 100644 index 0000000..eda0eaf Binary files /dev/null and b/out/production/main/core/basesyntax/Shark.class differ diff --git a/out/production/main/core/basesyntax/Sparrow.class b/out/production/main/core/basesyntax/Sparrow.class new file mode 100644 index 0000000..af146f3 Binary files /dev/null and b/out/production/main/core/basesyntax/Sparrow.class differ diff --git a/out/production/main/core/basesyntax/Swimable.class b/out/production/main/core/basesyntax/Swimable.class new file mode 100644 index 0000000..c92be3e Binary files /dev/null and b/out/production/main/core/basesyntax/Swimable.class differ diff --git a/out/production/main/core/basesyntax/Tiger.class b/out/production/main/core/basesyntax/Tiger.class new file mode 100644 index 0000000..88e4648 Binary files /dev/null and b/out/production/main/core/basesyntax/Tiger.class differ diff --git a/src/main/java/core/basesyntax/Cage.java b/src/main/java/core/basesyntax/Cage.java new file mode 100644 index 0000000..bafd6c0 --- /dev/null +++ b/src/main/java/core/basesyntax/Cage.java @@ -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 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"); + ListbirdList = 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"); + Listakvarium = 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!"); + } +} diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index 97db782..0000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,8 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { - -}