Commit hw3

This commit is contained in:
valerpan 2026-07-24 00:22:51 +03:00
parent 5642d29ea4
commit a986819960
15 changed files with 175 additions and 27 deletions

View File

@ -1,14 +1,9 @@
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 animals.*;
import employee.Worker;
import food.Grass;
import food.Meat;
import model.Aviary;
import model.Size;
import java.util.ArrayList;
import java.util.List;
@ -17,6 +12,9 @@ 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("Зоопарк: ");
@ -41,17 +39,9 @@ public class Zoo {
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);
//worker2.feed(sheep1, meat);
Duck duck2 = new Duck();
Worker worker3 = new Worker();
@ -80,6 +70,24 @@ public class Zoo {
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(){
@ -93,4 +101,23 @@ public class Zoo {
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 Animal getCarnivorous(String name){
return carnivorousAviary.getAnimal(name);
}
public static Animal getHerbivore(String name){
return herbivoreAviary.getAnimal(name);
}
}

View File

@ -1,5 +1,7 @@
package animals;
import food.WrongFoodException;
import model.Size;
import food.Food;
public abstract class Animal {
@ -22,7 +24,7 @@ public abstract class Animal {
this.satiety = satiety;
}
public abstract void eat(Food food);
public abstract void eat(Food food) throws WrongFoodException;
public Animal() {
}
@ -31,4 +33,6 @@ public abstract class Animal {
this.name = name;
this.satiety = 2;
}
public abstract Size getSize();
}

View File

@ -1,7 +1,14 @@
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);

View File

@ -1,6 +1,7 @@
package animals;
import food.Food;
import food.Meat;
import food.WrongFoodException;
public abstract class Carnivorous extends Animal{
@ -11,7 +12,7 @@ public abstract class Carnivorous extends Animal{
}
@Override
public void eat(Food food) {
public void eat(Food food) throws WrongFoodException {
int satiety = this.getSatiety();
if (food instanceof Meat) {
satiety += food.getEnergy();
@ -19,7 +20,7 @@ public abstract class Carnivorous extends Animal{
System.out.println("Животное ест.");
}
else {
System.out.println("Не получилось. Это животное ест только мясо.");
throw new WrongFoodException("Это животное хищник, ест только мясо.");
}
}
}

View File

@ -1,7 +1,14 @@
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);

View File

@ -1,7 +1,14 @@
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);

View File

@ -1,7 +1,14 @@
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);

View File

@ -1,6 +1,7 @@
package animals;
import food.Food;
import food.Grass;
import food.WrongFoodException;
public abstract class Herbivore extends Animal{
@ -11,7 +12,7 @@ public abstract class Herbivore extends Animal{
}
@Override
public void eat(Food food) {
public void eat(Food food) throws WrongFoodException {
int satiety = this.getSatiety();
if (food instanceof Grass) {
satiety += food.getEnergy();
@ -19,7 +20,7 @@ public abstract class Herbivore extends Animal{
System.out.println("Животное ест.");
}
else {
System.out.println("Не получилось. Это животное ест только траву.");
throw new WrongFoodException("Это животное тровоядное, ест только траву.");
}
}
}

View File

@ -1,7 +1,14 @@
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);

View File

@ -1,7 +1,14 @@
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);

View File

@ -5,6 +5,7 @@ import animals.Voice;
import food.Food;
import food.Grass;
import food.Meat;
import food.WrongFoodException;
public class Worker {
private String Name;
@ -23,6 +24,7 @@ public class Worker {
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);
@ -31,6 +33,10 @@ public class Worker {
animal.eat(food);
}
}
catch (WrongFoodException e){
e.printStackTrace();
}
}
public void getVoice(Voice voiceAnimal) {

View File

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

View File

@ -0,0 +1,48 @@
package model;
import animals.Animal;
import java.util.HashMap;
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> map = new HashMap<>();
public void addAnimal(A animal){
if (animal.getSize() != this.size){
throw new WrongSizeException("Размер животного - " + animal.getSize() + ", не подходит размеру вальера - " + this.size);
}
else {
map.put(animal.getName(), animal);
}
}
public A getAnimal(String name){
return map.get(name);
}
public boolean removeAnimal(String name){
return map.remove(name, getAnimal(name));
}
public HashMap<String, A> getMap() {
return map;
}
public void setMap(HashMap<String, A> map) {
this.map = map;
}
}

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);
}
}