This commit is contained in:
Дроздев 2025-11-22 21:44:06 +04:00
parent 3430673713
commit f4edc9ca61
2 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,23 @@
import animals.Kotik;
public class Application {
public static void main(String[] args) {
Kotik firstKot = new Kotik("Petr", "hi", 10, 5);
Kotik secondKot = new Kotik();
secondKot.setName("Barsik");
secondKot.setVoice("hello");
secondKot.setSatiety(5);
secondKot.setWeight(3);
for(String str : firstKot.liveAnotherDay()){
System.out.println(str);
}
System.out.println(secondKot.getName());
System.out.println(secondKot.getWeight());
System.out.println(Application.compareVoice(firstKot, secondKot));
System.out.println(Kotik.getCount());
}
public static boolean compareVoice(Kotik firstKot, Kotik secondKot){
return firstKot.getVoice().equals(secondKot.getVoice());
}
}

View File

@ -0,0 +1,179 @@
package animals;
import java.util.ArrayList;
public class Kotik {
private static final int METHODS = 5;
private static int count = 0;
private String name;
private String voice;
private int satiety;
private int weight;
public Kotik(String name, String voice, int satiety, int weight) {
this.name = name;
this.voice = voice;
this.satiety = satiety;
this.weight = weight;
count++;
}
public Kotik() {
count++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVoice() {
return voice;
}
public void setVoice(String voice) {
this.voice = voice;
}
public int getSatiety() {
return satiety;
}
public void setSatiety(int satiety) {
this.satiety = satiety;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public static int getCount() {
return count;
}
public void eat(int units) {
System.out.println("Kotik is eating");
this.satiety += units;
}
public void eat(int units, String food) {
System.out.println("Kotik is eating " + food);
this.satiety += units;
}
public void eat() {
eat(2, "fish");
}
public boolean play() {
if (satiety > 0) {
System.out.println("Kotik is playing");
satiety--;
return true;
} else {
System.out.println("kotik wanna eat");
eat();
return false;
}
}
public boolean sleep() {
if (satiety > 0) {
System.out.println("Kotik is sleeping");
satiety--;
return true;
} else {
System.out.println("kotik wanna eat");
eat();
return false;
}
}
public boolean wash() {
if (satiety > 0) {
System.out.println("Kotik is washing");
satiety--;
return true;
} else {
System.out.println("kotik wanna eat");
eat();
return false;
}
}
public boolean walk() {
if (satiety > 0) {
System.out.println("Kotik is walking");
satiety--;
return true;
} else {
System.out.println("kotik wanna eat");
eat();
return false;
}
}
public boolean hunt() {
if (satiety > 0) {
System.out.println("Kotik is hunting");
satiety--;
return true;
} else {
System.out.println("kotik wanna eat");
eat();
return false;
}
}
public ArrayList<String> liveAnotherDay() {
ArrayList<String> kotikToDoList = new ArrayList<>();
for (int i = 0; i < 24; i++) {
switch ((int) (Math.random() * METHODS) + 1) {
case (1):
if (play())
kotikToDoList.add(i + " - play");
else
kotikToDoList.add(i + " - eat");
break;
case (2):
if (sleep())
kotikToDoList.add(i + " - sleep");
else
kotikToDoList.add(i + " - eat");
break;
case (3):
if (wash())
kotikToDoList.add(i + " - wash");
else
kotikToDoList.add(i + " - eat");
break;
case (4):
if (walk())
kotikToDoList.add(i + " - walk");
else
kotikToDoList.add(i + " - eat");
break;
case (5):
if (hunt())
kotikToDoList.add(i + " - hunt");
else
kotikToDoList.add(i + " - eat");
break;
default:
System.out.println("something wrong");
break;
}
}
return kotikToDoList;
}
}