diff --git a/src/CollectionGenericObjects/ICollectionGenericObjects.java b/src/CollectionGenericObjects/ICollectionGenericObjects.java index 3fc89c1..a6fb6c2 100644 --- a/src/CollectionGenericObjects/ICollectionGenericObjects.java +++ b/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/src/CollectionGenericObjects/ListGenericObjects.java b/src/CollectionGenericObjects/ListGenericObjects.java index c7dbf28..4a27990 100644 --- a/src/CollectionGenericObjects/ListGenericObjects.java +++ b/src/CollectionGenericObjects/ListGenericObjects.java @@ -1,9 +1,9 @@ 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 +18,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 +42,34 @@ 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; + 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 bulldozer : _collection) { + bulldozer = null; + } + } } \ No newline at end of file diff --git a/src/CollectionGenericObjects/MassiveGenericObjects.java b/src/CollectionGenericObjects/MassiveGenericObjects.java index 3725fcc..76f3881 100644 --- a/src/CollectionGenericObjects/MassiveGenericObjects.java +++ b/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -2,9 +2,11 @@ package CollectionGenericObjects; import Drawings.DrawingBulldozer; import java.lang.reflect.Array; +import java.util.*; public class MassiveGenericObjects implements ICollectionGenericObjects{ private T[] _collection = null; + private CollectionType collectionType = CollectionType.Massive; private int Count; @Override public void SetMaxCount(int size) { @@ -20,6 +22,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()) @@ -46,4 +52,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 bulldozer : _collection) { + bulldozer = null; + } + } } diff --git a/src/CollectionGenericObjects/StorageCollection.java b/src/CollectionGenericObjects/StorageCollection.java index 6e7f8a7..f15a19a 100644 --- a/src/CollectionGenericObjects/StorageCollection.java +++ b/src/CollectionGenericObjects/StorageCollection.java @@ -1,8 +1,12 @@ package CollectionGenericObjects; +import Drawings.DrawingBulldozer; +import Drawings.ExtensionDrawingBulldozer; + +import java.io.*; import java.util.*; -public class StorageCollection { +public class StorageCollection { private Map> _storages; public StorageCollection() { @@ -38,4 +42,154 @@ 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 bulldozer : value.getValue().GetItems()) { + String data = ExtensionDrawingBulldozer.GetDataForSave((DrawingBulldozer) bulldozer); + 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 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) { + DrawingBulldozer bulldozer = ExtensionDrawingBulldozer.CreateDrawingBulldozer(elem); + if (collection.Insert((T) bulldozer) == -1) + { + return false; + } + } + _storages.put(record[0], collection); + } + return true; + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + public boolean SaveCollection(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 bulldozer : value.GetItems()) { + String data = ExtensionDrawingBulldozer.GetDataForSave((DrawingBulldozer) bulldozer); + 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 LoadCollection(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) { + DrawingBulldozer bulldozer = ExtensionDrawingBulldozer.CreateDrawingBulldozer(elem); + if (collection.Insert((T) bulldozer) == -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; + } } \ No newline at end of file diff --git a/src/Drawings/DrawingBulldozer.java b/src/Drawings/DrawingBulldozer.java index b42f9d7..eef0b70 100644 --- a/src/Drawings/DrawingBulldozer.java +++ b/src/Drawings/DrawingBulldozer.java @@ -1,5 +1,8 @@ package Drawings; +import DifferentRollers.DrawingRollersCross; +import DifferentRollers.DrawingRollersPlus; +import DifferentRollers.DrawingRollersStar; import DifferentRollers.IDifferentRollers; import Entities.EntityBulldozer; @@ -117,6 +120,19 @@ public class DrawingBulldozer extends JPanel { } } + public String[] GetStringRepresentationRollers() { + if (drawingRollers instanceof DrawingRollersPlus) { + return new String[]{String.valueOf(drawingRollers.getRollersCount().getNumOfRollers()), "DrawingRollersPlus"}; + } + else if (drawingRollers instanceof DrawingRollersCross) { + return new String[]{String.valueOf(drawingRollers.getRollersCount().getNumOfRollers()), "DrawingRollersCross"}; + } + else if (drawingRollers instanceof DrawingRollersStar) { + return new String[]{String.valueOf(drawingRollers.getRollersCount().getNumOfRollers()), "DrawingRollersStar"}; + } + return null; + } + // Отрисовка Экскаватора public void DrawTransport(Graphics2D g){ if (EntityBulldozer == null || _startPosX == null || _startPosY == null){ diff --git a/src/Drawings/ExtensionDrawingBulldozer.java b/src/Drawings/ExtensionDrawingBulldozer.java new file mode 100644 index 0000000..6cbd24f --- /dev/null +++ b/src/Drawings/ExtensionDrawingBulldozer.java @@ -0,0 +1,80 @@ +package Drawings; + +import DifferentRollers.DrawingRollersStar; +import DifferentRollers.DrawingRollersPlus; +import DifferentRollers.DrawingRollersCross; +import DifferentRollers.IDifferentRollers; +import Entities.*; + +import java.util.ArrayList; +import java.util.Collections; + + +public class ExtensionDrawingBulldozer { + private static String _separatorForObjectS = ":"; + private static String _separatorForObject = "\\:"; + public static DrawingBulldozer CreateDrawingBulldozer(String info) { + String[] strs = info.split(_separatorForObject); + EntityBulldozer bulldozer; + IDifferentRollers rollers = null; + if (strs.length == 9) + { + String s = strs[8]; + switch (s) { + case "DrawingRollersStar": + rollers = new DrawingRollersStar(); + break; + case "DrawingRollersPlus": + rollers = new DrawingRollersPlus(); + break; + case "DrawingRollersCross": + rollers = new DrawingRollersCross(); + break; + } + if (rollers != null) rollers.setRollersCount(Integer.parseInt(strs[7])); + } + else if (strs.length == 6) { + String s = strs[5]; + switch (s) { + case "DrawingRollersStar": + rollers = new DrawingRollersStar(); + break; + case "DrawingRollersPlus": + rollers = new DrawingRollersPlus(); + break; + case "DrawingRollersCross": + rollers = new DrawingRollersCross(); + break; + } + if (rollers != null) rollers.setRollersCount(Integer.parseInt(strs[4])); + } + bulldozer = EntityExcavator.CreateEntityExcavator(strs); + if (bulldozer != null) + { + return new DrawingExcavator((EntityExcavator)bulldozer, rollers); + } + bulldozer = EntityBulldozer.CreateEntityBulldozer(strs); + if (bulldozer != null) + { + return new DrawingBulldozer(bulldozer, rollers); + } + return null; + } + public static String GetDataForSave(DrawingBulldozer drawingBulldozer) + { + if (drawingBulldozer == null) return ""; + String[] arrayEntity = drawingBulldozer.EntityBulldozer.GetStringRepresentation(); + String[] arrayRollers = drawingBulldozer.GetStringRepresentationRollers(); + if (arrayEntity == null) + { + return ""; + } + ArrayList list = new ArrayList<>(); + Collections.addAll(list, arrayEntity); + if (arrayRollers == null) { + Collections.addAll(list, "0", " "); + } + else Collections.addAll(list, arrayRollers); + return String.join(_separatorForObjectS, list); + } +} diff --git a/src/Entities/EntityBulldozer.java b/src/Entities/EntityBulldozer.java index d159853..26b1aa0 100644 --- a/src/Entities/EntityBulldozer.java +++ b/src/Entities/EntityBulldozer.java @@ -1,12 +1,14 @@ package Entities; import java.awt.*; +import java.util.Objects; public class EntityBulldozer { private Integer Speed; + public Integer getSpeed() {return Speed;} public void setSpeed(int speed) {Speed = speed;} private Double Weight; - + public Double getWeight() {return Weight;} public void setWeight(double weight) {Weight = weight;} private Color BodyColor; @@ -22,4 +24,22 @@ public class EntityBulldozer { BodyColor = bodyColor; Step = Speed * 100 / Weight; } + public String[] GetStringRepresentation() + { + return new String[]{"EntityBulldozer", Speed.toString(), Weight.toString(), colorToHexString(BodyColor)}; + } + public static EntityBulldozer CreateEntityBulldozer(String[] strs) + { + if (strs.length != 6 || !Objects.equals(strs[0], "EntityBulldozer")) + { + return null; + } + return new EntityBulldozer(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/src/Entities/EntityExcavator.java b/src/Entities/EntityExcavator.java index b25ba0f..cabb1b3 100644 --- a/src/Entities/EntityExcavator.java +++ b/src/Entities/EntityExcavator.java @@ -1,6 +1,7 @@ package Entities; import java.awt.*; +import java.util.Objects; public class EntityExcavator extends EntityBulldozer{ private Color AdditionalColor; @@ -25,4 +26,20 @@ public class EntityExcavator extends EntityBulldozer{ Prop = prop; Ladle = ladle; } + @Override + public String[] GetStringRepresentation() + { + return new String[]{"EntityExcavator", getSpeed().toString(), getWeight().toString(), + colorToHexString(getBodyColor()), colorToHexString(getAdditionalColor()), + String.valueOf(Prop), String.valueOf(Ladle)}; + } + public static EntityExcavator CreateEntityExcavator(String[] strs) + { + if (strs.length != 9 || !Objects.equals(strs[0], "EntityExcavator")) + { + return null; + } + return new EntityExcavator(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/src/FormBulldozerCollection.java b/src/FormBulldozerCollection.java index 745531b..94283f3 100644 --- a/src/FormBulldozerCollection.java +++ b/src/FormBulldozerCollection.java @@ -34,6 +34,13 @@ public class FormBulldozerCollection extends JFrame{ private JButton RefreshButton = new JButton("Обновить"); private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); private JFormattedTextField MaskedTextField; + + private JMenuBar menuBar = new JMenuBar(); + private JMenu fileMenu = new JMenu("Файл"); + private JMenuItem saveItem = new JMenuItem("Сохранить"); + private JMenuItem loadItem = new JMenuItem("Загрузить"); + private JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию"); + private JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию"); public FormBulldozerCollection(String title, Dimension dimension) { this.title = title; this.dimension = dimension; @@ -129,7 +136,6 @@ public class FormBulldozerCollection extends JFrame{ } FormAdditionalCollection form = new FormAdditionalCollection(); form.setCompany(_company); - form.setLocationRelativeTo(null); } }); @@ -220,7 +226,37 @@ public class FormBulldozerCollection extends JFrame{ } FormExcavator form = new FormExcavator("Экскаватор", new Dimension(900,565)); form.Init(bulldozer); - form.setLocationRelativeTo(null); + } + }); + + 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("Сохранение коллекции"); + SaveCollection(); + } + }); + + loadCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.out.println("Загрузка коллекции"); + LoadCollection(); } }); @@ -229,6 +265,13 @@ public class FormBulldozerCollection extends JFrame{ radioButtonsGroup.add(radioButtonMassive); radioButtonsGroup.add(radioButtonList); + fileMenu.add(saveItem); + fileMenu.add(loadItem); + fileMenu.add(saveCollection); + fileMenu.add(loadCollection); + menuBar.add(fileMenu); + setJMenuBar(menuBar); + _canvasExcavator.setBounds(0, 0, getWidth()-200, getHeight()); labelCollectionName.setBounds(getWidth()-190, 30, 150, 20); textBoxCollection.setBounds(getWidth()-190, 52, 150, 25); @@ -245,7 +288,7 @@ public class FormBulldozerCollection extends JFrame{ GoToCheckButton.setBounds(getWidth()-190, 440, 150, 30); RandomButton.setBounds(getWidth()-190, 480, 150, 30); RemoveObjectsButton.setBounds(getWidth()-190, 520, 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); @@ -280,4 +323,59 @@ public class FormBulldozerCollection extends JFrame{ } listBoxCollection.setModel(list); } + + private String SaveWindow() { + FileDialog fileDialog = new FileDialog(this, "Сохранить файл", 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.SaveCollection(filename, listBoxCollection.getSelectedValue().toString())) { + JOptionPane.showMessageDialog(null, "Коллекция сохранена"); + } + else JOptionPane.showMessageDialog(null, "Ошибка сохранения"); + } + private String LoadWindow() { + FileDialog fileDialog = new FileDialog(this, "Загрузить файл", 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, "Загрузка прошла успешно"); + RefreshListBoxItems(); + } + else JOptionPane.showMessageDialog(null, "Не загрузилось"); + + } + private void LoadCollection() { + String filename = LoadWindow(); + if (_storageCollection.LoadCollection(filename)) { + JOptionPane.showMessageDialog(null, "Коллекция загружена"); + RefreshListBoxItems(); + } + else JOptionPane.showMessageDialog(null, "Ошибка загрузки"); + } } diff --git a/src/Program.java b/src/Program.java index b25f45d..a0b02e0 100644 --- a/src/Program.java +++ b/src/Program.java @@ -2,7 +2,7 @@ import java.awt.*; public class Program { public static void main(String[] args) { - FormBulldozerCollection form = new FormBulldozerCollection("Коллекция экскаваторов", new Dimension(1100, 650)); + FormBulldozerCollection form = new FormBulldozerCollection("Коллекция экскаваторов", new Dimension(1100, 660)); form.Init(); form.setLocationRelativeTo(null); }