diff --git a/src/main/java/lesson7/Cat.java b/src/main/java/lesson7/Cat.java new file mode 100644 index 0000000..4029473 --- /dev/null +++ b/src/main/java/lesson7/Cat.java @@ -0,0 +1,19 @@ +package lesson7; + +public class Cat { + private final String name; + private final int appetite; + private boolean satiety; + public Cat(String name, int appetite) { + this.name = name; + this.appetite = appetite; + this.satiety = false; + } + public void eat(Plate p) { + satiety = p.decreaseFood(appetite); + } + + public void printInfo() { + System.out.printf("Кот %s %s%n", name, satiety ? "сытый" : "голодный"); + } +} diff --git a/src/main/java/lesson7/Main.java b/src/main/java/lesson7/Main.java new file mode 100644 index 0000000..fce18df --- /dev/null +++ b/src/main/java/lesson7/Main.java @@ -0,0 +1,23 @@ +package lesson7; + +public class Main { + public static void main(String[] args) { + Plate plate = new Plate(12); + plate.printInfo(); + + Cat[] catArray = new Cat[3]; + catArray[0] = new Cat("Барсик", 5); + catArray[1] = new Cat("Ричрд", 4); + catArray[2] = new Cat("Вася", 6); + + for (Cat cat:catArray) { + cat.eat(plate); + plate.printInfo(); + } + for (Cat cat: catArray) { + cat.printInfo(); + } + plate.addFood(10); + plate.printInfo(); + } +} diff --git a/src/main/java/lesson7/Plate.java b/src/main/java/lesson7/Plate.java new file mode 100644 index 0000000..b43fbb4 --- /dev/null +++ b/src/main/java/lesson7/Plate.java @@ -0,0 +1,26 @@ +package lesson7; + +public class Plate { + private int foodCount; + public Plate(int foodCount) { + this.foodCount = foodCount; + } + public boolean decreaseFood(int n) { + if (foodCount >= n) { + foodCount -= n; + System.out.printf("Объём миски уменьшился на %s%n", n); + return true; + } + else { + System.out.println("В миске недостаточно корма"); + } + return false; + } + public void printInfo() { + System.out.printf("В миске осталось %s еды%n", foodCount); + } + + public void addFood(int i) { + foodCount += i; + } +}