diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/AbstractCompany.java b/ProjectCleaningCar/src/CollectionGenericObjects/AbstractCompany.java index 2ccfe1a..de2486d 100644 --- a/ProjectCleaningCar/src/CollectionGenericObjects/AbstractCompany.java +++ b/ProjectCleaningCar/src/CollectionGenericObjects/AbstractCompany.java @@ -21,7 +21,7 @@ public abstract class AbstractCompany { _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount(GetMaxCount(), (Class) DrawningTruck.class); + _collection.MaxCount(GetMaxCount(), (Class) DrawningTruck.class); } public DrawningTruck GetRandomObject() { Random rnd = new Random(); diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/ICollectionGenericObjects.java b/ProjectCleaningCar/src/CollectionGenericObjects/ICollectionGenericObjects.java index a57091c..14b3d8b 100644 --- a/ProjectCleaningCar/src/CollectionGenericObjects/ICollectionGenericObjects.java +++ b/ProjectCleaningCar/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -2,9 +2,12 @@ package CollectionGenericObjects; public interface ICollectionGenericObjects { int Count(); - void SetMaxCount(int value, Class type); + void MaxCount(int value); int Insert(T obj); int Insert(T obj, int position); T Remove(int position); T Get(int position); + CollectionType GetCollectionType(); + Iterable GetItems(); + void ClearCollection(); } diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/ListGenericObjects.java b/ProjectCleaningCar/src/CollectionGenericObjects/ListGenericObjects.java index afd056a..bff796a 100644 --- a/ProjectCleaningCar/src/CollectionGenericObjects/ListGenericObjects.java +++ b/ProjectCleaningCar/src/CollectionGenericObjects/ListGenericObjects.java @@ -12,7 +12,7 @@ public class ListGenericObjects implements ICollectionGenericObjects { } @Override - public void SetMaxCount(int value, Class type) { + public void MaxCount(int value) { if (value > 0) { _maxCount = value; } @@ -53,4 +53,19 @@ public class ListGenericObjects implements ICollectionGenericObjects { } return _collection.get(position); } + + @Override + public CollectionType GetCollectionType() { + return null; + } + + @Override + public Iterable GetItems() { + return null; + } + + @Override + public void ClearCollection() { + + } } diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java index 546b046..2efcaf1 100644 --- a/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java +++ b/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -1,11 +1,13 @@ package CollectionGenericObjects; import java.lang.reflect.Array; +import java.util.Iterator; +import java.util.NoSuchElementException; public class MassiveGenericObjects implements ICollectionGenericObjects{ private T[] _collection; @Override - public void SetMaxCount(int value, Class type) { + public void MaxCount(int value) { if (value > 0) { if (_collection == null) _collection = (T[]) Array.newInstance(type, value); @@ -69,4 +71,34 @@ public class MassiveGenericObjects implements ICollectionGenericObjects{ if (position < 0 || position >= Count()) return null; return (T) _collection[position]; } + + @Override + public CollectionType GetCollectionType() { + return CollectionType.Massive; + } + + @Override + public Iterable GetItems() { + return () -> new Iterator() { + private int currentIndex = 0; + @Override + public boolean hasNext() { + return currentIndex < Count(); + } + @Override + public T next() { + if (hasNext()) { + return _collection[currentIndex++]; + } + throw new NoSuchElementException(); + } + }; + } + + @Override + public void ClearCollection() { + for (T ship : _collection) { + ship = null; + } + } } diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/StorageCollection.java b/ProjectCleaningCar/src/CollectionGenericObjects/StorageCollection.java index 3da3891..ae02589 100644 --- a/ProjectCleaningCar/src/CollectionGenericObjects/StorageCollection.java +++ b/ProjectCleaningCar/src/CollectionGenericObjects/StorageCollection.java @@ -1,8 +1,15 @@ package CollectionGenericObjects; +import Drawnings.DrawningTruck; +import Drawnings.ExtentionDrawningTruck; + +import java.io.*; import java.util.*; -public class StorageCollection { +public class StorageCollection { + private String _collectionKey = "CollectionsStorage"; + private String _separatorForKeyValue = "|"; + private String _separatorItems = ";"; private Map> _storages; public Set Keys() { return _storages.keySet(); @@ -42,4 +49,84 @@ public class StorageCollection { return _storages.get(name).Remove(position); return null; } + + public boolean SaveData(String filename) { + if (_storages.isEmpty()) return false; + File file = new File(filename); + if (file.exists()) file.delete(); + try { + file.createNewFile(); + FileWriter writer = new FileWriter(file); + writer.write(_collectionKey); + writer.write("\n"); + for (Map.Entry> value : _storages.entrySet()) { + StringBuilder sb = new StringBuilder(); + sb.append(value.getKey()); + sb.append(_separatorForKeyValue); + sb.append(value.getValue().GetCollectionType()); + sb.append(_separatorForKeyValue); + sb.append(value.getValue().Count()); + sb.append(_separatorForKeyValue); + for (T truck : value.getValue().GetItems()) { + String data = ExtentionDrawningTruck.GetDataForSave((DrawningTruck) truck); + if (data.isEmpty()) continue; + sb.append(data); + sb.append(_separatorItems); + } + sb.append("\n"); + writer.write(String.valueOf(sb)); + } + writer.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + return true; + } + + public boolean LoadData(String filename) { + File file = new File(filename); + if (!file.exists()) return false; + try (BufferedReader fs = new BufferedReader(new FileReader(filename))) { + String s = fs.readLine(); + if (s == null || s.isEmpty() || !s.startsWith(_collectionKey)) + return false; + _storages.clear(); + s = ""; + while ((s = fs.readLine()) != null) { + String[] record = s.split(_separatorForKeyValue); + if (record.length != 4) { + continue; + } + ICollectionGenericObjects collection = CreateCollection(record[1]); + if (collection == null) + { + return false; + } + collection.MaxCount(Integer.parseInt(record[2])); + String[] set = record[3].split(_separatorItems); + for (String elem : set) { + DrawningTruck truck = ExtentionDrawningTruck.CreateDrawningTruck(elem); + if (collection.Insert((T) truck) == -1) + { + return false; + } + } + _storages.put(record[0], collection); + } + return true; + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + + public ICollectionGenericObjects CreateCollection(String s) { + switch (s) { + case "Massive": + return new MassiveGenericObjects(); + case "List": + return new ListGenericObjects(); + } + return null; + } } diff --git a/ProjectCleaningCar/src/Drawnings/DrawningTruck.java b/ProjectCleaningCar/src/Drawnings/DrawningTruck.java index ce782b7..551a764 100644 --- a/ProjectCleaningCar/src/Drawnings/DrawningTruck.java +++ b/ProjectCleaningCar/src/Drawnings/DrawningTruck.java @@ -154,4 +154,17 @@ public class DrawningTruck { g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12); } + + public String[] GetStringRepresentationDecks() { + if (drawningWheels instanceof DrawningRhombWheels) { + return new String[]{String.valueOf(drawningWheels.getNumberOfWheels()), "DrawningRhombWheels"}; + } + else if (drawningWheels instanceof DrawningCircleWheels) { + return new String[]{String.valueOf(drawningWheels.getNumberOfWheels()), "DrawningCircleWheels"}; + } + else if (drawningWheels instanceof DrawningTriangleWheels) { + return new String[]{String.valueOf(drawningWheels.getNumberOfWheels()), "DrawningTriangleWheels"}; + } + return null; + } } diff --git a/ProjectCleaningCar/src/Drawnings/ExtentionDrawningTruck.java b/ProjectCleaningCar/src/Drawnings/ExtentionDrawningTruck.java new file mode 100644 index 0000000..238784f --- /dev/null +++ b/ProjectCleaningCar/src/Drawnings/ExtentionDrawningTruck.java @@ -0,0 +1,66 @@ +package Drawnings; + +import Entities.EntityCleaningCar; +import Entities.EntityTruck; + +import java.util.ArrayList; +import java.util.Collections; + +public class ExtentionDrawningTruck { + private static String _separatorForObjectS = ":"; + private static String _separatorForObject = "/"; + public static DrawningTruck CreateDrawningTruck(String info) { + String[] strs = info.split(_separatorForObject); + EntityTruck truck; + IDrawningWheels wheels = null; + if (strs.length == 8) + { + String s = strs[8]; + wheels = switch (s) { + case "DrawningCircleWheels" -> new DrawningCircleWheels(); + case "DrawningTriangleWheels" -> new DrawningTriangleWheels(); + case "DrawningRhombWheels" -> new DrawningRhombWheels(); + default -> null; + }; + if (wheels != null) wheels.setCountWheels(Integer.parseInt(strs[7])); + } + else if (strs.length == 6) { + String s = strs[5]; + wheels = switch (s) { + case "DrawningCircleWheels" -> wheels = new DrawningCircleWheels(); + case "DrawningTriangleWheels" -> wheels = new DrawningTriangleWheels(); + case "DrawningRhombWheels" -> wheels = new DrawningRhombWheels(); + default -> null; + }; + if (wheels != null) wheels.setCountWheels(Integer.parseInt(strs[4])); + } + truck = EntityCleaningCar.CreateEntityShip(strs); + if (truck != null) + { + return new DrawningCleaningCar((EntityCleaningCar) truck, wheels); + } + truck = EntityTruck.CreateEntityShip(strs); + if (truck != null) + { + return new DrawningTruck(truck, wheels); + } + return null; + } + public static String GetDataForSave(DrawningTruck truck) + { + if (truck == null) return ""; + String[] array1 = truck.EntityTruck.GetStringRepresentation(); + String[] array2 = truck.GetStringRepresentationDecks(); + if (array1 == null) + { + return ""; + } + ArrayList list = new ArrayList<>(); + Collections.addAll(list, array1); + if (array2 == null) { + Collections.addAll(list, "0", " "); + } + else Collections.addAll(list, array2); + return String.join(_separatorForObjectS, list); + } +} diff --git a/ProjectCleaningCar/src/Entities/EntityTruck.java b/ProjectCleaningCar/src/Entities/EntityTruck.java index 63c95d2..3104955 100644 --- a/ProjectCleaningCar/src/Entities/EntityTruck.java +++ b/ProjectCleaningCar/src/Entities/EntityTruck.java @@ -1,6 +1,7 @@ package Entities; import java.awt.*; +import java.util.Objects; public class EntityTruck { private int Speed; @@ -26,4 +27,22 @@ public class EntityTruck { Weight = weight; BodyColor = bodyColor; } + public String[] GetStringRepresentation() + { + return new String[]{"EntityTruck", String.valueOf(Speed), String.valueOf(Weight), colorToHexString(BodyColor)}; + } + public static EntityTruck CreateEntityShip(String[] strs) + { + if (strs.length != 6 || !Objects.equals(strs[0], "EntityTruck")) + { + return null; + } + return new EntityTruck(Integer.parseInt(strs[1]), Double.parseDouble(strs[2]), hexIntoColor(strs[3])); + } + public static String colorToHexString(Color color) { + return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); + } + public static Color hexIntoColor(String hexString) { + return Color.decode(hexString); + } }