import java.awt.*; public class ArtillerySerde { // Artillery Serialization/Deserialization private static final char _separatorForObject = ':'; public static DrawingArtillery deserialize(String info) { String[] strings = info.split(Character.toString(_separatorForObject)); int speed = Integer.parseInt(strings[0]); float weight = Float.parseFloat(strings[1]); Color bodyColor = new Color(Integer.parseInt(strings[2])); IDrawingRollers rollers = switch (strings[3]) { case "DrawingRollers" -> new DrawingRollers(Integer.parseInt(strings[4]), bodyColor); case "DrawingCrossRollers" -> new DrawingCrossRollers(Integer.parseInt(strings[4]), bodyColor); case "DrawingSquaredRollers" -> new DrawingSquaredRollers(Integer.parseInt(strings[4]), bodyColor); }; if (strings.length == 5) { EntityArtillery entity = new EntityArtillery(speed, weight, bodyColor); return new DrawingArtillery(entity, rollers); } if (strings.length == 8) { Color dopColor = new Color(Integer.parseInt(strings[5])); boolean weapon = Boolean.parseBoolean(strings[6]); boolean salvoBattery = Boolean.parseBoolean(strings[7]); EntityAdvancedArtillery entity = new EntityAdvancedArtillery(speed, weight, bodyColor, dopColor, weapon, salvoBattery); return new DrawingAdvancedArtillery(entity, rollers); } return null; } public static String serialize(DrawingArtillery drawingArtillery) { EntityArtillery artillery = drawingArtillery.getArtillery(); String result = String.format( "%d%c%f%c%d%c%s%c%d", artillery.getSpeed(), _separatorForObject, artillery.getWeight(), _separatorForObject, artillery.getBodyColor().getRGB(), _separatorForObject, drawingArtillery.getRollers().getClass().getSimpleName(), _separatorForObject, drawingArtillery.getRollers().getRollersCount() ); if (!(artillery instanceof EntityAdvancedArtillery advanced)) { return result; } return String.format( "%s%c%d%c%b%c%b", result, _separatorForObject, advanced.getDopColor().getRGB(), _separatorForObject, advanced.getWeapon(), _separatorForObject, advanced.getSalvoBattery() ); } }