Skip to content

Commit 52f8b4f

Browse files
committed
Feat:Fix
1 parent 347d1bd commit 52f8b4f

File tree

1 file changed

+78
-0
lines changed
  • lesson_16/objects/objects_app/src/main/java

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
5+
public class Dog implements DogTest {
6+
7+
// Custom exception
8+
public static class DogAgeException extends Exception {
9+
10+
public DogAgeException(String message) {
11+
super(message);
12+
}
13+
}
14+
15+
// Enum for dog colors
16+
public enum Colors {
17+
WHITE, BROWN, BLONDE
18+
}
19+
20+
// Member variables
21+
private int age;
22+
private String breed;
23+
private String gender;
24+
private Colors color;
25+
private boolean isFed;
26+
private List<String> favoriteToys;
27+
28+
// Constructor
29+
public Dog(int age, String breed, String gender, Colors color, boolean isFed) throws DogAgeException {
30+
if (age < 0) {
31+
throw new DogAgeException("Age cannot be negative.");
32+
}
33+
this.age = age;
34+
this.breed = breed;
35+
this.gender = gender;
36+
this.color = color;
37+
this.isFed = isFed;
38+
this.favoriteToys = new ArrayList<>();
39+
}
40+
41+
// Member functions
42+
public String getDogDescription() {
43+
return age > 5 ? "Senior Dog" : "Young Dog"; // Conditional expression
44+
}
45+
46+
public void addFavoriteToy(String toy) {
47+
favoriteToys.add(toy);
48+
}
49+
50+
public List<String> getFavoriteToys() {
51+
List<String> toys = new ArrayList<>();
52+
for (String toy : favoriteToys) { // Loop
53+
toys.add(toy);
54+
}
55+
return toys;
56+
}
57+
58+
// Getters
59+
public int getAge() {
60+
return age;
61+
}
62+
63+
public String getBreed() {
64+
return breed;
65+
}
66+
67+
public String getGender() {
68+
return gender;
69+
}
70+
71+
public Colors getColor() {
72+
return color;
73+
}
74+
75+
public boolean isFed() {
76+
return isFed;
77+
}
78+
}

0 commit comments

Comments
 (0)