Compare commits
2 Commits
29ba58c11a
...
8dd844aa14
Author | SHA1 | Date | |
---|---|---|---|
8dd844aa14 | |||
02ac7e5533 |
@ -107,7 +107,4 @@ public class FormLocomotive extends JComponent{
|
|||||||
if (_locomotive != null) _locomotive.DrawTransport(g2);
|
if (_locomotive != null) _locomotive.DrawTransport(g2);
|
||||||
super.repaint();
|
super.repaint();
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
|
||||||
new FormMapWithSetLocomotives();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import java.awt.event.ActionListener;
|
|||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class FormMapWithSetLocomotives extends JComponent {
|
public class FormMapWithSetLocomotives extends JComponent {
|
||||||
private BufferedImage bufferImg = null;
|
private BufferedImage bufferImg = null;
|
||||||
@ -78,6 +79,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
|
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
|
||||||
|
Logger.getGlobal().info("Map " + textFieldNewMapName.getText() + " added");
|
||||||
ReloadMaps();
|
ReloadMaps();
|
||||||
});
|
});
|
||||||
statusPanel.add(addMapButton);
|
statusPanel.add(addMapButton);
|
||||||
@ -87,6 +89,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
listBoxMaps.addListSelectionListener(e -> {
|
listBoxMaps.addListSelectionListener(e -> {
|
||||||
if(listBoxMaps.getSelectedValue() == null) return;
|
if(listBoxMaps.getSelectedValue() == null) return;
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||||
|
Logger.getGlobal().info("Map switched to " + listBoxMaps.getSelectedValue().toString());
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
statusPanel.add(listBoxMaps);
|
statusPanel.add(listBoxMaps);
|
||||||
@ -105,6 +108,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
}
|
}
|
||||||
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
||||||
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
|
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
|
||||||
|
Logger.getGlobal().info("Map " +listBoxMaps.getSelectedValue().toString() +" deleted");
|
||||||
ReloadMaps();
|
ReloadMaps();
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
@ -113,24 +117,35 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
// Кнопка добавления локомотива
|
// Кнопка добавления локомотива
|
||||||
JButton addLocomotiveButton = new JButton("Add Locomotive");
|
JButton addLocomotiveButton = new JButton("Add Locomotive");
|
||||||
addLocomotiveButton.addActionListener(e -> {
|
addLocomotiveButton.addActionListener(e -> {
|
||||||
FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig();
|
FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig();
|
||||||
formLocomotiveConfig.setVisible(true);
|
formLocomotiveConfig.setVisible(true);
|
||||||
formLocomotiveConfig.AddListener(locomotive -> {
|
formLocomotiveConfig.AddListener(locomotive -> {
|
||||||
if (listBoxMaps.getSelectedIndex() == -1) {
|
if (listBoxMaps.getSelectedIndex() == -1) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
if (locomotive!=null) {
|
|
||||||
DrawningObjectLocomotive objectLocomotive = new DrawningObjectLocomotive(locomotive);
|
|
||||||
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){
|
|
||||||
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
|
||||||
repaint();
|
|
||||||
}
|
}
|
||||||
else {
|
if (locomotive!=null) {
|
||||||
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
|
try {
|
||||||
|
DrawningObjectLocomotive objectLocomotive = new DrawningObjectLocomotive(locomotive);
|
||||||
|
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||||
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||||
|
Logger.getGlobal().info("Object " + locomotive + " added");
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (StorageOverflowException ex) {
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage());
|
||||||
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
|
||||||
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
statusPanel.add(addLocomotiveButton);
|
statusPanel.add(addLocomotiveButton);
|
||||||
|
|
||||||
@ -149,20 +164,30 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
|
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
|
||||||
deleteLocomotiveButton.addActionListener(e -> {
|
deleteLocomotiveButton.addActionListener(e -> {
|
||||||
// логика удаления
|
// логика удаления
|
||||||
if ((String)mapSelectComboBox.getSelectedItem() == null) {
|
try{
|
||||||
return;
|
if ((String)mapSelectComboBox.getSelectedItem() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (finalMaskedTextFieldPosition == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
||||||
|
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||||
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||||
|
Logger.getGlobal().info("Locomotive deleted at position " + position);
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION);
|
||||||
|
}}
|
||||||
|
catch (LocomotiveNotFoundException ex) {
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Locomotive not found error: " + ex.getMessage());
|
||||||
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
}
|
}
|
||||||
if (finalMaskedTextFieldPosition == null) {
|
catch (Exception ex) {
|
||||||
return;
|
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
|
||||||
}
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
|
||||||
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
|
|
||||||
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
|
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
statusPanel.add(deleteLocomotiveButton);
|
statusPanel.add(deleteLocomotiveButton);
|
||||||
@ -186,8 +211,14 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
|
try {
|
||||||
repaint();
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Unknown error: "+ ex.getMessage());
|
||||||
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
|
}
|
||||||
});
|
});
|
||||||
statusPanel.add(showOnMapButton);
|
statusPanel.add(showOnMapButton);
|
||||||
|
|
||||||
@ -271,10 +302,15 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
fileChooser.setDialogTitle("Saving");
|
fileChooser.setDialogTitle("Saving");
|
||||||
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
||||||
if (result == JFileChooser.APPROVE_OPTION ) {
|
if (result == JFileChooser.APPROVE_OPTION ) {
|
||||||
if (_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath())) {
|
try {
|
||||||
|
_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath());
|
||||||
JOptionPane.showMessageDialog(null, "Save success");
|
JOptionPane.showMessageDialog(null, "Save success");
|
||||||
|
Logger.getGlobal().info("Saved all to " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Save error: " + ex.getMessage());
|
||||||
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
}
|
}
|
||||||
else JOptionPane.showMessageDialog(null, "Save failed");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -285,13 +321,18 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
fileChooser.setDialogTitle("Loading");
|
fileChooser.setDialogTitle("Loading");
|
||||||
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
||||||
if (result == JFileChooser.APPROVE_OPTION ) {
|
if (result == JFileChooser.APPROVE_OPTION ) {
|
||||||
if (_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath())) {
|
try {
|
||||||
|
_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath());
|
||||||
JOptionPane.showMessageDialog(null, "Load success");
|
JOptionPane.showMessageDialog(null, "Load success");
|
||||||
|
Logger.getGlobal().info("Loaded all from " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||||
|
ReloadMaps();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Load error: " + ex.getMessage());
|
||||||
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
}
|
}
|
||||||
else JOptionPane.showMessageDialog(null, "Load failed");
|
|
||||||
}
|
}
|
||||||
ReloadMaps();
|
|
||||||
repaint();
|
|
||||||
});
|
});
|
||||||
statusPanel.add(loadButton);
|
statusPanel.add(loadButton);
|
||||||
|
|
||||||
@ -306,11 +347,16 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
||||||
if (_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath())) {
|
try {
|
||||||
|
_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath());
|
||||||
JOptionPane.showMessageDialog(null, "Map saving success");
|
JOptionPane.showMessageDialog(null, "Map saving success");
|
||||||
|
Logger.getGlobal().info("Saved map to " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Map saving error: " + ex.getMessage());
|
||||||
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
}
|
}
|
||||||
else JOptionPane.showMessageDialog(null, "Map saving fail");
|
|
||||||
repaint();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
statusPanel.add(saveMapButton);
|
statusPanel.add(saveMapButton);
|
||||||
@ -321,13 +367,18 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
fileChooser.setDialogTitle("Loading");
|
fileChooser.setDialogTitle("Loading");
|
||||||
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
||||||
if (result == JFileChooser.APPROVE_OPTION ) {
|
if (result == JFileChooser.APPROVE_OPTION ) {
|
||||||
if (_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath())) {
|
try {
|
||||||
|
_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath());
|
||||||
JOptionPane.showMessageDialog(null, "Load Map success");
|
JOptionPane.showMessageDialog(null, "Load Map success");
|
||||||
|
Logger.getGlobal().info("Loaded map from " + fileChooser.getSelectedFile().getAbsolutePath());
|
||||||
|
ReloadMaps();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Load Map error: " + ex.getMessage());
|
||||||
|
Logger.getGlobal().severe("Error " + ex.getMessage());
|
||||||
}
|
}
|
||||||
else JOptionPane.showMessageDialog(null, "Load Map failed");
|
|
||||||
}
|
}
|
||||||
ReloadMaps();
|
|
||||||
repaint();
|
|
||||||
});
|
});
|
||||||
statusPanel.add(loadMapButton);
|
statusPanel.add(loadMapButton);
|
||||||
|
|
||||||
|
5
LocomotiveNotFoundException.java
Normal file
5
LocomotiveNotFoundException.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class LocomotiveNotFoundException extends Exception{
|
||||||
|
public LocomotiveNotFoundException(int i) {
|
||||||
|
super("Не найден объект по позиции " + i);
|
||||||
|
}
|
||||||
|
}
|
24
Main.java
Normal file
24
Main.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.logging.LogManager;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
private static Logger logger = null;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
FileInputStream stream = new FileInputStream(new File("C:/secondCourse/OOP/PIbd-23_Mochalov_D.V._Locomotive_Hard/log.config"));
|
||||||
|
LogManager.getLogManager().readConfiguration(stream);
|
||||||
|
logger = Logger.getGlobal();
|
||||||
|
stream.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
new FormMapWithSetLocomotives();
|
||||||
|
}
|
||||||
|
}
|
@ -13,9 +13,9 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
/// Высота окна отрисовки
|
/// Высота окна отрисовки
|
||||||
private final int _pictureHeight;
|
private final int _pictureHeight;
|
||||||
/// Размер занимаемого объектом места (ширина)
|
/// Размер занимаемого объектом места (ширина)
|
||||||
private final int _placeSizeWidth = 210;
|
private final int _placeSizeWidth = 150;
|
||||||
/// Размер занимаемого объектом места (высота)
|
/// Размер занимаемого объектом места (высота)
|
||||||
private final int _placeSizeHeight = 90;
|
private final int _placeSizeHeight = 150;
|
||||||
public final SetLocomotivesGeneric<T> _setLocomotives;
|
public final SetLocomotivesGeneric<T> _setLocomotives;
|
||||||
|
|
||||||
// Список удаленных объектов
|
// Список удаленных объектов
|
||||||
@ -39,12 +39,12 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Добавление
|
/// Добавление
|
||||||
public int Plus(T locomotive)
|
public int Plus(T locomotive) throws StorageOverflowException
|
||||||
{
|
{
|
||||||
return this._setLocomotives.Insert(locomotive);
|
return this._setLocomotives.Insert(locomotive);
|
||||||
}
|
}
|
||||||
/// Удаление
|
/// Удаление
|
||||||
public T Minus(int position)
|
public T Minus(int position) throws LocomotiveNotFoundException
|
||||||
{
|
{
|
||||||
T temp = this._setLocomotives.Remove(position);
|
T temp = this._setLocomotives.Remove(position);
|
||||||
if (temp == null) return null;
|
if (temp == null) return null;
|
||||||
@ -62,7 +62,7 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Просмотр объекта на карте
|
/// Просмотр объекта на карте
|
||||||
public BufferedImage ShowOnMap()
|
public BufferedImage ShowOnMap() throws LocomotiveNotFoundException, StorageOverflowException
|
||||||
{
|
{
|
||||||
Shaking();
|
Shaking();
|
||||||
for (var locomotive : _setLocomotives.GetLocomotives())
|
for (var locomotive : _setLocomotives.GetLocomotives())
|
||||||
@ -86,7 +86,7 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||||||
private void Shaking()
|
private void Shaking() throws LocomotiveNotFoundException, StorageOverflowException
|
||||||
{
|
{
|
||||||
int j = _setLocomotives.Count() - 1;
|
int j = _setLocomotives.Count() - 1;
|
||||||
for (int i = 0; i < _setLocomotives.Count(); i++)
|
for (int i = 0; i < _setLocomotives.Count(); i++)
|
||||||
@ -157,8 +157,8 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
/// Метод прорисовки объектов
|
/// Метод прорисовки объектов
|
||||||
private void DrawLocomotives(Graphics2D g)
|
private void DrawLocomotives(Graphics2D g)
|
||||||
{
|
{
|
||||||
int width = _pictureWidth / _placeSizeWidth;
|
int width = _pictureWidth / _placeSizeWidth - 1;
|
||||||
int height = _pictureHeight / _placeSizeHeight;
|
int height = _pictureHeight / _placeSizeHeight - 1;
|
||||||
|
|
||||||
int curWidth = 0;
|
int curWidth = 0;
|
||||||
int curHeight = 0;
|
int curHeight = 0;
|
||||||
@ -166,7 +166,7 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
for (var locomotive : _setLocomotives.GetLocomotives())
|
for (var locomotive : _setLocomotives.GetLocomotives())
|
||||||
{
|
{
|
||||||
// установка позиции
|
// установка позиции
|
||||||
if (locomotive != null) locomotive.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight);
|
if (locomotive != null) locomotive.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 80, _pictureWidth, _pictureHeight);
|
||||||
if (locomotive != null) locomotive.DrawningObject(g);
|
if (locomotive != null) locomotive.DrawningObject(g);
|
||||||
if (curWidth < width) curWidth++;
|
if (curWidth < width) curWidth++;
|
||||||
else
|
else
|
||||||
@ -189,7 +189,7 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Загрузка списка из массива строк
|
/// Загрузка списка из массива строк
|
||||||
public void LoadData(String[] records)
|
public void LoadData(String[] records) throws StorageOverflowException
|
||||||
{
|
{
|
||||||
Collections.reverse(Arrays.asList(records));
|
Collections.reverse(Arrays.asList(records));
|
||||||
for (var rec : records)
|
for (var rec : records)
|
||||||
|
@ -2,6 +2,8 @@ import java.io.*;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.zip.DataFormatException;
|
||||||
|
|
||||||
public class MapsCollection {
|
public class MapsCollection {
|
||||||
/// Словарь (хранилище) с картами
|
/// Словарь (хранилище) с картами
|
||||||
@ -51,59 +53,53 @@ public class MapsCollection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Сохранение информации по локомотивам в хранилище в файл
|
/// Сохранение информации по локомотивам в хранилище в файл
|
||||||
public boolean SaveData(String filename)
|
public void SaveData(String filename) throws IOException
|
||||||
{
|
{
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
if (file.exists())
|
if (file.exists())
|
||||||
{
|
{
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
|
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
|
||||||
bw.write("MapsCollection\n");
|
bw.write("MapsCollection\n");
|
||||||
for (var storage : _mapStorages.entrySet()) {
|
for (var storage : _mapStorages.entrySet()) {
|
||||||
bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n");
|
bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
bw.flush();
|
||||||
System.out.println(ex.getMessage());
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Сохранение одной выбранной карты
|
// Сохранение одной выбранной карты
|
||||||
public boolean SaveMap(String map_name, String filename) {
|
public void SaveMap(String map_name, String filename) throws IOException{
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> map = Get(map_name);
|
MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> map = Get(map_name);
|
||||||
if (file.exists())
|
if (file.exists())
|
||||||
{
|
{
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
|
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
|
||||||
bw.write("SingleMap\n");
|
bw.write("SingleMap\n");
|
||||||
bw.write("" + map_name + "\n");
|
bw.write("" + map_name + "\n");
|
||||||
bw.write("" + map.getClass() + "\n");
|
bw.write("" + map.getClass() + "\n");
|
||||||
for (var locomotive : map._setLocomotives.GetLocomotives()) {
|
for (var locomotive : map._setLocomotives.GetLocomotives()) {
|
||||||
bw.write("" + locomotive.getInfo() + "\n");
|
bw.write("" + locomotive.getInfo() + "\n");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
bw.flush();
|
||||||
System.out.println(ex.getMessage());
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Загрузка одной карты
|
// Загрузка одной карты
|
||||||
public boolean LoadMap(String filename){
|
public void LoadMap(String filename) throws StorageOverflowException, IOException, DataFormatException{
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
if (!file.exists())
|
if (!file.exists())
|
||||||
{
|
{
|
||||||
return false;
|
Logger.getGlobal().severe("FileNotFoundException " + filename);
|
||||||
}
|
throw new FileNotFoundException("Файл не найден");
|
||||||
try {
|
}
|
||||||
BufferedReader br = new BufferedReader(new FileReader(filename));
|
BufferedReader br = new BufferedReader(new FileReader(filename));
|
||||||
String curLine = br.readLine();
|
String curLine = br.readLine();
|
||||||
if (!curLine.contains("SingleMap")) {
|
if (!curLine.contains("SingleMap")) {
|
||||||
return false;
|
Logger.getGlobal().severe("FileFormatException " + filename);
|
||||||
|
throw new DataFormatException("Неверный формат данных");
|
||||||
}
|
}
|
||||||
String mapName = br.readLine();
|
String mapName = br.readLine();
|
||||||
String mapClass = br.readLine();
|
String mapClass = br.readLine();
|
||||||
@ -113,7 +109,6 @@ public class MapsCollection {
|
|||||||
_mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine));
|
_mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine));
|
||||||
}
|
}
|
||||||
_mapStorages.get(mapName)._setLocomotives.ReversePlaces();
|
_mapStorages.get(mapName)._setLocomotives.ReversePlaces();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
AbstractMap map = null;
|
AbstractMap map = null;
|
||||||
switch (mapClass)
|
switch (mapClass)
|
||||||
@ -133,53 +128,45 @@ public class MapsCollection {
|
|||||||
_mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine));
|
_mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine));
|
||||||
}
|
}
|
||||||
_mapStorages.get(mapName)._setLocomotives.ReversePlaces();
|
_mapStorages.get(mapName)._setLocomotives.ReversePlaces();
|
||||||
|
|
||||||
} catch (IOException ex) {
|
|
||||||
System.out.println(ex.getMessage());
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Загрузка информации по локомотивам в депо из файла
|
/// Загрузка информации по локомотивам в депо из файла
|
||||||
public boolean LoadData(String filename)
|
public void LoadData(String filename) throws DataFormatException, IOException, StorageOverflowException
|
||||||
{
|
{
|
||||||
File file = new File(filename);
|
File file = new File(filename);
|
||||||
if (!file.exists())
|
if (!file.exists())
|
||||||
{
|
{
|
||||||
return false;
|
Logger.getGlobal().severe("FileNotFoundException " + filename);
|
||||||
|
throw new FileNotFoundException("Файл не найден");
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
BufferedReader br = new BufferedReader(new FileReader(filename));
|
BufferedReader br = new BufferedReader(new FileReader(filename));
|
||||||
String curLine = br.readLine();
|
String curLine = br.readLine();
|
||||||
if (!curLine.contains("MapsCollection")) {
|
if (!curLine.contains("MapsCollection")) {
|
||||||
return false;
|
Logger.getGlobal().severe("FileFormatException " + filename);
|
||||||
|
throw new DataFormatException("Неверный формат данных");
|
||||||
|
}
|
||||||
|
|
||||||
|
_mapStorages.clear();
|
||||||
|
while ((curLine = br.readLine()) != null) {
|
||||||
|
var elems = curLine.split(separatorDict);
|
||||||
|
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.clear();
|
_mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||||
while ((curLine = br.readLine()) != null) {
|
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
|
||||||
var elems = curLine.split(separatorDict);
|
|
||||||
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<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
|
||||||
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
|
|
||||||
}
|
|
||||||
} catch (IOException ex) {
|
|
||||||
System.out.println(ex.getMessage());
|
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class SetLocomotivesGeneric <T>
|
public class SetLocomotivesGeneric <T>
|
||||||
{
|
{
|
||||||
@ -18,20 +19,28 @@ public class SetLocomotivesGeneric <T>
|
|||||||
_places = new ArrayList<>();
|
_places = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert (T locomotive) {
|
public int Insert (T locomotive) throws StorageOverflowException{
|
||||||
return Insert(locomotive, 0);
|
return Insert(locomotive, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert (T locomotive, int position) {
|
public int Insert (T locomotive, int position) throws StorageOverflowException{
|
||||||
if (position >= _maxCount|| position < 0) return -1;
|
if (position < 0) return -1;
|
||||||
_places.add(position, locomotive);
|
if (Count() >= _maxCount) {
|
||||||
return position;
|
Logger.getGlobal().warning("StorageOverflowException");
|
||||||
|
throw new StorageOverflowException(_maxCount);
|
||||||
|
}
|
||||||
|
_places.add(position, locomotive);
|
||||||
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Remove (int position) {
|
public T Remove (int position) throws LocomotiveNotFoundException {
|
||||||
if (position >= _maxCount || position < 0) return null;
|
if (position >= _maxCount || position < 0) return null;
|
||||||
|
if (_places.get(position) == null) {
|
||||||
|
Logger.getGlobal().warning("LocomotiveNotFoundException at " + position);
|
||||||
|
throw new LocomotiveNotFoundException(position);
|
||||||
|
}
|
||||||
T result = _places.get(position);
|
T result = _places.get(position);
|
||||||
_places.remove(position);
|
_places.set(position, null);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
StorageOverflowException.java
Normal file
5
StorageOverflowException.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class StorageOverflowException extends Exception{
|
||||||
|
public StorageOverflowException(int count) {
|
||||||
|
super("В наборе превышено допустимое количество: " + count);
|
||||||
|
}
|
||||||
|
}
|
7
log.config
Normal file
7
log.config
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
handlers = java.util.logging.FileHandler
|
||||||
|
java.util.logging.FileHandler.level = INFO
|
||||||
|
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
|
||||||
|
java.util.logging.SimpleFormatter.format= %4$S %5$S (%1$td.%1$tm.%1$tY) %n
|
||||||
|
java.util.logging.FileHandler.append = true
|
||||||
|
java.util.logging.FileHandler.pattern = log.txt
|
||||||
|
java.util.logging.FileHandler.limit = 100000
|
Loading…
Reference in New Issue
Block a user