LabWork6::base_part::Генерация ошибок.
This commit is contained in:
@@ -6,6 +6,7 @@ import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
@@ -98,19 +99,24 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
FormShipConfig formShipConfig = new FormShipConfig();
|
||||
formShipConfig.AddEvent(newShip -> {
|
||||
if (ListBoxMaps.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
if (newShip != null) {
|
||||
DrawingObjectShip ship = new DrawingObjectShip(newShip);
|
||||
if (_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||
try
|
||||
{
|
||||
if (ListBoxMaps.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
if (newShip != null) {
|
||||
DrawingObjectShip ship = new DrawingObjectShip(newShip);
|
||||
if (_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект","Ошибка",JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
}catch (StorageOverflowException ex)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Ошибка переполнения хранилища: "+ex.getMessage(),"Ошибка",JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
});
|
||||
formShipConfig.setSize(850, 300);
|
||||
formShipConfig.setVisible(true);
|
||||
@@ -137,14 +143,22 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
||||
return;
|
||||
}
|
||||
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
|
||||
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Delete(pos)!=null)
|
||||
try
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
||||
var delShip = _mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Delete(pos);
|
||||
if(delShip!=null)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект","Ошибка",JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (ShipNotFoundException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Ошибка удаления: "+ex.getMessage(),"Ошибка",JOptionPane.ERROR_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Неизвестная ошибка: "+ex.getMessage(),"Ошибка",JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -302,15 +316,11 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
||||
if(returnVal==JFileChooser.APPROVE_OPTION)
|
||||
{
|
||||
try {
|
||||
if (_mapsCollection.SaveData(fc.getSelectedFile().getPath())) {
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат",JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
File selectedFile=fc.getSelectedFile();
|
||||
_mapsCollection.SaveData(selectedFile.getPath());
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Не сохранилось: "+ex.getMessage(), "Результат",JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -323,17 +333,12 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
||||
if(returnVal==JFileChooser.APPROVE_OPTION)
|
||||
{
|
||||
try {
|
||||
if(_mapsCollection.LoadData(fc.getSelectedFile().getPath()))
|
||||
{
|
||||
ReloadMaps();
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат",JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
File selectedFile=fc.getSelectedFile();
|
||||
_mapsCollection.LoadData(selectedFile.getPath());
|
||||
ReloadMaps();
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Не загрузилось: "+ex.getMessage(), "Результат",JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,12 +20,10 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
}
|
||||
public int Add(T ship)
|
||||
{
|
||||
public int Add(T ship) throws StorageOverflowException {
|
||||
return _setShips.Insert(ship);
|
||||
}
|
||||
public T Delete(int position)
|
||||
{
|
||||
public T Delete(int position) throws ShipNotFoundException {
|
||||
T ship=_setShips.Remove(position);
|
||||
_deletedShips.add(ship);
|
||||
return ship;
|
||||
@@ -42,8 +40,7 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
|
||||
DrawShips(gr);
|
||||
return bmp;
|
||||
}
|
||||
public BufferedImage ShowOnMap()
|
||||
{
|
||||
public BufferedImage ShowOnMap(){
|
||||
Shaking();
|
||||
for(var ship : _setShips)
|
||||
{
|
||||
@@ -59,8 +56,7 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
|
||||
}
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||
}
|
||||
private void Shaking()
|
||||
{
|
||||
private void Shaking(){
|
||||
int j = _setShips.Count() - 1;
|
||||
for (int i = 0; i < _setShips.Count(); i++)
|
||||
{
|
||||
@@ -71,8 +67,16 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
|
||||
var ship = _setShips.Get(j);
|
||||
if (ship != null)
|
||||
{
|
||||
_setShips.Insert(ship, i);
|
||||
_setShips.Remove(j);
|
||||
try {
|
||||
_setShips.Insert(ship, i);
|
||||
} catch (StorageOverflowException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try {
|
||||
_setShips.Remove(j);
|
||||
} catch (ShipNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -154,8 +158,7 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
|
||||
}
|
||||
return data.toString();
|
||||
}
|
||||
public void LoadData(String[] records)
|
||||
{
|
||||
public void LoadData(String[] records) throws StorageOverflowException {
|
||||
for(var rec:records)
|
||||
{
|
||||
_setShips.Insert((T) DrawingObjectShip.Create(rec));
|
||||
|
||||
@@ -46,7 +46,7 @@ public class MapsCollection {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public boolean SaveData(String filename) throws IOException {
|
||||
public void SaveData(String filename) throws IOException {
|
||||
File file = new File(filename);
|
||||
if(file.exists())
|
||||
{
|
||||
@@ -60,20 +60,19 @@ public class MapsCollection {
|
||||
fw.write(String.format("%s%c%s%s",storage,separatorDict,_mapStorages.get(storage).GetData(separatorDict,separatorData),System.lineSeparator()));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public boolean LoadData(String filename) throws IOException {
|
||||
public void LoadData(String filename) throws IOException {
|
||||
File file=new File(filename);
|
||||
if(!file.exists())
|
||||
{
|
||||
return false;
|
||||
throw new FileNotFoundException("Файл не найден");
|
||||
}
|
||||
try(BufferedReader fr = new BufferedReader(new FileReader(file)))
|
||||
{
|
||||
String str="";
|
||||
if((str=fr.readLine())==null || !str.contains("MapsCollection"))
|
||||
{
|
||||
return false;
|
||||
throw new IllegalArgumentException("Формат данных в файле неправильный");
|
||||
}
|
||||
_mapStorages.clear();
|
||||
while((str=fr.readLine())!=null)
|
||||
@@ -95,8 +94,11 @@ public class MapsCollection {
|
||||
_mapStorages.get(tempElem[0]).LoadData(tempElem[2].split(String.valueOf(separatorData)));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (StorageOverflowException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public boolean SaveMapData(String filename,String Key)throws IOException
|
||||
{
|
||||
@@ -159,6 +161,10 @@ public class MapsCollection {
|
||||
if(tempElem.length==3) {
|
||||
_mapStorages.get(tempElem[0]).LoadData(tempElem[2].split(String.valueOf(separatorData)));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (StorageOverflowException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -12,21 +12,22 @@ public class SetShipsGeneric<T extends Object> implements Iterable<T>{
|
||||
_maxCount=count;
|
||||
_places = new ArrayList<>();
|
||||
}
|
||||
public int Insert(T ship)
|
||||
{
|
||||
public int Insert(T ship) throws StorageOverflowException {
|
||||
return Insert(ship, 0);
|
||||
}
|
||||
public int Insert(T ship, int position)
|
||||
{
|
||||
public int Insert(T ship, int position) throws StorageOverflowException {
|
||||
if(_maxCount == Count())
|
||||
{
|
||||
throw new StorageOverflowException(_maxCount);
|
||||
}
|
||||
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
|
||||
_places.add(position,ship);
|
||||
return position;
|
||||
}
|
||||
public T Remove(int position)
|
||||
{
|
||||
public T Remove(int position) throws ShipNotFoundException {
|
||||
if (position >= Count() || position < 0)
|
||||
{
|
||||
return null;
|
||||
throw new ShipNotFoundException(position);
|
||||
}
|
||||
T ship = (T) _places.get(position);
|
||||
_places.remove(ship);
|
||||
@@ -44,8 +45,7 @@ public class SetShipsGeneric<T extends Object> implements Iterable<T>{
|
||||
}
|
||||
return _places.get(position);
|
||||
}
|
||||
public void Set(int position,T value)
|
||||
{
|
||||
public void Set(int position,T value) throws StorageOverflowException {
|
||||
if (position < _maxCount || position >= 0)
|
||||
{
|
||||
Insert(value,position);
|
||||
|
||||
14
src/ShipNotFoundException.java
Normal file
14
src/ShipNotFoundException.java
Normal file
@@ -0,0 +1,14 @@
|
||||
public class ShipNotFoundException extends Exception{
|
||||
public ShipNotFoundException(int i){
|
||||
super(String.format("%s%d","Не найден объект по позиции",i));
|
||||
}
|
||||
public ShipNotFoundException(){
|
||||
super();
|
||||
}
|
||||
public ShipNotFoundException(String message){
|
||||
super(message);
|
||||
}
|
||||
public ShipNotFoundException(String message,Exception ex){
|
||||
super(message,ex);
|
||||
}
|
||||
}
|
||||
14
src/StorageOverflowException.java
Normal file
14
src/StorageOverflowException.java
Normal file
@@ -0,0 +1,14 @@
|
||||
public class StorageOverflowException extends Exception {
|
||||
public StorageOverflowException(int count){
|
||||
super("В наборе превышено допустимое количество: "+count);
|
||||
}
|
||||
public StorageOverflowException(){
|
||||
super();
|
||||
}
|
||||
public StorageOverflowException(String message){
|
||||
super(message);
|
||||
}
|
||||
public StorageOverflowException(String message,Exception ex){
|
||||
super(message,ex);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user