What is another path or method to add a user defined exception to the code listed below? Is it as simple as just changing the variables?
import java.util.Scanner; import java.util.ArrayList; import java.util.InputMismatchException;
public class WK7DISCHongA { static ArrayList shipYard = new ArrayList<>(); static Scanner stdin = new Scanner(System.in); static Scanner shipScan = new Scanner(System.in); static Scanner shipScan1 = new Scanner(System.in); static Scanner cruiseScan = new Scanner(System.in); static Scanner warScan = new Scanner(System.in); // if you enter your ship's name as Alex Hong in any case then this exception is thrown static class HongException extends Exception { String str1; // Constructor of user defined exception class HongException(String alexhong) { str1 = alexhong; } public String toString(){ return ("(HongException) invalid name entered: "+ str1 + ". Your ship has been assigned the name: TBD. Select option 3 in the menu to change it.") ; } } public static class Ship { int shipType; Scanner stdin = new Scanner(System.in); String shipName; String shipOwner; int yearBuilt; double fuelLevel = 100; public Ship(String shipName, String shipOwner, int yearBuilt, double fuelLevel, int shipType) { // USER DEFINED EXCEPTION - you cannot name your ship "Alex Hong" in any case. If that name is // detected then the ship will be automatically called "TBD" System.out.print("Enter ship name (NOTE: You cannot call your ship Alex Hong): "); boolean flag1 = true; while (flag1) { try { // verify if the name entered is "Alex Hong". If so then throw the user defined exception String nameInput = shipScan.nextLine(); if (nameInput.toLowerCase().equals("alex hong")) { throw new HongException(nameInput); } else { this.shipName = nameInput; flag1 = true; break; } } // if the user defined exception is caught then the program automatically assigns the default name "TBD" to the ship catch (HongException e) { System.out.println(e); this.shipName = "TBD"; break; } } System.out.print("Enter ship owner: "); this.shipOwner = readString(); System.out.print("Enter year built (must be from 2000 to 2022): "); boolean flag = true; while (flag) { // use Try/Catch statement to verify if the entry is an integer, if not then catch the exception and re-loop back to beginning try { int tempYear = shipScan1.nextInt(); if (tempYear >= 2000 && tempYear < 2023) { this.yearBuilt = tempYear; flag = false; break; } else { System.out.print("ERROR: Year must be from 2000 to 2022. Enter year built: "); flag = true; } } catch (InputMismatchException e) { System.out.print("ERROR - non integer detected. Enter the year built (2000-2022): "); this.yearBuilt = 0; shipScan1.next(); } } // when a ship is created it has a full tank of fuel. you can change this in the main menu using the two overloading constructors - one for int and one for float } public String getName() { return shipName; } public String getOwner() { return shipOwner; } public int getYear() { return yearBuilt; } // UPDATE - Use the user-defined exception in the set method for the ship name public void setShipName() { System.out.print("Update ship name: "); try { // throw the exception if illegal name detected String nameInput = readString(); if (nameInput.toLowerCase().equals("alex hong")) { throw new HongException(nameInput); } else { this.shipName = nameInput; } } // assign the default name "TBD" if the exception is caught catch (HongException e) { System.out.println(e); this.shipName = "TBD"; } } public void setOwner() { System.out.println("Update ship owner: "); this.shipOwner = readString(); } // constructor overload: you can enter either an integer or double as the fuel level public void setFuel(int fuelInput) { this.fuelLevel = fuelInput; } public void setFuel(double fuelInput) { this.fuelLevel = fuelInput; } // this method will be over ridden to add strings for sub-class specific attributes: StarRating and OnDuty public String toString() { return "(Ship Name): " + this.shipName + " (Ship Owner) " + this.shipOwner + " (Year Built): " + this.yearBuilt ; } public int getType() { return this.shipType; } // overriding method this method defines a generic method in Ship. the two subclasses will override this method with their own special action public void specialAction() { System.out.println("Flares Deployed!"); } } static void displayShips() { if (shipYard.size() == 0) { System.out.println("There are no ships to display"); return; } else // iterate through the array list and display the contents System.out.println("--SHIPYARD--"); for (int i=0; i< shipYard.size(); i++) { System.out.println(shipYard.get(i)); } } static class cruiseShip extends Ship{ public int starRating; public int shipType = 1; public String shipType1 = "Cruise Ship"; public cruiseShip(String shipName, String shipOwner, int yearBuilt, double fuelLevel, int shipType) { super(shipName, shipOwner, yearBuilt, fuelLevel, shipType); // EXCEPTION if the user inputs a non-integer, the program will loop back for another user prompt for input boolean cruiseFlag = true; while (cruiseFlag) { try { System.out.print("Enter Star Rating: "); boolean flag = true; while (flag) { int tempRating = cruiseScan.nextInt(); if (tempRating >= 0 && tempRating < 6) { this.starRating = tempRating; this.shipType = 1; cruiseFlag = false; break; } else { System.out.print("ERROR: Rating must be from 0 to 5. Enter cruise star rating: "); flag = true; } } } catch (InputMismatchException e) { System.out.print("ERROR: not an integer. Enter star rating (0-5): "); this.shipType = 1; cruiseScan.next(); cruiseFlag = true; } } } public int getType() { return this.shipType; } // UPDATE - Added try/catch from above to the setStars method public void setStars() { System.out.print("Enter New Star Rating: "); boolean flag = true; while (flag) { try { int tempRating = stdin.nextInt(); if (tempRating >= 0 && tempRating < 6) { this.starRating = tempRating; flag = false; break; } else { System.out.print("ERROR: Rating must be from 0 to 5. Enter cruise star rating: "); flag = true; } } catch (InputMismatchException e) { System.out.print("ERROR: not an integer. Enter star rating (0-5): "); this.starRating = 0; stdin.next(); } } } // method override of special Action. For the cruise ship it prints out a fire works show announcement public void specialAction() { if (this.starRating < 4) { System.out.println(" Fire Works Show! Boom! "); } else { System.out.println(" Free caviar and ball room dancing lessons for the ship! "); } } // this overrides the toString() method of String class so that we can read the output instead of seeing the // class name @ hash code output @Override // this method overrides the display method in ship to add strings for number of Stars public String toString() { return "(NAME): " + this.shipName + " (OWNER): " + this.shipOwner + " (CURRENT FUEL LEVEL): " + fuelLevel + " (STAR RATING): " + this.starRating + " (SHIP TYPE): " + shipType1 + " (YEAR BUILT): " + this.yearBuilt; } } static class warShip extends Ship{ public int shipType = 2; public boolean onDuty; public String shipType2 = "Warship"; public warShip(String boatName, String boatOwner, int yearBuilt, double fuelLevel, int shipType) { super(boatName, boatOwner, yearBuilt, fuelLevel, shipType); int selection; System.out.println("Enter ship status. Enter 1 for On Duty. Enter 2 for Off Duty"); boolean flag = true; while (flag) { selection = stdin.nextInt(); if (selection == 1) { onDuty = true; this.shipType = 2; flag = false; break; } else if (selection == 2) { onDuty = false; flag = false; break; } else { System.out.println("ERROR: Invalid choice. Enter 1 for On Duty. Enter 2 for Off Duty"); } } } public int getType() { return this.shipType; } public void setStatus() { System.out.println("Enter 1 for on duty, Enter 2 for off duty: "); int selection = stdin.nextInt(); boolean flag2 = true; while (flag2) { if(selection == 1) { this.onDuty = true; flag2 = false; break; } else if (selection == 2) { this.onDuty = false; flag2 = false; break; } else { System.out.println("invalid choice. must be 1 for on duty or 2 for off duty: "); } } } // method override public void specialAction() { if (this.onDuty == true) { System.out.println(" Guns are being tested! "); } else { System.out.println(" Polishing the decks. "); } } // this method overrides the display method in ship to add strings for OnDuty or not public String toString() { return "(NAME): " + this.shipName + " (OWNER): " + this.shipOwner + " (CURRENT FUEL LEVEL): " + fuelLevel + " (ON DUTY STATUS): " + this.onDuty + " (SHIP TYPE): " + shipType2 + " (YEAR BUILT): " + this.yearBuilt; } } public static void editShip() { if (shipYard.size() == 0) { System.out.println("Ship Yard is empty. Add some ships"); return; } else { System.out.print("Enter ship name (case sensitive): "); String tempName = readString(); for (int i = 0; i < shipYard.size(); i++) { if (shipYard.get(i).getName().equals(tempName) && shipYard.get(i).getType() == 1) { System.out.println("------- EDIT " + shipYard.get(i).getName() + " -------"); System.out.println("Enter 1 to change Name, Enter 2 to change Owner, Enter 3 to change Star Rating: "); int choice = stdin.nextInt(); switch (choice) { case 1 : shipYard.get(i).setShipName(); return; case 2 : shipYard.get(i).setOwner(); return; case 3 : ((cruiseShip)shipYard.get(i)).setStars(); return; default: System.out.println("Invalid Choice./n"); } } else if (shipYard.get(i).getName().equals(tempName) && shipYard.get(i).getType() == 2) { System.out.println("------- EDIT " + shipYard.get(i).getName() + " -------"); System.out.println("Enter 1 to change Name, Enter 2 to change Owner, Enter 3 to change Duty Status: "); int choice = stdin.nextInt(); switch (choice) { case 1 : shipYard.get(i).setShipName(); return; case 2 : shipYard.get(i).setOwner(); return; case 3 : ((warShip)shipYard.get(i)).setStatus(); return; default: System.out.println("Invalid Choice./n"); } } } System.out.println("Error - Ship name not found."); } } // this method looks up a ship by name and overrides the Ship special action method and performs the // special action that depends on the type of ship it is and which attributes it has public static void specialAction() { if (shipYard.size() == 0) { System.out.println("There are no ships to perform a special action. Add some ships"); return; } else { System.out.print("Enter ship name (case sensitive): "); String tempName = readString(); for (int i = 0; i < shipYard.size(); i++) { // look through the ship yard and if the name matches then perform the special action method which will override the ship special method // based on what type of ship it is and the attributes it has if (shipYard.get(i).getName().equals(tempName)) { shipYard.get(i).specialAction(); return; } } System.out.println("Error - ship not found."); } } static void editFuelLevel() { if (shipYard.size() == 0) { System.out.println("There are no ships to edit fuel. Add some ships"); return; } else { System.out.print("Enter ship name (case sensitive): "); String tempName = readString(); for (int i = 0; i < shipYard.size(); i++) { // look through the ship yard and if the name matches then perform the edit fuel method which is an overloaded method // you can either input fuel level as an integer or double // based on what type of ship it is and the attributes it has if (shipYard.get(i).getName().equals(tempName)) { boolean flag3 = true; while (flag3) { System.out.println("Enter 1 to enter imprecise amount (integer). Enter 2 to enter precise amount(double)"); int selection = stdin.nextInt(); if (selection == 1) { System.out.println("Enter fuel level percentage as an integer: "); int tempFuel = stdin.nextInt(); if (tempFuel >= 0 && tempFuel < 101) { // call the overloaded method to input an integer value for fuel shipYard.get(i).setFuel(tempFuel); return; } else { System.out.println("Invalid input. must be between 0 and 100"); } } else if (selection == 2) { System.out.println("Enter fuel level percentage as a double: "); double tempFuel = stdin.nextDouble(); if (tempFuel >= 0 && tempFuel < 101) { // call the overloaded method to input a double value for fuel shipYard.get(i).setFuel(tempFuel); return; } else { System.out.println("Invalid input. must be between 0 and 100"); } } else { System.out.println("Invalid selection"); } } } } System.out.println("Error - ship not found."); } } // display the options that the user can select from static void menu() { System.out.println("------- SHIPYARD MENU -------"); System.out.println("1: Create cruise ship"); System.out.println("2: Create war ship"); System.out.println("3: Edit a ship"); System.out.println("4: Perform special action"); System.out.println("5: Enter Fuel Level"); System.out.println("6: Show ShipYard"); System.out.println("9: Exit program"); } // pass in the integer selection and public static void processChoice(int c) { switch (c) { case 1 : shipYard.add(new cruiseShip("", "", 0, 100, 1)); break; case 2 : shipYard.add(new warShip("", "", 0, 100, 2)); break; case 3 : editShip(); break; case 4 : specialAction(); break; case 5 : editFuelLevel(); break; case 6 : displayShips(); break; case 9 : break; default: System.out.print("Invalid choice. "); break; } } public static void main(String[] args) { ArrayList shipYard = new ArrayList<>(); Scanner stdin = new Scanner (System.in); int selection; do { menu(); System.out.print("Enter Selection: "); try { selection = stdin.nextInt(); } catch (InputMismatchException e) { selection = 8; continue; } processChoice(selection); } while (selection != 9); System.out.print("Thanks for using the program. Goodbye!"); } // method to allow for multiple words to be entered private static String readString() { Scanner read = new Scanner(System.in); return read.nextLine(); } }