PIbd-21_Kouvshinoff_T._A._W.../laba1Loco/ExtentionDrawingTrain.java
2023-11-20 01:46:03 +04:00

93 lines
4.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package laba1Loco;
import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
public class ExtentionDrawingTrain {
/// <summary>
/// Создание объекта из строки
/// </summary>
/// <param name="info">Строка с данными для создания объекта</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
/// <returns>Объект</returns>
public static DrawingTrain CreateDrawingTrain(String info, String separatorForObject, int width, int height)
{
String[] strs = info.split(separatorForObject);
if (strs.length == 5)
{
String[] colorValues = strs[2].split(",");
DrawingTrain drawingTrain = new DrawingTrain(Integer.parseInt(strs[0]), (double)Integer.parseInt(strs[1]),
new Color(
Integer.parseInt(colorValues[0].replaceAll("\\D", "")),
Integer.parseInt(colorValues[1].replaceAll("\\D", "")),
Integer.parseInt(colorValues[2].replaceAll("\\D", ""))
),
Integer.parseInt(strs[3]),
width, height);
try{
drawingTrain.wheelDrawing = (IWheelDrawing)Class.forName(strs[4]).getDeclaredConstructor().newInstance();
drawingTrain.wheelDrawing.setNumWheel(drawingTrain.EntityTrain.numWheel);
}
catch(Exception e){
return null;
}
return drawingTrain;
}
if (strs.length == 9)
{
String[] colorValues = strs[2].split(",");
String[] colorValues2 = strs[5].split(",");
DrawingLoco drawingLoco = new DrawingLoco(
Integer.parseInt(strs[0]),
(double)Integer.parseInt(strs[1]),
new Color(
Integer.parseInt(colorValues[0].replaceAll("\\D", "")),
Integer.parseInt(colorValues[1].replaceAll("\\D", "")),
Integer.parseInt(colorValues[2].replaceAll("\\D", ""))
),
Integer.parseInt(strs[3]),
new Color(
Integer.parseInt(colorValues2[0].replaceAll("\\D", "")),
Integer.parseInt(colorValues2[1].replaceAll("\\D", "")),
Integer.parseInt(colorValues2[2].replaceAll("\\D", ""))
),
strs[6].equals("true"),
strs[7].equals("true"),
strs[8].equals("true"),
width, height);
try{
drawingLoco.wheelDrawing = (IWheelDrawing)Class.forName(strs[4]).getDeclaredConstructor().newInstance();
drawingLoco.wheelDrawing.setNumWheel(drawingLoco.EntityTrain.numWheel);
}
catch(Exception e){
return null;
}
return drawingLoco;
}
return null;
}
/// <summary>
/// Получение данных для сохранения в файл
/// </summary>
/// <param name="drawningtrain">Сохраняемый объект</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <returns>Строка с данными по объекту</returns>
public static String GetDataForSave(DrawingTrain drawningTrain, String separatorForObject)
{
EntityTrain train = drawningTrain.EntityTrain;
if (train == null)
{
return null;
}
String str = "" + train.Speed + separatorForObject + (int)train.Weight + separatorForObject + train.BodyColor + separatorForObject + train.numWheel + separatorForObject + drawningTrain.wheelDrawing.getClass().getName();
if (!(train instanceof EntityLoco))
{
return str;
}
return str+separatorForObject+((EntityLoco)train).AdditionalColor.toString()+separatorForObject+((EntityLoco)train).Tube+separatorForObject+((EntityLoco)train).FuelTank+separatorForObject+((EntityLoco)train).LocoLine;
}
}