Skip to content

Commit fd098fd

Browse files
feat: adds Rasheed's custom camera data type (#519)
1 parent a51414e commit fd098fd

File tree

6 files changed

+212
-0
lines changed

6 files changed

+212
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.codedifferently.lesson16.rmillcamera;
2+
3+
import java.util.ArrayList;
4+
5+
public class Camera {
6+
private String brand;
7+
private int megapixels;
8+
private double price;
9+
private ArrayList<String> supportedFormats;
10+
private CameraMode mode;
11+
12+
public Camera(String brand, int megapixels, double price, CameraMode mode) {
13+
this.brand = brand;
14+
this.megapixels = megapixels;
15+
this.price = price;
16+
this.mode = mode;
17+
this.supportedFormats = new ArrayList<>();
18+
}
19+
20+
public void addFormat(String format) throws CameraException {
21+
if (format == null || format.isEmpty()) {
22+
throw new CameraException("Format cannot be empty.");
23+
}
24+
supportedFormats.add(format);
25+
System.out.println("Added format: " + format);
26+
}
27+
28+
public void showFormats() {
29+
if (supportedFormats.isEmpty()) {
30+
System.out.println("No formats supported yet.");
31+
} else {
32+
System.out.println("Supported formats:");
33+
for (String format : supportedFormats) {
34+
System.out.println("- " + format);
35+
}
36+
}
37+
}
38+
39+
public void isAffordable(double budget) {
40+
String result =
41+
(price <= budget) ? "This camera is within your budget." : "This camera is too expensive.";
42+
System.out.println(result);
43+
}
44+
45+
public static void main(String[] args) {
46+
try {
47+
Camera myCamera = new Camera("Sony", 24, 899.99, CameraMode.PORTRAIT);
48+
49+
myCamera.addFormat("JPEG");
50+
myCamera.addFormat("RAW");
51+
52+
myCamera.showFormats();
53+
myCamera.isAffordable(1000);
54+
55+
} catch (CameraException e) {
56+
System.out.println("Error: " + e.getMessage());
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.rmillcamera;
2+
3+
public class CameraException extends Exception {
4+
public CameraException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.rmillcamera;
2+
3+
public enum CameraMode {
4+
AUTO,
5+
MANUAL,
6+
PORTRAIT,
7+
NIGHT
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codedifferently.lesson16.rmillcamera;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class CameraExceptionTest {
8+
9+
@Test
10+
public void testExceptionMessage() {
11+
CameraException ex = new CameraException("Test error message");
12+
assertEquals("Test error message", ex.getMessage());
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.codedifferently.lesson16.rmillcamera;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class CameraModeTest {
8+
9+
@Test
10+
public void testCameraModeValues() {
11+
CameraMode[] modes = CameraMode.values();
12+
assertArrayEquals(
13+
new CameraMode[] {
14+
CameraMode.AUTO, CameraMode.MANUAL, CameraMode.PORTRAIT, CameraMode.NIGHT
15+
},
16+
modes);
17+
}
18+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.codedifferently.lesson16.rmillcamera;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class CameraTest {
13+
14+
private Camera camera;
15+
16+
@BeforeEach
17+
public void setUp() {
18+
camera = new Camera("Canon", 20, 750.00, CameraMode.AUTO);
19+
}
20+
21+
@Test
22+
public void testAddValidFormat() throws CameraException {
23+
camera.addFormat("PNG");
24+
}
25+
26+
@Test
27+
public void testAddEmptyFormatThrowsException() {
28+
Exception exception = assertThrows(CameraException.class, () -> camera.addFormat(""));
29+
assertEquals("Format cannot be empty.", exception.getMessage());
30+
}
31+
32+
@Test
33+
public void testAddNullFormatThrowsException() {
34+
Exception exception = assertThrows(CameraException.class, () -> camera.addFormat(null));
35+
assertEquals("Format cannot be empty.", exception.getMessage());
36+
}
37+
38+
@Test
39+
public void testIsAffordableWithinBudget() {
40+
ByteArrayOutputStream out = new ByteArrayOutputStream();
41+
System.setOut(new PrintStream(out));
42+
43+
camera.isAffordable(800);
44+
String result = out.toString().trim();
45+
assertTrue(result.contains("within your budget"));
46+
}
47+
48+
@Test
49+
public void testIsAffordableOverBudget() {
50+
ByteArrayOutputStream out = new ByteArrayOutputStream();
51+
System.setOut(new PrintStream(out));
52+
53+
camera.isAffordable(500);
54+
String result = out.toString().trim();
55+
assertTrue(result.contains("too expensive"));
56+
}
57+
58+
@Test
59+
public void testIsAffordableAtExactBudget() {
60+
ByteArrayOutputStream out = new ByteArrayOutputStream();
61+
System.setOut(new PrintStream(out));
62+
63+
camera.isAffordable(750.00);
64+
String result = out.toString().trim();
65+
assertTrue(result.contains("within your budget"));
66+
}
67+
68+
@Test
69+
public void testShowFormatsWhenEmpty() {
70+
ByteArrayOutputStream out = new ByteArrayOutputStream();
71+
System.setOut(new PrintStream(out));
72+
73+
camera.showFormats();
74+
String result = out.toString().trim();
75+
assertTrue(result.contains("No formats supported yet."));
76+
}
77+
78+
@Test
79+
public void testShowFormatsWithValues() throws CameraException {
80+
camera.addFormat("JPEG");
81+
camera.addFormat("RAW");
82+
83+
ByteArrayOutputStream out = new ByteArrayOutputStream();
84+
System.setOut(new PrintStream(out));
85+
86+
camera.showFormats();
87+
String result = out.toString();
88+
assertTrue(result.contains("Supported formats"));
89+
assertTrue(result.contains("JPEG"));
90+
assertTrue(result.contains("RAW"));
91+
}
92+
93+
@Test
94+
public void testConstructorSetsValuesCorrectly() throws Exception {
95+
Camera cam = new Camera("Nikon", 24, 900.0, CameraMode.MANUAL);
96+
97+
var brandField = cam.getClass().getDeclaredField("brand");
98+
var priceField = cam.getClass().getDeclaredField("price");
99+
100+
brandField.setAccessible(true);
101+
priceField.setAccessible(true);
102+
103+
assertEquals("Nikon", brandField.get(cam));
104+
assertEquals(900.0, priceField.get(cam));
105+
}
106+
}

0 commit comments

Comments
 (0)