From 239f4bdec85fd81821b311ebd701a8870cd0cafe Mon Sep 17 00:00:00 2001 From: "pyzhov.egor" <142977368+nxf1ve@users.noreply.github.com> Date: Mon, 6 May 2024 19:49:52 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BoatSharingService.java | 2 +- .../ICollectionGenericObjects.java | 5 + .../ListGenericCollection.java | 59 ----- .../ListGenericObjects.java | 103 ++++++++ .../MassiveGenericObjects.java | 43 ++++ .../StorageCollection.java | 225 +++++++++++++++++- .../Drawnings/DrawningAbstractCompany.java | 3 + .../src/Drawnings/DrawningBoat.java | 13 + .../src/Drawnings/DrawningOvalPaddles.java | 4 + .../src/Drawnings/DrawningPaddles.java | 4 + .../Drawnings/DrawningRectanglePaddles.java | 4 + .../src/Drawnings/ExtentionDrawningBoat.java | 76 ++++++ .../src/Drawnings/IDrawPaddles.java | 1 + ProjectCatamaran/src/Entities/EntityBoat.java | 28 ++- .../src/Entities/EntityCatamaran.java | 18 ++ .../src/Forms/FormBoatCollection.java | 110 ++++++++- 16 files changed, 619 insertions(+), 79 deletions(-) delete mode 100644 ProjectCatamaran/src/CollectionGenericObjects/ListGenericCollection.java create mode 100644 ProjectCatamaran/src/CollectionGenericObjects/ListGenericObjects.java create mode 100644 ProjectCatamaran/src/Drawnings/ExtentionDrawningBoat.java diff --git a/ProjectCatamaran/src/CollectionGenericObjects/BoatSharingService.java b/ProjectCatamaran/src/CollectionGenericObjects/BoatSharingService.java index 2ec9407..5c0cfd7 100644 --- a/ProjectCatamaran/src/CollectionGenericObjects/BoatSharingService.java +++ b/ProjectCatamaran/src/CollectionGenericObjects/BoatSharingService.java @@ -22,7 +22,7 @@ public class BoatSharingService extends AbstractCompany { g.fillRect(0, 0, _pictureWidth, _pictureHeight); g.setColor(new Color(165, 42, 42)); // Brown - int offsetX = 10, offsetY = -5; + int offsetX = 10, offsetY = -25; int x = 1 + offsetX, y = _pictureHeight - _placeSizeHeight + offsetY; numRows = 0; while (y >= 0) { diff --git a/ProjectCatamaran/src/CollectionGenericObjects/ICollectionGenericObjects.java b/ProjectCatamaran/src/CollectionGenericObjects/ICollectionGenericObjects.java index 26c6221..e38ee20 100644 --- a/ProjectCatamaran/src/CollectionGenericObjects/ICollectionGenericObjects.java +++ b/ProjectCatamaran/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -6,6 +6,7 @@ public interface ICollectionGenericObjects { int getCount(); void setMaxCount(int count); + int getMaxCount(); int insert(T obj); @@ -14,4 +15,8 @@ public interface ICollectionGenericObjects { T remove(int index); T get(int index); + + CollectionType GetCollectionType(); + Iterable GetItems(); + void ClearCollection(); } diff --git a/ProjectCatamaran/src/CollectionGenericObjects/ListGenericCollection.java b/ProjectCatamaran/src/CollectionGenericObjects/ListGenericCollection.java deleted file mode 100644 index 6c12dc3..0000000 --- a/ProjectCatamaran/src/CollectionGenericObjects/ListGenericCollection.java +++ /dev/null @@ -1,59 +0,0 @@ -package CollectionGenericObjects; - -import Drawnings.DrawningBoat; - -import java.util.ArrayList; - -public class ListGenericCollection implements ICollectionGenericObjects { - private final ArrayList _collection; - public ListGenericCollection() { - _collection = new ArrayList<>(); - } - private int maxCount; - public int count; - @Override - public int getCount() { - return count; - } - @Override - public void setMaxCount(int count) { - maxCount = count; - } - @Override - public int insert(T obj) { - return insert(obj, count); - } - - @Override - public int insert(T obj, int index) { - if (index > maxCount || index < 0 || index > count){ - return -1; - } - if(index == count){ - _collection.add(obj); - } - else { - _collection.add(index, obj); - } - count = _collection.size(); - return index; - } - - @Override - public T remove(int index) { - if (index > maxCount || index < 0 || index >= count){ - return null; - } - count = _collection.size() - 1; - return _collection.remove(index); - - } - - @Override - public T get(int index) { - if (index >= count){ - return null; - } - return _collection.get(index); - } -} diff --git a/ProjectCatamaran/src/CollectionGenericObjects/ListGenericObjects.java b/ProjectCatamaran/src/CollectionGenericObjects/ListGenericObjects.java new file mode 100644 index 0000000..9ec04dd --- /dev/null +++ b/ProjectCatamaran/src/CollectionGenericObjects/ListGenericObjects.java @@ -0,0 +1,103 @@ +package CollectionGenericObjects; + +import Drawnings.DrawningBoat; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ListGenericObjects implements ICollectionGenericObjects { + private final ArrayList _collection; + private CollectionType collectionType = CollectionType.List; + public ListGenericObjects() { + _collection = new ArrayList<>(); + } + private int maxCount; + public int count; + @Override + public int getCount() { + return count; + } + @Override + public void setMaxCount(int count) { + maxCount = count; + } + @Override + public int getMaxCount(){ + return maxCount; + } + @Override + public int insert(T obj) { + return insert(obj, count); + } + + @Override + public int insert(T obj, int index) { + if (index > maxCount || index < 0 || index > count){ + return -1; + } + if(index == count){ + _collection.add(obj); + } + else { + _collection.add(index, obj); + } + count = _collection.size(); + return index; + } + + @Override + public T remove(int index) { + if (index > maxCount || index < 0 || index >= count){ + return null; + } + count = _collection.size() - 1; + return _collection.remove(index); + + } + + @Override + public T get(int index) { + if (index >= count){ + return null; + } + return _collection.get(index); + } + + @Override + public Iterable GetItems() { + return new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + private int currentIndex = 0; + //нужен ли count + private int count = 0; + @Override + public boolean hasNext() { + return currentIndex < getCount(); + } + @Override + public T next() { + if (hasNext()) { + count++; + return _collection.get(currentIndex++); + } + throw new NoSuchElementException(); + } + }; + } + }; + } + + @Override + public CollectionType GetCollectionType() { + return collectionType; + } + @Override + public void ClearCollection() { + for (T boat : _collection) { + boat = null; + } + } +} diff --git a/ProjectCatamaran/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectCatamaran/src/CollectionGenericObjects/MassiveGenericObjects.java index 3dc4a82..2867f2b 100644 --- a/ProjectCatamaran/src/CollectionGenericObjects/MassiveGenericObjects.java +++ b/ProjectCatamaran/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -2,9 +2,12 @@ package CollectionGenericObjects; import Drawnings.DrawningBoat; import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; public class MassiveGenericObjects implements ICollectionGenericObjects { private ArrayList _collection; + private CollectionType collectionType = CollectionType.Massive; public MassiveGenericObjects() { _collection = new ArrayList<>(); @@ -16,6 +19,10 @@ public class MassiveGenericObjects implements ICollectio return _collection.size(); } @Override + public int getMaxCount(){ + return maxCount; + } + @Override public int insert(T obj) { return insert(obj, 0); } @@ -88,4 +95,40 @@ public class MassiveGenericObjects implements ICollectio } } + @Override + public CollectionType GetCollectionType() { + return collectionType; + } + @Override + public Iterable GetItems() { + return new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + private int currentIndex = 0; + //нужен ли count + private int count = 0; + @Override + public boolean hasNext() { + return currentIndex < getCount(); + } + @Override + public T next() { + if (hasNext()) { + count++; + return _collection.get(currentIndex++); + } + throw new NoSuchElementException(); + } + }; + } + }; + } + @Override + public void ClearCollection() { + for (var boat: _collection) { + boat = null; + + } + } } diff --git a/ProjectCatamaran/src/CollectionGenericObjects/StorageCollection.java b/ProjectCatamaran/src/CollectionGenericObjects/StorageCollection.java index e3a76f3..ff22f0e 100644 --- a/ProjectCatamaran/src/CollectionGenericObjects/StorageCollection.java +++ b/ProjectCatamaran/src/CollectionGenericObjects/StorageCollection.java @@ -1,20 +1,236 @@ package CollectionGenericObjects; import Drawnings.DrawningBoat; +import Drawnings.ExtentionDrawningBoat; -import java.util.ArrayList; -import java.util.HashMap; +import javax.swing.*; +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.*; public class StorageCollection { private final HashMap> _storages; public final ArrayList keys; + private final String _collectionKey = "CollectionsStorage"; + + private final String _collectionSingleKey = "CollectionSingle"; + + private final String _separatorForKeyValue = "/"; + + private final String _separatorItems = ";"; public StorageCollection() { _storages = new HashMap<>(); keys = new ArrayList<>(); } - public void addCollection(String name, CollectionType collectionType){ + public boolean SaveData(String filename){ + if (_storages.isEmpty()) { + return false; + } + File file = new File(filename); + if (file.exists()){ + file.delete(); + } + try { + file.createNewFile(); + } + catch (IOException e){ + return false; + } + FileWriter fw; + try { + fw = new FileWriter(filename, StandardCharsets.UTF_8, false); + StringBuilder sb = new StringBuilder(); + sb.append(_collectionKey); + fw.write(sb.toString()); + for (int i = 0; i < keys.size(); i++) { + sb = new StringBuilder(); + sb.append('\n'); + if (_storages.get(keys.get(i)) == null) { + continue; + } + sb.append(keys.get(i)); + sb.append(_separatorForKeyValue); + sb.append(_storages.get(keys.get(i)).GetCollectionType()); + sb.append(_separatorForKeyValue); + sb.append(_storages.get(keys.get(i)).getMaxCount()); + sb.append(_separatorForKeyValue); + + for (int j = 0; j < _storages.get(keys.get(i)).getCount(); j++) { + String data = ExtentionDrawningBoat.GetDataForSave(this.getObjectFromChooseCollection(keys.get(i), j)); + if (data==null) { + continue; + } + sb.append(data); + sb.append(_separatorItems); + } + fw.write(sb.toString()); + } + fw.close(); + }catch (IOException e){ + e.getStackTrace(); + return false; + } + return true; + } + + public boolean SaveOneCollection(String filename, JList stringJList){ + if (_storages.isEmpty() || stringJList.getSelectedIndex() == -1) { + return false; + } + FileWriter fw; + File file = new File(filename); + if (file.exists()){ + file.delete(); + } + try { + file.createNewFile(); + } + catch (IOException e){ + return false; + } + + try { + fw = new FileWriter(filename, StandardCharsets.UTF_8, false); + StringBuilder sb = new StringBuilder(); + sb.append(_collectionSingleKey); + int i = stringJList.getSelectedIndex(); + fw.write(sb.toString()); + sb = new StringBuilder(); + sb.append('\n'); + if (_storages.get(keys.get(i)) == null) { + fw.close(); + return false; + } + sb.append(keys.get(i)); + sb.append(_separatorForKeyValue); + sb.append(_storages.get(keys.get(i)).GetCollectionType()); + sb.append(_separatorForKeyValue); + sb.append(_storages.get(keys.get(i)).getMaxCount()); + sb.append(_separatorForKeyValue); + for (int j = 0; j < _storages.get(keys.get(i)).getCount(); j++) { + String data = ExtentionDrawningBoat.GetDataForSave(this.getObjectFromChooseCollection(keys.get(i), j)); + if (data==null) { + continue; + } + sb.append(data); + sb.append(_separatorItems); + } + fw.write(sb.toString()); + fw.close(); + }catch (IOException e){ + e.getStackTrace(); + return false; + } + return true; + } + + public boolean LoadData(String filename) { + File file = new File(filename); + if (!file.exists()) { + return false; + } + FileReader fr; + try { + String data = ""; + fr = new FileReader(file, StandardCharsets.UTF_8); + BufferedReader bufferedReader = new BufferedReader(fr); + String buf = bufferedReader.readLine(); + if (buf.equals(_collectionKey)) { + _storages.clear(); + data = bufferedReader.readLine(); + while (data != null) { + String[] record = data.split(_separatorForKeyValue); + if (record.length != 4) { + continue; + } + CollectionType collectionType = Enum.valueOf(CollectionType.class, record[1]); + ICollectionGenericObjects collection = StorageCollection.CreateCollection(collectionType); + if (collection == null) { + bufferedReader.close(); + return false; + } + collection.setMaxCount(Integer.parseInt(record[2])); + String[] set = record[3].split(_separatorItems); + + for (int i = 0; i < set.length; i++) { + DrawningBoat boat; + if ((boat = ExtentionDrawningBoat.CreateDrawningBoat(set[i])) != null) { + if (collection.insert(boat) == -1) { + bufferedReader.close(); + return false; + } + } + } + + _storages.put(record[0], (ICollectionGenericObjects) collection); + keys.add(record[0]); + data = bufferedReader.readLine(); + } + } + else { + return false; + } + bufferedReader.close(); + }catch (IOException e){ + e.getStackTrace(); + return false; + } + return true; + } + public boolean LoadOneCollection(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.startsWith(_collectionSingleKey)) + return false; + if (keys.contains(s)) { + _storages.get(s).ClearCollection(); + } + s = fs.readLine(); + String[] record = s.split(_separatorForKeyValue); + if (record.length != 4) { + return false; + } + CollectionType collectionType = Enum.valueOf(CollectionType.class, record[1]); + ICollectionGenericObjects collection = CreateCollection(collectionType); + if (collection == null) + { + return false; + } + collection.setMaxCount(Integer.parseInt(record[2])); + String[] set = record[3].split(_separatorItems); + for (String elem : set) { + DrawningBoat boat = ExtentionDrawningBoat.CreateDrawningBoat(elem); + if (collection.insert((T) boat) == -1) + { + return false; + } + } + if (keys.contains(record[0])) { + keys.set(keys.indexOf(record[0]), record[0]); + } + else { + keys.add(record[0]); + } + _storages.put(record[0], (ICollectionGenericObjects) collection); + return true; + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + private static ICollectionGenericObjects CreateCollection(CollectionType collectionType) { + return switch (collectionType) { + case List -> new ListGenericObjects<>(); + case Massive -> new MassiveGenericObjects<>(); + case None -> null; + }; + } + + public void addCollection(String name, CollectionType collectionType){ if (_storages.containsKey(name) || name.isEmpty()){ return; } @@ -26,7 +242,7 @@ public class StorageCollection { keys.add(name); break; case List: - _storages.put(name, new ListGenericCollection<>()); + _storages.put(name, new ListGenericObjects<>()); keys.add(name); break; } @@ -37,6 +253,7 @@ public class StorageCollection { keys.remove(name); } + public ICollectionGenericObjects getCollection(String name){ return _storages.get(name); } diff --git a/ProjectCatamaran/src/Drawnings/DrawningAbstractCompany.java b/ProjectCatamaran/src/Drawnings/DrawningAbstractCompany.java index ddec325..863cd66 100644 --- a/ProjectCatamaran/src/Drawnings/DrawningAbstractCompany.java +++ b/ProjectCatamaran/src/Drawnings/DrawningAbstractCompany.java @@ -17,6 +17,9 @@ public class DrawningAbstractCompany extends JComponent { private AbstractCompany _company = null; DrawningBoat _drawningBoat; private final StorageCollection storageCollection = new StorageCollection<>(); + public StorageCollection getStorageCollection() { + return storageCollection; + } private Stack rubbishBinStack = new Stack<>(); public boolean collectionComboBox_SelectedIndexChanged(JFrame frame, JList collectionList, JComboBox obj, int width, int height) { switch (obj.getSelectedIndex()) { diff --git a/ProjectCatamaran/src/Drawnings/DrawningBoat.java b/ProjectCatamaran/src/Drawnings/DrawningBoat.java index dd1ba57..0f84054 100644 --- a/ProjectCatamaran/src/Drawnings/DrawningBoat.java +++ b/ProjectCatamaran/src/Drawnings/DrawningBoat.java @@ -149,4 +149,17 @@ public class DrawningBoat { drawPaddles.drawPaddles(g2d, entityBoat.getBodyColor(), _startPosX, _startPosY); } + + public String[] GetStringRepresentationPaddles() { + if (drawPaddles instanceof DrawningPaddles) { + return new String[]{String.valueOf(drawPaddles.getNumbersOfPaddles().getEnumNumber()), "DrawningPaddles"}; + } + else if (drawPaddles instanceof DrawningOvalPaddles) { + return new String[]{String.valueOf(drawPaddles.getNumbersOfPaddles().getEnumNumber()), "DrawningOvalPaddles"}; + } + else if (drawPaddles instanceof DrawningRectanglePaddles) { + return new String[]{String.valueOf(drawPaddles.getNumbersOfPaddles().getEnumNumber()), "DrawningRectanglePaddles"}; + } + return null; + } } diff --git a/ProjectCatamaran/src/Drawnings/DrawningOvalPaddles.java b/ProjectCatamaran/src/Drawnings/DrawningOvalPaddles.java index 42e3b71..adf0532 100644 --- a/ProjectCatamaran/src/Drawnings/DrawningOvalPaddles.java +++ b/ProjectCatamaran/src/Drawnings/DrawningOvalPaddles.java @@ -14,6 +14,10 @@ public class DrawningOvalPaddles implements IDrawPaddles { } } @Override + public PaddlesCount getNumbersOfPaddles() { + return _paddlesCount; + } + @Override public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) { g2d.setColor(color); g2d.setStroke(new BasicStroke(4)); diff --git a/ProjectCatamaran/src/Drawnings/DrawningPaddles.java b/ProjectCatamaran/src/Drawnings/DrawningPaddles.java index 013d24f..3ea2a7e 100644 --- a/ProjectCatamaran/src/Drawnings/DrawningPaddles.java +++ b/ProjectCatamaran/src/Drawnings/DrawningPaddles.java @@ -14,6 +14,10 @@ public class DrawningPaddles implements IDrawPaddles { } } @Override + public PaddlesCount getNumbersOfPaddles() { + return _paddlesCount; + } + @Override public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) { g2d.setColor(color); g2d.setStroke(new BasicStroke(4)); diff --git a/ProjectCatamaran/src/Drawnings/DrawningRectanglePaddles.java b/ProjectCatamaran/src/Drawnings/DrawningRectanglePaddles.java index 21243af..8beabfb 100644 --- a/ProjectCatamaran/src/Drawnings/DrawningRectanglePaddles.java +++ b/ProjectCatamaran/src/Drawnings/DrawningRectanglePaddles.java @@ -14,6 +14,10 @@ public class DrawningRectanglePaddles implements IDrawPaddles { } } } + @Override + public PaddlesCount getNumbersOfPaddles() { + return _paddlesCount; + } @Override public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) { diff --git a/ProjectCatamaran/src/Drawnings/ExtentionDrawningBoat.java b/ProjectCatamaran/src/Drawnings/ExtentionDrawningBoat.java new file mode 100644 index 0000000..c30bd6b --- /dev/null +++ b/ProjectCatamaran/src/Drawnings/ExtentionDrawningBoat.java @@ -0,0 +1,76 @@ +package Drawnings; + +import Entities.EntityBoat; +import Entities.EntityCatamaran; + +import java.util.ArrayList; +import java.util.Collections; + +public class ExtentionDrawningBoat { + private static String _separatorForObjectS = ":"; + private static String _separatorForObject = "\\:"; + public static DrawningBoat CreateDrawningBoat(String info) { + String[] strs = info.split(_separatorForObject); + EntityBoat boat; + IDrawPaddles paddles = null; + if (strs.length == 9) + { + String s = strs[8]; + switch (s) { + case "DrawningPaddles": + paddles = new DrawningPaddles(); + break; + case "DrawningOvalPaddles": + paddles = new DrawningOvalPaddles(); + break; + case "DrawningRectanglePaddles": + paddles = new DrawningRectanglePaddles(); + break; + } + if (paddles != null) paddles.setNumber(Integer.parseInt(strs[7])); + } + else if (strs.length == 6) { + String s = strs[5]; + switch (s) { + case "DrawningPaddles": + paddles = new DrawningPaddles(); + break; + case "DrawningOvalPaddles": + paddles = new DrawningOvalPaddles(); + break; + case "DrawningRectanglePaddles": + paddles = new DrawningRectanglePaddles(); + break; + } + if (paddles != null) paddles.setNumber(Integer.parseInt(strs[4])); + } + boat = EntityCatamaran.CreateEntityCatamaran(strs); + if (boat != null) + { + return new DrawningCatamaran((EntityCatamaran) boat, paddles); + } + boat = EntityBoat.CreateEntityBoat(strs); + if (boat != null) + { + return new DrawningBoat(boat, paddles); + } + return null; + } + public static String GetDataForSave(DrawningBoat drawningBoat) + { + if (drawningBoat == null) return ""; + String[] array1 = drawningBoat.entityBoat.GetStringRepresentation(); + String[] array2 = drawningBoat.GetStringRepresentationPaddles(); + 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/ProjectCatamaran/src/Drawnings/IDrawPaddles.java b/ProjectCatamaran/src/Drawnings/IDrawPaddles.java index 5c64b2d..4ad6b2b 100644 --- a/ProjectCatamaran/src/Drawnings/IDrawPaddles.java +++ b/ProjectCatamaran/src/Drawnings/IDrawPaddles.java @@ -5,4 +5,5 @@ import java.awt.*; public interface IDrawPaddles { void setNumber(int x); void drawPaddles(Graphics2D graphics2D, Color color, int _startX, int _startY); + PaddlesCount getNumbersOfPaddles(); } diff --git a/ProjectCatamaran/src/Entities/EntityBoat.java b/ProjectCatamaran/src/Entities/EntityBoat.java index e130687..1a55b14 100644 --- a/ProjectCatamaran/src/Entities/EntityBoat.java +++ b/ProjectCatamaran/src/Entities/EntityBoat.java @@ -1,20 +1,21 @@ package Entities; import java.awt.*; +import java.util.Objects; public class EntityBoat { - private int Speed; - public int getSpeed() { + private Integer Speed; + public Integer getSpeed() { return Speed; } public void setSpeed(int speed) { Speed = speed; } - private double Weight; + private Double Weight; public void setWeight(double weight){ Weight = weight; } - public double getWeight() { + public Double getWeight() { return Weight; } private Color BodyColor; @@ -44,4 +45,23 @@ public class EntityBoat { this.BodyColor.getRed(), this.BodyColor.getGreen(), this.BodyColor.getBlue()); return buffer; } + + public String[] GetStringRepresentation() + { + return new String[]{"EntityBoat", Speed.toString(), Weight.toString(), colorToHexString(BodyColor)}; + } + public static EntityBoat CreateEntityBoat(String[] strs) + { + if (strs.length != 6 || !Objects.equals(strs[0], "EntityBoat")) + { + return null; + } + return new EntityBoat(Integer.parseInt(strs[1]), Double.parseDouble(strs[2]), hexStringToColor(strs[3])); + } + public static String colorToHexString(Color color) { + return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); + } + public static Color hexStringToColor(String hexString) { + return Color.decode(hexString); + } } diff --git a/ProjectCatamaran/src/Entities/EntityCatamaran.java b/ProjectCatamaran/src/Entities/EntityCatamaran.java index 0ce3b9c..c736890 100644 --- a/ProjectCatamaran/src/Entities/EntityCatamaran.java +++ b/ProjectCatamaran/src/Entities/EntityCatamaran.java @@ -1,6 +1,7 @@ package Entities; import java.awt.*; +import java.util.Objects; public class EntityCatamaran extends EntityBoat { private Color AdditionalColor; @@ -39,4 +40,21 @@ public class EntityCatamaran extends EntityBoat { buffer += ", Поплавки: " + this.Floaters; return buffer; } + + @Override + public String[] GetStringRepresentation() + { + return new String[]{"EntityCatamaran", getSpeed().toString(), getWeight().toString(), + colorToHexString(getBodyColor()), colorToHexString(getAdditionalColor()), + String.valueOf(Sail), String.valueOf(Floaters)}; + } + public static EntityCatamaran CreateEntityCatamaran(String[] strs) + { + if (strs.length != 9 || !Objects.equals(strs[0], "EntityCatamaran")) + { + return null; + } + return new EntityCatamaran(Integer.parseInt(strs[1]), Double.parseDouble(strs[2]), hexStringToColor(strs[3]), + hexStringToColor(strs[4]), Boolean.parseBoolean(strs[5]), Boolean.parseBoolean(strs[6])); + } } diff --git a/ProjectCatamaran/src/Forms/FormBoatCollection.java b/ProjectCatamaran/src/Forms/FormBoatCollection.java index 8f6c971..8f769b0 100644 --- a/ProjectCatamaran/src/Forms/FormBoatCollection.java +++ b/ProjectCatamaran/src/Forms/FormBoatCollection.java @@ -6,11 +6,13 @@ import Drawnings.DrawningAbstractCompany; import Drawnings.DrawningBoat; import javax.swing.*; +import javax.swing.text.MaskFormatter; import javax.swing.text.NumberFormatter; import java.awt.*; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; +import java.awt.event.*; +import java.io.FileFilter; import java.text.NumberFormat; +import java.text.ParseException; import java.util.ArrayList; public class FormBoatCollection extends JFrame { @@ -64,14 +66,43 @@ public class FormBoatCollection extends JFrame { final private JButton goGoToCheckFromRubbishBinButton = new JButton("
Отправить на тест
объект из мусорки"); final private JPanel goGoToCheckFromRubbishBinPanel = new JPanel(); + final private JMenuBar menuBar = new JMenuBar(); + final private JMenu fileMenu = new JMenu("Файл"); + final private JMenuItem loadItem = new JMenuItem("Загрузить"); + final private JMenuItem saveItem = new JMenuItem("Сохранить"); + final private JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию"); + final private JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию"); public void OpenFrame() { + fileMenu.add(loadItem); + fileMenu.add(saveItem); + fileMenu.add(loadCollection); + fileMenu.add(saveCollection); + fileMenu.addSeparator(); + menuBar.add(fileMenu); + + saveItem.addActionListener(e -> { + SaveFile(); + }); + loadItem.addActionListener(e -> { + LoadFile(); + }); + + saveCollection.addActionListener(e -> { + SaveCollection(); + }); + loadCollection.addActionListener(e -> { + LoadCollection(); + }); + + jFrameCollectionBoats.setJMenuBar(menuBar); + jFrameCollectionBoats.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Toolkit tolls = Toolkit.getDefaultToolkit(); Dimension dimension = tolls.getScreenSize(); jFrameCollectionBoats.setBounds(dimension.width / 2 - 250, dimension.height / 2 - 250, - 1200, 665); + 1200, 675); jFrameCollectionBoats.setTitle("Коллекция лодок"); toolsPanel.setBackground(Color.BLACK); @@ -178,6 +209,8 @@ public class FormBoatCollection extends JFrame { listOfDownPanel.add(refreshButton); setEnableComponentsOfList(listOfDownPanel, false); + + jFrameCollectionBoats.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent componentEvent) { @@ -187,11 +220,11 @@ public class FormBoatCollection extends JFrame { radioButtonsPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 78); addCollectionPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 103); collectionsListPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 137); - deleteCollectionPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 227); + deleteCollectionPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, 241); comboBoxPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 400); createCompanyPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 370); goToCheckPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, - jFrameCollectionBoats.getHeight() - 320); + jFrameCollectionBoats.getHeight() - 325); goGoToCheckFromRubbishBinPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 295); addBoatPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 241); @@ -199,13 +232,15 @@ public class FormBoatCollection extends JFrame { textBoxPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 146); removePanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 119); - refreshPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 67); + refreshPanel.setLocation(jFrameCollectionBoats.getWidth() - 200, jFrameCollectionBoats.getHeight() - 67- menuBar.getHeight()); toolsPanel.setSize(new Dimension(10, jFrameCollectionBoats.getHeight())); jFrameCollectionBoats.repaint(); } }); + + buttonAddBoat.addActionListener(e -> { _company.createObject(jFrameCollectionBoats); jFrameCollectionBoats.repaint(); @@ -240,7 +275,7 @@ public class FormBoatCollection extends JFrame { }); createCompanyButton.addActionListener(e -> { boolean res = _company.collectionComboBox_SelectedIndexChanged(jFrameCollectionBoats, collectionsList, comboBoxSelectorCompany, - jFrameCollectionBoats.getWidth() - 233, jFrameCollectionBoats.getHeight()); + jFrameCollectionBoats.getWidth() - 233, jFrameCollectionBoats.getHeight() - 15); setEnableComponentsOfList(listOfDownPanel, res); jFrameCollectionBoats.repaint(); }); @@ -260,15 +295,68 @@ public class FormBoatCollection extends JFrame { jFrameCollectionBoats.setVisible(true); } + private String SaveWindow() { + FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.SAVE); + fileDialog.setVisible(true); + String directory = fileDialog.getDirectory(); + String file = fileDialog.getFile(); + if (directory == null || file == null) return null; + return directory + file; + } + private void SaveFile() { + String filename = SaveWindow(); + if (_company.getStorageCollection().SaveData(filename)) { + JOptionPane.showMessageDialog(jFrameCollectionBoats, "Сохранено"); + } + else JOptionPane.showMessageDialog(jFrameCollectionBoats, "Ошибка сохранения"); + } + private void SaveCollection() { + String filename = SaveWindow(); + if (filename == null) { + JOptionPane.showMessageDialog(jFrameCollectionBoats, "Файл не выбран"); + return; + } + if (collectionsList.getSelectedIndex() < 0 || collectionsList.getSelectedValue() == null) { + JOptionPane.showMessageDialog(jFrameCollectionBoats, "Коллекция не выбрана"); + } + if (_company.getStorageCollection().SaveOneCollection(filename, collectionsList)) { + JOptionPane.showMessageDialog(jFrameCollectionBoats, "Коллекция сохранена"); + } + else JOptionPane.showMessageDialog(jFrameCollectionBoats, "Ошибка сохранения"); + } + private String LoadWindow() { + FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.LOAD); + fileDialog.setVisible(true); + String directory = fileDialog.getDirectory(); + String file = fileDialog.getFile(); + if (directory == null || file == null) return null; + return directory + file; + } + private void LoadFile() { + String filename = LoadWindow(); + if (_company.getStorageCollection().LoadData(filename)) { + JOptionPane.showMessageDialog(jFrameCollectionBoats, "Загрузка прошла успешно"); + updateCollectionsList(_company.getStorageCollection()); + } + else JOptionPane.showMessageDialog(jFrameCollectionBoats, "Не загрузилось"); + + } + private void LoadCollection() { + String filename = LoadWindow(); + if (_company.getStorageCollection().LoadOneCollection(filename)) { + JOptionPane.showMessageDialog(jFrameCollectionBoats, "Коллекция загружена"); + updateCollectionsList(_company.getStorageCollection()); + } + else JOptionPane.showMessageDialog(jFrameCollectionBoats, "Не загрузилось"); + } private void updateCollectionsList(StorageCollection storageCollection) { if (storageCollection == null) { return; } - listModel.clear(); - ArrayList keys = storageCollection.keys; - for (String key : keys) { - listModel.addElement(key); + listModel.removeAllElements(); + for (int i = storageCollection.keys.size() - 1; i >= 0; i--) { + listModel.add(0, storageCollection.keys.get(i)); } } private void setEnableComponentsOfList(ArrayList list, boolean type) { -- 2.25.1 From f2b0279b173eeac3a9ede7fcd08846e6f2ae4db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80=20=D0=9F=D1=8B=D0=B6=D0=BE=D0=B2?= Date: Tue, 7 May 2024 14:28:19 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BE=D1=87=D0=B8=D1=89=D0=B5=D0=BD=D0=B8=D0=B5,=20=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=B9=20=D0=BA=D0=BE=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D0=BA=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageCollection.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ProjectCatamaran/src/CollectionGenericObjects/StorageCollection.java b/ProjectCatamaran/src/CollectionGenericObjects/StorageCollection.java index ff22f0e..f973ed3 100644 --- a/ProjectCatamaran/src/CollectionGenericObjects/StorageCollection.java +++ b/ProjectCatamaran/src/CollectionGenericObjects/StorageCollection.java @@ -186,16 +186,22 @@ public class StorageCollection { String s = fs.readLine(); if (s == null || !s.startsWith(_collectionSingleKey)) return false; - if (keys.contains(s)) { - _storages.get(s).ClearCollection(); - } + ICollectionGenericObjects collection; + s = fs.readLine(); String[] record = s.split(_separatorForKeyValue); if (record.length != 4) { return false; } - CollectionType collectionType = Enum.valueOf(CollectionType.class, record[1]); - ICollectionGenericObjects collection = CreateCollection(collectionType); + + if (keys.contains(s)) { + _storages.get(s).ClearCollection(); + collection = (ICollectionGenericObjects) _storages.get(s); + } + else { + CollectionType collectionType = Enum.valueOf(CollectionType.class, record[1]); + collection = CreateCollection(collectionType); + } if (collection == null) { return false; -- 2.25.1