усложненная

This commit is contained in:
Hells Hound 2022-11-15 23:37:27 +04:00
parent 930a436de9
commit 386a0038c1
7 changed files with 290 additions and 98 deletions

View File

@ -14,6 +14,10 @@ public class DrawingObjectWarship implements IDrawingObject {
return 0; return 0;
} }
public DrawingWarship GetWarship() {
return _warship;
}
@Override @Override
public void SetObject(int x, int y, int width, int height) { public void SetObject(int x, int y, int width, int height) {
_warship.SetPosition(x, y, width, height); _warship.SetPosition(x, y, width, height);

View File

@ -3,7 +3,7 @@
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="20" y="20" width="884" height="479"/> <xy x="20" y="20" width="884" height="676"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -37,33 +37,6 @@
<font/> <font/>
</border> </border>
<children> <children>
<component id="d5ebd" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Преграды-линии"/>
</model>
</properties>
</component>
<component id="9e5dd" class="javax.swing.JButton" binding="ButtonAddWarship">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Add warship"/>
</properties>
</component>
<component id="16e2c" class="javax.swing.JButton" binding="ButtonRemoveWarship">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Remove warship"/>
</properties>
</component>
<component id="1fc5b" class="javax.swing.JButton" binding="ButtonShowStorage"> <component id="1fc5b" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints> <constraints>
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="6" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
@ -80,14 +53,6 @@
<text value="Show map"/> <text value="Show map"/>
</properties> </properties>
</component> </component>
<component id="608d7" class="javax.swing.JTextField" binding="TextBoxPosition">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="2ea34" class="javax.swing.JButton" binding="ButtonRight"> <component id="2ea34" class="javax.swing.JButton" binding="ButtonRight">
<constraints> <constraints>
<grid row="10" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"> <grid row="10" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
@ -136,14 +101,9 @@
<text value=""/> <text value=""/>
</properties> </properties>
</component> </component>
<vspacer id="aa195">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="fa6fc"> <vspacer id="fa6fc">
<constraints> <constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</vspacer> </vspacer>
<vspacer id="f6c4c"> <vspacer id="f6c4c">
@ -151,6 +111,93 @@
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> <grid row="8" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</vspacer> </vspacer>
<component id="14e42" class="javax.swing.JButton" binding="deletedWarshipButtom">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Deleted warship"/>
</properties>
</component>
<component id="16e2c" class="javax.swing.JButton" binding="ButtonRemoveWarship">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Remove warship"/>
</properties>
</component>
<component id="608d7" class="javax.swing.JTextField" binding="TextBoxPosition">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="9e5dd" class="javax.swing.JButton" binding="ButtonAddWarship">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Add warship"/>
</properties>
</component>
<grid id="53593" binding="MapPanel" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<name value=""/>
</properties>
<border type="bevel-lowered" title="Maps" title-justification="1" title-position="2"/>
<children>
<component id="91aeb" class="javax.swing.JTextField" binding="TextFieldMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="ddbe9" class="javax.swing.JButton" binding="CreateMapButton">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Create map"/>
</properties>
</component>
<component id="b2e9e" class="javax.swing.JButton" binding="DeleteMapButton">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Delete map"/>
</properties>
</component>
<component id="9cadb" class="javax.swing.JList" binding="ListBoxMaps">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<component id="8d7d6" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Преграды-линии"/>
</model>
</properties>
</component>
</children>
</grid>
</children> </children>
</grid> </grid>
</children> </children>

View File

@ -1,5 +1,8 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
public class FormMapWithSetWarships extends JFrame{ public class FormMapWithSetWarships extends JFrame{
private JPanel mainPanel; private JPanel mainPanel;
@ -15,8 +18,19 @@ public class FormMapWithSetWarships extends JFrame{
private JButton ButtonUp; private JButton ButtonUp;
private JButton ButtonDown; private JButton ButtonDown;
private JButton ButtonLeft; private JButton ButtonLeft;
private JButton deletedWarshipButtom;
private JPanel MapPanel;
private JTextField TextFieldMap;
private JButton CreateMapButton;
private JButton DeleteMapButton;
private JList ListBoxMaps;
private Image bufferedImage; private Image bufferedImage;
private MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> _mapWarshipsCollectionGeneric; private MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> _mapWarshipsCollectionGeneric;
private MapsCollection _mapsCollection;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
put("Простая карта", new SimpleMap());
put("Преграды-линии", new LineMap());
}};
public FormMapWithSetWarships(){ public FormMapWithSetWarships(){
InitializeComponent(); InitializeComponent();
@ -31,13 +45,35 @@ public class FormMapWithSetWarships extends JFrame{
} }
} }
public void InitializeComponent(){ private void ReloadMaps()
{
int index = ListBoxMaps.getSelectedIndex();
ListBoxMaps.setListData(_mapsCollection.Keys().toArray(new String[0]));
if (ListBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= ListBoxMaps.getModel().getSize()))
{
ListBoxMaps.setSelectedIndex(0);
}
else if (ListBoxMaps.getModel().getSize() > 0 && index > -1 && index < ListBoxMaps.getModel().getSize())
{
ListBoxMaps.setSelectedIndex(index);
}
repaint();
}
private void InitializeComponent(){
setContentPane(mainPanel); setContentPane(mainPanel);
setTitle("Warship"); setTitle("Warship");
setSize(1000, 693); setSize(935, 693);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true); setVisible(true);
_mapsCollection = new MapsCollection(getWidth(), getHeight());
СomboBoxSelectorMap.removeAllItems();
for(String elem:_mapsDict.keySet()){
СomboBoxSelectorMap.addItem(elem);
}
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg"); Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
ButtonUp.setIcon(iconUp); ButtonUp.setIcon(iconUp);
Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg"); Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg");
@ -47,24 +83,56 @@ public class FormMapWithSetWarships extends JFrame{
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
ButtonRight.setIcon(iconRight); ButtonRight.setIcon(iconRight);
СomboBoxSelectorMap.addActionListener(e -> { ListBoxMaps.addListSelectionListener(e -> {
AbstractMap map = switch (СomboBoxSelectorMap.getSelectedItem().toString()) { if(ListBoxMaps.getSelectedIndex() == -1)
case "Простая карта" -> new SimpleMap(); return;
case "Преграды-линии" -> new LineMap(); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
default -> null; repaint();
}; });
if( map != null){
_mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>( DeleteMapButton.addActionListener(e -> {
PictureBox.getWidth(), PictureBox.getHeight(), map); if (ListBoxMaps.getSelectedIndex() == -1)
}
else
{ {
_mapWarshipsCollectionGeneric = null; return;
}
if(JOptionPane.showConfirmDialog(this,"Удалить карту " + ListBoxMaps.getSelectedValue().toString() + "?",
"Удаление",JOptionPane.YES_NO_OPTION) == 0)
{
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
ReloadMaps();
} }
}); });
CreateMapButton.addActionListener(e -> {
if (СomboBoxSelectorMap.getSelectedIndex() == -1 || TextFieldMap.getText() == null || TextFieldMap.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem()))
{
JOptionPane.showMessageDialog(this,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
});
deletedWarshipButtom.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
return;
DrawingObjectWarship warship = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetWarshipInDeleted();
if (warship == null) {
JOptionPane.showMessageDialog(null, "Стек пуст", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
FormWarship form = new FormWarship(warship);
form.setSize(1000, 700);
form.setVisible(true);
});
ButtonAddWarship.addActionListener(e -> { ButtonAddWarship.addActionListener(e -> {
if(_mapWarshipsCollectionGeneric == null){ if(ListBoxMaps.getSelectedIndex() == -1){
return; return;
} }
FormWarshipCreator warshipCreator = new FormWarshipCreator(); FormWarshipCreator warshipCreator = new FormWarshipCreator();
@ -75,10 +143,10 @@ public class FormMapWithSetWarships extends JFrame{
if (warshipCreator.getSelectedWarship() != null) { if (warshipCreator.getSelectedWarship() != null) {
DrawingObjectWarship warship = new DrawingObjectWarship(warshipCreator.getSelectedWarship()); DrawingObjectWarship warship = new DrawingObjectWarship(warshipCreator.getSelectedWarship());
if (_mapWarshipsCollectionGeneric.Plus(warship) >= 0) { if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Plus(warship)>=0) {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
"Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint(); repaint();
} else { } else {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
@ -88,26 +156,26 @@ public class FormMapWithSetWarships extends JFrame{
}); });
ButtonRemoveWarship.addActionListener(e -> { ButtonRemoveWarship.addActionListener(e -> {
String text = TextBoxPosition.getText();
if (text == null || _mapWarshipsCollectionGeneric == null || text.isEmpty()) if (TextBoxPosition.getText().isEmpty())
{ {
return; return;
} }
int result = JOptionPane.showConfirmDialog( int result = JOptionPane.showConfirmDialog(null,
null,
"Удалить объект?", "Удалить объект?",
"Удаление", "Удаление",
JOptionPane.YES_NO_CANCEL_OPTION); JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.NO_OPTION) if (result == JOptionPane.NO_OPTION)
{ {
return; return;
} }
int pos = Integer.parseInt(text); int pos = Integer.parseInt(TextBoxPosition.getText());
if (_mapWarshipsCollectionGeneric.Minus(pos) != null) if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Minus(pos)!=null)
{ {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); "Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint(); repaint();
} }
else else

View File

@ -4,9 +4,9 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.util.Random; import java.util.Random;
public class FormWarship extends JFrame { public class FormWarship extends JDialog {
private DrawingWarship _warship; private DrawingWarship _warship;
public DrawingWarship selectedWarship; DrawingWarship selectedWarship;
public DrawingWarship getSelectedCar(){ public DrawingWarship getSelectedCar(){
return selectedWarship; return selectedWarship;
} }
@ -29,6 +29,13 @@ public class FormWarship extends JFrame {
InitializeComponent(); InitializeComponent();
} }
public FormWarship(DrawingObjectWarship warship){
InitializeComponent();
_warship = warship.GetWarship();
SetData();
}
private void Draw(){ private void Draw(){
Graphics2D graphics = (Graphics2D) drawPanel.getGraphics(); Graphics2D graphics = (Graphics2D) drawPanel.getGraphics();
graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight()); graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight());
@ -36,6 +43,16 @@ public class FormWarship extends JFrame {
_warship.DrawTransport(graphics); _warship.DrawTransport(graphics);
} }
@Override
public void paint(Graphics g) {
super.paint(g);
if (_warship != null) {
mainPanel.paintComponents(mainPanel.getGraphics());
_warship.DrawTransport(g);
}
}
private void SetData(){ private void SetData(){
Random rnd = new Random(); Random rnd = new Random();
_warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), mainPanel.getWidth(), mainPanel.getHeight()); _warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), mainPanel.getWidth(), mainPanel.getHeight());
@ -54,7 +71,6 @@ public class FormWarship extends JFrame {
setContentPane(mainPanel); setContentPane(mainPanel);
setTitle("Warship"); setTitle("Warship");
setSize(900, 700); setSize(900, 700);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true); setVisible(true);
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg"); Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");

View File

@ -1,5 +1,6 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Stack;
public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends AbstractMap> { public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends AbstractMap> {
private final int _pictureWidth; private final int _pictureWidth;
@ -7,6 +8,7 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
private final int _placeSizeWidth = 120; private final int _placeSizeWidth = 120;
private final int _placeSizeHeight = 50; private final int _placeSizeHeight = 50;
private final SetWarshipsGeneric<T> _setWarships; private final SetWarshipsGeneric<T> _setWarships;
private Stack<T> _deletedWarship;
private final U _map; private final U _map;
public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map) public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map)
@ -17,6 +19,7 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
_pictureWidth = picWidth; _pictureWidth = picWidth;
_pictureHeight = picHeight; _pictureHeight = picHeight;
_map = map; _map = map;
_deletedWarship = new Stack<>();
} }
public int Plus(T warship) public int Plus(T warship)
@ -26,7 +29,9 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
public T Minus(int position) public T Minus(int position)
{ {
return _setWarships.Remove(position); T warship=_setWarships.Remove(position);
_deletedWarship.push(warship);
return warship;
} }
public Image ShowSet() public Image ShowSet()
@ -120,4 +125,15 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
} }
} }
} }
public T GetWarshipInList(int ind){
return _setWarships.Get(ind);
}
public T GetWarshipInDeleted()
{
if(_deletedWarship.empty())
return null;
return _deletedWarship.pop();
}
} }

48
src/MapsCollection.java Normal file
View File

@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
private final HashMap<String, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>> _mapStorages;
public ArrayList<String> Keys() {
return new ArrayList<>(_mapStorages.keySet());
}
private final int _pictureWidth;
private final int _pictureHeight;
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(String name, AbstractMap map)
{
if (_mapStorages.containsKey(name))
{
return;
}
_mapStorages.put(name, new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> get(String ind){
if (_mapStorages.containsKey(ind))
{
return _mapStorages.get(ind);
}
return null;
}
public DrawingObjectWarship Get(String name, int ind) {
if (_mapStorages.containsKey(name))
{
return _mapStorages.get(name).GetWarshipInList(ind);
}
return null;
}
}

View File

@ -1,13 +1,17 @@
public class SetWarshipsGeneric<T extends Object>{ import java.util.ArrayList;
private final Object[] _places; import java.util.Iterator;
public class SetWarshipsGeneric<T extends Object> implements Iterable<T>{
private ArrayList<T> _places;
public int getCount() { public int getCount() {
return _places.length; return _places.size();
} }
private final int _maxCount;
public SetWarshipsGeneric(int count) public SetWarshipsGeneric(int count)
{ {
_places = new Object[count]; _places = new ArrayList<>();
_maxCount = count;
} }
public int Insert(T warship) public int Insert(T warship)
@ -17,41 +21,30 @@ public class SetWarshipsGeneric<T extends Object>{
public int Insert(T warship, int position) public int Insert(T warship, int position)
{ {
if (position < 0 || position >= getCount()) if (position < 0 || position >= _maxCount)
return -1; return -1;
_places.add(position, warship);
int empty = -1; return position;
for (int i = position + 1; i < getCount(); i++)
{
if (_places[i] == null)
empty = i;
}
if (empty == -1)
return 0;
else
{
for (int i = empty; i > position; i--)
_places[i] = _places[i - 1];
}
_places[position] = warship;
return 1;
} }
public T Remove(int position) public T Remove(int position)
{ {
if (position >= getCount() || position < 0 || _places[position] == null) if (position >= _maxCount || position < 0)
return null; return null;
T deleted = _places.get(position);
T deleted =(T) _places[position]; _places.remove(position);
_places[position] = null;
return deleted; return deleted;
} }
public T Get(int position) public T Get(int position)
{ {
if (position >= getCount() || position < 0) if (position > getCount() || position < 0)
return null; return null;
return _places.get(position);
}
return (T) _places[position]; @Override
public Iterator<T> iterator() {
return _places.iterator();
} }
} }