Kashin M.I. Lab work 6 #11
@ -43,7 +43,9 @@ public class DrawingGasolineTanker {
|
||||
public void SetBodyColor(Color color){
|
||||
GasolineTanker= new EntityGasolineTanker(GasolineTanker.getSpeed(), GasolineTanker.getWeight(), color);
|
||||
}
|
||||
|
||||
public IDrawningObjectWheels GetWheels(){
|
||||
return Wheels;
|
||||
}
|
||||
public IDrawningObjectWheels GetFormOfWheels(int FormOfWheel){
|
||||
OrnamentForm temp = null;
|
||||
for (OrnamentForm form:OrnamentForm.values()) {
|
||||
|
@ -53,5 +53,11 @@ public class DrawingOrnamentWheelsFirst implements IDrawningObjectWheels{
|
||||
g.drawOval(_startPosX + 30, _startPosY + 45, 20, 5);
|
||||
g.drawOval(_startPosX + 10, _startPosY + 45, 20, 5);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetCount() {
|
||||
return Integer.toString(_wheels.getCountWheels());
|
||||
}
|
||||
}
|
||||
|
@ -54,5 +54,11 @@ public class DrawingOrnamentWheelsSecond implements IDrawningObjectWheels{
|
||||
g.drawOval(_startPosX + 30, _startPosY + 45, 20, 5);
|
||||
g.drawOval(_startPosX + 10, _startPosY + 45, 20, 5);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetCount() {
|
||||
return Integer.toString(_wheels.getCountWheels());
|
||||
}
|
||||
}
|
||||
|
@ -42,4 +42,9 @@ public class DrawingWheels implements IDrawningObjectWheels{
|
||||
g.fillOval(_startPosX + 50, _startPosY + 35, 20, 20);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetCount() {
|
||||
return Integer.toString(_wheels.getCountWheels());
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public class ExtentionGasolineTanker {
|
||||
EntityGasolineTanker ent=new EntityGasolineTanker(spd,wght,bodyClr);
|
||||
return new DrawingGasolineTanker(ent,block);
|
||||
}
|
||||
if(strs.length==9){
|
||||
if(strs.length==8){
|
||||
Color dopClr=new Color(Integer.parseInt(strs[5]));
|
||||
boolean bdkit=Boolean.parseBoolean(strs[6]);
|
||||
boolean ant=Boolean.parseBoolean(strs[7]);
|
||||
@ -31,7 +31,7 @@ public class ExtentionGasolineTanker {
|
||||
|
||||
public static String GetDataForSave(DrawingGasolineTanker drawingGasolineTanker){
|
||||
var gasolineTanker = drawingGasolineTanker.GasolineTanker;
|
||||
var str = ""+gasolineTanker.getSpeed()+_separatorForObject+gasolineTanker.getWeight()+_separatorForObject+gasolineTanker.getBodyColor().getRGB()+_separatorForObject+drawingGasolineTanker.getWhees().getClass().getSimpleName()+_separatorForObject+drawingGasolineTanker.GetBlocks().GetCount();
|
||||
var str = ""+gasolineTanker.getSpeed()+_separatorForObject+gasolineTanker.getWeight()+_separatorForObject+gasolineTanker.getBodyColor().getRGB()+_separatorForObject+drawingGasolineTanker.GetWheels().getClass().getSimpleName()+_separatorForObject+drawingGasolineTanker.GetWheels().GetCount();
|
||||
if(!(gasolineTanker instanceof EntityImprovedGasolineTanker adv)){
|
||||
return str;
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
|
||||
public class FormMapWithSetGasolineTanker extends JFrame{
|
||||
private JPanel GroupBoxTools;
|
||||
@ -23,6 +26,7 @@ public class FormMapWithSetGasolineTanker extends JFrame{
|
||||
private JButton ButtonShowDeleted;
|
||||
private JList ListBoxMaps;
|
||||
private Image bufferedImage;
|
||||
private JMenuBar MenuBar;
|
||||
|
||||
private MapsCollection _mapsCollection;
|
||||
private final HashMap<String,AbstractMap> _mapsDict=new HashMap<String,AbstractMap>(){
|
||||
@ -85,6 +89,87 @@ public class FormMapWithSetGasolineTanker extends JFrame{
|
||||
|
||||
private void ShowWindow(){
|
||||
|
||||
MenuBar=new JMenuBar();
|
||||
|
||||
JMenu fileMenu =new JMenu("File");
|
||||
MenuBar.add(fileMenu);
|
||||
|
||||
JMenuItem SaveMenuItem = new JMenuItem("Save");
|
||||
SaveMenuItem.addActionListener(e -> {
|
||||
JFileChooser dialog = new JFileChooser();
|
||||
dialog.setFileFilter(new FileNameExtensionFilter("txt", "txt"));
|
||||
dialog.showSaveDialog(this);
|
||||
|
||||
try {
|
||||
if (_mapsCollection.SaveData(dialog.getSelectedFile().getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(this, "Save was successful", "Outcome", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "\n" + "Not preserved", "Outcome", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
fileMenu.add(SaveMenuItem);
|
||||
|
||||
JMenuItem LoadMenuItem = new JMenuItem("Load");
|
||||
LoadMenuItem.addActionListener(e -> {
|
||||
JFileChooser dialog = new JFileChooser();
|
||||
dialog.setFileFilter(new FileNameExtensionFilter("txt", "txt"));
|
||||
dialog.showOpenDialog(this);
|
||||
|
||||
try {
|
||||
if (_mapsCollection.LoadData(dialog.getSelectedFile().getAbsolutePath())) {
|
||||
ReloadMaps();
|
||||
JOptionPane.showMessageDialog(this, "Download successful", "Outcome", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "\n" + "Didn't load", "Outcome", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
fileMenu.add(LoadMenuItem);
|
||||
|
||||
JMenuItem SaveMapMenuItem = new JMenuItem("Save maps");
|
||||
SaveMapMenuItem.addActionListener(e -> {
|
||||
JFileChooser dialog = new JFileChooser();
|
||||
dialog.setFileFilter(new FileNameExtensionFilter("txt", "txt"));
|
||||
dialog.showSaveDialog(this);
|
||||
|
||||
try {
|
||||
if (_mapsCollection.SaveMap((String) Optional.ofNullable(ListBoxMaps.getSelectedValue()).orElse(""), dialog.getSelectedFile().getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(this, "\n" + "Save was successful", "Outcome", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Not preserved", "Outcome", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
fileMenu.add(SaveMapMenuItem);
|
||||
|
||||
JMenuItem LoadMapMenuItem = new JMenuItem("Load maps");
|
||||
LoadMapMenuItem.addActionListener(e -> {
|
||||
JFileChooser dialog = new JFileChooser();
|
||||
dialog.setFileFilter(new FileNameExtensionFilter("txt", "txt"));
|
||||
dialog.showOpenDialog(this);
|
||||
|
||||
try {
|
||||
if (_mapsCollection.LoadMap(dialog.getSelectedFile().getAbsolutePath())) {
|
||||
ReloadMaps();
|
||||
JOptionPane.showMessageDialog(this, "Download successful", "Outcome", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "\n" + "Didn't load", "Outcome", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
fileMenu.add(LoadMapMenuItem);
|
||||
|
||||
setJMenuBar(MenuBar);
|
||||
|
||||
ListBoxMaps.addListSelectionListener(e -> {
|
||||
if(ListBoxMaps.getSelectedIndex()==-1)
|
||||
return;
|
||||
@ -98,7 +183,7 @@ public class FormMapWithSetGasolineTanker extends JFrame{
|
||||
return;
|
||||
}
|
||||
if(JOptionPane.showConfirmDialog(this,"Delete map "+ListBoxMaps.getSelectedValue().toString()+"?",
|
||||
"Удаление",JOptionPane.YES_NO_OPTION)==0)
|
||||
"Removal",JOptionPane.YES_NO_OPTION)==0)
|
||||
{
|
||||
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
|
||||
ReloadMaps();
|
||||
|
@ -3,4 +3,5 @@ import java.awt.*;
|
||||
public interface IDrawningObjectWheels {
|
||||
void SetCountWheels(int count);
|
||||
void DrawWheels(Graphics2D g, int _startPosX, int _startPosY);
|
||||
String GetCount();
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends
|
||||
private final int _pictureHeight;
|
||||
private final int _placeSizeWidth = 210;
|
||||
private final int _placeSizeHeight = 90;
|
||||
private final SetGasolineTankerGeneric<T> _setGasolineTanker;
|
||||
public final SetGasolineTankerGeneric<T> _setGasolineTanker;
|
||||
private final ArrayDeque<T> DeletedGasolineTanker;
|
||||
private final U _map;
|
||||
|
||||
@ -22,6 +22,9 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends
|
||||
DeletedGasolineTanker = new ArrayDeque<>();
|
||||
}
|
||||
|
||||
public U GetMap(){
|
||||
return _map;
|
||||
}
|
||||
public int plus(T gasolineTanker)
|
||||
{
|
||||
return _setGasolineTanker.Insert(gasolineTanker);
|
||||
@ -132,4 +135,18 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends
|
||||
return null;
|
||||
return DeletedGasolineTanker.pop();
|
||||
}
|
||||
public String GetData(char separatorType,char separatorData){
|
||||
String data=""+_map.getClass().getSimpleName()+separatorType;
|
||||
for (var gasolineTanker : _setGasolineTanker){
|
||||
data+=gasolineTanker.GetInfo()+separatorData;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void LoadData(String[] records){
|
||||
for (int i=records.length-1;i>=0;i--){
|
||||
_setGasolineTanker.Insert((T) DrawingObjectGasolineTanker.Create(records[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.io.*;
|
||||
|
||||
public class MapsCollection {
|
||||
private final HashMap<String,MapWithSetGasolineTankerGeneric<DrawingObjectGasolineTanker,AbstractMap>> _mapStorages;
|
||||
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<>();
|
||||
@ -35,4 +38,113 @@ public class MapsCollection {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public boolean SaveData(String filename) throws IOException {
|
||||
|
||||
File file = new File(filename);
|
||||
|
||||
if (file.exists())
|
||||
file.delete();
|
||||
|
||||
file.createNewFile();
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(file)) {
|
||||
writer.println("MapsCollection");
|
||||
|
||||
for (var storage : _mapStorages.entrySet()) {
|
||||
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().GetData(separatorDict, separatorData)));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadData(String filename) throws IOException {
|
||||
|
||||
File file = new File(filename);
|
||||
|
||||
if (!file.exists())
|
||||
return false;
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
String currentLine = reader.readLine();
|
||||
|
||||
if (currentLine == null || !currentLine.contains("MapsCollection"))
|
||||
return false;
|
||||
|
||||
_mapStorages.clear();
|
||||
|
||||
while ((currentLine = reader.readLine()) != null) {
|
||||
var elements = currentLine.split(String.format("\\%c", separatorDict));
|
||||
AbstractMap map = switch (elements[1]) {
|
||||
case "SimpleMap" -> new SimpleMap();
|
||||
case "LongMap" -> new LongMap();
|
||||
default -> null;
|
||||
};
|
||||
_mapStorages.put(elements[0], new MapWithSetGasolineTankerGeneric<>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages.get(elements[0]).LoadData(elements[2].split(separatorData + "\n?"));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public boolean SaveMap(String mapName, String filename) throws IOException {
|
||||
File file = new File(filename);
|
||||
|
||||
if (file.exists())
|
||||
file.delete();
|
||||
|
||||
file.createNewFile();
|
||||
|
||||
MapWithSetGasolineTankerGeneric<DrawingObjectGasolineTanker, AbstractMap> map = _mapStorages.getOrDefault(mapName, null);
|
||||
|
||||
if (map == null)
|
||||
return false;
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(file)) {
|
||||
writer.println("Map");
|
||||
writer.println(mapName);
|
||||
writer.println(map.GetMap().getClass().getSimpleName());
|
||||
for (var gasolineTanker : map._setGasolineTanker.GetGasolineTanker()) {
|
||||
writer.println(gasolineTanker.GetInfo());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadMap(String filename) throws IOException {
|
||||
File file = new File(filename);
|
||||
|
||||
if (!file.exists())
|
||||
return false;
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
String currentLine = reader.readLine();
|
||||
|
||||
if (currentLine == null || !currentLine.contains("Map"))
|
||||
return false;
|
||||
|
||||
String mapName = reader.readLine();
|
||||
|
||||
MapWithSetGasolineTankerGeneric<DrawingObjectGasolineTanker, AbstractMap> map;
|
||||
if (_mapStorages.containsKey(mapName)) {
|
||||
map = _mapStorages.get(mapName);
|
||||
if (!map.GetMap().getClass().getSimpleName().equals(reader.readLine())) {
|
||||
return false;
|
||||
}
|
||||
map._setGasolineTanker.Clear();
|
||||
} else {
|
||||
map = switch (reader.readLine()) {
|
||||
case "SimpleMap" -> new MapWithSetGasolineTankerGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap());
|
||||
case "LongMap" -> new MapWithSetGasolineTankerGeneric<>(_pictureWidth, _pictureHeight, new LongMap());
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
while ((currentLine = reader.readLine()) != null) {
|
||||
map._setGasolineTanker.Insert((DrawingObjectGasolineTanker) DrawingObjectGasolineTanker.Create(currentLine));
|
||||
}
|
||||
_mapStorages.put(mapName, map);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Objects;
|
||||
public class SetGasolineTankerGeneric <T extends Object> implements Iterable<T>{
|
||||
private final ArrayList<T> _places;
|
||||
public int Count() {
|
||||
@ -55,4 +56,9 @@ public class SetGasolineTankerGeneric <T extends Object> implements Iterable<T>{
|
||||
public Iterator<T> iterator(){
|
||||
return _places.iterator();
|
||||
}
|
||||
|
||||
public Iterable<T> GetGasolineTanker(){ return ()-> _places.stream().filter(Objects::nonNull).iterator();}
|
||||
public void Clear(){
|
||||
_places.clear();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user