Compare commits
No commits in common. "homework2" and "main" have entirely different histories.
40
.gitignore
vendored
40
.gitignore
vendored
@ -1,40 +0,0 @@
|
|||||||
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
|
|
||||||
17
pom.xml
17
pom.xml
@ -1,17 +0,0 @@
|
|||||||
<?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>
|
|
||||||
@ -1,96 +0,0 @@
|
|||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,34 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
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 "гррр";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
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("Не получилось. Это животное ест только мясо.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
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("Олень бегает.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,47 +0,0 @@
|
|||||||
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 "кря";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,22 +0,0 @@
|
|||||||
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("Рыба плавает.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package animals;
|
|
||||||
|
|
||||||
public interface Fly {
|
|
||||||
void fly();
|
|
||||||
}
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
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("Не получилось. Это животное ест только траву.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,27 +0,0 @@
|
|||||||
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 "мяу";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package animals;
|
|
||||||
|
|
||||||
public interface Run {
|
|
||||||
void run();
|
|
||||||
}
|
|
||||||
@ -1,27 +0,0 @@
|
|||||||
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 "бее";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package animals;
|
|
||||||
|
|
||||||
public interface Swim {
|
|
||||||
void swim();
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package animals;
|
|
||||||
|
|
||||||
public interface Voice {
|
|
||||||
String getVoice();
|
|
||||||
}
|
|
||||||
@ -1,56 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package food;
|
|
||||||
|
|
||||||
public abstract class Food {
|
|
||||||
public abstract int getEnergy();
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
package food;
|
|
||||||
|
|
||||||
public class Grass extends Food{
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getEnergy() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
package food;
|
|
||||||
|
|
||||||
public class Meat extends Food{
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getEnergy() {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user