test/Kotik.java

103 lines
3.3 KiB
Java

package main.java.animals;
public class Kotik {
private String name;
private String voice;
private int satiety;
private int weight;
private static int count = 0;
public static final int METHODS = 5;
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 String getVoice() { return voice; }
public int getSatiety() { return satiety; }
public int getWeight() { return weight; }
public static int getCount() { return count; }
public void setName(String name) { this.name = name; }
public void setVoice(String voice) { this.voice = voice; }
public void setSatiety(int satiety) { this.satiety = satiety; }
public void setWeight(int weight) { this.weight = weight; }
public boolean play() { return performAction("играл"); }
public boolean sleep() { return performAction("спал"); }
public boolean wash() { return performAction("умывался"); }
public boolean walk() { return performAction("гулял"); }
public boolean hunt() { return performAction("охотился"); }
private boolean performAction(String action) {
if (satiety > 0) {
satiety--;
System.out.println(name + " " + action + ". Сытость: " + satiety);
return true;
} else {
System.out.println(name + " просит есть!");
return false;
}
}
public void eat(int units) {
satiety += units;
System.out.println(name + " поел и сытость увеличилась на " + units);
}
public void eat(int units, String food) {
satiety += units;
System.out.println(name + " поел " + food + " и сытость увеличилась на " + units);
}
public void eat() {
eat(5, "кошачий корм");
}
public String[] liveAnotherDay() {
String[] activities = new String[24];
for (int hour = 0; hour < 24; hour++) {
int choice = (int) (Math.random() * METHODS) + 1;
boolean actionDone = false;
switch (choice) {
case 1: actionDone = play(); break;
case 2: actionDone = sleep(); break;
case 3: actionDone = wash(); break;
case 4: actionDone = walk(); break;
case 5: actionDone = hunt(); break;
}
if (!actionDone) {
eat();
activities[hour] = hour + " - ел";
} else {
activities[hour] = hour + " - " + getActionName(choice);
}
}
return activities;
}
private String getActionName(int choice) {
switch (choice) {
case 1: return "играл";
case 2: return "спал";
case 3: return "умывался";
case 4: return "гулял";
case 5: return "охотился";
default: return "";
}
}
}