PIbd-12_Karamushko_M.K._Air.../AircraftFactory.java
2022-12-02 10:17:40 +03:00

72 lines
2.8 KiB
Java

import java.awt.*;
import java.util.Objects;
public class AircraftFactory {
private static char _separatorForObject = ':';
private static Color convertStrToColor(String str) {
String color[] = str.split(",");
int r = Integer.parseInt(color[0]);
int g = Integer.parseInt(color[1]);
int b = Integer.parseInt(color[2]);
return new Color(r, g, b);
}
public static DrawingAircraft createAircraftFromData(String data) {
String strsArr[] = data.split(_separatorForObject + "");
DrawingAircraft result;
IDrawingEngines engines;
switch(strsArr[3]) {
case "DrawingTruncatedEngines":
engines = new DrawingTruncatedEngines(Integer.parseInt(strsArr[4]), convertStrToColor(strsArr[5]));
break;
case "DrawingWavyEngines":
engines = new DrawingWavyEngines(Integer.parseInt(strsArr[4]), convertStrToColor(strsArr[5]));
break;
default:
engines = new DrawingEngines(Integer.parseInt(strsArr[4]), convertStrToColor(strsArr[5]));
}
if(strsArr.length == 9) {
result = new DrawingModernAircraft(
Integer.parseInt(strsArr[0]), Float.parseFloat(strsArr[1]), convertStrToColor(strsArr[2]),
convertStrToColor(strsArr[6]), Objects.equals(strsArr[8], "true"), Objects.equals(strsArr[7], "true")
);
} else {
result = new DrawingAircraft(
Integer.parseInt(strsArr[0]), Float.parseFloat(strsArr[1]), convertStrToColor(strsArr[2])
);
}
result.setEngines(engines);
return result;
}
public static String getDataForSave(DrawingAircraft drawingAircraft) {
EntityAircraft aircraft = drawingAircraft.AirFighter;
String res = aircraft.Speed + "" + _separatorForObject;
res += aircraft.Weight + "" + _separatorForObject;
res += aircraft.BodyColor.getRed() + "," + aircraft.BodyColor.getGreen() + "," + aircraft.BodyColor.getBlue();
res += _separatorForObject;
IDrawingEngines engines = drawingAircraft.getEngines();
res += engines.getClass().getName() + _separatorForObject;
res += engines.getCount() + "" + _separatorForObject;
res += engines.getColor().getRed() + "," + engines.getColor().getGreen() + "," + engines.getColor().getBlue();
if(aircraft instanceof EntityModernAircraft a) {
res += _separatorForObject;
res += a.DopColor.getRed() + "," + a.DopColor.getGreen() + "," + a.DopColor.getBlue();
res += _separatorForObject;
res += a.Rockets + "" + _separatorForObject;
res += a.DopWings + "";
}
return res;
}
}