-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_7.java
More file actions
97 lines (86 loc) · 2.82 KB
/
java_7.java
File metadata and controls
97 lines (86 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Java. level 1. Lesson 7. Example of homework,
* autor Rudenko Alexander
* version date 12/12/2018
*
*/
import java.util.Scanner;
public class java_7 {
public static void main(String[] args) {
int amount_food;
int variant;
Cat[] arrCat = {
new Cat("Barsik", 3),
new Cat("Murzik", 8),
new Cat("Tabasik", 12)
};
System.out.println("Введите кол-во еды в тарелке");
Scanner scan = new Scanner(System.in);
amount_food = scan.nextInt();
Plate plate = new Plate(amount_food);
System.out.println("Кол-во еды: " + plate);
for (Cat arrcat : arrCat) {
if (plate.getFood() >= arrcat.getAppetite()) {
arrcat.eat(plate);
System.out.println(arrcat);
} else {
System.out.println(arrcat);
System.out.println("Не хватает еды в тарелке");
System.out.println("Добавить еды? 1 - да, 2 - нет");
variant = scan.nextInt();
switch (variant) {
case 1:
System.out.println("Сколько добавить еды?");
amount_food = scan.nextInt();
plate.increaseFood(amount_food);
arrcat.eat(plate);
System.out.println(arrcat);
break;
case 2:
System.out.println(arrcat);
break;
}
}
}
System.out.println("после кормежки осталось еды: " + plate);
}
}
class Cat {
private String name;
private int appetite;
private boolean satiety;
Cat(String name, int appetite) {
this.name = name;
this.appetite = appetite;
}
public int getAppetite() {
return appetite;
}
void eat(Plate plate) {
plate.dicreaseFood(appetite);
satiety = true;
}
@Override
public String toString() {
return name + " " + "сьел - " + appetite + " " + "Поел? - " + satiety;
}
}
class Plate {
private int food;
Plate(int food) {
this.food = food;
}
void dicreaseFood(int food) {
this.food -= food;
}
void increaseFood(int food) {
this.food += food;
}
public int getFood() {
return food;
}
@Override
public String toString() {
return "" + food;
}
}