-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathHW7Lesson.java
More file actions
88 lines (74 loc) · 1.92 KB
/
HW7Lesson.java
File metadata and controls
88 lines (74 loc) · 1.92 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
/**
* Java. Level 1. Lesson 7. Example of homework
*
* @author Sergey Iryupin
* @version dated Oct 20, 2017
*/
class HW7Lesson {
public static void main(String[] args) {
Cat[] cats = {
new Cat("Barsik", 15), new Cat("Murzik", 10), new Cat("Vasily", 20)
};
Plate plate = new Plate(50, 30);
System.out.println(plate);
// feeding cats first time
for (Cat cat : cats) {
cat.eat(plate);
System.out.println(cat);
}
// adding food in the plate
System.out.println(plate);
plate.add(40);
System.out.println(plate);
// feeding cats second time
for (Cat cat : cats) {
cat.setFullness(false);
cat.eat(plate);
System.out.println(cat);
}
System.out.println(plate);
}
}
class Cat {
protected String name;
protected int appetite; // ability to eat for 1 time
protected boolean fullness; // satiety status
Cat(String name, int appetite) {
this.name = name;
this.appetite = appetite;
fullness = false;
}
void setFullness(boolean status) {
fullness = status;
}
void eat(Plate plate) {
if (!fullness)
fullness = plate.decreaseFood(appetite);
}
@Override
public String toString() {
return "{name=" + name + ", appetite=" +
appetite + ", fullness=" + fullness + "}";
}
}
class Plate {
protected int volume;
protected int food;
Plate(int volume, int food) {
this.volume = volume;
this.food = food;
}
boolean decreaseFood(int portion) {
if (food < portion) return false;
food -= portion;
return true;
}
void add(int food) {
if (this.food + food <= volume)
this.food += food;
}
@Override
public String toString() {
return "plate: " + food;
}
}