From 686e6e55343efc29629e53ecd022a51b1acb3762 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 10 Apr 2025 20:01:33 +0000 Subject: [PATCH 01/13] feat: implement Xbox class with game management and loading functionality Todo: Add error handling and logging --- .../codedifferently/lesson16/Lesson16.java | 1 + .../lesson16/dylans_xbox/Xbox.java | 113 ++++++++++++++++++ .../lesson16/dylans_xbox/games.csv | 11 ++ .../lesson16/dylans_xbox/loadGames.java | 28 +++++ .../src/test/java/XboxTest/XboxTest.java | 66 ++++++++++ 5 files changed, 219 insertions(+) create mode 100644 lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java create mode 100644 lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv create mode 100644 lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java create mode 100644 lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java index fd5348f0..0ee2dfab 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java @@ -12,4 +12,5 @@ public static void main(String[] args) { var application = new SpringApplication(Lesson16.class); application.run(args); } + } diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java new file mode 100644 index 00000000..4bcfe048 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java @@ -0,0 +1,113 @@ +package com.codedifferently.lesson16.dylans_xbox; +import java.util.HashMap; + + /* + Create a sub-folder in the main app folder with a unique name for your work. + Design at least one custom class that represents a real-world object. //Object: Xbox HashMap + You must also incorporate an enum type as well. + Genre + The class must have at least one constructor. + I have this + The class must have at least 3 member functions. + I need this to be added maybe I can do addGame, removeGame, and getGame + One of your functions must make use of a conditional expression. + I can do this with the addGame method by implementing a check if the xbox has a disk drive && if the disk drive is empty + + One of your functions must make use of your collection member variable. + I can do this with the getGame method by calling the games inside of the hashmap + + One of your functions must make use of a loop. + I can do this with the removeGame method by using a for loop to iterate through the games and remove the game that matches the id passed in. + You must use at least one custom exception. + + I can do this with the addGame method by creating a custom exception that checks if the disk drive is empty and throws an exception if it is not. + Create a matching subfolder in the test folder and a test file. Your test must include at least 5 test methods. + Tests: + 1. testAddGame + 2. testRemoveGame + 3. testGetGame + 4. testGetAllGames + 5. testGetDiskDrive + */ +public class Xbox { + private HashMap games; + private XboxModel model; //Use the enum type here + private String color; + private int price; + private static final int MAX_GAMES = 10; + private boolean diskDrive; //Declares if there is a disk drive on the Xbox + private boolean diskDriveFull; //If there is a disk drive, this will be t/f based on if there is a disk inside the xbox + //Creates a file path that will call to the games.csv file + + //Defines a fixed set of constants for GameGenre + public enum XboxModel { + XBOX360, + XBOXONE, + XBOXONES, + XBOXONEX, + XBOXSERIESS, + XBOXSERIESX + } + + public Xbox(String model, int price, String color, boolean diskDrive) { + this.model = model; + this.price = price; + this.color = color; + this.diskDrive = diskDrive; + this.games = new HashMap<>(); + } + + public XboxModel getModel() { + return model; + } + + public int getPrice() { + return price; + } + + public String getColor() { + return color; + } + + public boolean DiskDrive() { + return diskDrive; + } + + public boolean DiskDriveFull() { + return DiskDriveFull(); + } + + public void inputGame(int id, String name) throws Exception { + + if(!diskDrive) { + throw new Exception("This Xbox does not have a disk drive. Cannot insert game."); + } + if(diskDriveFull) { + throw new Exception("Disk drive is full. Cannot insert game."); + } + games.put(id, name); + diskDriveFull = true; + System.out.println("Game with ID: " + id + " was added to the disk drive."); + + } + + public void ejectGame(int id) { + if(games.containsKey(id)) { + games.remove(id); + diskDrive = false; + System.out.println("Game with ID: " + id + " was removed from the disk drive."); + } else { + System.out.println("Game with ID: " + id + " not found in the disk drive."); + } + } + + public void printAllGames() { + for(Integer id : games.keySet()) { + System.out.println("Game ID: " + id + ", Game Name: " + games.get(id)); + } + } + + + + } + diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv new file mode 100644 index 00000000..6177872b --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv @@ -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 \ No newline at end of file diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java new file mode 100644 index 00000000..31eb42d0 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java @@ -0,0 +1,28 @@ + + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class loadGames extends Xbox { + private String filePath; + + public loadGames(String filePath) { + this.filePath = filePath; + } + + public void loadGamesFromFile() { + try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { + String line; + while ((line = br.readLine()) != null) { + String[] gameDetails = line.split(","); + int id = Integer.parseInt(gameDetails[0]); + String name = gameDetails[4]; + addGame(id, name); + } + } catch (IOException e) { + System.err.println("Error reading file: " + e.getMessage()); + } + } + +} diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java new file mode 100644 index 00000000..aa7ad241 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java @@ -0,0 +1,66 @@ +package com.codedifferently.lesson16.dylans_xbox; + +import java.beans.Transient; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.HashMap; + +import main.dylans_xbox.java.loadGames; + +public class XboxTest { + @Test + public void testaddGame() throws GameException { + Xbox xbox = new Xbox("Series X", 600, "Black", false); + loadGames loader = new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + loader.loadGamesFromFile(xbox); + + HashMap games = xbox.getGames(); + assertTrue(games.containsKey(10)); + assertEguals("Baldur's Gate 3", games.get(10)); + } + + @Test + public void testXboxModelEnumValues() { + Xbox.XboxModel[] models = Xbox.XboxModel.values(); + assertEquals(6, models.length); + assertEquals(Xbox.XboxModel.XBOXONE, models[0]); + assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]); + } + + @Test + public void testDiskDrive() { + Xbox xbox = new Xbox("XBOXONE", 400, "White", true); + assertFalse("Disk drive should be empty", xbox.DiskDrive()); + } + + @Test + public void testPrintAllGames() { + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true); + loadGames loader = new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + loader.loadGamesFromFile(xbox); + + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream originalOut = System.out; + System.setOut(new PrintStream(outputStream)); + + //Calls the printAllGames method + xbox.printAllGames(); + + System,setOut(originalOut); + String expectedOutput = "Game ID: 1, Game Name: Call of Duty\n" + + "Game ID: 2, Game Name: Elden Ring\n" + + "Game ID: 3, Game Name: Minecraft\n" + + "Game ID: 4, Game Name: Monster Hunter\n" + + "Game ID: 5, Game Name: Fortnite\n" + + "Game ID: 6, Game Name: Marvel Rivals\n" + + "Game ID: 7, Game Name: Tetris\n" + + "Game ID: 8, Game Name: Madden NFL\n" + + "Game ID: 9, Game Name: Terraria\n" + + "Game ID: 10, Game Name: Baldur's Gate 3\n"; + assertEquals(expectedOutput.trim(), outputStream.toString().trim()); + + + + } +} From 2c7568c97bdeb3c2a811831dab34bf18f65d8d0e Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 03:11:03 +0000 Subject: [PATCH 02/13] feat: refactor LoadGames class for improved game loading functionality --- .../lesson16/dylans_xbox/loadGames.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java index 31eb42d0..128e6762 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java @@ -1,28 +1,29 @@ - +package com.codedifferently.lesson16.dylans_xbox; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; -public class loadGames extends Xbox { - private String filePath; +public class LoadGames { + private String filePath; - public loadGames(String filePath) { - this.filePath = filePath; - } + public LoadGames(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(","); + int id = Integer.parseInt(gameDetails[0].trim()); + String name = gameDetails[1].trim(); - public void loadGamesFromFile() { - try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { - String line; - while ((line = br.readLine()) != null) { - String[] gameDetails = line.split(","); - int id = Integer.parseInt(gameDetails[0]); - String name = gameDetails[4]; - addGame(id, name); - } - } catch (IOException e) { - System.err.println("Error reading file: " + e.getMessage()); - } + xbox.inputGame(id, name); + } + } catch (IOException e) { + throw new Exception("Error reading the games file: " + e.getMessage()); } - + } } From ac4b84ba662c333abdf7368ec99181e5e8107aa9 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 03:12:30 +0000 Subject: [PATCH 03/13] test: enhance XboxTest with improved assertions and error handling, Currently Failing --- .../lesson16/dylans_xbox/Xbox.java | 196 +++++++++--------- .../src/test/java/XboxTest/XboxTest.java | 121 ++++++----- 2 files changed, 171 insertions(+), 146 deletions(-) diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java index 4bcfe048..b88f62ec 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java @@ -1,113 +1,115 @@ package com.codedifferently.lesson16.dylans_xbox; + import java.util.HashMap; - /* - Create a sub-folder in the main app folder with a unique name for your work. - Design at least one custom class that represents a real-world object. //Object: Xbox HashMap - You must also incorporate an enum type as well. - Genre - The class must have at least one constructor. - I have this - The class must have at least 3 member functions. - I need this to be added maybe I can do addGame, removeGame, and getGame - One of your functions must make use of a conditional expression. - I can do this with the addGame method by implementing a check if the xbox has a disk drive && if the disk drive is empty - - One of your functions must make use of your collection member variable. - I can do this with the getGame method by calling the games inside of the hashmap - - One of your functions must make use of a loop. - I can do this with the removeGame method by using a for loop to iterate through the games and remove the game that matches the id passed in. - You must use at least one custom exception. - - I can do this with the addGame method by creating a custom exception that checks if the disk drive is empty and throws an exception if it is not. - Create a matching subfolder in the test folder and a test file. Your test must include at least 5 test methods. - Tests: - 1. testAddGame - 2. testRemoveGame - 3. testGetGame - 4. testGetAllGames - 5. testGetDiskDrive - */ +/* + Create a sub-folder in the main app folder with a unique name for your work. + Design at least one custom class that represents a real-world object. //Object: Xbox HashMap + You must also incorporate an enum type as well. + Genre + The class must have at least one constructor. + I have this + The class must have at least 3 member functions. + I need this to be added maybe I can do addGame, removeGame, and getGame + One of your functions must make use of a conditional expression. + I can do this with the addGame method by implementing a check if the xbox has a disk drive && if the disk drive is empty + + One of your functions must make use of your collection member variable. + I can do this with the getGame method by calling the games inside of the hashmap + + One of your functions must make use of a loop. + I can do this with the removeGame method by using a for loop to iterate through the games and remove the game that matches the id passed in. + You must use at least one custom exception. + + I can do this with the addGame method by creating a custom exception that checks if the disk drive is empty and throws an exception if it is not. + Create a matching subfolder in the test folder and a test file. Your test must include at least 5 test methods. + Tests: + 1. testAddGame + 2. testRemoveGame + 3. testGetGame + 4. testGetAllGames + 5. testGetDiskDrive +*/ public class Xbox { - private HashMap games; - private XboxModel model; //Use the enum type here - private String color; - private int price; - private static final int MAX_GAMES = 10; - private boolean diskDrive; //Declares if there is a disk drive on the Xbox - private boolean diskDriveFull; //If there is a disk drive, this will be t/f based on if there is a disk inside the xbox - //Creates a file path that will call to the games.csv file - - //Defines a fixed set of constants for GameGenre - public enum XboxModel { - XBOX360, - XBOXONE, - XBOXONES, - XBOXONEX, - XBOXSERIESS, - XBOXSERIESX - } + private HashMap games; + private XboxModel model; // Use the enum type here + private String color; + private int price; + private static final int MAX_GAMES = 10; + private boolean diskDrive; // Declares if there is a disk drive on the Xbox + private boolean + diskDriveFull; // If there is a disk drive, this will be t/f based on if there is a disk + + // inside the xbox + + // Defines a fixed set of constants for GameGenre + public enum XboxModel { + XBOX360, + XBOXONE, + XBOXONES, + XBOXONEX, + XBOXSERIESS, + XBOXSERIESX + } - public Xbox(String model, int price, String color, boolean diskDrive) { - this.model = model; - this.price = price; - this.color = color; - this.diskDrive = diskDrive; - this.games = new HashMap<>(); - } + public Xbox(String model, int price, String color, boolean diskDrive) { + this.model = XboxModel.valueOf(model.toUpperCase()); + this.price = price; + this.color = color; + this.diskDrive = diskDrive; + this.games = new HashMap<>(); + } - public XboxModel getModel() { - return model; - } + public XboxModel getModel() { + return model; + } - public int getPrice() { - return price; - } + public HashMap getGames() { + return games; + } - public String getColor() { - return color; - } + public int getPrice() { + return price; + } - public boolean DiskDrive() { - return diskDrive; - } + public String getColor() { + return color; + } - public boolean DiskDriveFull() { - return DiskDriveFull(); - } + public boolean DiskDrive() { + return diskDrive; + } - public void inputGame(int id, String name) throws Exception { - - if(!diskDrive) { - throw new Exception("This Xbox does not have a disk drive. Cannot insert game."); - } - if(diskDriveFull) { - throw new Exception("Disk drive is full. Cannot insert game."); - } - games.put(id, name); - diskDriveFull = true; - System.out.println("Game with ID: " + id + " was added to the disk drive."); - - } - - public void ejectGame(int id) { - if(games.containsKey(id)) { - games.remove(id); - diskDrive = false; - System.out.println("Game with ID: " + id + " was removed from the disk drive."); - } else { - System.out.println("Game with ID: " + id + " not found in the disk drive."); - } - } - - public void printAllGames() { - for(Integer id : games.keySet()) { - System.out.println("Game ID: " + id + ", Game Name: " + games.get(id)); - } - } + public boolean DiskDriveFull() { + return diskDriveFull; + } + public void inputGame(int id, String name) throws Exception { + if (!diskDrive) { + throw new Exception("This Xbox does not have a disk drive. Cannot insert game."); + } + if (diskDriveFull) { + throw new Exception("Disk drive is full. Cannot insert game."); + } + games.put(id, name); + diskDriveFull = true; + System.out.println("Game with ID: " + id + " was added to the disk drive."); + } + public void ejectGame(int id) { + if (games.containsKey(id)) { + games.remove(id); + diskDrive = false; + System.out.println("Game with ID: " + id + " was removed from the disk drive."); + } else { + System.out.println("Game with ID: " + id + " not found in the disk drive."); + } } + public void printAllGames() { + for (Integer id : games.keySet()) { + System.out.println("Game ID: " + id + ", Game Name: " + games.get(id)); + } + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java index aa7ad241..d125e5b9 100644 --- a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java @@ -1,66 +1,89 @@ -package com.codedifferently.lesson16.dylans_xbox; +package XboxTest; -import java.beans.Transient; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.HashMap; -import main.dylans_xbox.java.loadGames; +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 org.junit.jupiter.api.Test; + +import com.codedifferently.lesson16.dylans_xbox.Xbox; public class XboxTest { - @Test - public void testaddGame() throws GameException { - Xbox xbox = new Xbox("Series X", 600, "Black", false); - loadGames loader = new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); - loader.loadGamesFromFile(xbox); - HashMap games = xbox.getGames(); - assertTrue(games.containsKey(10)); - assertEguals("Baldur's Gate 3", games.get(10)); - } - - @Test - public void testXboxModelEnumValues() { - Xbox.XboxModel[] models = Xbox.XboxModel.values(); - assertEquals(6, models.length); - assertEquals(Xbox.XboxModel.XBOXONE, models[0]); - assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]); + @Test + public void testAddGame() { + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", false); + loadGames loader = + new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + try { + loader.loadGamesFromFile(xbox); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception occurred while loading games: " + e.getMessage()); } - @Test - public void testDiskDrive() { - Xbox xbox = new Xbox("XBOXONE", 400, "White", true); - assertFalse("Disk drive should be empty", xbox.DiskDrive()); - } + HashMap games = + xbox.getGames(); // Fixed: Changed from `loadGames()` to `getGames()` + assertTrue(games.containsKey(10)); + assertEquals( + "Baldur's Gate 3", games.get(10)); // Fixed: Ensured the game name matches the CSV file + } - @Test - public void testPrintAllGames() { - Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true); - loadGames loader = new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); - loader.loadGamesFromFile(xbox); - + @Test + public void testXboxModelEnumValues() { + Xbox.XboxModel[] models = Xbox.XboxModel.values(); + assertEquals(6, models.length); + assertEquals( + Xbox.XboxModel.XBOX360, models[0]); // Fixed: Corrected the first model to match the enum + assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]); + } - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - PrintStream originalOut = System.out; - System.setOut(new PrintStream(outputStream)); + @Test + public void testDiskDrive() { + Xbox xbox = + new Xbox( + "XBOXONE", 400, "White", true); // Fixed: Set diskDrive to `false` to match the test + assertTrue( + xbox.DiskDrive(), "Disk drive should be empty"); // Fixed: Corrected the assertion syntax + } - //Calls the printAllGames method - xbox.printAllGames(); + @Test + public void testPrintAllGames() { + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true); + loadGames loader = + new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + try { + loader.loadGamesFromFile(xbox); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception occurred while loading games: " + e.getMessage()); + } - System,setOut(originalOut); - String expectedOutput = "Game ID: 1, Game Name: Call of Duty\n" + - "Game ID: 2, Game Name: Elden Ring\n" + - "Game ID: 3, Game Name: Minecraft\n" + - "Game ID: 4, Game Name: Monster Hunter\n" + - "Game ID: 5, Game Name: Fortnite\n" + - "Game ID: 6, Game Name: Marvel Rivals\n" + - "Game ID: 7, Game Name: Tetris\n" + - "Game ID: 8, Game Name: Madden NFL\n" + - "Game ID: 9, Game Name: Terraria\n" + - "Game ID: 10, Game Name: Baldur's Gate 3\n"; - assertEquals(expectedOutput.trim(), outputStream.toString().trim()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream originalOut = System.out; + System.setOut(new PrintStream(outputStream)); - + // Act: Calls the printAllGames method + xbox.printAllGames(); - } + // Restore the original console output + System.setOut(originalOut); + + // Assert: Verify the captured output + String expectedOutput = + "Game ID: 1, Game Name: Call of Duty\n" + + "Game ID: 2, Game Name: Elden Ring\n" + + "Game ID: 3, Game Name: Minecraft\n" + + "Game ID: 4, Game Name: Monster Hunter\n" + + "Game ID: 5, Game Name: Fortnite\n" + + "Game ID: 6, Game Name: Marvel Rivals\n" + + "Game ID: 7, Game Name: Tetris\n" + + "Game ID: 8, Game Name: Madden NFL\n" + + "Game ID: 9, Game Name: Terraria\n" + + "Game ID: 10, Game Name: Baldur's Gate 3\n"; + assertEquals(expectedOutput.trim(), outputStream.toString().trim()); + } } From 0d27828410bb61687de08927815e95ca99b25d0c Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 04:10:57 +0000 Subject: [PATCH 04/13] feat: implement LoadGame class for loading games from a file and update Xbox class for disk drive management --- .../{loadGames.java => LoadGame.java} | 13 ++- .../lesson16/dylans_xbox/Xbox.java | 11 +- .../src/test/java/XboxTest/XboxTest.java | 100 ++++++++++++++---- 3 files changed, 93 insertions(+), 31 deletions(-) rename lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/{loadGames.java => LoadGame.java} (64%) diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/LoadGame.java similarity index 64% rename from lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java rename to lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/LoadGame.java index 128e6762..fefa0a36 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/loadGames.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/LoadGame.java @@ -4,10 +4,10 @@ import java.io.FileReader; import java.io.IOException; -public class LoadGames { +public class LoadGame { private String filePath; - public LoadGames(String filePath) { + public LoadGame(String filePath) { this.filePath = filePath; } @@ -17,10 +17,13 @@ public void loadGamesFromFile(Xbox xbox) throws Exception { br.readLine(); // Skip the header line while ((line = br.readLine()) != null) { String[] gameDetails = line.split(","); - int id = Integer.parseInt(gameDetails[0].trim()); - String name = gameDetails[1].trim(); + if (gameDetails.length >= 2) { - xbox.inputGame(id, name); + int id = Integer.parseInt(gameDetails[0].trim()); + String name = gameDetails[1].trim(); + xbox.inputGame(id, name); + xbox.setDiskDriveFull(false); // Set diskDriveFull to true after adding a game + } } } catch (IOException e) { throw new Exception("Error reading the games file: " + e.getMessage()); diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java index b88f62ec..9115b220 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java @@ -37,8 +37,8 @@ public class Xbox { private int price; private static final int MAX_GAMES = 10; private boolean diskDrive; // Declares if there is a disk drive on the Xbox - private boolean - diskDriveFull; // If there is a disk drive, this will be t/f based on if there is a disk + private boolean diskDriveFull = + true; // If there is a disk drive, this will be t/f based on if there is a disk // inside the xbox @@ -52,11 +52,12 @@ public enum XboxModel { XBOXSERIESX } - public Xbox(String model, int price, String color, boolean diskDrive) { + 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.diskDriveFull = diskDriveFull; this.games = new HashMap<>(); } @@ -112,4 +113,8 @@ public void printAllGames() { System.out.println("Game ID: " + id + ", Game Name: " + games.get(id)); } } + + public void setDiskDriveFull(boolean b) { + this.diskDriveFull = b; + } } diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java index d125e5b9..a41f43bd 100644 --- a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java @@ -4,20 +4,23 @@ import java.io.PrintStream; import java.util.HashMap; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; // Ensure LoadGame is imported import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import org.junit.jupiter.api.Test; +import com.codedifferently.lesson16.dylans_xbox.LoadGame; import com.codedifferently.lesson16.dylans_xbox.Xbox; public class XboxTest { @Test public void testAddGame() { - Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", false); - loadGames loader = - new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + LoadGame loader = + new LoadGame( + "src/main/java/com/codedifferently/lesson16/dylans_xbox/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) { @@ -25,11 +28,20 @@ public void testAddGame() { fail("Exception occurred while loading games: " + e.getMessage()); } - HashMap games = - xbox.getGames(); // Fixed: Changed from `loadGames()` to `getGames()` - assertTrue(games.containsKey(10)); - assertEquals( - "Baldur's Gate 3", games.get(10)); // Fixed: Ensured the game name matches the CSV file + HashMap 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, true); // Set diskDriveFull to true + try { + xbox.inputGame(1, "Call of Duty"); + fail("Expected an exception to be thrown when adding a game to a full disk drive."); + } catch (Exception e) { + assertEquals("Disk drive is full. Cannot insert game.", e.getMessage()); + } } @Test @@ -45,16 +57,17 @@ public void testXboxModelEnumValues() { public void testDiskDrive() { Xbox xbox = new Xbox( - "XBOXONE", 400, "White", true); // Fixed: Set diskDrive to `false` to match the test + "XBOXONE", 400, "White", true, + false); // Fixed: Set diskDrive to `false` to match the test assertTrue( xbox.DiskDrive(), "Disk drive should be empty"); // Fixed: Corrected the assertion syntax } @Test public void testPrintAllGames() { - Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true); - loadGames loader = - new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); + LoadGame loader = + new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); try { loader.loadGamesFromFile(xbox); } catch (Exception e) { @@ -74,16 +87,57 @@ public void testPrintAllGames() { // Assert: Verify the captured output String expectedOutput = - "Game ID: 1, Game Name: Call of Duty\n" - + "Game ID: 2, Game Name: Elden Ring\n" - + "Game ID: 3, Game Name: Minecraft\n" - + "Game ID: 4, Game Name: Monster Hunter\n" - + "Game ID: 5, Game Name: Fortnite\n" - + "Game ID: 6, Game Name: Marvel Rivals\n" - + "Game ID: 7, Game Name: Tetris\n" - + "Game ID: 8, Game Name: Madden NFL\n" - + "Game ID: 9, Game Name: Terraria\n" - + "Game ID: 10, Game Name: Baldur's Gate 3\n"; + """ + 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/games.csv"); + try { + loader.loadGamesFromFile(xbox); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception occurred while loading games: " + e.getMessage()); + } + + // Act: Eject a game + // Act: Eject a game + xbox.ejectGame(1); + + // Assert: Verify the game was ejected (if needed, check the state of the Xbox object) + assertTrue(!xbox.getGames().containsKey(1), "Game with ID 1 should be removed from the games list."); + } + + @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/games.csv"); + try { + loader.loadGamesFromFile(xbox); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception occurred while loading games: " + e.getMessage()); + } + + // Act: Get the games + HashMap games = xbox.getGames(); + + // Assert: Verify the games are loaded correctly + assertEquals(10, games.size(), "There should be 10 games loaded."); + } } From 7adbb8d56f59b79093619a98369511a3323aa2b8 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 13:27:21 +0000 Subject: [PATCH 05/13] feat: add DiskDriveFullException --- .../lesson16/dylans_xbox/DiskDriveFullException.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/DiskDriveFullException.java diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/DiskDriveFullException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/DiskDriveFullException.java new file mode 100644 index 00000000..466a7bea --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/DiskDriveFullException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.dylans_xbox; + +public class DiskDriveFullException extends Exception { + public DiskDriveFullException(String message) { + super(message); + } +} From f52c9c815925854be1d823cefc839da8757d2e6e Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 13:28:05 +0000 Subject: [PATCH 06/13] Chore: enhance Xbox class with cleaner comments and organization --- .../lesson16/dylans_xbox/Xbox.java | 56 +++++++------------ .../src/test/java/XboxTest/XboxTest.java | 21 +++---- 2 files changed, 29 insertions(+), 48 deletions(-) diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java index 9115b220..e41ea186 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java @@ -2,45 +2,16 @@ import java.util.HashMap; -/* - Create a sub-folder in the main app folder with a unique name for your work. - Design at least one custom class that represents a real-world object. //Object: Xbox HashMap - You must also incorporate an enum type as well. - Genre - The class must have at least one constructor. - I have this - The class must have at least 3 member functions. - I need this to be added maybe I can do addGame, removeGame, and getGame - One of your functions must make use of a conditional expression. - I can do this with the addGame method by implementing a check if the xbox has a disk drive && if the disk drive is empty - - One of your functions must make use of your collection member variable. - I can do this with the getGame method by calling the games inside of the hashmap - - One of your functions must make use of a loop. - I can do this with the removeGame method by using a for loop to iterate through the games and remove the game that matches the id passed in. - You must use at least one custom exception. - - I can do this with the addGame method by creating a custom exception that checks if the disk drive is empty and throws an exception if it is not. - Create a matching subfolder in the test folder and a test file. Your test must include at least 5 test methods. - Tests: - 1. testAddGame - 2. testRemoveGame - 3. testGetGame - 4. testGetAllGames - 5. testGetDiskDrive -*/ public class Xbox { private HashMap games; - private XboxModel model; // Use the enum type here + // Declares the model of the Xbox by using the enum XboxModel + private XboxModel model; private String color; private int price; - private static final int MAX_GAMES = 10; - private boolean diskDrive; // Declares if there is a disk drive on the Xbox - private boolean diskDriveFull = - true; // If there is a disk drive, this will be t/f based on if there is a disk - - // inside the xbox + // Declares if there is a disk drive on the Xbox + private boolean diskDrive; + // If there is a disk drive, this will be t/f based on if there is a disk inside of the xbox + private boolean diskDriveFull = true; // Defines a fixed set of constants for GameGenre public enum XboxModel { @@ -52,6 +23,7 @@ public enum XboxModel { 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; @@ -61,6 +33,7 @@ public Xbox(String model, int price, String color, boolean diskDrive, boolean di this.games = new HashMap<>(); } + // Getters for the Xbox class public XboxModel getModel() { return model; } @@ -85,19 +58,27 @@ public boolean DiskDriveFull() { return diskDriveFull; } + // 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 turnign it to true. public void inputGame(int id, String name) throws Exception { + // These are my custom exceptions that will be thrown if the disk drive is empty or if the disk + // drive is full. if (!diskDrive) { throw new Exception("This Xbox does not have a disk drive. Cannot insert game."); } if (diskDriveFull) { - throw new Exception("Disk drive is full. Cannot insert game."); + throw new DiskDriveFullException("Disk drive is full. Cannot insert game."); } + games.put(id, name); diskDriveFull = true; System.out.println("Game with 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 (games.containsKey(id)) { games.remove(id); @@ -108,12 +89,15 @@ public void ejectGame(int id) { } } + // 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)); } } + // This method will remove a game from the HashMap public void setDiskDriveFull(boolean b) { this.diskDriveFull = b; } diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java index a41f43bd..75b36a68 100644 --- a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java @@ -1,16 +1,15 @@ package XboxTest; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.HashMap; - import static org.junit.jupiter.api.Assertions.assertEquals; // Ensure LoadGame is imported import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; -import org.junit.jupiter.api.Test; 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; public class XboxTest { @@ -79,13 +78,10 @@ public void testPrintAllGames() { PrintStream originalOut = System.out; System.setOut(new PrintStream(outputStream)); - // Act: Calls the printAllGames method xbox.printAllGames(); - // Restore the original console output System.setOut(originalOut); - // Assert: Verify the captured output String expectedOutput = """ Game ID: 1, Game Name: Call of Duty @@ -119,14 +115,17 @@ public void testEjectGame() { xbox.ejectGame(1); // Assert: Verify the game was ejected (if needed, check the state of the Xbox object) - assertTrue(!xbox.getGames().containsKey(1), "Game with ID 1 should be removed from the games list."); + assertTrue( + !xbox.getGames().containsKey(1), "Game with ID 1 should be removed from the games list."); } - @Test + @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/games.csv"); + try { loader.loadGamesFromFile(xbox); } catch (Exception e) { @@ -134,10 +133,8 @@ public void testGetGames() { fail("Exception occurred while loading games: " + e.getMessage()); } - // Act: Get the games HashMap games = xbox.getGames(); - // Assert: Verify the games are loaded correctly assertEquals(10, games.size(), "There should be 10 games loaded."); } } From 4549442bbe048f9e3a085d65919166ac8f5abac7 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 14:12:30 +0000 Subject: [PATCH 07/13] test: add unit tests for DiskDriveFullException and LoadGame functionality, Also simplifies imports for XboxTest --- .../test/java/XboxTest/DiskdrivefullTest.java | 23 ++++++++++++ .../src/test/java/XboxTest/LoadgameTest.java | 35 +++++++++++++++++++ .../src/test/java/XboxTest/XboxTest.java | 18 +++++++--- 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java create mode 100644 lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java new file mode 100644 index 00000000..d61bcecd --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java @@ -0,0 +1,23 @@ +package XboxTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import org.junit.jupiter.api.Test; + +import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException; +import com.codedifferently.lesson16.dylans_xbox.Xbox; + +public class DiskdrivefullTest { + @Test + public void testDiskDriveFullException() throws Exception { + // Create an instance of Xbox with diskDriveFull set to true + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true); + + try { + xbox.inputGame(1, "Call of Duty"); + fail("Expected DiskDriveFullException to be thrown."); + } catch (DiskDriveFullException e) { + assertEquals("Disk drive is full. Cannot insert game.", e.getMessage()); + } + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java new file mode 100644 index 00000000..3b1d9e7d --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java @@ -0,0 +1,35 @@ +package XboxTest; + +import java.util.HashMap; + +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 org.junit.jupiter.api.Test; + +import com.codedifferently.lesson16.dylans_xbox.LoadGame; +import com.codedifferently.lesson16.dylans_xbox.Xbox; + +public class LoadgameTest { + @Test + public void testLoadGame() { + // 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/games.csv"); + + // Load games from the file + try { + loader.loadGamesFromFile(xbox); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception occurred while loading games: " + e.getMessage()); + } + + // Check if the game was loaded correctly + HashMap 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 + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java index 75b36a68..60066c07 100644 --- a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java @@ -1,15 +1,16 @@ package XboxTest; -import static org.junit.jupiter.api.Assertions.assertEquals; // Ensure LoadGame is imported +import java.io.ByteArrayOutputStream; // Ensure LoadGame is imported +import java.io.PrintStream; +import java.util.HashMap; + +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 org.junit.jupiter.api.Test; 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; public class XboxTest { @@ -137,4 +138,11 @@ public void testGetGames() { 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."); + } } From 97e489dfdb9e18ce6a60875740577789bcf3b43e Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 14:21:54 +0000 Subject: [PATCH 08/13] feat: add games.csv file into a data folder and update Test Files to be able to find filepath. --- .../codedifferently/lesson16/Lesson16.java | 1 - .../lesson16/dylans_xbox/{ => data}/games.csv | 0 .../test/java/XboxTest/DiskdrivefullTest.java | 24 +++++----- .../src/test/java/XboxTest/LoadgameTest.java | 46 +++++++++---------- .../src/test/java/XboxTest/XboxTest.java | 22 ++++----- 5 files changed, 46 insertions(+), 47 deletions(-) rename lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/{ => data}/games.csv (100%) diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java index 0ee2dfab..fd5348f0 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java @@ -12,5 +12,4 @@ public static void main(String[] args) { var application = new SpringApplication(Lesson16.class); application.run(args); } - } diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv similarity index 100% rename from lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv rename to lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java index d61bcecd..3b1bede3 100644 --- a/lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java @@ -2,22 +2,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; -import org.junit.jupiter.api.Test; import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException; import com.codedifferently.lesson16.dylans_xbox.Xbox; +import org.junit.jupiter.api.Test; public class DiskdrivefullTest { - @Test - public void testDiskDriveFullException() throws Exception { - // Create an instance of Xbox with diskDriveFull set to true - Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true); - - try { - xbox.inputGame(1, "Call of Duty"); - fail("Expected DiskDriveFullException to be thrown."); - } catch (DiskDriveFullException e) { - assertEquals("Disk drive is full. Cannot insert game.", e.getMessage()); - } + @Test + public void testDiskDriveFullException() throws Exception { + // Create an instance of Xbox with diskDriveFull set to true + Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true); + + try { + xbox.inputGame(1, "Call of Duty"); + fail("Expected DiskDriveFullException to be thrown."); + } catch (DiskDriveFullException e) { + assertEquals("Disk drive is full. Cannot insert game.", e.getMessage()); } + } } diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java index 3b1d9e7d..45d03685 100644 --- a/lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java @@ -1,35 +1,35 @@ package XboxTest; -import java.util.HashMap; - 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 org.junit.jupiter.api.Test; 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() { - // 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/games.csv"); - - // Load games from the file - try { - loader.loadGamesFromFile(xbox); - } catch (Exception e) { - e.printStackTrace(); - fail("Exception occurred while loading games: " + e.getMessage()); - } - - // Check if the game was loaded correctly - HashMap 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 testLoadGame() { + // 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"); + + // Load games from the file + try { + loader.loadGamesFromFile(xbox); + } catch (Exception e) { + e.printStackTrace(); + fail("Exception occurred while loading games: " + e.getMessage()); } + + // Check if the game was loaded correctly + HashMap 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 + } } diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java index 60066c07..893ffedb 100644 --- a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java @@ -1,16 +1,15 @@ package XboxTest; -import java.io.ByteArrayOutputStream; // Ensure LoadGame is imported -import java.io.PrintStream; -import java.util.HashMap; - 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 org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.fail; // Ensure LoadGame is imported 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; public class XboxTest { @@ -18,7 +17,8 @@ public class XboxTest { public void testAddGame() { LoadGame loader = new LoadGame( - "src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); // Ensure 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 { @@ -67,7 +67,7 @@ public void testDiskDrive() { public void testPrintAllGames() { Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); LoadGame loader = - new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); try { loader.loadGamesFromFile(xbox); } catch (Exception e) { @@ -103,7 +103,7 @@ public void testPrintAllGames() { public void testEjectGame() { Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); LoadGame loader = - new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); try { loader.loadGamesFromFile(xbox); } catch (Exception e) { @@ -125,7 +125,7 @@ public void testGetGames() { Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); LoadGame loader = - new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); + new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); try { loader.loadGamesFromFile(xbox); @@ -139,7 +139,7 @@ public void testGetGames() { assertEquals(10, games.size(), "There should be 10 games loaded."); } - @Test + @Test public void testGetPrice() { Xbox xbox = new Xbox("XBOX360", 400, "White", true, false); int price = xbox.getPrice(); From 65a12e45e352113b1bc7932c1ae63e0f25337233 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 14:32:14 +0000 Subject: [PATCH 09/13] test: add unit tests for DiskDriveFullException and LoadGame functionality --- .../DiskdrivefullTest.java | 0 .../XboxTest => xboxtest}/LoadgameTest.java | 0 .../{java/XboxTest => xboxtest}/XboxTest.java | 34 +++++++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) rename lesson_16/objects/objects_app/src/test/{java/XboxTest => xboxtest}/DiskdrivefullTest.java (100%) rename lesson_16/objects/objects_app/src/test/{java/XboxTest => xboxtest}/LoadgameTest.java (100%) rename lesson_16/objects/objects_app/src/test/{java/XboxTest => xboxtest}/XboxTest.java (86%) diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java b/lesson_16/objects/objects_app/src/test/xboxtest/DiskdrivefullTest.java similarity index 100% rename from lesson_16/objects/objects_app/src/test/java/XboxTest/DiskdrivefullTest.java rename to lesson_16/objects/objects_app/src/test/xboxtest/DiskdrivefullTest.java diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java b/lesson_16/objects/objects_app/src/test/xboxtest/LoadgameTest.java similarity index 100% rename from lesson_16/objects/objects_app/src/test/java/XboxTest/LoadgameTest.java rename to lesson_16/objects/objects_app/src/test/xboxtest/LoadgameTest.java diff --git a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java b/lesson_16/objects/objects_app/src/test/xboxtest/XboxTest.java similarity index 86% rename from lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java rename to lesson_16/objects/objects_app/src/test/xboxtest/XboxTest.java index 893ffedb..51e1b898 100644 --- a/lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/xboxtest/XboxTest.java @@ -1,15 +1,16 @@ package XboxTest; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.HashMap; // Ensure LoadGame is imported + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; // Ensure LoadGame is imported +import static org.junit.jupiter.api.Assertions.fail; +import org.junit.jupiter.api.Test; 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; public class XboxTest { @@ -49,7 +50,7 @@ public void testXboxModelEnumValues() { Xbox.XboxModel[] models = Xbox.XboxModel.values(); assertEquals(6, models.length); assertEquals( - Xbox.XboxModel.XBOX360, models[0]); // Fixed: Corrected the first model to match the enum + Xbox.XboxModel.XBOX360, models[0]); assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]); } @@ -58,9 +59,9 @@ public void testDiskDrive() { Xbox xbox = new Xbox( "XBOXONE", 400, "White", true, - false); // Fixed: Set diskDrive to `false` to match the test + false); assertTrue( - xbox.DiskDrive(), "Disk drive should be empty"); // Fixed: Corrected the assertion syntax + xbox.DiskDrive(), "Disk drive should be empty"); } @Test @@ -111,11 +112,8 @@ public void testEjectGame() { fail("Exception occurred while loading games: " + e.getMessage()); } - // Act: Eject a game - // Act: Eject a game xbox.ejectGame(1); - // Assert: Verify the game was ejected (if needed, check the state of the Xbox object) assertTrue( !xbox.getGames().containsKey(1), "Game with ID 1 should be removed from the games list."); } @@ -145,4 +143,18 @@ public void testGetPrice() { 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."); + } } From 27b9c6a47a16d894d99c96376ac65f0663bfb54d Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 16:54:49 +0000 Subject: [PATCH 10/13] feat: refactor LoadGame and Xbox classes to improve game loading and insert disk logic; add unit tests for DiskDriveFullException and LoadGame functionality --- .../lesson16/dylans_xbox/LoadGame.java | 3 +- .../lesson16/dylans_xbox/Xbox.java | 52 +++++++-------- .../xboxtest/DiskdrivefullTest.java | 17 +++-- .../{ => java}/xboxtest/LoadgameTest.java | 2 +- .../test/{ => java}/xboxtest/XboxTest.java | 63 ++++++++++--------- 5 files changed, 74 insertions(+), 63 deletions(-) rename lesson_16/objects/objects_app/src/test/{ => java}/xboxtest/DiskdrivefullTest.java (53%) rename lesson_16/objects/objects_app/src/test/{ => java}/xboxtest/LoadgameTest.java (98%) rename lesson_16/objects/objects_app/src/test/{ => java}/xboxtest/XboxTest.java (77%) diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/LoadGame.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/LoadGame.java index fefa0a36..be931034 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/LoadGame.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/LoadGame.java @@ -21,8 +21,7 @@ public void loadGamesFromFile(Xbox xbox) throws Exception { int id = Integer.parseInt(gameDetails[0].trim()); String name = gameDetails[1].trim(); - xbox.inputGame(id, name); - xbox.setDiskDriveFull(false); // Set diskDriveFull to true after adding a game + xbox.getGames().put(id, name); } } } catch (IOException e) { diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java index e41ea186..84622f6c 100644 --- a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java @@ -1,17 +1,17 @@ package com.codedifferently.lesson16.dylans_xbox; +import java.util.ArrayList; import java.util.HashMap; public class Xbox { + private ArrayList insertedGames = new ArrayList<>(); private HashMap 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; - // If there is a disk drive, this will be t/f based on if there is a disk inside of the xbox - private boolean diskDriveFull = true; + private boolean diskDrive = false; // Defines a fixed set of constants for GameGenre public enum XboxModel { @@ -29,10 +29,14 @@ public Xbox(String model, int price, String color, boolean diskDrive, boolean di this.price = price; this.color = color; this.diskDrive = diskDrive; - this.diskDriveFull = diskDriveFull; + this.insertedGames = new ArrayList<>(); this.games = new HashMap<>(); } + public int getInsertedGamesSize() { + return insertedGames.size(); + } + // Getters for the Xbox class public XboxModel getModel() { return model; @@ -54,36 +58,37 @@ public boolean DiskDrive() { return diskDrive; } - public boolean DiskDriveFull() { - return diskDriveFull; - } - // 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 turnign it to true. - public void inputGame(int id, String name) throws Exception { - - // These are my custom exceptions that will be thrown if the disk drive is empty or if the disk - // drive is full. + // 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 (diskDriveFull) { + if (insertedGames.size() >= 2) { throw new DiskDriveFullException("Disk drive is full. Cannot insert game."); } - games.put(id, name); - diskDriveFull = true; - System.out.println("Game with ID: " + id + " was added to the disk drive."); + 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 (games.containsKey(id)) { - games.remove(id); - diskDrive = false; - System.out.println("Game with ID: " + id + " was removed from the disk drive."); + 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."); } @@ -96,9 +101,4 @@ public void printAllGames() { System.out.println("Game ID: " + id + ", Game Name: " + games.get(id)); } } - - // This method will remove a game from the HashMap - public void setDiskDriveFull(boolean b) { - this.diskDriveFull = b; - } } diff --git a/lesson_16/objects/objects_app/src/test/xboxtest/DiskdrivefullTest.java b/lesson_16/objects/objects_app/src/test/java/xboxtest/DiskdrivefullTest.java similarity index 53% rename from lesson_16/objects/objects_app/src/test/xboxtest/DiskdrivefullTest.java rename to lesson_16/objects/objects_app/src/test/java/xboxtest/DiskdrivefullTest.java index 3b1bede3..b2de9523 100644 --- a/lesson_16/objects/objects_app/src/test/xboxtest/DiskdrivefullTest.java +++ b/lesson_16/objects/objects_app/src/test/java/xboxtest/DiskdrivefullTest.java @@ -1,20 +1,29 @@ -package XboxTest; +package xboxtest; import static org.junit.jupiter.api.Assertions.assertEquals; 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 org.junit.jupiter.api.Test; public class DiskdrivefullTest { @Test public void testDiskDriveFullException() throws Exception { - // Create an instance of Xbox with diskDriveFull set to true - Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true); + // 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); try { - xbox.inputGame(1, "Call of Duty"); + xbox.inputGame(3); // This should trigger the exception fail("Expected DiskDriveFullException to be thrown."); } catch (DiskDriveFullException e) { assertEquals("Disk drive is full. Cannot insert game.", e.getMessage()); diff --git a/lesson_16/objects/objects_app/src/test/xboxtest/LoadgameTest.java b/lesson_16/objects/objects_app/src/test/java/xboxtest/LoadgameTest.java similarity index 98% rename from lesson_16/objects/objects_app/src/test/xboxtest/LoadgameTest.java rename to lesson_16/objects/objects_app/src/test/java/xboxtest/LoadgameTest.java index 45d03685..cfe676d2 100644 --- a/lesson_16/objects/objects_app/src/test/xboxtest/LoadgameTest.java +++ b/lesson_16/objects/objects_app/src/test/java/xboxtest/LoadgameTest.java @@ -1,4 +1,4 @@ -package XboxTest; +package xboxtest; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; diff --git a/lesson_16/objects/objects_app/src/test/xboxtest/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/xboxtest/XboxTest.java similarity index 77% rename from lesson_16/objects/objects_app/src/test/xboxtest/XboxTest.java rename to lesson_16/objects/objects_app/src/test/java/xboxtest/XboxTest.java index 51e1b898..3ac710d6 100644 --- a/lesson_16/objects/objects_app/src/test/xboxtest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/java/xboxtest/XboxTest.java @@ -1,16 +1,16 @@ -package XboxTest; +package xboxtest; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.HashMap; // Ensure LoadGame is imported - -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; // Ensure LoadGame is imported import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; -import org.junit.jupiter.api.Test; +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; public class XboxTest { @@ -36,12 +36,25 @@ public void testAddGame() { @Test public void testAddGameIfFull() { - Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true); // Set diskDriveFull to true + 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 { - xbox.inputGame(1, "Call of Duty"); - fail("Expected an exception to be thrown when adding a game to a full disk drive."); - } catch (Exception e) { + 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()); } } @@ -49,21 +62,10 @@ public void testAddGameIfFull() { public void testXboxModelEnumValues() { Xbox.XboxModel[] models = Xbox.XboxModel.values(); assertEquals(6, models.length); - assertEquals( - Xbox.XboxModel.XBOX360, models[0]); + assertEquals(Xbox.XboxModel.XBOX360, models[0]); assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]); } - @Test - public void testDiskDrive() { - Xbox xbox = - new Xbox( - "XBOXONE", 400, "White", true, - false); - assertTrue( - xbox.DiskDrive(), "Disk drive should be empty"); - } - @Test public void testPrintAllGames() { Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); @@ -105,17 +107,18 @@ 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); + 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 while loading games: " + e.getMessage()); + fail("Exception occurred: " + e.getMessage()); } - - xbox.ejectGame(1); - - assertTrue( - !xbox.getGames().containsKey(1), "Game with ID 1 should be removed from the games list."); } @Test From f9801a0ffdcaf48f77d6800aa995f9fb4a62feb3 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 11 Apr 2025 22:21:22 +0000 Subject: [PATCH 11/13] test: add unit tests for DiskDriveFullException and LoadGame functionality; include tests for game insertion and ejection --- .../DiskdrivefullTest.java | 6 +----- .../LoadgameTest.java | 7 ++++--- .../XboxTest.java | 14 +++++--------- 3 files changed, 10 insertions(+), 17 deletions(-) rename lesson_16/objects/objects_app/src/test/{java/xboxtest => dylans_xboxTest.java}/DiskdrivefullTest.java (81%) rename lesson_16/objects/objects_app/src/test/{java/xboxtest => dylans_xboxTest.java}/LoadgameTest.java (97%) rename lesson_16/objects/objects_app/src/test/{java/xboxtest => dylans_xboxTest.java}/XboxTest.java (93%) diff --git a/lesson_16/objects/objects_app/src/test/java/xboxtest/DiskdrivefullTest.java b/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/DiskdrivefullTest.java similarity index 81% rename from lesson_16/objects/objects_app/src/test/java/xboxtest/DiskdrivefullTest.java rename to lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/DiskdrivefullTest.java index b2de9523..62043272 100644 --- a/lesson_16/objects/objects_app/src/test/java/xboxtest/DiskdrivefullTest.java +++ b/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/DiskdrivefullTest.java @@ -1,11 +1,7 @@ -package xboxtest; +package com.codedifferently.lesson16.dylans_xbox; import static org.junit.jupiter.api.Assertions.assertEquals; 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 org.junit.jupiter.api.Test; public class DiskdrivefullTest { diff --git a/lesson_16/objects/objects_app/src/test/java/xboxtest/LoadgameTest.java b/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/LoadgameTest.java similarity index 97% rename from lesson_16/objects/objects_app/src/test/java/xboxtest/LoadgameTest.java rename to lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/LoadgameTest.java index cfe676d2..6a90ec1d 100644 --- a/lesson_16/objects/objects_app/src/test/java/xboxtest/LoadgameTest.java +++ b/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/LoadgameTest.java @@ -1,13 +1,14 @@ -package xboxtest; +package dylans_xboxTest.java; + +import java.util.HashMap; 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 org.junit.jupiter.api.Test; 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 diff --git a/lesson_16/objects/objects_app/src/test/java/xboxtest/XboxTest.java b/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/XboxTest.java similarity index 93% rename from lesson_16/objects/objects_app/src/test/java/xboxtest/XboxTest.java rename to lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/XboxTest.java index 3ac710d6..e292d1f1 100644 --- a/lesson_16/objects/objects_app/src/test/java/xboxtest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/XboxTest.java @@ -1,15 +1,11 @@ -package xboxtest; +package com.codedifferently.lesson16.dylans_xbox; +import java.io.ByteArrayOutputStream; // Ensure LoadGame is imported +import java.io.PrintStream; +import java.util.HashMap; -import static org.junit.jupiter.api.Assertions.assertEquals; // Ensure LoadGame is imported +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; public class XboxTest { From 0a7baed17418ae6128cdd835cfcf76e665842c15 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 12 Apr 2025 01:32:32 +0000 Subject: [PATCH 12/13] Feat: Fixed folder name of test folder to be more conventional --- .../dylans_xboxTest}/DiskdrivefullTest.java | 6 +++++- .../lesson16/dylans_xboxTest}/LoadgameTest.java | 7 +++---- .../lesson16/dylans_xboxTest}/XboxTest.java | 14 +++++++++----- 3 files changed, 17 insertions(+), 10 deletions(-) rename lesson_16/objects/objects_app/src/test/{dylans_xboxTest.java => java/com/codedifferently/lesson16/dylans_xboxTest}/DiskdrivefullTest.java (79%) rename lesson_16/objects/objects_app/src/test/{dylans_xboxTest.java => java/com/codedifferently/lesson16/dylans_xboxTest}/LoadgameTest.java (95%) rename lesson_16/objects/objects_app/src/test/{dylans_xboxTest.java => java/com/codedifferently/lesson16/dylans_xboxTest}/XboxTest.java (93%) diff --git a/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/DiskdrivefullTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.java similarity index 79% rename from lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/DiskdrivefullTest.java rename to lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.java index 62043272..dad45ba5 100644 --- a/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/DiskdrivefullTest.java +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.java @@ -1,7 +1,11 @@ -package com.codedifferently.lesson16.dylans_xbox; +package com.codedifferently.lesson16.dylans_xboxTest; import static org.junit.jupiter.api.Assertions.assertEquals; 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 org.junit.jupiter.api.Test; public class DiskdrivefullTest { diff --git a/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/LoadgameTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.java similarity index 95% rename from lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/LoadgameTest.java rename to lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.java index 6a90ec1d..9b1ea9bf 100644 --- a/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/LoadgameTest.java +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.java @@ -1,14 +1,13 @@ -package dylans_xboxTest.java; - -import java.util.HashMap; +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 org.junit.jupiter.api.Test; 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 diff --git a/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.java similarity index 93% rename from lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/XboxTest.java rename to lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.java index e292d1f1..205185e6 100644 --- a/lesson_16/objects/objects_app/src/test/dylans_xboxTest.java/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.java @@ -1,12 +1,16 @@ -package com.codedifferently.lesson16.dylans_xbox; -import java.io.ByteArrayOutputStream; // Ensure LoadGame is imported -import java.io.PrintStream; -import java.util.HashMap; +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 org.junit.jupiter.api.Test; + +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 { From ba88be3f0222b8d6d96fe71350a00fdddaf852b0 Mon Sep 17 00:00:00 2001 From: Dylan Date: Mon, 14 Apr 2025 13:14:43 +0000 Subject: [PATCH 13/13] Fix: Converts try/catch methods into assertthrows methods --- .../dylans_xboxTest/DiskdrivefullTest.java | 16 +++++++++------- .../lesson16/dylans_xboxTest/LoadgameTest.java | 12 ++---------- .../lesson16/dylans_xboxTest/XboxTest.java | 10 +++------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.java index dad45ba5..e6057d9b 100644 --- a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.java +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.java @@ -1,7 +1,7 @@ package com.codedifferently.lesson16.dylans_xboxTest; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException; import com.codedifferently.lesson16.dylans_xbox.LoadGame; @@ -22,11 +22,13 @@ public void testDiskDriveFullException() throws Exception { xbox.inputGame(1); xbox.inputGame(2); - try { - xbox.inputGame(3); // This should trigger the exception - fail("Expected DiskDriveFullException to be thrown."); - } catch (DiskDriveFullException e) { - assertEquals("Disk drive is full. Cannot insert game.", e.getMessage()); - } + DiskDriveFullException exception = + assertThrows( + DiskDriveFullException.class, + () -> { + xbox.inputGame(3); // This should trigger the exception + }); + + assertEquals("Disk drive is full. Cannot insert game.", exception.getMessage()); } } diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.java index 9b1ea9bf..ea8bdbfd 100644 --- a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.java +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.java @@ -2,7 +2,6 @@ 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.LoadGame; import com.codedifferently.lesson16.dylans_xbox.Xbox; @@ -11,7 +10,7 @@ public class LoadgameTest { @Test - public void testLoadGame() { + public void testLoadGame() throws Exception { // Create an instance of Xbox Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); @@ -19,14 +18,7 @@ public void testLoadGame() { LoadGame loader = new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); - // Load games from the file - try { - loader.loadGamesFromFile(xbox); - } catch (Exception e) { - e.printStackTrace(); - fail("Exception occurred while loading games: " + e.getMessage()); - } - + loader.loadGamesFromFile(xbox); // Check if the game was loaded correctly HashMap games = xbox.getGames(); assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1) diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.java index 205185e6..25d48d7d 100644 --- a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.java +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.java @@ -67,16 +67,12 @@ public void testXboxModelEnumValues() { } @Test - public void testPrintAllGames() { + 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"); - try { - loader.loadGamesFromFile(xbox); - } catch (Exception e) { - e.printStackTrace(); - fail("Exception occurred while loading games: " + e.getMessage()); - } + + loader.loadGamesFromFile(xbox); // Load games into library ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PrintStream originalOut = System.out;