import java.util.Scanner; class Character { private String name; private String classType; private int level; // Constructor for Character class public Character(String name, String classType, int level) { this.name = name; this.classType = classType; this.level = level; } // Static method to create a character with validation public static Character createCharacter(String name, String classType, int level) throws IllegalArgumentException { if (name == null || name.trim().isEmpty()) { throw new IllegalArgumentException("Name cannot be empty."); } if (!classType.equalsIgnoreCase("warrior") && !classType.equalsIgnoreCase("mage")) { throw new IllegalArgumentException("Class type must be either 'warrior' or 'mage'."); } if (level < 1 || level > 100) { throw new IllegalArgumentException("Level must be between 1 and 100."); } return new Character(name, classType, level); } @Override public String toString() { return "Character { Name: " + name + ", Class Type: " + classType + ", Level: " + level + " }"; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { System.out.print("Enter character name: "); String name = scanner.nextLine(); System.out.print("Enter class type (warrior/mage): "); String classType = scanner.nextLine(); System.out.print("Enter level (1-100): "); int level = scanner.nextInt(); // Attempt to create the character Character character = Character.createCharacter(name, classType, level); System.out.println("Character created successfully: " + character); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } finally { scanner.close(); } } }