Compare commits

...

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

25 changed files with 659 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>

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

@ -0,0 +1,123 @@
import animals.*;
import employee.Worker;
import food.Grass;
import food.Meat;
import model.Aviary;
import model.Size;
import java.util.ArrayList;
import java.util.List;
public class Zoo {
private static final List<Animal> allAnimals = new ArrayList<>();
private static final Aviary<Carnivorous> carnivorousAviary = new Aviary<>(Size.LARGE);
private static final Aviary<Herbivore> herbivoreAviary = new Aviary<>(Size.MEDIUM);
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();
worker1.feed(kotik1, meat);
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("В пруду никого нет.");
}
fillCarnivorousAviary();
fillHerbivoreAviary();
if(getCarnivorous("Белла") != null){
System.out.println(getCarnivorous("Белла"));
}
else {
System.out.println("Животное с именем 'Белла' не найдено");
}
if(getHerbivore("Бека") != null){
System.out.println(getHerbivore("Бека"));
}
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]);
}
public static void fillCarnivorousAviary(){
carnivorousAviary.addAnimal(new Bear ("Маша"));
carnivorousAviary.addAnimal(new Bear("Белла"));
}
public static void fillHerbivoreAviary(){
herbivoreAviary.addAnimal(new Deer ("Аня"));
herbivoreAviary.addAnimal(new Deer("Олли"));
herbivoreAviary.addAnimal(new Sheep("Бека"));
}
public static Carnivorous getCarnivorous(String name){
return carnivorousAviary.getAnimal(name);
}
public static Herbivore getHerbivore(String name){
return herbivoreAviary.getAnimal(name);
}
}

View File

@ -0,0 +1,38 @@
package animals;
import food.WrongFoodException;
import model.Size;
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) throws WrongFoodException;
public Animal() {
}
public Animal(String name) {
this.name = name;
this.satiety = 2;
}
public abstract Size getSize();
}

View File

@ -0,0 +1,35 @@
package animals;
import model.Size;
public class Bear extends Carnivorous implements Run, Voice {
@Override
public Size getSize() {
return Size.LARGE;
}
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,26 @@
package animals;
import food.Food;
import food.Meat;
import food.WrongFoodException;
public abstract class Carnivorous extends Animal{
public Carnivorous() {}
public Carnivorous(String name) {
super(name);
}
@Override
public void eat(Food food) throws WrongFoodException {
int satiety = this.getSatiety();
if (food instanceof Meat) {
satiety += food.getEnergy();
this.setSatiety(satiety);
System.out.println("Животное ест.");
}
else {
throw new WrongFoodException("Это животное хищник, ест только мясо.");
}
}
}

View File

@ -0,0 +1,30 @@
package animals;
import model.Size;
public class Deer extends Herbivore implements Run {
@Override
public Size getSize() {
return Size.MEDIUM;
}
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,54 @@
package animals;
import model.Size;
public class Duck extends Herbivore implements Run, Swim, Fly, Voice {
@Override
public Size getSize() {
return Size.SMALL;
}
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,29 @@
package animals;
import model.Size;
public class Fish extends Carnivorous implements Swim {
@Override
public Size getSize() {
return Size.MEDIUM;
}
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,26 @@
package animals;
import food.Food;
import food.Grass;
import food.WrongFoodException;
public abstract class Herbivore extends Animal{
public Herbivore() {}
public Herbivore(String name) {
super(name);
}
@Override
public void eat(Food food) throws WrongFoodException {
int satiety = this.getSatiety();
if (food instanceof Grass) {
satiety += food.getEnergy();
this.setSatiety(satiety);
System.out.println("Животное ест.");
}
else {
throw new WrongFoodException("Это животное тровоядное, ест только траву.");
}
}
}

View File

@ -0,0 +1,34 @@
package animals;
import model.Size;
public class Kotik extends Carnivorous implements Run, Voice {
@Override
public Size getSize() {
return Size.SMALL;
}
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,34 @@
package animals;
import model.Size;
public class Sheep extends Herbivore implements Run, Voice {
@Override
public Size getSize() {
return Size.MEDIUM;
}
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,62 @@
package employee;
import animals.Animal;
import animals.Voice;
import food.Food;
import food.Grass;
import food.Meat;
import food.WrongFoodException;
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 = " мясом. ";
try {
if (this.Name != null && animal.getName() != null) {
System.out.println(this.getName() + " кормит животное " + animal.getName() + type);
animal.eat(food);
}
else{
animal.eat(food);
}
}
catch (WrongFoodException e){
e.printStackTrace();
}
}
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;
}
}

View File

@ -0,0 +1,7 @@
package food;
public class WrongFoodException extends Exception {
public WrongFoodException(String message) {
super(message);
}
}

View File

@ -0,0 +1,49 @@
package model;
import animals.Animal;
import java.util.HashMap;
import java.util.Map;
public class Aviary<A extends Animal>{
private Size size;
public Aviary(Size size){
this.size = size;
}
public Size getSize() {
return size;
}
public void setSize(Size size) {
this.size = size;
}
private HashMap<String, A> aviaryMap = new HashMap<>();
public void addAnimal(A animal){
if (animal.getSize() != this.size){
throw new WrongSizeException("Размер животного - " + animal.getSize() + ", не подходит размеру вальера - " + this.size);
}
else {
aviaryMap.put(animal.getName(), animal);
}
}
public A getAnimal(String name){
return aviaryMap.get(name);
}
public boolean removeAnimal(String name){
return aviaryMap.remove(name, getAnimal(name));
}
public HashMap<String, A> getAviaryMap() {
return aviaryMap;
}
public void setAviaryMap(HashMap<String, A> aviaryMap) {
this.aviaryMap = aviaryMap;
}
}

View File

@ -0,0 +1,5 @@
package model;
public enum Size {
SMALL, MEDIUM, LARGE
}

View File

@ -0,0 +1,7 @@
package model;
public class WrongSizeException extends RuntimeException {
public WrongSizeException(String message) {
super(message);
}
}