From 1b17183c14fd1f7c7e45148373fab5beb7178c33 Mon Sep 17 00:00:00 2001 From: IlyasValiulov <148232695+IlyasValiulov@users.noreply.github.com> Date: Thu, 25 Apr 2024 21:27:57 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=966=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AdditionalCollections.java | 3 - .../ICollectionGenericObjects.java | 3 + .../ListGenericObjects.java | 40 ++++- .../MassiveGenericObjects.java | 37 +++++ .../StorageCollection.java | 153 +++++++++++++++++- WarmlyShip/src/DrawingShip/DrawingShip.java | 17 +- .../DrawingShip/ExtentionDrawningShip.java | 74 +++++++++ WarmlyShip/src/Entities/EntityShip.java | 25 ++- WarmlyShip/src/Entities/EntityWarmlyShip.java | 18 +++ WarmlyShip/src/FormShipCollection.java | 101 +++++++++++- WarmlyShip/src/FormShipConfig.java | 2 +- 11 files changed, 462 insertions(+), 11 deletions(-) create mode 100644 WarmlyShip/src/DrawingShip/ExtentionDrawningShip.java diff --git a/WarmlyShip/src/CollectionAdditionalObjects/AdditionalCollections.java b/WarmlyShip/src/CollectionAdditionalObjects/AdditionalCollections.java index 56aa2c9..ef5318a 100644 --- a/WarmlyShip/src/CollectionAdditionalObjects/AdditionalCollections.java +++ b/WarmlyShip/src/CollectionAdditionalObjects/AdditionalCollections.java @@ -6,10 +6,7 @@ import DrawingShip.DrawingWarmlyShip; import Entities.EntityShip; import Entities.EntityWarmlyShip; -import java.awt.*; import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.List; import java.util.Random; public class AdditionalCollections { diff --git a/WarmlyShip/src/CollectionGenericObjects/ICollectionGenericObjects.java b/WarmlyShip/src/CollectionGenericObjects/ICollectionGenericObjects.java index 3fc89c1..a6fb6c2 100644 --- a/WarmlyShip/src/CollectionGenericObjects/ICollectionGenericObjects.java +++ b/WarmlyShip/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -7,5 +7,8 @@ public interface ICollectionGenericObjects int Insert(T obj); T Remove(int position); T Get(int position); + CollectionType GetCollectionType(); + Iterable GetItems(); + void ClearCollection(); } diff --git a/WarmlyShip/src/CollectionGenericObjects/ListGenericObjects.java b/WarmlyShip/src/CollectionGenericObjects/ListGenericObjects.java index bcbc1a7..7f16b2d 100644 --- a/WarmlyShip/src/CollectionGenericObjects/ListGenericObjects.java +++ b/WarmlyShip/src/CollectionGenericObjects/ListGenericObjects.java @@ -1,9 +1,10 @@ package CollectionGenericObjects; -import java.util.ArrayList; -import java.util.List; +import java.util.*; + public class ListGenericObjects implements ICollectionGenericObjects { private List _collection; + private CollectionType collectionType = CollectionType.List; private int _maxCount; public int getCount() { return _collection.size(); @@ -18,6 +19,10 @@ public class ListGenericObjects implements ICollectionGenericObjects { _collection = new ArrayList(); } @Override + public CollectionType GetCollectionType() { + return collectionType; + } + @Override public T Get(int position) { if (position >= getCount() || position < 0) return null; @@ -38,4 +43,35 @@ public class ListGenericObjects implements ICollectionGenericObjects { _collection.remove(position); return obj; } + @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 (T ship : _collection) { + ship = null; + } + } } diff --git a/WarmlyShip/src/CollectionGenericObjects/MassiveGenericObjects.java b/WarmlyShip/src/CollectionGenericObjects/MassiveGenericObjects.java index c708376..6d47e72 100644 --- a/WarmlyShip/src/CollectionGenericObjects/MassiveGenericObjects.java +++ b/WarmlyShip/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -3,10 +3,12 @@ package CollectionGenericObjects; import DrawingShip.DrawingShip; import java.lang.reflect.Array; +import java.util.*; public class MassiveGenericObjects implements ICollectionGenericObjects{ private T[] _collection = null; private int Count; + private CollectionType collectionType = CollectionType.Massive; @Override public void SetMaxCount(int size) { if (size > 0) { @@ -21,6 +23,10 @@ public class MassiveGenericObjects implements ICollectionGenericObjects{ return Count; } @Override + public CollectionType GetCollectionType() { + return collectionType; + } + @Override public int Insert(T obj) { int index = 0; while (index < getCount()) @@ -47,4 +53,35 @@ public class MassiveGenericObjects implements ICollectionGenericObjects{ if (position >= getCount() || position < 0) return null; return (T) _collection[position]; } + @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[currentIndex++]; + } + throw new NoSuchElementException(); + } + }; + } + }; + } + @Override + public void ClearCollection() { + for (T ship : _collection) { + ship = null; + } + } } diff --git a/WarmlyShip/src/CollectionGenericObjects/StorageCollection.java b/WarmlyShip/src/CollectionGenericObjects/StorageCollection.java index 3fcf559..3486e82 100644 --- a/WarmlyShip/src/CollectionGenericObjects/StorageCollection.java +++ b/WarmlyShip/src/CollectionGenericObjects/StorageCollection.java @@ -1,10 +1,12 @@ package CollectionGenericObjects; import DrawingShip.DrawingShip; +import DrawingShip.ExtentionDrawningShip; +import java.io.*; import java.util.*; -public class StorageCollection { +public class StorageCollection { private Map> _storages; public StorageCollection() { @@ -40,4 +42,153 @@ public class StorageCollection { return _storages.get(name).Remove(position); return null; } + private String _collectionKey = "CollectionsStorage"; + private String _collectionName = "StorageCollection"; + private String _separatorForKeyValueS = "|"; + private String _separatorForKeyValue = "\\|"; + private String _separatorItemsS = ";"; + private String _separatorItems = "\\;"; + 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(_separatorForKeyValueS); + sb.append(value.getValue().GetCollectionType()); + sb.append(_separatorForKeyValueS); + sb.append(value.getValue().getCount()); + sb.append(_separatorForKeyValueS); + for (T ship : value.getValue().GetItems()) { + String data = ExtentionDrawningShip.GetDataForSave((DrawingShip) ship); + if (data.isEmpty()) continue; + sb.append(data); + sb.append(_separatorItemsS); + } + sb.append("\n"); + writer.write(String.valueOf(sb)); + } + writer.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + return true; + } + public boolean SaveOneCollection(String filename, String name) { + 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(_collectionName); + writer.write("\n"); + ICollectionGenericObjects value = _storages.get(name); + StringBuilder sb = new StringBuilder(); + sb.append(name); + sb.append(_separatorForKeyValueS); + sb.append(value.GetCollectionType()); + sb.append(_separatorForKeyValueS); + sb.append(value.getCount()); + sb.append(_separatorForKeyValueS); + for (T ship : value.GetItems()) { + String data = ExtentionDrawningShip.GetDataForSave((DrawingShip) ship); + if (data.isEmpty()) continue; + sb.append(data); + sb.append(_separatorItemsS); + } + writer.append(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.SetMaxCount(Integer.parseInt(record[2])); + String[] set = record[3].split(_separatorItems); + for (String elem : set) { + DrawingShip ship = ExtentionDrawningShip.CreateDrawingShip(elem); + if (collection.Insert((T) ship) == -1) + { + return false; + } + } + _storages.put(record[0], collection); + } + return true; + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + 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.isEmpty() || !s.startsWith(_collectionName)) + return false; + if (_storages.containsKey(s)) { + _storages.get(s).ClearCollection(); + } + s = fs.readLine(); + String[] record = s.split(_separatorForKeyValue); + if (record.length != 4) { + return false; + } + ICollectionGenericObjects collection = CreateCollection(record[1]); + if (collection == null) + { + return false; + } + collection.SetMaxCount(Integer.parseInt(record[2])); + String[] set = record[3].split(_separatorItems); + for (String elem : set) { + DrawingShip ship = ExtentionDrawningShip.CreateDrawingShip(elem); + if (collection.Insert((T) ship) == -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/WarmlyShip/src/DrawingShip/DrawingShip.java b/WarmlyShip/src/DrawingShip/DrawingShip.java index c52a0ba..eba1bfb 100644 --- a/WarmlyShip/src/DrawingShip/DrawingShip.java +++ b/WarmlyShip/src/DrawingShip/DrawingShip.java @@ -1,8 +1,11 @@ package DrawingShip; - +import DiffetentsDrawingDecks.DrawingDecksType2; +import DiffetentsDrawingDecks.DrawingDecksType3; import DiffetentsDrawingDecks.IDifferentDecks; import Entities.EntityShip; +import DiffetentsDrawingDecks.DrawingDecksType1; + import javax.swing.*; import java.awt.*; @@ -131,4 +134,16 @@ public class DrawingShip extends JPanel { g.fillPolygon(poly); drawingShipHeight = y + 50 - _StartPosY; } + public String[] GetStringRepresentationDecks() { + if (drawingDecks instanceof DrawingDecksType1) { + return new String[]{String.valueOf(drawingDecks.getNumberOfDecks().getNumdecks()), "DrawingDecksType1"}; + } + else if (drawingDecks instanceof DrawingDecksType2) { + return new String[]{String.valueOf(drawingDecks.getNumberOfDecks().getNumdecks()), "DrawingDecksType2"}; + } + else if (drawingDecks instanceof DrawingDecksType3) { + return new String[]{String.valueOf(drawingDecks.getNumberOfDecks().getNumdecks()), "DrawingDecksType3"}; + } + return null; + } } \ No newline at end of file diff --git a/WarmlyShip/src/DrawingShip/ExtentionDrawningShip.java b/WarmlyShip/src/DrawingShip/ExtentionDrawningShip.java new file mode 100644 index 0000000..101c18d --- /dev/null +++ b/WarmlyShip/src/DrawingShip/ExtentionDrawningShip.java @@ -0,0 +1,74 @@ +package DrawingShip; + +import DiffetentsDrawingDecks.DrawingDecksType1; +import DiffetentsDrawingDecks.DrawingDecksType2; +import DiffetentsDrawingDecks.DrawingDecksType3; +import DiffetentsDrawingDecks.IDifferentDecks; +import Entities.EntityShip; +import Entities.EntityWarmlyShip; + +import java.util.ArrayList; +import java.util.Collections; + +public class ExtentionDrawningShip { + private static String _separatorForObjectS = ":"; + private static String _separatorForObject = "\\:"; + public static DrawingShip CreateDrawingShip(String info) { + String[] strs = info.split(_separatorForObject); + EntityShip ship; + IDifferentDecks decks = null; + if (strs.length == 8) + { + String s = strs[8]; + switch (s) { + case "DrawingDecksType1": + decks = new DrawingDecksType1(); + case "DrawingDecksType2": + decks = new DrawingDecksType2(); + case "DrawingDecksType3": + decks = new DrawingDecksType3(); + } + if (decks != null) decks.setNumberOfDecks(Integer.parseInt(strs[7])); + } + else if (strs.length == 6) { + String s = strs[5]; + switch (s) { + case "DrawingDecksType1": + decks = new DrawingDecksType1(); + case "DrawingDecksType2": + decks = new DrawingDecksType2(); + case "DrawingDecksType3": + decks = new DrawingDecksType3(); + } + if (decks != null) decks.setNumberOfDecks(Integer.parseInt(strs[4])); + } + ship = EntityWarmlyShip.CreateEntityWarmlyShip(strs); + if (ship != null) + { + return new DrawingWarmlyShip((EntityWarmlyShip)ship, decks); + } + ship = EntityShip.CreateEntityShip(strs); + if (ship != null) + { + return new DrawingShip(ship, decks); + } + return null; + } + public static String GetDataForSave(DrawingShip drawningShip) + { + if (drawningShip == null) return ""; + String[] array1 = drawningShip.EntityShip.GetStringRepresentation(); + String[] array2 = drawningShip.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/WarmlyShip/src/Entities/EntityShip.java b/WarmlyShip/src/Entities/EntityShip.java index 8f4ce89..7944f20 100644 --- a/WarmlyShip/src/Entities/EntityShip.java +++ b/WarmlyShip/src/Entities/EntityShip.java @@ -1,12 +1,15 @@ package Entities; import java.awt.*; +import java.util.Objects; public class EntityShip { - private int Speed; + private Integer Speed; public void setSpeed(int speed) {Speed = speed;} - private double Weight; + public Integer getSpeed() {return Speed;} + private Double Weight; public void setWeight(double weight) {Weight = weight;} + public Double getWeight() {return Weight;} private Color BodyColor; public Color getBodyColor() {return BodyColor;} public void setBodyColor(Color color) {BodyColor = color;} @@ -18,4 +21,22 @@ public class EntityShip { BodyColor = bodycolor; Step = Speed * 100 / Weight; } + public String[] GetStringRepresentation() + { + return new String[]{"EntityShip", Speed.toString(), Weight.toString(), colorToHexString(BodyColor)}; + } + public static EntityShip CreateEntityShip(String[] strs) + { + if (strs.length != 6 || !Objects.equals(strs[0], "EntityShip")) + { + return null; + } + return new EntityShip(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/WarmlyShip/src/Entities/EntityWarmlyShip.java b/WarmlyShip/src/Entities/EntityWarmlyShip.java index 564e334..96c1e66 100644 --- a/WarmlyShip/src/Entities/EntityWarmlyShip.java +++ b/WarmlyShip/src/Entities/EntityWarmlyShip.java @@ -1,6 +1,8 @@ package Entities; import java.awt.*; +import java.util.Objects; + public class EntityWarmlyShip extends EntityShip{ public Color AdditionalColor; public Color getAdditionalColor() {return AdditionalColor;} @@ -17,4 +19,20 @@ public class EntityWarmlyShip extends EntityShip{ ShipPipes = sheeppipes; FuelTank = fueltank; } + @Override + public String[] GetStringRepresentation() + { + return new String[]{"EntityWarmlyShip", getSpeed().toString(), getWeight().toString(), + colorToHexString(getBodyColor()), colorToHexString(getAdditionalColor()), + String.valueOf(ShipPipes), String.valueOf(FuelTank)}; + } + public static EntityWarmlyShip CreateEntityWarmlyShip(String[] strs) + { + if (strs.length != 9 || !Objects.equals(strs[0], "EntityWarmlyShip")) + { + return null; + } + return new EntityWarmlyShip(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/WarmlyShip/src/FormShipCollection.java b/WarmlyShip/src/FormShipCollection.java index 6898f8c..1dfd2fd 100644 --- a/WarmlyShip/src/FormShipCollection.java +++ b/WarmlyShip/src/FormShipCollection.java @@ -36,6 +36,12 @@ public class FormShipCollection extends JFrame{ private JButton RefreshButton = new JButton("Refresh"); private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); private JFormattedTextField MaskedTextField; + private JMenuBar menuBar = new JMenuBar(); + private JMenu fileMenu = new JMenu("File"); + private JMenuItem loadItem = new JMenuItem("Load"); + private JMenuItem saveItem = new JMenuItem("Save"); + private JMenuItem loadCollection = new JMenuItem("Load coll"); + private JMenuItem saveCollection = new JMenuItem("Save coll"); public FormShipCollection(String title, Dimension dimension) { this.title = title; this.dimension = dimension; @@ -224,11 +230,50 @@ public class FormShipCollection extends JFrame{ } }); + saveItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + SaveFile(); + } + }); + + + loadItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + LoadFile(); + } + }); + + saveCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.out.println("Save coll"); + SaveCollection(); + } + }); + + loadCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.out.println("Load coll"); + LoadCollection(); + } + }); + JLabel labelCollectionName = new JLabel("Название коллекции"); ButtonGroup radiobuttonsGroup = new ButtonGroup(); radiobuttonsGroup.add(radioButtonMassive); radiobuttonsGroup.add(radioButtonList); + fileMenu.add(loadItem); + fileMenu.add(saveItem); + fileMenu.add(loadCollection); + fileMenu.add(saveCollection); + fileMenu.addSeparator(); + menuBar.add(fileMenu); + setJMenuBar(menuBar); + _canvasWarmlyShip.setBounds(0, 0, getWidth()-200, getHeight()); labelCollectionName.setBounds(getWidth()-190, 10, 150, 20); textBoxCollection.setBounds(getWidth()-190, 32, 150, 25); @@ -245,7 +290,7 @@ public class FormShipCollection extends JFrame{ GoToCheckButton.setBounds(getWidth()-190, 435, 150, 30); RandomButton.setBounds(getWidth()-190, 470, 150, 30); RemoveObjectsButton.setBounds(getWidth()-190, 505, 150, 30); - RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); + RefreshButton.setBounds(getWidth()-190, getHeight()-100, 150, 30); setSize(dimension.width,dimension.height); setLayout(null); @@ -268,6 +313,60 @@ public class FormShipCollection extends JFrame{ add(RefreshButton); 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 (_storageCollection.SaveData(filename)) { + JOptionPane.showMessageDialog(null, "Сохранено"); + } + else JOptionPane.showMessageDialog(null, "Ошибка сохранения"); + } + private void SaveCollection() { + String filename = SaveWindow(); + if (filename == null) { + JOptionPane.showMessageDialog(null, "Файл не выбран"); + return; + } + if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) { + JOptionPane.showMessageDialog(null, "Коллекция не выбрана"); + } + if (_storageCollection.SaveOneCollection(filename, listBoxCollection.getSelectedValue().toString())) { + JOptionPane.showMessageDialog(null, "Коллекция сохранена"); + } + else JOptionPane.showMessageDialog(null, "Ошибка сохранения"); + } + 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 (_storageCollection.LoadData(filename)) { + JOptionPane.showMessageDialog(null, "Загрузка прошла успешно"); + RerfreshListBoxItems(); + } + else JOptionPane.showMessageDialog(null, "Не загрузилось"); + + } + private void LoadCollection() { + String filename = LoadWindow(); + if (_storageCollection.LoadOneCollection(filename)) { + JOptionPane.showMessageDialog(null, "Коллекция загружена"); + RerfreshListBoxItems(); + } + else JOptionPane.showMessageDialog(null, "Не загрузилось"); + } private void RerfreshListBoxItems() { DefaultListModel list = new DefaultListModel(); diff --git a/WarmlyShip/src/FormShipConfig.java b/WarmlyShip/src/FormShipConfig.java index deae769..dd53c60 100644 --- a/WarmlyShip/src/FormShipConfig.java +++ b/WarmlyShip/src/FormShipConfig.java @@ -36,7 +36,7 @@ public class FormShipConfig extends JFrame { private JLabel labelBeamsDeck = new JLabel("Beams Deck", SwingConstants.CENTER); private JLabel labelWindowDeck = new JLabel("Window Deck", SwingConstants.CENTER); private JLabel labelAdditionalColor = new JLabel("Additi Color", SwingConstants.CENTER); - private JLabel labelNumberOfDecks = new JLabel("Numb od decks"); + private JLabel labelNumberOfDecks = new JLabel("Numb Of decks"); private JSpinner spinnerSpeed = new JSpinner(); private JSpinner spinnerWeight = new JSpinner(); private JSpinner spinnerNumberOfDecks = new JSpinner(); -- 2.25.1