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