diff --git a/.gitignore b/.gitignore index a0e4cf4..3455901 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ dist/ nbdist/ .nb-gradle/ +/.vs/PIbd-22_DozorovaAA_ArmoredVehicle_Hard/v17/.suo diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite index 171755e..ab0c3bf 100644 Binary files a/.vs/slnx.sqlite and b/.vs/slnx.sqlite differ diff --git a/ArmoredVehicle/src/AbstractMap.java b/ArmoredVehicle/src/AbstractMap.java index e2e1679..2713c15 100644 --- a/ArmoredVehicle/src/AbstractMap.java +++ b/ArmoredVehicle/src/AbstractMap.java @@ -136,24 +136,28 @@ public abstract class AbstractMap { } int x = random.nextInt(0, 10); int y = random.nextInt(0, 10); - drawingObject.SetObject(x, y, width, height); HashMap hashMap = drawingObject.GetCurrentPosition(); - float left = hashMap.get("Left"); - float right = hashMap.get("Right"); - float top = hashMap.get("Top"); - float bottom = hashMap.get("Bottom"); - for (int i = (int)(x / size_x); i <= (int) (right / size_x); i++) - { - for (int j = (int)(y / size_y); j <= (int) (bottom / size_y); j++) + int beginI = (int)(x / size_x); + int endI = (int)(hashMap.get("Right") / size_x); + + int beginJ = (int)(y / size_y); + int endJ = (int)(hashMap.get("Bottom") / size_y); + + for (int i = beginI; i <= endI; i++) { - if (map[i][j] == _barrier) + for(int j = beginJ; j <= endJ; j++) { - return false; + if(map[i][j] == _barrier) + { + return false; + } } + } - } + + drawingObject.SetObject(x, y, width, height); return true; } private Image DrawMapWithObject() diff --git a/ArmoredVehicle/src/FormMapWithSetMachine.form b/ArmoredVehicle/src/FormMapWithSetMachine.form index 3284271..f88f0b3 100644 --- a/ArmoredVehicle/src/FormMapWithSetMachine.form +++ b/ArmoredVehicle/src/FormMapWithSetMachine.form @@ -24,7 +24,7 @@ - + @@ -72,18 +72,13 @@ - - - - - - + @@ -108,15 +103,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -126,7 +157,7 @@ - + @@ -265,6 +296,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ArmoredVehicle/src/FormMapWithSetMachine.java b/ArmoredVehicle/src/FormMapWithSetMachine.java index e8ab1f0..2734bed 100644 --- a/ArmoredVehicle/src/FormMapWithSetMachine.java +++ b/ArmoredVehicle/src/FormMapWithSetMachine.java @@ -3,19 +3,31 @@ import java.awt.Graphics; import java.awt.Image; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.HashMap; +import javax.swing.DefaultListModel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class FormMapWithSetMachine extends javax.swing.JFrame { private Image img; - private MapWithSetArmoredCarsGeneric _mapCollectionGeneric; + private MapsCollection _mapCollection; + private final HashMap _mapsDict = new HashMap<>() {{ + put("Простая карта", new SimpleMap()); + put("Вертикальная карта", new VerticalMap()); + put("Горизонтальная карта", new HorizontalMap()); + }}; public FormMapWithSetMachine() { initComponents(); LeftButton.setLabel("<"); RightButton.setLabel(">"); UpButton.setLabel("/\\"); DownButton.setLabel("\\/"); + MapComboBox.removeAllItems(); + for (String elem : _mapsDict.keySet()) { + MapComboBox.addItem(elem); + } + _mapCollection = new MapsCollection(PicturePanel.getWidth(), PicturePanel.getHeight()); } @@ -36,6 +48,11 @@ private Image img; DownButton = new java.awt.Button(); LeftButton = new java.awt.Button(); UpButton = new java.awt.Button(); + MapNameTextField = new java.awt.TextField(); + AddMapButton = new java.awt.Button(); + DeleteMapButton = new java.awt.Button(); + jScrollPane1 = new javax.swing.JScrollPane(); + MapList = new javax.swing.JList<>(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); @@ -124,21 +141,33 @@ private Image img; } }); + AddMapButton.setLabel("Добавить карту"); + AddMapButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + AddMapButtonMouseClicked(evt); + } + }); + + DeleteMapButton.setLabel("Удалить карту"); + DeleteMapButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mouseClicked(java.awt.event.MouseEvent evt) { + DeleteMapButtonMouseClicked(evt); + } + }); + + jScrollPane1.setViewportView(MapList); + javax.swing.GroupLayout InstrumentPanelLayout = new javax.swing.GroupLayout(InstrumentPanel); InstrumentPanel.setLayout(InstrumentPanelLayout); InstrumentPanelLayout.setHorizontalGroup( InstrumentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(InstrumentPanelLayout.createSequentialGroup() - .addComponent(jLabel1) - .addGap(0, 0, Short.MAX_VALUE)) .addGroup(InstrumentPanelLayout.createSequentialGroup() .addGap(22, 22, 22) .addGroup(InstrumentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(MapComboBox, 0, 165, Short.MAX_VALUE) .addComponent(AddMachineButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(TextBoxPosition, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(DeleteButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(StoreButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(StoreButton, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE) .addComponent(MapButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, InstrumentPanelLayout.createSequentialGroup() @@ -156,14 +185,43 @@ private Image img; .addComponent(DownButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(29, 29, 29)))) .addGap(49, 49, 49)) + .addGroup(InstrumentPanelLayout.createSequentialGroup() + .addComponent(jLabel1) + .addGap(0, 0, Short.MAX_VALUE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, InstrumentPanelLayout.createSequentialGroup() + .addContainerGap() + .addComponent(MapNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + .addGroup(InstrumentPanelLayout.createSequentialGroup() + .addContainerGap() + .addComponent(DeleteMapButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + .addGroup(InstrumentPanelLayout.createSequentialGroup() + .addContainerGap() + .addGroup(InstrumentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(AddMapButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(InstrumentPanelLayout.createSequentialGroup() + .addComponent(MapComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + .addGroup(InstrumentPanelLayout.createSequentialGroup() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(0, 0, Short.MAX_VALUE)))) ); InstrumentPanelLayout.setVerticalGroup( InstrumentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(InstrumentPanelLayout.createSequentialGroup() .addComponent(jLabel1) + .addGap(21, 21, 21) + .addComponent(MapNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(MapComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(AddMapButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(9, 9, 9) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(DeleteMapButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(AddMachineButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(TextBoxPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) @@ -173,7 +231,7 @@ private Image img; .addComponent(StoreButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(MapButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE) + .addGap(25, 25, 25) .addComponent(UpButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(1, 1, 1) .addGroup(InstrumentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -191,7 +249,7 @@ private Image img; layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() - .addComponent(PicturePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE) + .addComponent(PicturePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(InstrumentPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) ); @@ -221,38 +279,37 @@ private Image img; }//GEN-LAST:event_UpButtonMouseClicked private void MapComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MapComboBoxActionPerformed - AbstractMap map = null; - String name = (String) MapComboBox.getSelectedItem(); - switch (name) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Горизонтальная карта": - map = new HorizontalMap(); - break; - case "Вертикальная карта": - map = new VerticalMap(); - break; - } - if (map != null) - { - _mapCollectionGeneric = new MapWithSetArmoredCarsGeneric( - PicturePanel.getWidth(), PicturePanel.getHeight(), map); - } - else - { - _mapCollectionGeneric = null; - } +// AbstractMap map = null; +// String name = (String) MapComboBox.getSelectedItem(); +// switch (name) +// { +// case "Простая карта": +// map = new SimpleMap(); +// break; +// case "Горизонтальная карта": +// map = new HorizontalMap(); +// break; +// case "Вертикальная карта": +// map = new VerticalMap(); +// break; +// } +// if (map != null) +// { +// _mapCollection = new MapsCollection(PicturePanel.getWidth(), PicturePanel.getHeight()); +// } +// else +// { +// _mapCollection = null; +// } }//GEN-LAST:event_MapComboBoxActionPerformed private void AddMachineButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddMachineButtonMouseClicked - if (_mapCollectionGeneric == null) + if (_mapCollection == null) { JOptionPane.showMessageDialog(null, "Не выбрана карта"); return; } - PolimorphForm form = new PolimorphForm(); + MapForm form = new MapForm(); form.setVisible(true); form.addWindowListener(new WindowAdapter() { @Override @@ -260,12 +317,14 @@ private Image img; if (form.getSelectedCar() == null) return; DrawingObject machine = new DrawingObject(form.getSelectedCar()); - if (_mapCollectionGeneric.add(machine) > -1) + if (_mapCollection.Get(MapList.getSelectedValue().toString()).add(machine) != -1) { + if (form.DialogResult) { JOptionPane.showMessageDialog(null, "Объект добавлен"); - img = _mapCollectionGeneric.ShowSet(); + img = _mapCollection.Get(MapList.getSelectedValue().toString()).ShowSet(); Draw(); - } + } + } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); @@ -284,10 +343,10 @@ private Image img; "Удаление", JOptionPane.YES_NO_OPTION); if (res == JOptionPane.YES_OPTION) { int pos = Integer.parseInt(TextBoxPosition.getText()); - if (_mapCollectionGeneric.remove(pos) != null) + if (_mapCollection.Get(MapList.getSelectedValue().toString()).remove(pos) != null) { JOptionPane.showMessageDialog(null, "Объект удален"); - img = _mapCollectionGeneric.ShowSet(); + img = _mapCollection.Get(MapList.getSelectedValue().toString()).ShowSet(); Draw(); } else @@ -298,30 +357,70 @@ private Image img; }//GEN-LAST:event_DeleteButtonMouseClicked private void StoreButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_StoreButtonMouseClicked - if (_mapCollectionGeneric == null) + if (_mapCollection == null) { return; } - img = _mapCollectionGeneric.ShowSet(); + img = _mapCollection.Get(MapList.getSelectedValue().toString()).ShowSet(); Draw(); }//GEN-LAST:event_StoreButtonMouseClicked private void MapButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MapButtonMouseClicked - if (_mapCollectionGeneric == null) + if (_mapCollection == null) { return; } - img = _mapCollectionGeneric.ShowOnMap(); + img = _mapCollection.Get(MapList.getSelectedValue().toString()).ShowOnMap(); Draw(); }//GEN-LAST:event_MapButtonMouseClicked + + private void AddMapButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddMapButtonMouseClicked + if (MapComboBox.getSelectedIndex() == -1 || MapNameTextField.getText().isEmpty()) { + JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; + } + if (!_mapsDict.containsKey(MapComboBox.getSelectedItem())) { + JOptionPane.showMessageDialog(null, "Нет такой карты", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; + } + _mapCollection.AddMap(MapNameTextField.getText(), _mapsDict.get(MapComboBox.getSelectedItem().toString())); + ReloadMaps(); + }//GEN-LAST:event_AddMapButtonMouseClicked +private void ReloadMaps() { + int index = MapList.getSelectedIndex(); + + DefaultListModel model = (DefaultListModel) MapList.getModel(); + model.removeAllElements(); + for (int i = 0; i < _mapCollection.Keys.size(); i++) { + model.addElement(_mapCollection.Keys.get(i)); + } + + if (MapList.getModel().getSize() > 0 && + (index == -1 || index >= MapList.getModel().getSize())) { + MapList.setSelectedIndex(0); + } else if (MapList.getModel().getSize() > 0 && + index > -1 && index < MapList.getModel().getSize()) { + MapList.setSelectedIndex(index); + } + } + private void DeleteMapButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_DeleteMapButtonMouseClicked + if (MapList.getSelectedIndex() == -1) { + return; + } + if (JOptionPane.showConfirmDialog(null, "Удалить карту " + MapList.getSelectedValue().toString() + "?", + "Удаление", JOptionPane.YES_NO_OPTION) == 0) { + _mapCollection.DelMap(MapList.getSelectedValue().toString()); + ReloadMaps(); + } + }//GEN-LAST:event_DeleteMapButtonMouseClicked public void Move(String name) { switch (name) { - case "UpButton" -> img = _mapCollectionGeneric.MoveObject(Direction.Up); - case "DownButton" -> img = _mapCollectionGeneric.MoveObject(Direction.Down); - case "LeftButton" -> img = _mapCollectionGeneric.MoveObject(Direction.Left); - case "RightButton" -> img = _mapCollectionGeneric.MoveObject(Direction.Right); + case "UpButton" -> img = _mapCollection.Get(MapList.getSelectedValue().toString()).MoveObject(Direction.Up); + case "DownButton" -> img = _mapCollection.Get(MapList.getSelectedValue().toString()).MoveObject(Direction.Down); + case "LeftButton" -> img =_mapCollection.Get(MapList.getSelectedValue().toString()).MoveObject(Direction.Left); + case "RightButton" -> img = _mapCollection.Get(MapList.getSelectedValue().toString()).MoveObject(Direction.Right); } Draw(); @@ -367,17 +466,22 @@ private Image img; // Variables declaration - do not modify//GEN-BEGIN:variables private java.awt.Button AddMachineButton; + private java.awt.Button AddMapButton; private java.awt.Button DeleteButton; + private java.awt.Button DeleteMapButton; private java.awt.Button DownButton; private javax.swing.JPanel InstrumentPanel; private java.awt.Button LeftButton; private java.awt.Button MapButton; private javax.swing.JComboBox MapComboBox; + private javax.swing.JList MapList; + private java.awt.TextField MapNameTextField; private javax.swing.JPanel PicturePanel; private java.awt.Button RightButton; private java.awt.Button StoreButton; private java.awt.TextField TextBoxPosition; private java.awt.Button UpButton; private javax.swing.JLabel jLabel1; + private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables } diff --git a/ArmoredVehicle/src/MapForm.java b/ArmoredVehicle/src/MapForm.java index bfb921a..f88cbaf 100644 --- a/ArmoredVehicle/src/MapForm.java +++ b/ArmoredVehicle/src/MapForm.java @@ -16,6 +16,7 @@ public class MapForm extends JFrame{ private DrawingArmoredVehicle _ArmoredVehicle; private DrawingArmoredVehicle SelectedMachine; + public boolean DialogResult = false; @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents @@ -197,7 +198,6 @@ public class MapForm extends JFrame{ }//GEN-LAST:event_CreateButtonMouseClicked public void SetData() { - Random rnd = new Random(); _ArmoredVehicle.SetPosition(rnd.nextInt(0, 100), rnd.nextInt(25, 200), DrawPanel.getWidth(), DrawPanel.getHeight()); @@ -260,6 +260,7 @@ public class MapForm extends JFrame{ private void SetButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SetButtonMouseClicked if (_ArmoredVehicle != null) { SelectedMachine = _ArmoredVehicle; + DialogResult = true; dispose(); } }//GEN-LAST:event_SetButtonMouseClicked diff --git a/ArmoredVehicle/src/MapWithSetArmoredCarsGeneric.java b/ArmoredVehicle/src/MapWithSetMachineGeneric.java similarity index 83% rename from ArmoredVehicle/src/MapWithSetArmoredCarsGeneric.java rename to ArmoredVehicle/src/MapWithSetMachineGeneric.java index 2b648ca..87422c7 100644 --- a/ArmoredVehicle/src/MapWithSetArmoredCarsGeneric.java +++ b/ArmoredVehicle/src/MapWithSetMachineGeneric.java @@ -1,7 +1,7 @@ import java.awt.*; import java.awt.image.BufferedImage; -public class MapWithSetArmoredCarsGeneric { +public class MapWithSetMachineGeneric { private int _pictureWidth; @@ -15,7 +15,7 @@ public class MapWithSetArmoredCarsGeneric + /// Словарь (хранилище) с картами + /// + Map > _mapStorages; + /// + /// Возвращение списка названий карт + /// + public ArrayList Keys; + /// + /// Ширина окна отрисовки + /// + private int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new HashMap>(); + Keys = new ArrayList(_mapStorages.keySet()); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление карты + /// + /// Название карты + /// Карта + public void AddMap(String name, AbstractMap map) + { + if (Keys.contains(name)) + { + JOptionPane.showMessageDialog(null, "Такая карта уже есть"); + return; + } + else + { + var NewElem = new MapWithSetMachineGeneric( + _pictureWidth, _pictureHeight, map); + _mapStorages.put(name, NewElem); + } + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(String name) + { + if (Keys.contains(name)) + { + _mapStorages.remove(name); + } + else + { + JOptionPane.showMessageDialog(null, "Такой карты нет"); + return; + } + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetMachineGeneric Get(String ind) + { + if(Keys.contains(ind)) + { + return _mapStorages.get(ind); + } + JOptionPane.showMessageDialog(null, "Такой карты нет"); + return null; + + } + + } diff --git a/ArmoredVehicle/src/SetArmoredCarsGeneric.java b/ArmoredVehicle/src/SetArmoredCarsGeneric.java index f342073..729eda2 100644 --- a/ArmoredVehicle/src/SetArmoredCarsGeneric.java +++ b/ArmoredVehicle/src/SetArmoredCarsGeneric.java @@ -1,35 +1,41 @@ -import java.lang.reflect.Array; import java.util.ArrayList; +import java.util.Iterator; -public class SetArmoredCarsGeneric { - private T[] _places; - +public class SetArmoredCarsGeneric implements Iterable{ + private ArrayList _places; + private int _MaxCount; public SetArmoredCarsGeneric(int count) { - _places = (T[]) new Object[count]; + _MaxCount = count; + _places = new ArrayList<>(_MaxCount); } public int getCount() { - return _places != null ? _places.length : 0; + return _places.isEmpty() ? 0 : _places.size(); } public int Insert(T armoredCar) { - return Insert(armoredCar, 0); + if(_places.size()+1 <= _MaxCount) return Insert(armoredCar, 0); + else return -1; } public int Insert(T armoredCar, int position) { - if (position < 0 || position >= getCount()) + if ((position < 0 || position > getCount())) return -1; - - if (!(_places[position] == null)) + if(_places.isEmpty()) + { + _places.add(armoredCar); + return 0; + } + if (!(_places.get(position) == null) && !_places.isEmpty()) { int index_empty = -1; // поиск первого пустого элемента for (int i = position + 1; i < getCount(); i++) { - if (_places[i] == null) + if (_places.get(i) == null) { index_empty = i; } @@ -40,20 +46,22 @@ public class SetArmoredCarsGeneric { { for (int i = index_empty; i > position; i--) { - _places[i] = _places[i - 1]; + _places.set(i, _places.get(i-1)); } } } - _places[position] = armoredCar; + _places.add(position, armoredCar); + return position; } public T Remove(int position) { - if (position < 0 || position >= getCount()) + if (position < 0 || position >= getCount() || _places.get(position) == null) return null; - T armoredCar = _places[position]; - _places[position] = null; + T armoredCar = _places.get(position); + _places.set(position, null); + return armoredCar; } @@ -61,6 +69,16 @@ public class SetArmoredCarsGeneric { { if (position < 0 || position >= getCount()) return null; - return _places[position]; + return _places.get(position); + } + public void Set(int position, T value){ + if (position < 0 || position + 1 >= getCount()) + return; + Insert(value, position); + } + + @Override + public Iterator iterator() { + return _places.iterator(); } }