Skip to content

Feat: adds Xbox class with test and exception handling #516

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
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,7 @@
package com.codedifferently.lesson16.dylans_xbox;

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

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class LoadGame {
private String filePath;

public LoadGame(String filePath) {
this.filePath = filePath;
}

public void loadGamesFromFile(Xbox xbox) throws Exception {
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
br.readLine(); // Skip the header line
while ((line = br.readLine()) != null) {
String[] gameDetails = line.split(",");
if (gameDetails.length >= 2) {

int id = Integer.parseInt(gameDetails[0].trim());
String name = gameDetails[1].trim();
xbox.getGames().put(id, name);
}
}
} catch (IOException e) {
throw new Exception("Error reading the games file: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.codedifferently.lesson16.dylans_xbox;

import java.util.ArrayList;
import java.util.HashMap;

public class Xbox {
private ArrayList<String> insertedGames = new ArrayList<>();
private HashMap<Integer, String> games;
// Declares the model of the Xbox by using the enum XboxModel
private XboxModel model;
private String color;
private int price;
// Declares if there is a disk drive on the Xbox
private boolean diskDrive = false;

// Defines a fixed set of constants for GameGenre
public enum XboxModel {
XBOX360,
XBOXONE,
XBOXONES,
XBOXONEX,
XBOXSERIESS,
XBOXSERIESX
}

// Constructor for the Xbox class
public Xbox(String model, int price, String color, boolean diskDrive, boolean diskDriveFull) {
this.model = XboxModel.valueOf(model.toUpperCase());
this.price = price;
this.color = color;
this.diskDrive = diskDrive;
this.insertedGames = new ArrayList<>();
this.games = new HashMap<>();
}

public int getInsertedGamesSize() {
return insertedGames.size();
}

// Getters for the Xbox class
public XboxModel getModel() {
return model;
}

public HashMap<Integer, String> getGames() {
return games;
}

public int getPrice() {
return price;
}

public String getColor() {
return color;
}

public boolean DiskDrive() {
return diskDrive;
}

// Method that will add a game to the disk drive
// it will check if the disk drive is empty and if it is, it will add the game to the disk drive
// by turning it to true.
public void inputGame(int id) throws Exception {
if (!diskDrive) {
throw new Exception("This Xbox does not have a disk drive. Cannot insert game.");
}
if (insertedGames.size() >= 2) {
throw new DiskDriveFullException("Disk drive is full. Cannot insert game.");
}

String gameName = games.get(id);
if (gameName == null) {
throw new Exception("Game with ID: " + id + " does not exist in the library.");
}
if (insertedGames.contains(gameName)) {
throw new Exception("Game \"" + gameName + "\" is already inserted.");
}

insertedGames.add(gameName);
games.remove(id);

System.out.println("Game \"" + gameName + "\" (ID: " + id + ") was added to the disk drive.");
}

// Method that will eject a game from the disk drive
// it will check if the game is in the drive and if it is, it will turn the drive to false.
public void ejectGame(int id) {
if (insertedGames.size() >= 1) {
insertedGames.removeAll(insertedGames);
System.out.println("Game with ID: " + id + " was ejected from the disk drive.");
} else {
System.out.println("Game with ID: " + id + " not found in the disk drive.");
}
}

// This method will print all the games in the HashMap
// By running a for loop that will iterate through the games
public void printAllGames() {
for (Integer id : games.keySet()) {
System.out.println("Game ID: " + id + ", Game Name: " + games.get(id));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
id, name
1, Call of Duty
2, Elden Ring
3, Minecraft
4, Monster Hunter
5, Fortnite
6, Marvel Rivals
7, Tetris
8, Madden NFL
9, Terraria
10,Baldur's Gate 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.codedifferently.lesson16.dylans_xboxTest;

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

import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException;
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
import com.codedifferently.lesson16.dylans_xbox.Xbox;
import org.junit.jupiter.api.Test;

public class DiskdrivefullTest {
@Test
public void testDiskDriveFullException() throws Exception {
// Create Xbox with a working disk drive
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
LoadGame loader =
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");

loader.loadGamesFromFile(xbox); // Load game library

// Insert two games to fill the disk drive
xbox.inputGame(1);
xbox.inputGame(2);

DiskDriveFullException exception =
assertThrows(
DiskDriveFullException.class,
() -> {
xbox.inputGame(3); // This should trigger the exception
});

assertEquals("Disk drive is full. Cannot insert game.", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.codedifferently.lesson16.dylans_xboxTest;

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

import com.codedifferently.lesson16.dylans_xbox.LoadGame;
import com.codedifferently.lesson16.dylans_xbox.Xbox;
import java.util.HashMap;
import org.junit.jupiter.api.Test;

public class LoadgameTest {
@Test
public void testLoadGame() throws Exception {
// Create an instance of Xbox
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);

// Create an instance of LoadGame
LoadGame loader =
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");

loader.loadGamesFromFile(xbox);
// Check if the game was loaded correctly
HashMap<Integer, String> games = xbox.getGames();
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1)
assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.codedifferently.lesson16.dylans_xboxTest;

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

import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException;
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
import com.codedifferently.lesson16.dylans_xbox.Xbox;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import org.junit.jupiter.api.Test; // Ensure LoadGame is imported

public class XboxTest {

@Test
public void testAddGame() {
LoadGame loader =
new LoadGame(
"src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); // Ensure
// LoadGame

Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // Create an instance of Xbox
try {
loader.loadGamesFromFile(xbox);
} catch (Exception e) {
e.printStackTrace();
fail("Exception occurred while loading games: " + e.getMessage());
}

HashMap<Integer, String> games = xbox.getGames();
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1)
assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV
}

@Test
public void testAddGameIfFull() {
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // false means not full yet
LoadGame loader =
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");

try {
loader.loadGamesFromFile(xbox);

// Insert two games (disk limit)
xbox.inputGame(1);
xbox.inputGame(2);

// This third insert should throw an exception
xbox.inputGame(3);
fail("Expected DiskDriveFullException to be thrown.");
} catch (DiskDriveFullException e) {
assertEquals("Disk drive is full. Cannot insert game.", e.getMessage());
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception: " + e.getMessage());
}
}

@Test
public void testXboxModelEnumValues() {
Xbox.XboxModel[] models = Xbox.XboxModel.values();
assertEquals(6, models.length);
assertEquals(Xbox.XboxModel.XBOX360, models[0]);
assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]);
}

@Test
public void testPrintAllGames() throws Exception {
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
LoadGame loader =
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");

loader.loadGamesFromFile(xbox); // Load games into library

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

xbox.printAllGames();

System.setOut(originalOut);

String expectedOutput =
"""
Game ID: 1, Game Name: Call of Duty
Game ID: 2, Game Name: Elden Ring
Game ID: 3, Game Name: Minecraft
Game ID: 4, Game Name: Monster Hunter
Game ID: 5, Game Name: Fortnite
Game ID: 6, Game Name: Marvel Rivals
Game ID: 7, Game Name: Tetris
Game ID: 8, Game Name: Madden NFL
Game ID: 9, Game Name: Terraria
Game ID: 10, Game Name: Baldur's Gate 3
""";
assertEquals(expectedOutput.trim(), outputStream.toString().trim());
}

@Test
public void testEjectGame() {
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
LoadGame loader =
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");

try {
loader.loadGamesFromFile(xbox); // Load games into library
xbox.inputGame(1); // Insert game with ID 1
assertEquals(1, xbox.getInsertedGamesSize(), "Game should be inserted.");

xbox.ejectGame(1); // Eject by name (you may use ID instead if implemented that way)
assertEquals(0, xbox.getInsertedGamesSize(), "The game should be ejected.");
} catch (Exception e) {
e.printStackTrace();
fail("Exception occurred: " + e.getMessage());
}
}

@Test
public void testGetGames() {

Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
LoadGame loader =
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");

try {
loader.loadGamesFromFile(xbox);
} catch (Exception e) {
e.printStackTrace();
fail("Exception occurred while loading games: " + e.getMessage());
}

HashMap<Integer, String> games = xbox.getGames();

assertEquals(10, games.size(), "There should be 10 games loaded.");
}

@Test
public void testGetPrice() {
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false);
int price = xbox.getPrice();
assertEquals(400, price, "The price should be 400.");
}

@Test
public void testGetColor() {
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false);
String color = xbox.getColor();
assertEquals("White", color, "The color should be White.");
}

@Test
public void testDiskDrive() {
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false);
boolean diskDrive = xbox.DiskDrive();
assertTrue(diskDrive, "The disk drive should be present.");
}
}