1. Работа с исключениями
This commit is contained in:
parent
83924c620c
commit
212cfcde38
@ -88,16 +88,24 @@ public class FormMapWithArmoredCars extends JFrame{
|
||||
"Удаление", JOptionPane.YES_NO_OPTION);
|
||||
if (res == JOptionPane.YES_OPTION) {
|
||||
int pos = Integer.parseInt(maskedTextBoxPosition.getText());
|
||||
DrawingObjectArmoredCar deletedArmoredCar = (DrawingObjectArmoredCar) _mapsCollection.get((String) listBoxMaps.getSelectedValue(), pos);
|
||||
if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()).remove(pos) != null)
|
||||
{
|
||||
queue.add(deletedArmoredCar);
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
|
||||
try {
|
||||
var armoredCar = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).remove(pos);
|
||||
if (armoredCar != null)
|
||||
{
|
||||
queue.add((DrawingObjectArmoredCar) armoredCar);
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
||||
catch (ArmoredCarNotFoundException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Ошибка удаления: " + ex.getMessage());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Неизвестная ошибка: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -244,15 +252,22 @@ public class FormMapWithArmoredCars extends JFrame{
|
||||
return;
|
||||
}
|
||||
DrawingObjectArmoredCar armoredCar = new DrawingObjectArmoredCar(drawingArmoredCar);
|
||||
if (_mapsCollection.get((String)listBoxMaps.getSelectedValue()).add(armoredCar) > -1)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
|
||||
repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||
try {
|
||||
if (_mapsCollection.get((String)listBoxMaps.getSelectedValue()).add(armoredCar) > -1)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
|
||||
repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||
}
|
||||
} catch (StorageOverflowException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Ошибка добавления: " + ex.getMessage());
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Неизвестная ошибка: " + ex.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -263,12 +278,13 @@ public class FormMapWithArmoredCars extends JFrame{
|
||||
String filename = fd.getFile();
|
||||
if (filename != null) {
|
||||
if (type.equals("Файл")) {
|
||||
if (_mapsCollection.SaveData(fd.getDirectory() + filename))
|
||||
{
|
||||
try {
|
||||
_mapsCollection.SaveData(fd.getDirectory() + filename);
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно");
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не сохранилось");
|
||||
catch (Exception ex)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не сохранилось " + ex.getMessage());
|
||||
}
|
||||
} else if (type.equals("Файл карты")){
|
||||
if (_mapsCollection.SaveDataMap(fd.getDirectory() + filename, (String) listBoxMaps.getSelectedValue())) {
|
||||
@ -287,14 +303,16 @@ public class FormMapWithArmoredCars extends JFrame{
|
||||
String filename = fd.getFile();
|
||||
if (filename != null) {
|
||||
if (type.equals("Файл")) {
|
||||
if (_mapsCollection.LoadData(fd.getDirectory() + filename))
|
||||
{
|
||||
try {
|
||||
_mapsCollection.LoadData(fd.getDirectory() + filename);
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно");
|
||||
ReloadMaps();
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не загрузилось");
|
||||
catch (Exception exception)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не загрузилось " + exception.getMessage());
|
||||
}
|
||||
|
||||
} else if (type.equals("Файл карты")){
|
||||
if (_mapsCollection.LoadDataMap(fd.getDirectory() + filename)) {
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно");
|
||||
|
@ -49,7 +49,7 @@ public class MapsCollection {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean SaveData(String filename)
|
||||
public void SaveData(String filename)
|
||||
{
|
||||
File f = new File(filename);
|
||||
if (f.exists())
|
||||
@ -67,19 +67,17 @@ public class MapsCollection {
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadData(String filename) {
|
||||
public void LoadData(String filename) throws Exception {
|
||||
File f = new File(filename);
|
||||
if (!f.exists()) {
|
||||
return false;
|
||||
throw new FileNotFoundException("Файл не найден");
|
||||
}
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||||
if (!reader.readLine().contains("MapsCollection"))
|
||||
return false;
|
||||
throw new Exception("Формат данных в файле не правильный");
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
var elem = line.split(String.valueOf(separatorDict), -1);
|
||||
@ -100,9 +98,7 @@ public class MapsCollection {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean SaveDataMap(String filename, String map) {
|
||||
|
@ -21,7 +21,9 @@ public class SetArmoredCarsGeneric<T> {
|
||||
|
||||
public int Insert(T armoredCar, int position)
|
||||
{
|
||||
if (getCount() >= maxCount || position < 0 || position >= maxCount)
|
||||
if (getCount() >= maxCount)
|
||||
throw new StorageOverflowException(getCount());
|
||||
if (position < 0 || position >= maxCount)
|
||||
return -1;
|
||||
|
||||
_places.add(position, armoredCar);
|
||||
@ -33,6 +35,8 @@ public class SetArmoredCarsGeneric<T> {
|
||||
if (position < 0 || position >= _places.size())
|
||||
return null;
|
||||
T armoredCar = _places.get(position);
|
||||
if (armoredCar == null)
|
||||
throw new ArmoredCarNotFoundException(position);
|
||||
_places.remove(position);
|
||||
return armoredCar;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user