|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.Arrays;
|
5 | 5 |
|
6 |
| -//Exception |
| 6 | +// Exception |
7 | 7 | class InvalidCatNameException extends Exception {
|
8 |
| - public InvalidCatNameException (String message) { |
9 |
| - super(message); |
10 |
| - } |
| 8 | + public InvalidCatNameException(String message) { |
| 9 | + super(message); |
| 10 | + } |
11 | 11 | }
|
12 | 12 |
|
13 | 13 | // Enum for Cat Breed
|
14 | 14 | enum Breed {
|
15 |
| - SIAMESE, PERSIAN, MAINCOON, SPHYNX, BENGAL, UNKNOWN |
| 15 | + SIAMESE, |
| 16 | + PERSIAN, |
| 17 | + MAINCOON, |
| 18 | + SPHYNX, |
| 19 | + BENGAL, |
| 20 | + UNKNOWN |
16 | 21 | }
|
17 | 22 |
|
18 | 23 | public class Cat {
|
19 |
| - // Member variables |
20 |
| - private String name; |
21 |
| - private int age; |
22 |
| - private double weight; |
23 |
| - private boolean isIndoor; |
24 |
| - private Breed breed; |
25 |
| - private ArrayList<String> favoriteFoods; |
| 24 | + // Member variables |
| 25 | + private String name; |
| 26 | + private int age; |
| 27 | + private double weight; |
| 28 | + private boolean isIndoor; |
| 29 | + private Breed breed; |
| 30 | + private ArrayList<String> favoriteFoods; |
26 | 31 |
|
27 |
| - // Constructor |
28 |
| - public Cat(String name, int age, double weight, Breed breed, boolean isIndoor) throws InvalidCatNameException { |
29 |
| - if (name == null || name.trim().isEmpty()) { |
30 |
| - throw new InvalidCatNameException("Cat name cannot be empty."); |
31 |
| - } |
32 |
| - this.name = name; |
33 |
| - this.age = age; |
34 |
| - this.weight = weight; |
35 |
| - this.isIndoor = isIndoor; |
36 |
| - this.breed = breed; |
37 |
| - this.favoriteFoods = new ArrayList<>(Arrays.asList("Fancy Feast", "Purina Naturals")); //Default for Ninja |
| 32 | + // Constructor |
| 33 | + public Cat(String name, int age, double weight, Breed breed, boolean isIndoor) |
| 34 | + throws InvalidCatNameException { |
| 35 | + if (name == null || name.trim().isEmpty()) { |
| 36 | + throw new InvalidCatNameException("Cat name cannot be empty."); |
38 | 37 | }
|
| 38 | + this.name = name; |
| 39 | + this.age = age; |
| 40 | + this.weight = weight; |
| 41 | + this.isIndoor = isIndoor; |
| 42 | + this.breed = breed; |
| 43 | + this.favoriteFoods = |
| 44 | + new ArrayList<>(Arrays.asList("Fancy Feast", "Purina Naturals")); // Default for Ninja |
| 45 | + } |
39 | 46 |
|
40 |
| - // Function 1: Add Favorite Food (uses collection) |
41 |
| - public void addfavoriteFood(String food) { |
42 |
| - favoriteFoods.add(food); |
43 |
| - } |
| 47 | + // Function 1: Add Favorite Food (uses collection) |
| 48 | + public void addfavoriteFood(String food) { |
| 49 | + favoriteFoods.add(food); |
| 50 | + } |
44 | 51 |
|
45 |
| - //Function 2: Print Favorite Foods (uses loop) |
46 |
| - public void printfavoriteFoods() { |
47 |
| - System.out.println(name + "'s favorite foods:"); |
48 |
| - for (String v : favoriteFoods) { |
49 |
| - System.out.println("- " + v); |
50 |
| - } |
| 52 | + // Function 2: Print Favorite Foods (uses loop) |
| 53 | + public void printfavoriteFoods() { |
| 54 | + System.out.println(name + "'s favorite foods:"); |
| 55 | + for (String v : favoriteFoods) { |
| 56 | + System.out.println("- " + v); |
51 | 57 | }
|
| 58 | + } |
52 | 59 |
|
53 |
| - // Function 3: Is the cat a senior? (uses conditional) |
54 |
| - public boolean isSenior() { |
55 |
| - return age >= 10; |
56 |
| - } |
| 60 | + // Function 3: Is the cat a senior? (uses conditional) |
| 61 | + public boolean isSenior() { |
| 62 | + return age >= 10; |
| 63 | + } |
| 64 | + |
| 65 | + // Getters for testing |
| 66 | + public String getName() { |
| 67 | + return name; |
| 68 | + } |
| 69 | + |
| 70 | + public int getAge() { |
| 71 | + return age; |
| 72 | + } |
| 73 | + |
| 74 | + public double getWeight() { |
| 75 | + return weight; |
| 76 | + } |
| 77 | + |
| 78 | + public boolean getIsIndoor() { |
| 79 | + return isIndoor; |
| 80 | + } |
| 81 | + |
| 82 | + public Breed getBreed() { |
| 83 | + return breed; |
| 84 | + } |
57 | 85 |
|
58 |
| - // Getters for testing |
59 |
| - public String getName() { return name; } |
60 |
| - public int getAge() { return age; } |
61 |
| - public double getWeight() { return weight; } |
62 |
| - public boolean getIsIndoor() { return isIndoor; } |
63 |
| - public Breed getBreed() { return breed; } |
64 |
| - public ArrayList<String> getfavoriteFoods() {return favoriteFoods; } |
65 |
| - } |
| 86 | + public ArrayList<String> getfavoriteFoods() { |
| 87 | + return favoriteFoods; |
| 88 | + } |
66 | 89 | }
|
0 commit comments