From 1812efd16ec14d86fb1db8caebe1e94f4fabe9f0 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 12:51:23 +0400 Subject: [PATCH 1/8] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B2=20DrawningLoco?= =?UTF-8?q?motive=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BE=D1=85=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=BE=D0=B1=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B5=20=D0=B8=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 53 ++++++++++++++++++++++++++++++++++++++++ ExtraRoundWheelDraw.java | 9 +++++++ ExtraStarWheelDraw.java | 10 ++++++++ ExtraWheelsDraw.java | 10 ++++++++ IDrawningExtra.java | 2 ++ 5 files changed, 84 insertions(+) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 015f2ef..e20d0ca 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -150,4 +150,57 @@ public class DrawningLocomotive { { return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _locomotiveWidth, /*DOWN*/ _startPosY + _locomotiveHeight, /*LEFT*/ _startPosX}; } + + private static final char _separatorForObject = ':'; + public String getDataForSave() + { + var str = "" + Locomotive.getSpeed() + _separatorForObject + + Locomotive.getWeight() + _separatorForObject + + Locomotive.getBodyColor().getRed() + _separatorForObject + + Locomotive.getBodyColor().getGreen() + _separatorForObject + + Locomotive.getBodyColor().getBlue() + _separatorForObject + + drawningExtra.TypeString() + _separatorForObject + + drawningExtra.getWheelsCount(); + if (!(Locomotive instanceof EntityWarmlyLocomotive)) + { + return str; + } + return str + _separatorForObject + + ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getRed() + _separatorForObject + + ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getGreen() + _separatorForObject + + ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getBlue() + _separatorForObject + + ((EntityWarmlyLocomotive) Locomotive).Pipe + _separatorForObject + + ((EntityWarmlyLocomotive) Locomotive).FuelStorage; + } + + public static DrawningLocomotive createDrawningLocomotive(String info) { + IDrawningExtra drawningExtra = null; + EntityLocomotive Locomotive = null; + String[] strs = info.split(Character.toString(_separatorForObject)); + if (strs[5] == "Simple") drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); + if (strs[5] == "Star") drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); + if (strs[5] == "Round") drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); + if (drawningExtra == null) + if (strs.length == 7) + { + Locomotive = new EntityLocomotive( + Integer.parseInt(strs[0]), + Float.parseFloat(strs[1]), + new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])) + ); + return new DrawningLocomotive(Locomotive, drawningExtra); + } + if (strs.length == 12) { + Locomotive = new EntityWarmlyLocomotive( + Integer.parseInt(strs[0]), + Float.parseFloat(strs[1]), + new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])), + new Color(Integer.parseInt(strs[7]), Integer.parseInt(strs[8]), Integer.parseInt(strs[9])), + Boolean.parseBoolean(strs[10]), + Boolean.getBoolean(strs[11]) + ); + return new DrawningWarmlyLocomotive(Locomotive, drawningExtra); + } + return null; + } } diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java index 669ef3c..8824c27 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -19,6 +19,15 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{ } } + public String TypeString() { + return "Round"; + } + public int getWheelsCount() { + if (wheelsCount == WheelsCount.Two) return 2; + if (wheelsCount == WheelsCount.Three) return 3; + else return 4; + } + public ExtraRoundWheelDraw (int num, Color bodyColor) { setExtraNum(num); extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java index 17a8b33..a1ca0d5 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -19,6 +19,16 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ } } + public String TypeString() { + return "Star"; + } + + public int getWheelsCount() { + if (wheelsCount == WheelsCount.Two) return 2; + if (wheelsCount == WheelsCount.Three) return 3; + else return 4; + } + public ExtraStarWheelDraw (int num, Color bodyColor) { setExtraNum(num); extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); diff --git a/ExtraWheelsDraw.java b/ExtraWheelsDraw.java index eb37d42..5af3526 100644 --- a/ExtraWheelsDraw.java +++ b/ExtraWheelsDraw.java @@ -26,6 +26,16 @@ public class ExtraWheelsDraw implements IDrawningExtra{ this.color = color; } + public String TypeString() { + return "Simple"; + } + + public int getWheelsCount() { + if (wheelsCount == WheelsCount.Two) return 2; + if (wheelsCount == WheelsCount.Three) return 3; + else return 4; + } + public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { g.setColor(Color.BLACK); g.drawOval(startPosX, startPosY + 30, 20, 20); diff --git a/IDrawningExtra.java b/IDrawningExtra.java index fc9cf41..ce860f0 100644 --- a/IDrawningExtra.java +++ b/IDrawningExtra.java @@ -4,4 +4,6 @@ public interface IDrawningExtra { void setExtraNum(int num); void DrawExtra(int startPosX, int startPosY, Graphics2D g); void SetColor(Color color); + String TypeString(); + int getWheelsCount(); } -- 2.25.1 From 0ca1f9efcb6b81ed4dc6661d539e3b7fe5323b08 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 12:58:00 +0400 Subject: [PATCH 2/8] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D0=B0=20IDrawningObject=20=D0=B8=20=D0=B5=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?DrawningObjectLocomotive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 2 +- DrawningObjectLocomotive.java | 5 +++++ IDrawningObject.java | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index e20d0ca..8f7e68d 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -180,7 +180,7 @@ public class DrawningLocomotive { if (strs[5] == "Simple") drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); if (strs[5] == "Star") drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); if (strs[5] == "Round") drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); - if (drawningExtra == null) + if (drawningExtra == null) return null; if (strs.length == 7) { Locomotive = new EntityLocomotive( diff --git a/DrawningObjectLocomotive.java b/DrawningObjectLocomotive.java index 2695ed0..417cc14 100644 --- a/DrawningObjectLocomotive.java +++ b/DrawningObjectLocomotive.java @@ -39,4 +39,9 @@ public class DrawningObjectLocomotive implements IDrawningObject { { if (_locomotive != null) _locomotive.SetPosition(x, y, width, height); } + + public String getInfo(){ + if (_locomotive == null) return null; + return _locomotive.getDataForSave(); + } } diff --git a/IDrawningObject.java b/IDrawningObject.java index 80b49ff..1167973 100644 --- a/IDrawningObject.java +++ b/IDrawningObject.java @@ -8,6 +8,9 @@ public interface IDrawningObject { void MoveObject(Direction direction); /// Отрисовка объекта void DrawningObject(Graphics g); + + String getInfo(); + /// Получение текущей позиции объекта float[] GetCurrentPosition(); //0 - up -- 2.25.1 From a9f39a20a9193d884b8c0d551dd526af5a701bc1 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 14:06:39 +0400 Subject: [PATCH 3/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B2=20?= =?UTF-8?q?MapWithSetLocomotivesGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningObjectLocomotive.java | 4 ++++ MapWithSetLocomotivesGeneric.java | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/DrawningObjectLocomotive.java b/DrawningObjectLocomotive.java index 417cc14..7c46d4d 100644 --- a/DrawningObjectLocomotive.java +++ b/DrawningObjectLocomotive.java @@ -44,4 +44,8 @@ public class DrawningObjectLocomotive implements IDrawningObject { if (_locomotive == null) return null; return _locomotive.getDataForSave(); } + + public static IDrawningObject Create(String data) { + return new DrawningObjectLocomotive(DrawningLocomotive.createDrawningLocomotive(data)); + } } diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java index a30b6c6..e7d3c43 100644 --- a/MapWithSetLocomotivesGeneric.java +++ b/MapWithSetLocomotivesGeneric.java @@ -174,4 +174,25 @@ public class MapWithSetLocomotivesGeneric } } + /// Получение данных в виде строки + public String GetData(char separatorType, char separatorData) + { + //string data = $"{_map.GetType().Name}{separatorType}"; + String data = ""+ _map.getClass() + separatorType; + for (var locomotive : _setLocomotives.GetLocomotives()) + { + data += "" + locomotive.getInfo() + separatorData; + } + return data; + } + + /// Загрузка списка из массива строк + public void LoadData(String[] records) + { + for (var rec : records) + { + _setLocomotives.Insert((T)DrawningObjectLocomotive.Create(rec)); + } + } + } -- 2.25.1 From 1d30c011aa1a1748bc65e13f054865cbc7877d5b Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 16:11:28 +0400 Subject: [PATCH 4/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20=D1=81?= =?UTF-8?q?=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=20=D0=B2=20MapsColle?= =?UTF-8?q?ction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MapWithSetLocomotivesGeneric.java | 4 ++ MapsCollection.java | 78 +++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java index e7d3c43..c0ad0b7 100644 --- a/MapWithSetLocomotivesGeneric.java +++ b/MapWithSetLocomotivesGeneric.java @@ -1,5 +1,8 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Collections; import java.util.LinkedList; public class MapWithSetLocomotivesGeneric @@ -189,6 +192,7 @@ public class MapWithSetLocomotivesGeneric /// Загрузка списка из массива строк public void LoadData(String[] records) { + Collections.reverse(Arrays.asList(records)); for (var rec : records) { _setLocomotives.Insert((T)DrawningObjectLocomotive.Create(rec)); diff --git a/MapsCollection.java b/MapsCollection.java index 4aae1fa..2077e27 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -1,9 +1,10 @@ +import java.io.*; import java.util.ArrayList; import java.util.HashMap; public class MapsCollection { /// Словарь (хранилище) с картами - final HashMap> _mapStorages; + final HashMap> _mapStorages; /// Возвращение списка названий карт public ArrayList keys() { @@ -13,10 +14,13 @@ public class MapsCollection { private final int _pictureWidth; /// Высота окна отрисовки private final int _pictureHeight; + // Сепараторы + private final char separatorDict = '|'; + private final char separatorData = ';'; /// Конструктор public MapsCollection(int pictureWidth, int pictureHeight) { - _mapStorages = new HashMap>(); + _mapStorages = new HashMap>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -24,7 +28,7 @@ public class MapsCollection { public void AddMap(String name, AbstractMap map) { // Логика для добавления - if (!_mapStorages.containsKey(name)) _mapStorages.put(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + if (!_mapStorages.containsKey(name)) _mapStorages.put(name, new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); } /// Удаление карты public void DelMap(String name) @@ -33,15 +37,79 @@ public class MapsCollection { if (_mapStorages.containsKey(name)) _mapStorages.remove(name); } /// Доступ к парковке - public MapWithSetLocomotivesGeneric Get(String ind) + public MapWithSetLocomotivesGeneric Get(String ind) { // Логика получения объекта if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind); return null; } // Доп.индексатор из задания - public DrawningObjectLocomotive Get (String name, int position) { + public IDrawningObject Get (String name, int position) { if (_mapStorages.containsKey(name)) return _mapStorages.get(name)._setLocomotives.Get(position); else return null; } + + /// Сохранение информации по локомотивам в хранилище в файл + public boolean SaveData(String filename) + { + File file = new File(filename); + if (file.exists()) + { + file.delete(); + } + try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { + bw.write("MapsCollection\n"); + for (var storage : _mapStorages.entrySet()) { + bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict, separatorData) + "\n"); + } + } + catch (IOException ex) { + System.out.println(ex.getMessage()); + } + return true; + } + + /// Загрузка информации по локомотивам в депо из файла + public boolean LoadData(String filename) + { + File file = new File(filename); + if (!file.exists()) + { + return false; + } + try { + BufferedReader br = new BufferedReader(new FileReader(filename)); + String curLine = br.readLine(); + if (!curLine.contains("MapsCollection")) { + return false; + } + + _mapStorages.clear(); + while ((curLine = br.readLine()) != null) { + var elems = curLine.split(Character.toString(separatorDict)); + AbstractMap map = null; + + switch (elems[1]) + { //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + case "SimpleMap": + map = new SimpleMap(); + break; + case "SpikeMap": + map = new SpikeMap(); + break; + case "RailMap": + map = new RailMap(); + break; + } + + _mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData))); + } + } catch (IOException ex) { + System.out.println(ex.getMessage()); + } + return true; + } + + } -- 2.25.1 From 9f39107385b474f7292064840140de72616a3a33 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 19:32:56 +0400 Subject: [PATCH 5/8] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B1=D0=B0=D0=B7=D0=B0=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B16?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 7 ++++--- ExtraRoundWheelDraw.java | 4 ++-- ExtraStarWheelDraw.java | 4 ++-- FormLocomotive.java | 1 - FormMapWithSetLocomotives.java | 30 +++++++++++++++++++++++++++++- MapWithSetLocomotivesGeneric.java | 1 - MapsCollection.java | 12 ++++++------ 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 8f7e68d..4cbd4f3 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -1,4 +1,5 @@ import java.awt.*; +import java.util.Objects; import java.util.Random; public class DrawningLocomotive { @@ -177,9 +178,9 @@ public class DrawningLocomotive { IDrawningExtra drawningExtra = null; EntityLocomotive Locomotive = null; String[] strs = info.split(Character.toString(_separatorForObject)); - if (strs[5] == "Simple") drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); - if (strs[5] == "Star") drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); - if (strs[5] == "Round") drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor()); + if (strs[5].equals("Simple")) drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4]))); + if (strs[5].equals("Star")) drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4]))); + if (Objects.equals(strs[5], "Round")) drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4]))); if (drawningExtra == null) return null; if (strs.length == 7) { diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java index 8824c27..e578f0b 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -6,11 +6,11 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{ private Color color; public void setExtraNum(int num) { switch (num) { - case 0: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java index a1ca0d5..b8ca923 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -6,11 +6,11 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ private Color color; public void setExtraNum(int num) { switch (num) { - case 0: { + case 3: { wheelsCount = WheelsCount.Three; break; } - case 1: { + case 4: { wheelsCount = WheelsCount.Four; break; } diff --git a/FormLocomotive.java b/FormLocomotive.java index 57fd25a..765b90b 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -108,7 +108,6 @@ public class FormLocomotive extends JComponent{ super.repaint(); } public static void main(String[] args) { - new FormMapWithSetLocomotives(); } } diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index ce8dfab..d30af27 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -263,7 +263,35 @@ public class FormMapWithSetLocomotives extends JComponent { dialog.setVisible(true); } }); - statusPanel.add(showDeletedButton); + + JFileChooser fileChooser = new JFileChooser(); + JButton saveButton = new JButton("Save"); + saveButton.addActionListener(e -> { + fileChooser.setDialogTitle("Saving"); + int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this); + if (result == JFileChooser.APPROVE_OPTION ) { + if (_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Save success"); + } + else JOptionPane.showMessageDialog(null, "Save failed"); + } + + }); + statusPanel.add(saveButton); + JButton loadButton = new JButton("Load"); + loadButton.addActionListener(e -> { + fileChooser.setDialogTitle("Loading"); + int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this); + if (result == JFileChooser.APPROVE_OPTION ) { + if (_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Load success"); + } + else JOptionPane.showMessageDialog(null, "Load failed"); + } + ReloadMaps(); + repaint(); + }); + statusPanel.add(loadButton); formFrame.getContentPane().add(this); formFrame.setVisible(true); diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java index c0ad0b7..0df89e8 100644 --- a/MapWithSetLocomotivesGeneric.java +++ b/MapWithSetLocomotivesGeneric.java @@ -180,7 +180,6 @@ public class MapWithSetLocomotivesGeneric /// Получение данных в виде строки public String GetData(char separatorType, char separatorData) { - //string data = $"{_map.GetType().Name}{separatorType}"; String data = ""+ _map.getClass() + separatorType; for (var locomotive : _setLocomotives.GetLocomotives()) { diff --git a/MapsCollection.java b/MapsCollection.java index 2077e27..ccdcd81 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -15,7 +15,7 @@ public class MapsCollection { /// Высота окна отрисовки private final int _pictureHeight; // Сепараторы - private final char separatorDict = '|'; + private final String separatorDict = "\\|"; private final char separatorData = ';'; /// Конструктор public MapsCollection(int pictureWidth, int pictureHeight) @@ -60,7 +60,7 @@ public class MapsCollection { try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { bw.write("MapsCollection\n"); for (var storage : _mapStorages.entrySet()) { - bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict, separatorData) + "\n"); + bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n"); } } catch (IOException ex) { @@ -86,18 +86,18 @@ public class MapsCollection { _mapStorages.clear(); while ((curLine = br.readLine()) != null) { - var elems = curLine.split(Character.toString(separatorDict)); + var elems = curLine.split(separatorDict); AbstractMap map = null; switch (elems[1]) { //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - case "SimpleMap": + case "class SimpleMap": map = new SimpleMap(); break; - case "SpikeMap": + case "class SpikeMap": map = new SpikeMap(); break; - case "RailMap": + case "class RailMap": map = new RailMap(); break; } -- 2.25.1 From 2fe33e7569eb5aa03dae662b79438ca9b2098aa9 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 19:58:47 +0400 Subject: [PATCH 6/8] =?UTF-8?q?=D0=B1=D0=B0=D0=B3=20=D1=84=D0=B8=D0=BA?= =?UTF-8?q?=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 2 +- MapsCollection.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 4cbd4f3..08e46f6 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -198,7 +198,7 @@ public class DrawningLocomotive { new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])), new Color(Integer.parseInt(strs[7]), Integer.parseInt(strs[8]), Integer.parseInt(strs[9])), Boolean.parseBoolean(strs[10]), - Boolean.getBoolean(strs[11]) + Boolean.parseBoolean(strs[11]) ); return new DrawningWarmlyLocomotive(Locomotive, drawningExtra); } diff --git a/MapsCollection.java b/MapsCollection.java index ccdcd81..e236bb8 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -15,7 +15,7 @@ public class MapsCollection { /// Высота окна отрисовки private final int _pictureHeight; // Сепараторы - private final String separatorDict = "\\|"; + private final String separatorDict = "~"; private final char separatorData = ';'; /// Конструктор public MapsCollection(int pictureWidth, int pictureHeight) -- 2.25.1 From f329788ce7654d217e067b018508f1d61bacd24e Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 19 Nov 2022 21:53:44 +0400 Subject: [PATCH 7/8] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=206=20=D1=85=D0=B0?= =?UTF-8?q?=D1=80=D0=B4=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0,=20=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BF=D0=BE=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=B0=D1=82=D1=8C=20=D0=B1=D0=B0=D0=B3=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=B8=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormMapWithSetLocomotives.java | 40 ++++++++++++++++++++- MapsCollection.java | 64 +++++++++++++++++++++++++++++++++- SetLocomotivesGeneric.java | 4 +++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index d30af27..18610d1 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -263,8 +263,9 @@ public class FormMapWithSetLocomotives extends JComponent { dialog.setVisible(true); } }); - + // Сохранения и загрузки JFileChooser fileChooser = new JFileChooser(); + // Сохранение всех хранилищ JButton saveButton = new JButton("Save"); saveButton.addActionListener(e -> { fileChooser.setDialogTitle("Saving"); @@ -278,6 +279,7 @@ public class FormMapWithSetLocomotives extends JComponent { }); statusPanel.add(saveButton); + // Загрузка всех хранилищ JButton loadButton = new JButton("Load"); loadButton.addActionListener(e -> { fileChooser.setDialogTitle("Loading"); @@ -293,6 +295,42 @@ public class FormMapWithSetLocomotives extends JComponent { }); statusPanel.add(loadButton); + // Сохранение выбранной карты + JButton saveMapButton = new JButton("Save Map"); + saveMapButton.addActionListener(e -> { + fileChooser.setDialogTitle("Saving Map"); + int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this); + if (result == JFileChooser.APPROVE_OPTION ) { + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } + if(listBoxMaps.getSelectedValue().toString() == null) return; + if (_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Map saving success"); + } + else JOptionPane.showMessageDialog(null, "Map saving fail"); + repaint(); + } + }); + statusPanel.add(saveMapButton); + + // Загрузка одной карты + JButton loadMapButton = new JButton("Load Map"); + loadMapButton.addActionListener(e -> { + fileChooser.setDialogTitle("Loading"); + int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this); + if (result == JFileChooser.APPROVE_OPTION ) { + if (_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath())) { + JOptionPane.showMessageDialog(null, "Load Map success"); + } + else JOptionPane.showMessageDialog(null, "Load Map failed"); + } + ReloadMaps(); + repaint(); + }); + statusPanel.add(loadMapButton); + formFrame.getContentPane().add(this); formFrame.setVisible(true); } diff --git a/MapsCollection.java b/MapsCollection.java index e236bb8..1459e92 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -1,5 +1,6 @@ import java.io.*; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; public class MapsCollection { @@ -69,6 +70,67 @@ public class MapsCollection { return true; } + // Сохранение одной выбранной карты + public boolean SaveMap(String map_name, String filename) { + File file = new File(filename); + MapWithSetLocomotivesGeneric map = Get(map_name); + if (file.exists()) + { + file.delete(); + } + try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { + bw.write("SingleMap\n"); + bw.write("" + map_name + separatorDict +map.GetData(separatorDict.charAt(0), separatorData) + "\n"); + } + catch (IOException ex) { + System.out.println(ex.getMessage()); + } + return true; + } + + // Загрузка одной карты + public boolean LoadMap(String filename){ + File file = new File(filename); + if (!file.exists()) + { + return false; + } + try { + BufferedReader br = new BufferedReader(new FileReader(filename)); + String curLine = br.readLine(); + if (!curLine.contains("SingleMap")) { + return false; + } + + curLine = br.readLine(); + var elems = curLine.split(separatorDict); + if (_mapStorages.containsKey(elems[0])) { + _mapStorages.get(elems[0])._setLocomotives.Clear(); + _mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData))); + return true; + } + AbstractMap map = null; + switch (elems[1]) + { + case "class SimpleMap": + map = new SimpleMap(); + break; + case "class SpikeMap": + map = new SpikeMap(); + break; + case "class RailMap": + map = new RailMap(); + break; + } + _mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); + _mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData))); + + } catch (IOException ex) { + System.out.println(ex.getMessage()); + } + return true; + } + /// Загрузка информации по локомотивам в депо из файла public boolean LoadData(String filename) { @@ -90,7 +152,7 @@ public class MapsCollection { AbstractMap map = null; switch (elems[1]) - { //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + { case "class SimpleMap": map = new SimpleMap(); break; diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java index 72f1f94..9aff477 100644 --- a/SetLocomotivesGeneric.java +++ b/SetLocomotivesGeneric.java @@ -34,6 +34,10 @@ public class SetLocomotivesGeneric return result; } + public void Clear() { + _places.clear(); + } + public T Get(int position) { if (position >= _maxCount || position < 0) -- 2.25.1 From 29ba58c11aa92551bae758f12b3494135c6a2867 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sun, 20 Nov 2022 20:37:09 +0400 Subject: [PATCH 8/8] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80=D0=B0?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BA=D0=B0=D1=80=D1=82?= =?UTF-8?q?=D1=8B=20=D0=9B=D0=B0=D0=B1=D0=B0=206=20=D1=85=D0=B0=D1=80?= =?UTF-8?q?=D0=B4=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0,=20=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BF=D0=BE=D0=B8=D1=81?= =?UTF-8?q?=D0=BA=D0=B0=D1=82=D1=8C=20=D0=B1=D0=B0=D0=B3=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=B8=D1=82=D1=8C=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MapsCollection.java | 29 +++++++++++++++++++---------- SetLocomotivesGeneric.java | 4 ++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/MapsCollection.java b/MapsCollection.java index 1459e92..0c666b0 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -80,7 +80,11 @@ public class MapsCollection { } try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) { bw.write("SingleMap\n"); - bw.write("" + map_name + separatorDict +map.GetData(separatorDict.charAt(0), separatorData) + "\n"); + bw.write("" + map_name + "\n"); + bw.write("" + map.getClass() + "\n"); + for (var locomotive : map._setLocomotives.GetLocomotives()) { + bw.write("" + locomotive.getInfo() + "\n"); + } } catch (IOException ex) { System.out.println(ex.getMessage()); @@ -101,16 +105,18 @@ public class MapsCollection { if (!curLine.contains("SingleMap")) { return false; } - - curLine = br.readLine(); - var elems = curLine.split(separatorDict); - if (_mapStorages.containsKey(elems[0])) { - _mapStorages.get(elems[0])._setLocomotives.Clear(); - _mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData))); + String mapName = br.readLine(); + String mapClass = br.readLine(); + if (_mapStorages.containsKey(mapName)) { + _mapStorages.get(mapName)._setLocomotives.Clear(); + while((curLine = br.readLine()) != null) { + _mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine)); + } + _mapStorages.get(mapName)._setLocomotives.ReversePlaces(); return true; } AbstractMap map = null; - switch (elems[1]) + switch (mapClass) { case "class SimpleMap": map = new SimpleMap(); @@ -122,8 +128,11 @@ public class MapsCollection { map = new RailMap(); break; } - _mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric(_pictureWidth, _pictureHeight, map)); - _mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData))); + _mapStorages.put(mapName, new MapWithSetLocomotivesGeneric<>(_pictureWidth, _pictureHeight, map)); + while((curLine = br.readLine()) != null) { + _mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine)); + } + _mapStorages.get(mapName)._setLocomotives.ReversePlaces(); } catch (IOException ex) { System.out.println(ex.getMessage()); diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java index 9aff477..07fee4a 100644 --- a/SetLocomotivesGeneric.java +++ b/SetLocomotivesGeneric.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.Collections; public class SetLocomotivesGeneric { @@ -50,4 +51,7 @@ public class SetLocomotivesGeneric { return _places; } + public void ReversePlaces() { + Collections.reverse(_places); + } } -- 2.25.1