Compare commits

...

No commits in common. "main" and "homework2" have entirely different histories.

21 changed files with 510 additions and 1 deletions

40
.gitignore vendored Normal file
View File

@ -0,0 +1,40 @@
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/
/.idea
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

View File

@ -1 +0,0 @@
Примеры выполнения заданий

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>

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

@ -0,0 +1,96 @@
import animals.Animal;
import animals.Swim;
import animals.Bear;
import animals.Fish;
import animals.Kotik;
import animals.Deer;
import animals.Duck;
import animals.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);
Duck duck2 = new Duck();
Worker worker3 = new Worker();
System.out.println("Кормим уток. Начальная сытость = " + duck2.getSatiety() + ".");
worker3.feed(duck2, grass);
System.out.println("Конечная сытость = " + duck2.getSatiety() + ".");
worker1.getVoice(bear1);
worker1.getVoice(kotik1);
Swim[] pond = createPond();
if (pond.length > 0){
System.out.println("Пруд: ");
for (Swim swimmer : pond) {
if (swimmer instanceof Animal) {
Animal animal = (Animal) swimmer;
if (animal.getName() != null) {
swimmer.swim();
}
}
}
}
else {
System.out.println("В пруду никого нет.");
}
}
public static Swim[] createPond(){
List<Animal> pondList = new ArrayList<>();
for (Animal animal : allAnimals) {
if (animal instanceof Swim) {
pondList.add(animal);
}
}
return pondList.toArray(new Swim[0]);
}
}

View File

@ -0,0 +1,34 @@
package animals;
import food.Food;
public abstract class Animal {
private String name;
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,28 @@
package animals;
public class Bear extends Carnivorous implements Run, Voice {
public Bear(String name) {
super(name);
this.setSatiety(2);
}
public Bear(){
this.setSatiety(2);
}
@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,25 @@
package animals;
import food.Food;
import food.Meat;
public abstract class Carnivorous extends Animal{
public Carnivorous() {}
public Carnivorous(String name) {
super(name);
}
@Override
public void eat(Food food) {
int satiety = this.getSatiety();
if (food instanceof Meat) {
satiety += food.getEnergy();
this.setSatiety(satiety);
System.out.println("Животное ест.");
}
else {
System.out.println("Не получилось. Это животное ест только мясо.");
}
}
}

View File

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

View File

@ -0,0 +1,47 @@
package animals;
public class Duck extends Herbivore implements Run, Swim, Fly, Voice {
public Duck(String name) {
super(name);
this.setSatiety(2);
}
public Duck(){
this.setSatiety(2);}
@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,22 @@
package animals;
public class Fish extends Carnivorous implements Swim {
public Fish(String name) {
super(name);
this.setSatiety(2);
}
public Fish (){
this.setSatiety(2);}
@Override
public void swim() {
if (this.getName() != null){
System.out.println("Рыба " + this.getName() + " плавает.");
}
else {
System.out.println("Рыба плавает.");
}
}
}

View File

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

View File

@ -0,0 +1,25 @@
package animals;
import food.Food;
import food.Grass;
public abstract 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();
this.setSatiety(satiety);
System.out.println("Животное ест.");
}
else {
System.out.println("Не получилось. Это животное ест только траву.");
}
}
}

View File

@ -0,0 +1,27 @@
package animals;
public class Kotik extends Carnivorous implements Run, Voice {
public Kotik(String name) {
super(name);
this.setSatiety(2);
}
public Kotik(){
this.setSatiety(2);}
@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,5 @@
package animals;
public interface Run {
void run();
}

View File

@ -0,0 +1,27 @@
package animals;
public class Sheep extends Herbivore implements Run, Voice {
public Sheep(String name) {
super(name);
this.setSatiety(2);
}
public Sheep (){
this.setSatiety(2);}
@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,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,56 @@
package employee;
import animals.Animal;
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 = "";
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);
}
else{
animal.eat(food);
}
}
public void getVoice(Voice voiceAnimal) {
if (voiceAnimal instanceof Animal) {
Animal animal = (Animal) voiceAnimal;
if (animal.getName() != null && this.Name != null) {
System.out.println(this.getName() + " говорит с животным: " + animal.getName()
+ ". Животное отвечает - " + voiceAnimal.getVoice() + ".");
}
else {
System.out.println(this.getName() + " пытается говорить с животным: " + animal.getName() + ". Это животное не умеет издавать звук.");
}
}
}
public Worker(){}
public Worker(String name){
setName(name);
}
}

View File

@ -0,0 +1,5 @@
package food;
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;
}
}