Initial commit

This commit is contained in:
valerpan 2026-07-18 23:57:01 +03:00
commit 47e1eb9506
20 changed files with 482 additions and 0 deletions

39
.gitignore vendored Normal file
View File

@ -0,0 +1,39 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.kotlin
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

17
pom.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>PW2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

89
src/main/java/Zoo.java Normal file
View File

@ -0,0 +1,89 @@
import animals.Animal;
import animals.Swim;
import animals.carnivores.Bear;
import animals.carnivores.Fish;
import animals.carnivores.Kotik;
import animals.herbivores.Deer;
import animals.herbivores.Duck;
import animals.herbivores.Sheep;
import employee.Worker;
import food.Grass;
import food.Meat;
import java.util.ArrayList;
import java.util.List;
public class Zoo {
private static final List<Animal> allAnimals = new ArrayList<>();
public static void main(String[] args) {
System.out.println("Зоопарк: ");
Bear bear1 = new Bear("Миша");
Kotik kotik1 = new Kotik("Мурка");
Fish fish1 = new Fish("Олег");
Deer deer1 = new Deer("Бемби");
Duck duck1 = new Duck("Кряква");
Sheep sheep1 = new Sheep("Долли");
allAnimals.add(bear1);
allAnimals.add(kotik1);
allAnimals.add(fish1);
allAnimals.add(deer1);
allAnimals.add(duck1);
allAnimals.add(sheep1);
Worker worker1 = new Worker("Алина");
Worker worker2 = new Worker("Сергей");
Grass grass = new Grass();
Meat meat = new Meat();
kotik1.run();
bear1.run();
duck1.fly();
fish1.swim();
worker1.feed(bear1, grass);
worker1.feed(kotik1, meat);
worker1.feed(fish1, meat);
worker2.feed(deer1, grass);
worker2.feed(duck1, grass);
worker2.feed(sheep1, meat);
worker1.getVoice(bear1);
worker1.getVoice(kotik1);
worker2.getVoice(fish1);
Animal[] pond = createPond();
if (pond.length > 0){
System.out.println("Пруд: ");
for (Animal animal : pond) {
if (animal.getName() != null) {
((Swim) animal).swim();
}
}
}
else {
System.out.println("В пруду никого нет.");
}
}
public static Animal[] createPond(){
List<Animal> pondList = new ArrayList<>();
for (Animal animal : allAnimals) {
if (animal instanceof Swim) {
pondList.add(animal);
}
}
return pondList.toArray(new Animal[0]);
}
}

View File

@ -0,0 +1,34 @@
package animals;
import food.Food;
public abstract class Animal {
private String name;
private int satiety;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public int getSatiety(){
return this.satiety;
}
public void setSatiety(int satiety){
this.satiety = satiety;
}
public abstract void eat(Food food);
public Animal() {
}
public Animal(String name) {
this.name = name;
this.satiety = 2;
}
}

View File

@ -0,0 +1,24 @@
package animals;
import food.Food;
import food.Meat;
public abstract class Carnivore extends Animal{
public Carnivore() {}
public Carnivore(String name) {
super(name);
}
@Override
public void eat(Food food) {
int satiety = this.getSatiety();
if (food instanceof Meat) {
satiety += food.getEnergy();
System.out.println("Животное ест.");
}
else {
System.out.println("Не получилось. Это животное ест только мясо.");
}
}
}

View File

@ -0,0 +1,5 @@
package animals;
public interface Fly {
void fly();
}

View File

@ -0,0 +1,24 @@
package animals;
import food.Food;
import food.Grass;
public class Herbivore extends Animal{
public Herbivore() {}
public Herbivore(String name) {
super(name);
}
@Override
public void eat(Food food) {
int satiety = this.getSatiety();
if (food instanceof Grass) {
satiety += food.getEnergy();
System.out.println("Животное ест.");
}
else {
System.out.println("Не получилось. Это животное ест только траву.");
}
}
}

View File

@ -0,0 +1,5 @@
package animals;
public interface Run {
void run();
}

View File

@ -0,0 +1,5 @@
package animals;
public interface Swim {
void swim();
}

View File

@ -0,0 +1,5 @@
package animals;
public interface Voice {
String getVoice();
}

View File

@ -0,0 +1,26 @@
package animals.carnivores;
import animals.Carnivore;
import animals.Run;
import animals.Voice;
public class Bear extends Carnivore implements Run, Voice {
public Bear(String name) {
super(name);
}
@Override
public void run() {
if (this.getName() != null){
System.out.println("Медведь " + this.getName() + " бегает.");
}
else {
System.out.println("Медведь + бегает.");
}
}
@Override
public String getVoice(){
return "гррр";
}
}

View File

@ -0,0 +1,20 @@
package animals.carnivores;
import animals.Carnivore;
import animals.Swim;
public class Fish extends Carnivore implements Swim {
public Fish(String name) {
super(name);
}
@Override
public void swim() {
if (this.getName() != null){
System.out.println("Рыба " + this.getName() + " плавает.");
}
else {
System.out.println("Рыба плавает.");
}
}
}

View File

@ -0,0 +1,26 @@
package animals.carnivores;
import animals.Carnivore;
import animals.Run;
import animals.Voice;
public class Kotik extends Carnivore implements Run, Voice {
public Kotik(String name) {
super(name);
}
@Override
public void run() {
if (this.getName() != null){
System.out.println("Котик " + this.getName() + " бегает.");
}
else {
System.out.println("Котик бегает.");
}
}
@Override
public String getVoice(){
return "мяу";
}
}

View File

@ -0,0 +1,21 @@
package animals.herbivores;
import animals.Herbivore;
import animals.Run;
public class Deer extends Herbivore implements Run {
public Deer(String name) {
super(name);
}
@Override
public void run() {
if (this.getName() != null){
System.out.println("Олень " + this.getName() + " бегает.");
}
else {
System.out.println("Олень бегает.");
}
}
}

View File

@ -0,0 +1,44 @@
package animals.herbivores;
import animals.*;
public class Duck extends Herbivore implements Run, Swim, Fly, Voice {
public Duck(String name) {
super(name);
}
@Override
public void run() {
if (this.getName() != null){
System.out.println("Утка " + this.getName() + " бегает.");
}
else {
System.out.println("Утка бегает.");
}
}
@Override
public void swim() {
if (this.getName() != null){
System.out.println("Утка " + this.getName() + " плавает.");
}
else {
System.out.println("Утка плаваеет.");
}
}
@Override
public void fly() {
if (this.getName() != null){
System.out.println("Утка " + this.getName() + " летает.");
}
else {
System.out.println("Утка летает.");
}
}
@Override
public String getVoice(){
return "кря";
}
}

View File

@ -0,0 +1,26 @@
package animals.herbivores;
import animals.Herbivore;
import animals.Run;
import animals.Voice;
public class Sheep extends Herbivore implements Run, Voice {
public Sheep(String name) {
super(name);
}
@Override
public void run() {
if (this.getName() != null){
System.out.println("Овечка " + this.getName() + " бегает.");
}
else {
System.out.println("Овечка бегает.");
}
}
@Override
public String getVoice(){
return "бее";
}
}

View File

@ -0,0 +1,47 @@
package employee;
import animals.Animal;
import animals.Swim;
import animals.Voice;
import food.Food;
import food.Grass;
import food.Meat;
public class Worker {
private String Name;
public void setName(String name) {
Name = name;
}
public String getName() {
return Name;
}
public void feed(Animal animal, Food food){
String type = new String();
if (food instanceof Grass) type = " травой. ";
if (food instanceof Meat) type = " мясом. ";
if (this.Name != null && animal.getName() != null) {
System.out.println(this.getName() + " кормит животное " + animal.getName() + type);
animal.eat(food);
}
}
public void getVoice(Animal animal){
if (this.Name != null || animal.getName() != null){
if (animal instanceof Voice){
System.out.println(this.getName() + " говорит с животным: " + animal.getName() + ". Животное отвечает - "+ ((Voice) animal).getVoice() +".");
}
else {
System.out.println(this.getName() + " пытается говорить с животным: " + animal.getName() + ". Это животное не умеет издавать звук.");
}
}
}
public Worker(String name){
setName(name);
}
}

View File

@ -0,0 +1,7 @@
package food;
import javax.naming.Name;
public abstract class Food {
public abstract int getEnergy();
}

View File

@ -0,0 +1,9 @@
package food;
public class Grass extends Food{
@Override
public int getEnergy() {
return 1;
}
}

View File

@ -0,0 +1,9 @@
package food;
public class Meat extends Food{
@Override
public int getEnergy() {
return 2;
}
}