Skip to content

chore: RMiller_lesson_16_homework_design at least one custom class that represents a real-world object #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.codedifferently.lesson16.rmillcamera;

import java.util.ArrayList;

public class Camera {
private String brand;
private int megapixels;
private double price;
private ArrayList<String> supportedFormats;
private CameraMode mode;

public Camera(String brand, int megapixels, double price, CameraMode mode) {
this.brand = brand;
this.megapixels = megapixels;
this.price = price;
this.mode = mode;
this.supportedFormats = new ArrayList<>();
}

public void addFormat(String format) throws CameraException {
if (format == null || format.isEmpty()) {
throw new CameraException("Format cannot be empty.");
}
supportedFormats.add(format);
System.out.println("Added format: " + format);
}

public void showFormats() {
if (supportedFormats.isEmpty()) {
System.out.println("No formats supported yet.");
} else {
System.out.println("Supported formats:");
for (String format : supportedFormats) {
System.out.println("- " + format);
}
}
}

public void isAffordable(double budget) {
String result =
(price <= budget) ? "This camera is within your budget." : "This camera is too expensive.";
System.out.println(result);
}

public static void main(String[] args) {
try {
Camera myCamera = new Camera("Sony", 24, 899.99, CameraMode.PORTRAIT);

myCamera.addFormat("JPEG");
myCamera.addFormat("RAW");

myCamera.showFormats();
myCamera.isAffordable(1000);

} catch (CameraException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.rmillcamera;

public class CameraException extends Exception {
public CameraException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.codedifferently.lesson16.rmillcamera;

public enum CameraMode {
AUTO,
MANUAL,
PORTRAIT,
NIGHT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.codedifferently.lesson16.rmillcamera;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class CameraExceptionTest {

@Test
public void testExceptionMessage() {
CameraException ex = new CameraException("Test error message");
assertEquals("Test error message", ex.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.codedifferently.lesson16.rmillcamera;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import org.junit.jupiter.api.Test;

public class CameraModeTest {

@Test
public void testCameraModeValues() {
CameraMode[] modes = CameraMode.values();
assertArrayEquals(
new CameraMode[] {
CameraMode.AUTO, CameraMode.MANUAL, CameraMode.PORTRAIT, CameraMode.NIGHT
},
modes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.codedifferently.lesson16.rmillcamera;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CameraTest {

private Camera camera;

@BeforeEach
public void setUp() {
camera = new Camera("Canon", 20, 750.00, CameraMode.AUTO);
}

@Test
public void testAddValidFormat() throws CameraException {
camera.addFormat("PNG");
}

@Test
public void testAddEmptyFormatThrowsException() {
Exception exception = assertThrows(CameraException.class, () -> camera.addFormat(""));
assertEquals("Format cannot be empty.", exception.getMessage());
}

@Test
public void testAddNullFormatThrowsException() {
Exception exception = assertThrows(CameraException.class, () -> camera.addFormat(null));
assertEquals("Format cannot be empty.", exception.getMessage());
}

@Test
public void testIsAffordableWithinBudget() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));

camera.isAffordable(800);
String result = out.toString().trim();
assertTrue(result.contains("within your budget"));
}

@Test
public void testIsAffordableOverBudget() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));

camera.isAffordable(500);
String result = out.toString().trim();
assertTrue(result.contains("too expensive"));
}

@Test
public void testIsAffordableAtExactBudget() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));

camera.isAffordable(750.00);
String result = out.toString().trim();
assertTrue(result.contains("within your budget"));
}

@Test
public void testShowFormatsWhenEmpty() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));

camera.showFormats();
String result = out.toString().trim();
assertTrue(result.contains("No formats supported yet."));
}

@Test
public void testShowFormatsWithValues() throws CameraException {
camera.addFormat("JPEG");
camera.addFormat("RAW");

ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));

camera.showFormats();
String result = out.toString();
assertTrue(result.contains("Supported formats"));
assertTrue(result.contains("JPEG"));
assertTrue(result.contains("RAW"));
}

@Test
public void testConstructorSetsValuesCorrectly() throws Exception {
Camera cam = new Camera("Nikon", 24, 900.0, CameraMode.MANUAL);

var brandField = cam.getClass().getDeclaredField("brand");
var priceField = cam.getClass().getDeclaredField("price");

brandField.setAccessible(true);
priceField.setAccessible(true);

assertEquals("Nikon", brandField.get(cam));
assertEquals(900.0, priceField.get(cam));
}
}