|
| 1 | +package com.codedifferently.lesson16.NintendoSwitch; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +public class NintendoSwitch { |
| 6 | + |
| 7 | + public enum Model { |
| 8 | + STANDARD, |
| 9 | + LITE, |
| 10 | + OLED |
| 11 | + } |
| 12 | + |
| 13 | + private String serialNumber; |
| 14 | + private Model model; |
| 15 | + private boolean isDocked; |
| 16 | + private double batteryLife; // This will be in hours. |
| 17 | + private ArrayList<String> installedGames; |
| 18 | + |
| 19 | + public class InvalidBatteryException extends Exception { |
| 20 | + public InvalidBatteryException(String message) { |
| 21 | + super(message); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public NintendoSwitch( |
| 26 | + String serialNumber, |
| 27 | + Model model, |
| 28 | + boolean isDocked, |
| 29 | + double batteryLife, |
| 30 | + ArrayList<String> installedGames) { |
| 31 | + this.serialNumber = serialNumber; |
| 32 | + this.model = model; |
| 33 | + this.isDocked = isDocked; |
| 34 | + this.batteryLife = batteryLife; |
| 35 | + this.installedGames = installedGames; |
| 36 | + } |
| 37 | + |
| 38 | + // Function 1: This will check the battery status. |
| 39 | + public void checkBatteryStatus() throws InvalidBatteryException { |
| 40 | + // Conditional expression checking battery status |
| 41 | + if (batteryLife < 0) { |
| 42 | + throw new InvalidBatteryException("Battery life cannot be negative."); |
| 43 | + } |
| 44 | + System.out.println("Battery life: " + batteryLife + " hours."); |
| 45 | + } |
| 46 | + |
| 47 | + // Function 2: Adds a game to the installed games collection |
| 48 | + public void installGame(String game) { |
| 49 | + installedGames.add(game); |
| 50 | + System.out.println(game + " has been added to your Nintendo Switch."); |
| 51 | + } |
| 52 | + |
| 53 | + // Function 3: Displays all installed games using a loop |
| 54 | + public void displayInstalledGames() { |
| 55 | + System.out.println("Installed games on the Nintendo Switch:"); |
| 56 | + // Using a loop to iterate over installed games |
| 57 | + for (String game : installedGames) { |
| 58 | + System.out.println("- " + game); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Function 4: Checks if the Switch is docked and suggests a mode (using conditional expression) |
| 63 | + public void suggestMode() { |
| 64 | + // Using a conditional expression to suggest a mode |
| 65 | + String mode = isDocked ? "TV mode" : "Handheld mode"; |
| 66 | + System.out.println("Your Nintendo Switch is in " + mode + "."); |
| 67 | + } |
| 68 | + |
| 69 | + // Getter for Model |
| 70 | + public Model getModel() { |
| 71 | + return model; |
| 72 | + } |
| 73 | + |
| 74 | + // Getter for Serial Number |
| 75 | + public String getSerialNumber() { |
| 76 | + return serialNumber; |
| 77 | + } |
| 78 | + |
| 79 | + // Main method to test the class |
| 80 | + public static void main(String[] args) { |
| 81 | + // Create an ArrayList to hold installed games |
| 82 | + ArrayList<String> games = new ArrayList<>(); |
| 83 | + games.add("Super Mario Odyssey"); |
| 84 | + games.add("The Legend of Zelda: Breath of the Wild"); |
| 85 | + |
| 86 | + // This will create a NintendoSwitch object |
| 87 | + NintendoSwitch switchConsole = new NintendoSwitch("SN123456789", Model.OLED, true, 4.5, games); |
| 88 | + |
| 89 | + try { |
| 90 | + // Test methods |
| 91 | + switchConsole.checkBatteryStatus(); // Check battery life |
| 92 | + switchConsole.installGame("Animal Crossing: New Horizons"); // Install a new game |
| 93 | + switchConsole.displayInstalledGames(); // Display installed games |
| 94 | + switchConsole.suggestMode(); // This suggests whether to use in docked or handheld mode |
| 95 | + } catch (InvalidBatteryException e) { |
| 96 | + System.out.println("Error: " + e.getMessage()); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments