diff --git a/src/DrawingField.java b/src/DrawingField.java
deleted file mode 100644
index 825f30d..0000000
--- a/src/DrawingField.java
+++ /dev/null
@@ -1,66 +0,0 @@
-import javax.swing.*;
-import java.awt.*;
-import java.util.Random;
-
-public class DrawingField extends JPanel{
- private final FormGasolineTanker Field;
- DrawingGasolineTanker _gasolineTanker=null;
- public DrawingField(FormGasolineTanker field) {
- Field = field;
- }
-
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 =(Graphics2D)g;
- if (_gasolineTanker!=null)
- _gasolineTanker.DrawTransport(g2);
- else return;
- }
- public void DirectionButtonAction(Direction side){
- if(_gasolineTanker == null)
- return;
- _gasolineTanker.MoveTransport(side);
- }
- private void SetData() {
- Random rand=new Random();
- _gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
- Field.SpeedLabel.setText("Speed: "+_gasolineTanker.getGasolineTanker().getSpeed());
- Field.WeightLabel.setText("Weight: "+_gasolineTanker.getGasolineTanker().getWeight());
- Field.BodyColorLabel.setText("Color: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2));
- }
- public void CreateButtonAction(){
- Random rand=new Random();
- Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
- if(color1==null)
- color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
- _gasolineTanker=new DrawingGasolineTanker(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3));
- SetData();
- }
- public void CreateModifButtonAction(){
- Random rand=new Random();
- Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
- Color color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
- if(color1==null)
- color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
- if (color2==null)
- color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
- _gasolineTanker = new DrawingImprovedGasolineTanker(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1,
- color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextInt(3));
- SetData();
- }
- public void ResizeField(){
- if (_gasolineTanker!=null)
- _gasolineTanker.ChangeBorders(getWidth(),getHeight());
- else return;
- }
-
- public void Draw(Graphics2D graphics) {
- if (_gasolineTanker!=null)
- _gasolineTanker.DrawTransport(graphics);
- else return;
- }
- public DrawingGasolineTanker GetDrawingGasolineTanker() {
- return _gasolineTanker;
- }
-}
diff --git a/src/DrawingObjectGasolineTanker.java b/src/DrawingObjectGasolineTanker.java
index 7a1c0d7..c637751 100644
--- a/src/DrawingObjectGasolineTanker.java
+++ b/src/DrawingObjectGasolineTanker.java
@@ -2,7 +2,7 @@ import java.awt.*;
public class DrawingObjectGasolineTanker implements IDrawingObject {
- private DrawingGasolineTanker _gasolineTanker=null;
+ private DrawingGasolineTanker _gasolineTanker;
public DrawingObjectGasolineTanker(DrawingGasolineTanker gasolineTanker)
{
@@ -36,4 +36,7 @@ public class DrawingObjectGasolineTanker implements IDrawingObject {
return _gasolineTanker.GetCurrentPosition();
return null;
}
+ public DrawingGasolineTanker GetGasolineTanker() {
+ return _gasolineTanker;
+ }
}
diff --git a/src/FormGasolineTanker.java b/src/FormGasolineTanker.java
index adba354..eaed5d9 100644
--- a/src/FormGasolineTanker.java
+++ b/src/FormGasolineTanker.java
@@ -2,11 +2,11 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
+import java.util.Random;
public class FormGasolineTanker extends JFrame{
private int Width;
private int Height;
- DrawingField field = new DrawingField(this);
DrawingGasolineTanker SelectedGasolineTanker;
private JButton ButtonDown;
private JButton ButtonRight;
@@ -19,18 +19,17 @@ public class FormGasolineTanker extends JFrame{
public JLabel BodyColorLabel;
private JPanel PictureBox;
private JButton ButtonSelect;
+ private DrawingGasolineTanker _gasolineTanker=null;
public FormGasolineTanker(){
- super("Gasoline Tanker");
+ setTitle("Gasoline Tanker");
setContentPane(PictureBox);
- setSize(1000,700);
- Width = getWidth();
- Height = getHeight();
+ setSize(1000,500);
+ setResizable(false);
ShowWindow();
- field.setBounds(0,0,Width,Height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
}
+
ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")).
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
ImageIcon spriteDown =new ImageIcon((new ImageIcon("Material\\KeyDown.png")).
@@ -39,38 +38,69 @@ public class FormGasolineTanker extends JFrame{
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
ImageIcon spriteRight =new ImageIcon((new ImageIcon("Material\\KeyRight.png")).
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
+
+ private void SetData() {
+ Random rand=new Random();
+ _gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
+ SpeedLabel.setText("Скорость: "+_gasolineTanker.getGasolineTanker().getSpeed());
+ WeightLabel.setText("Вес: "+_gasolineTanker.getGasolineTanker().getWeight());
+ BodyColorLabel.setText("Цвет: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2));
+ }
+
+ @Override
+ public void paint(Graphics g) {
+ super.paint(g);
+
+ if (_gasolineTanker != null) {
+ PictureBox.paintComponents(PictureBox.getGraphics());
+ _gasolineTanker.DrawTransport(g);
+ }
+ }
private void ShowWindow(){
ButtonCreate.addActionListener(e -> {
- field.CreateButtonAction();
- ReDraw();
+ Random rand=new Random();
+ _gasolineTanker=new DrawingGasolineTanker(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),rand.nextInt(3));
+ SetData();
+ repaint();
});
ButtonCreateModif.addActionListener(e -> {
- field.CreateModifButtonAction();
- ReDraw();
+ Random rand=new Random();
+ _gasolineTanker = new DrawingImprovedGasolineTanker(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
+ new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3));
+ SetData();
+ repaint();
});
ButtonUp.setIcon(spriteUp);
ButtonUp.addActionListener(e -> {
- field.DirectionButtonAction(Direction.Up);
- ReDraw();
+ if(_gasolineTanker == null)
+ return;
+ _gasolineTanker.MoveTransport(Direction.Up);
+ repaint();
});
ButtonLeft.setIcon(spriteLeft);
ButtonLeft.addActionListener(e -> {
- field.DirectionButtonAction(Direction.Left);
- ReDraw();
+ if(_gasolineTanker == null)
+ return;
+ _gasolineTanker.MoveTransport(Direction.Left);
+ repaint();;
});
ButtonRight.setIcon(spriteRight);
ButtonRight.addActionListener(e -> {
- field.DirectionButtonAction(Direction.Right);
- ReDraw();
+ if(_gasolineTanker == null)
+ return;
+ _gasolineTanker.MoveTransport(Direction.Right);
+ repaint();
});
ButtonDown.setIcon(spriteDown);
ButtonDown.addActionListener(e -> {
- field.DirectionButtonAction(Direction.Down);
- ReDraw();
+ if(_gasolineTanker == null)
+ return;
+ _gasolineTanker.MoveTransport(Direction.Down);
+ repaint();
});
ButtonSelect.addActionListener(e -> {
- SelectedGasolineTanker=field.GetDrawingGasolineTanker();
+ SelectedGasolineTanker=GetDrawingGasolineTanker();
JOptionPane.showMessageDialog(PictureBox, "Gasoline tanker added.");
});
@@ -81,18 +111,21 @@ public class FormGasolineTanker extends JFrame{
Width=getWidth();
Height=getHeight();
- field.ResizeField();
- ReDraw();
- field.setBounds(0,0,Width,Height);
+ if (_gasolineTanker!=null)
+ _gasolineTanker.ChangeBorders(getWidth(),getHeight());
+ else return;
+ repaint();
+ setBounds(0,0,Width,Height);
}
});
}
- private void ReDraw()
- {
- Graphics2D graphics = (Graphics2D) PictureBox.getGraphics();
- graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
- PictureBox.paintComponents(graphics);
- field.Draw(graphics);
+ public DrawingGasolineTanker GetDrawingGasolineTanker() {
+ return _gasolineTanker;
+ }
+ public void SetDrawingGasolineTanker(DrawingGasolineTanker gasolineTanker) {
+ _gasolineTanker=gasolineTanker;
+ SetData();
+ repaint();
}
}
diff --git a/src/FormMapWithSetGasolineTanker.form b/src/FormMapWithSetGasolineTanker.form
index 30f7091..3ea9891 100644
--- a/src/FormMapWithSetGasolineTanker.form
+++ b/src/FormMapWithSetGasolineTanker.form
@@ -3,12 +3,12 @@
-
+
-
+
@@ -19,7 +19,7 @@
-
+
@@ -31,17 +31,6 @@
-
-
-
-
-
-
-
-
-
-
-
@@ -52,7 +41,7 @@
-
+
@@ -68,7 +57,7 @@
-
+
@@ -92,6 +81,67 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/FormMapWithSetGasolineTanker.java b/src/FormMapWithSetGasolineTanker.java
index e07db24..dc6d5f1 100644
--- a/src/FormMapWithSetGasolineTanker.java
+++ b/src/FormMapWithSetGasolineTanker.java
@@ -1,10 +1,6 @@
import javax.swing.*;
import java.awt.*;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.ComponentAdapter;
-import java.awt.event.ComponentEvent;
-import java.util.Objects;
+import java.util.HashMap;
public class FormMapWithSetGasolineTanker extends JFrame{
private JPanel GroupBoxTools;
@@ -20,8 +16,22 @@ public class FormMapWithSetGasolineTanker extends JFrame{
private JPanel MainPanel;
private JTextField TextBoxPosition;
private JPanel PictureBox;
+ private JPanel MapPanel;
+ private JTextField TextFieldMap;
+ private JButton CreateMapButton;
+ private JButton DeleteMapButton;
+ private JButton ButtonShowDeleted;
+ private JList ListBoxMaps;
private Image bufferedImage;
+ private MapsCollection _mapsCollection;
+ private final HashMap _mapsDict=new HashMap(){
+ {
+ put("Simple map",new SimpleMap());
+ put("Long map",new LongMap());
+ }
+ };
+
private MapWithSetGasolineTankerGeneric _mapGasolineTankerCollectionGeneric;
ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")).
@@ -40,6 +50,11 @@ public class FormMapWithSetGasolineTanker extends JFrame{
ShowWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
+ _mapsCollection=new MapsCollection(getWidth(),getHeight());
+ СomboBoxSelectorMap.removeAllItems();
+ for(String elem:_mapsDict.keySet()){
+ СomboBoxSelectorMap.addItem(elem);
+ }
}
@Override
@@ -52,28 +67,79 @@ public class FormMapWithSetGasolineTanker extends JFrame{
}
}
+ 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 ShowWindow(){
- ButtonShowOnMap.addActionListener(e -> {
- if (_mapGasolineTankerCollectionGeneric == null)
+ ListBoxMaps.addListSelectionListener(e -> {
+ if(ListBoxMaps.getSelectedIndex()==-1)
+ return;
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
+ repaint();
+ });
+
+ DeleteMapButton.addActionListener(e -> {
+ if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
- bufferedImage = _mapGasolineTankerCollectionGeneric.ShowOnMap();
+ if(JOptionPane.showConfirmDialog(this,"Delete map "+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,"\n" + "Not all data is complete","Error",JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem()))
+ {
+ JOptionPane.showMessageDialog(this,"No such card","Error",JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ _mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString()));
+ ReloadMaps();
+ });
+
+ ButtonShowOnMap.addActionListener(e -> {
+ if (ListBoxMaps.getSelectedIndex() ==-1)
+ {
+ return;
+ }
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap();
repaint();
});
ButtonShowStorage.addActionListener(e -> {
- if (_mapGasolineTankerCollectionGeneric == null)
+ if (ListBoxMaps.getSelectedIndex() ==-1)
{
return;
}
- bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet();
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
});
ButtonAddGasolineTanker.addActionListener(e -> {
- if (_mapGasolineTankerCollectionGeneric == null)
+ if (ListBoxMaps.getSelectedIndex() ==-1)
{
return;
}
@@ -85,9 +151,9 @@ public class FormMapWithSetGasolineTanker extends JFrame{
if (dialog.getSelectedGasolineTanker()!=null) {
DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(dialog.getSelectedGasolineTanker());
- if (_mapGasolineTankerCollectionGeneric.plus(gasolineTanker) >= 0) {
- JOptionPane.showMessageDialog(this, "\n" + "Object added", "Success", JOptionPane.INFORMATION_MESSAGE);
- bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet();
+ if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(gasolineTanker)>=0) {
+ JOptionPane.showMessageDialog(this, "Object added", "Success", JOptionPane.INFORMATION_MESSAGE);
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE);
@@ -97,79 +163,68 @@ public class FormMapWithSetGasolineTanker extends JFrame{
ButtonRemoveGasolineTanker.addActionListener(e -> {
String txt=TextBoxPosition.getText();
- if (txt==null||_mapGasolineTankerCollectionGeneric==null||txt.isEmpty())
+ if (txt==null||ListBoxMaps.getSelectedIndex() ==-1)
{
return;
}
int result = JOptionPane.showConfirmDialog(
this,
"Delete object?",
- "Delete",
+ "Removal",
JOptionPane.YES_NO_CANCEL_OPTION);
- if (result == JOptionPane.NO_OPTION)
+ if (result!=0)
{
return;
}
int pos = Integer.parseInt(txt);
- if (_mapGasolineTankerCollectionGeneric.minus(pos) !=null)
- {
- JOptionPane.showMessageDialog(this,"Object added", "Success",JOptionPane.INFORMATION_MESSAGE);
- bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet();
+ if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).minus(pos)!=null){
+ JOptionPane.showMessageDialog(this, "Object removed", "Success", JOptionPane.INFORMATION_MESSAGE);
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else
{
- JOptionPane.showMessageDialog(this,"Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE);
+ JOptionPane.showMessageDialog(this,"Failed to delete object","\n" + "Error",JOptionPane.INFORMATION_MESSAGE);
}
});
- СomboBoxSelectorMap.addActionListener(e -> {
- AbstractMap map = null;
-
- switch (СomboBoxSelectorMap.getSelectedItem().toString())
- {
- case "Simple map":
- map = new SimpleMap();
- break;
- case "Long map":
- map = new LongMap();
- break;
- }
- if (map != null)
- {
- _mapGasolineTankerCollectionGeneric = new MapWithSetGasolineTankerGeneric<>(
- PictureBox.getWidth(), PictureBox.getHeight(), map);
- }
- else
- {
- _mapGasolineTankerCollectionGeneric = null;
+ ButtonShowDeleted.addActionListener(e -> {
+ if (ListBoxMaps.getSelectedIndex()==-1)
+ return;
+ DrawingObjectGasolineTanker gasolineTanker=(DrawingObjectGasolineTanker)_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetGasolineTankerInDeleted();
+ if(gasolineTanker!=null){
+ FormGasolineTanker dialog=new FormGasolineTanker();
+ dialog.SetDrawingGasolineTanker(gasolineTanker.GetGasolineTanker());
+ dialog.setSize(1200,700);
+ dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+ dialog.setVisible(true);
}
});
ButtonUp.setIcon(spriteUp);
ButtonUp.addActionListener(e -> {
- if (_mapGasolineTankerCollectionGeneric != null) {
- bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Up);
+ if (ListBoxMaps.getSelectedIndex() !=-1) {
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up);
repaint();
}
});
ButtonDown.setIcon(spriteDown);
ButtonDown.addActionListener(e -> {
- if (_mapGasolineTankerCollectionGeneric != null) {
- bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Down);
+ if (ListBoxMaps.getSelectedIndex() !=-1) {
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down);
repaint();
}
});
ButtonRight.setIcon(spriteRight);
ButtonRight.addActionListener(e -> {
- if (_mapGasolineTankerCollectionGeneric != null) {
- bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Right);
+ if (ListBoxMaps.getSelectedIndex() !=-1) {
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right);
repaint();
}
});
ButtonLeft.setIcon(spriteLeft);
ButtonLeft.addActionListener(e -> {
- if (_mapGasolineTankerCollectionGeneric != null) {
- bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Left);
+ if (ListBoxMaps.getSelectedIndex() !=-1) {
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left);
repaint();
}
});
diff --git a/src/MapWithSetGasolineTankerGeneric.java b/src/MapWithSetGasolineTankerGeneric.java
index effb49a..cf714fd 100644
--- a/src/MapWithSetGasolineTankerGeneric.java
+++ b/src/MapWithSetGasolineTankerGeneric.java
@@ -1,5 +1,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
+import java.util.ArrayDeque;
public class MapWithSetGasolineTankerGeneric {
private final int _pictureWidth;
@@ -7,6 +8,7 @@ public class MapWithSetGasolineTankerGeneric _setGasolineTanker;
+ private final ArrayDeque DeletedGasolineTanker;
private final U _map;
public MapWithSetGasolineTankerGeneric(int picWidth,int picHeight, U map)
@@ -17,6 +19,7 @@ public class MapWithSetGasolineTankerGeneric ();
}
public int plus(T gasolineTanker)
@@ -26,7 +29,9 @@ public class MapWithSetGasolineTankerGeneric > _mapStorages;
+ private final int _pictureWidth;
+ private final int _pictureHeight;
+
+ public MapsCollection(int pictureWidth,int pictureHeight){
+ _mapStorages=new HashMap<>();
+ _pictureWidth=pictureWidth;
+ _pictureHeight=pictureHeight;
+ }
+
+ public ArrayList Keys(){
+ return new ArrayList<>(_mapStorages.keySet());
+ }
+
+ public void AddMap(String name, AbstractMap map){
+ if (_mapStorages.containsKey(name)){
+ return;
+ }
+ else{
+ _mapStorages.put(name,new MapWithSetGasolineTankerGeneric<>(_pictureWidth,_pictureHeight,map));
+ }
+ }
+
+ public void DelMap(String name){
+ _mapStorages.remove(name);
+ }
+
+ public MapWithSetGasolineTankerGeneric get(String name){
+ if (_mapStorages.containsKey(name)){
+ return _mapStorages.get(name);
+ }
+ return null;
+ }
+}
diff --git a/src/SetGasolineTankerGeneric.java b/src/SetGasolineTankerGeneric.java
index 1e55731..0fb6725 100644
--- a/src/SetGasolineTankerGeneric.java
+++ b/src/SetGasolineTankerGeneric.java
@@ -1,68 +1,58 @@
-public class SetGasolineTankerGeneric {
- private final Object[] _places;
-
+import java.util.ArrayList;
+import java.util.Iterator;
+public class SetGasolineTankerGeneric implements Iterable{
+ private final ArrayList _places;
public int Count() {
- return _places.length;
+ return _places.size();
}
-
+ private final int _maxCount;
public SetGasolineTankerGeneric(int count)
{
- _places = new Object[count];
+ _maxCount=count;
+ _places = new ArrayList<>();
}
public int Insert(T gasolineTanker)
{
- return Insert(gasolineTanker,0);
+ if (_places.size()+1>=_maxCount)
+ return -1;
+ _places.add(0,gasolineTanker);
+ return 0;
}
public int Insert(T gasolineTanker, int position)
{
- int EmptyEl=-1;
-
- if (position>=Count() || position < 0)
+ if (position>=_maxCount||position<0)
return -1;
-
- if (_places[position] == null)
- {
- _places[position] = gasolineTanker;
- return 1;
- }
-
- else if (_places[position] != null)
- {
- for (int i = position + 1; i < Count(); i++)
- if (_places[i] == null)
- {
- EmptyEl = i;
- break;
- }
-
- if (EmptyEl == -1)
- return -1;
-
- for (int i = EmptyEl; i > position; i--)
- _places[i] = _places[i - 1];
- }
-
- _places[position] = gasolineTanker;
- return 1;
+ if (_places.size()+1>=_maxCount)
+ return -1;
+ _places.add(position,gasolineTanker);
+ return position;
}
public T Remove(int position)
{
- if (position >= Count() || position < 0 || _places[position]==null)
+ if (position>=_maxCount||position<0)
return null;
-
- T deleted =(T)_places[position];
- _places[position] = null;
+ T deleted=_places.get(position);
+ _places.remove(position);
return deleted;
}
public T Get(int position)
{
- if (position >= Count() || position < 0)
+ if (position>=_maxCount||position<0)
return null;
+ return _places.get(position);
+ }
- return (T)_places[position];
+ public void Set(int position,T gasolineTanker){
+ if (position >= _maxCount||position<0)
+ return;
+ Insert(gasolineTanker,position);
+ }
+ @Override
+ public Iterator iterator(){
+ return _places.iterator();
}
}