Laba4 KozyrevSS GasolineTanker JAVA HARD
This commit is contained in:
parent
264bf6c6af
commit
4d7b7e95a9
100
BaseFrame.java
100
BaseFrame.java
@ -9,6 +9,7 @@ class DrawGasoline extends JComponent {
|
|||||||
private AbstractStrategy _abstractStrategy;
|
private AbstractStrategy _abstractStrategy;
|
||||||
private DrawTanker SelectedCar;
|
private DrawTanker SelectedCar;
|
||||||
public DrawTanker GetSelectedCar() {return SelectedCar;}
|
public DrawTanker GetSelectedCar() {return SelectedCar;}
|
||||||
|
protected void SetTanker(DrawTanker tanker) {_drawTanker = tanker;}
|
||||||
public void paintComponent(Graphics g)
|
public void paintComponent(Graphics g)
|
||||||
{
|
{
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
@ -104,6 +105,11 @@ class BaseFrame extends JFrame {
|
|||||||
initUI();
|
initUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BaseFrame(DrawTanker gas)
|
||||||
|
{
|
||||||
|
initUIFrom();
|
||||||
|
Gasoline.SetTanker(gas);
|
||||||
|
}
|
||||||
protected static final int Width = 1000;
|
protected static final int Width = 1000;
|
||||||
protected static final int Height = 600;
|
protected static final int Height = 600;
|
||||||
DrawGasoline Gasoline;
|
DrawGasoline Gasoline;
|
||||||
@ -203,6 +209,100 @@ class BaseFrame extends JFrame {
|
|||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
private void initUIFrom()
|
||||||
|
{
|
||||||
|
setSize(Width, Height);
|
||||||
|
setTitle("TankGasoline");
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setLayout(null);
|
||||||
|
setResizable(false);
|
||||||
|
Gasoline = new DrawGasoline();
|
||||||
|
Gasoline.setBounds(0, 0, Width, Height);
|
||||||
|
add(Gasoline);
|
||||||
|
|
||||||
|
MoveAL moving = new MoveAL();
|
||||||
|
|
||||||
|
JButton buttonUp = new JButton();
|
||||||
|
buttonUp.setActionCommand("U");
|
||||||
|
buttonUp.setBounds(Width - 90, Height - 120,30,30);
|
||||||
|
buttonUp.setLayout(null);
|
||||||
|
add(buttonUp);
|
||||||
|
buttonUp.addActionListener(moving);
|
||||||
|
|
||||||
|
JButton buttonRight = new JButton();
|
||||||
|
buttonRight.setActionCommand("R");
|
||||||
|
buttonRight.setBounds(Width - 60, Height - 90,30,30);
|
||||||
|
buttonRight.setLayout(null);
|
||||||
|
add(buttonRight);
|
||||||
|
buttonRight.addActionListener(moving);
|
||||||
|
|
||||||
|
JButton buttonLeft = new JButton();
|
||||||
|
buttonLeft.setActionCommand("L");
|
||||||
|
buttonLeft.setBounds(Width - 120, Height - 90,30,30);
|
||||||
|
buttonLeft.setLayout(null);
|
||||||
|
add(buttonLeft);
|
||||||
|
buttonLeft.addActionListener(moving);
|
||||||
|
|
||||||
|
JButton buttonDown = new JButton();
|
||||||
|
buttonDown.setActionCommand("D");
|
||||||
|
buttonDown.setBounds(Width - 90, Height - 90,30,30);
|
||||||
|
buttonDown.setLayout(null);
|
||||||
|
add(buttonDown);
|
||||||
|
buttonDown.addActionListener(moving);
|
||||||
|
|
||||||
|
String[] items = {
|
||||||
|
"0",
|
||||||
|
"1"
|
||||||
|
};
|
||||||
|
JComboBox<String> strategy = new JComboBox<String>(items);
|
||||||
|
|
||||||
|
strategy.setBounds(Width - 100, 100, 80, 20);
|
||||||
|
strategy.setLayout(null);
|
||||||
|
strategy.setEnabled(true);
|
||||||
|
add(strategy);
|
||||||
|
|
||||||
|
JButton buttonStrategy = new JButton();
|
||||||
|
buttonStrategy.setBounds(Width - 60, 120, 40, 20);
|
||||||
|
buttonStrategy.setLayout(null);
|
||||||
|
add(buttonStrategy);
|
||||||
|
buttonStrategy.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Gasoline.ButtonStep_Click(strategy, Width, Height);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
buttonUp.setIcon(new ImageIcon("Arrows/Up.png"));
|
||||||
|
buttonDown.setIcon(new ImageIcon("Arrows/Down.png"));
|
||||||
|
buttonLeft.setIcon(new ImageIcon("Arrows/Left.png"));
|
||||||
|
buttonRight.setIcon(new ImageIcon("Arrows/Right.png"));
|
||||||
|
|
||||||
|
JButton buttonCreateTanker = new JButton("Create Base Tanker");
|
||||||
|
buttonCreateTanker.setBounds(60, Height - 120, 200, 50);
|
||||||
|
buttonCreateTanker.setLayout(null);
|
||||||
|
add(buttonCreateTanker);
|
||||||
|
buttonCreateTanker.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Gasoline.CreateBaseTankerButton_Click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
JButton buttonCreateGasolineTanker = new JButton("Create Update Tanker");
|
||||||
|
buttonCreateGasolineTanker.setBounds(260, Height - 120, 200, 50);
|
||||||
|
buttonCreateGasolineTanker.setLayout(null);
|
||||||
|
add(buttonCreateGasolineTanker);
|
||||||
|
buttonCreateGasolineTanker.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Gasoline.CreateGasolineTankerButton_Click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
private void SetTanker(DrawTanker tanker)
|
||||||
|
{
|
||||||
|
Gasoline.SetTanker(tanker);
|
||||||
|
}
|
||||||
public class MoveAL implements ActionListener {
|
public class MoveAL implements ActionListener {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
@ -21,7 +21,7 @@ public class CarsGenericCollection <T extends DrawTanker, U extends IMoveableObj
|
|||||||
return _collection.Insert(gasTanker);
|
return _collection.Insert(gasTanker);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean minus(int position)
|
public T minus(int position)
|
||||||
{
|
{
|
||||||
return _collection.Remove(position);
|
return _collection.Remove(position);
|
||||||
}
|
}
|
||||||
|
46
CarsGenericStorage.java
Normal file
46
CarsGenericStorage.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class CarsGenericStorage {
|
||||||
|
final HashMap<String, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> _carStorages;
|
||||||
|
public ArrayList<String> Keys()
|
||||||
|
{
|
||||||
|
return new ArrayList<>(_carStorages.keySet());
|
||||||
|
}
|
||||||
|
private final int _pictureWidth;
|
||||||
|
private final int _pictureHeight;
|
||||||
|
public CarsGenericStorage(int pictureWidth, int pictureHeight)
|
||||||
|
{
|
||||||
|
_pictureHeight = pictureHeight;
|
||||||
|
_pictureWidth = pictureWidth;
|
||||||
|
_carStorages = new HashMap<>();
|
||||||
|
}
|
||||||
|
public void AddSet(String name)
|
||||||
|
{
|
||||||
|
if (Keys().contains(name))
|
||||||
|
return;
|
||||||
|
_carStorages.put(name, new CarsGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||||
|
}
|
||||||
|
public void DelSet(String name)
|
||||||
|
{
|
||||||
|
if (!Keys().contains(name))
|
||||||
|
return;
|
||||||
|
_carStorages.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CarsGenericCollection<DrawTanker, DrawingObjectTanker> get(String ind)
|
||||||
|
{
|
||||||
|
if (Keys().contains(ind))
|
||||||
|
return _carStorages.get(ind);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public DrawingObjectTanker get(String name, int ind)
|
||||||
|
{
|
||||||
|
if (!Keys().contains(name))
|
||||||
|
return null;
|
||||||
|
return _carStorages.get(name).GetU(ind);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,33 +1,98 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.event.ListSelectionEvent;
|
||||||
|
import javax.swing.event.ListSelectionListener;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.image.CropImageFilter;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.PriorityQueue;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
class CollectionFrame extends JComponent {
|
class CollectionFrame extends JComponent {
|
||||||
private final CarsGenericCollection<DrawTanker, DrawingObjectTanker> _tanks;
|
private final CarsGenericStorage _tanksStorage;
|
||||||
|
private final GarageFrame frame;
|
||||||
|
private Queue<DrawTanker> removedTankers;
|
||||||
protected final int Width;
|
protected final int Width;
|
||||||
protected final int Height;
|
protected final int Height;
|
||||||
public CollectionFrame(int width, int height)
|
public CollectionFrame(int width, int height, GarageFrame fr)
|
||||||
{
|
{
|
||||||
Width = width;
|
Width = width;
|
||||||
Height = height;
|
Height = height;
|
||||||
_tanks = new CarsGenericCollection<>(Width, Height);
|
_tanksStorage = new CarsGenericStorage(Width, Height);
|
||||||
|
frame = fr;
|
||||||
|
removedTankers = new LinkedList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
g.drawImage(_tanks.ShowCars(), 0, 0, this);
|
if (frame.listStorage.getSelectedIndex() == -1)
|
||||||
|
return;
|
||||||
|
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||||||
|
if (obj == null)
|
||||||
|
return;
|
||||||
|
if (obj.ShowCars() == null)
|
||||||
|
return;
|
||||||
|
g.drawImage(obj.ShowCars(), 0, 0, this);
|
||||||
super.repaint();
|
super.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void ReloadObjects()
|
||||||
|
{
|
||||||
|
int index = frame.listStorage.getSelectedIndex();
|
||||||
|
frame.listModel.clear();
|
||||||
|
for (String key : _tanksStorage.Keys())
|
||||||
|
{
|
||||||
|
frame.listModel.addElement(key);
|
||||||
|
}
|
||||||
|
if (!frame.listModel.isEmpty() && (index == -1 || index >= frame.listModel.size()))
|
||||||
|
{
|
||||||
|
frame.listStorage.setSelectedIndex(0);
|
||||||
|
}
|
||||||
|
else if (!frame.listModel.isEmpty() && index > -1 && index < frame.listModel.size())
|
||||||
|
{
|
||||||
|
frame.listStorage.setSelectedIndex(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
protected void ButtonAddObject_Click()
|
||||||
|
{
|
||||||
|
String name = frame.nameStorageField.getText();
|
||||||
|
if (name.length() == 0)
|
||||||
|
return;
|
||||||
|
_tanksStorage.AddSet(name);
|
||||||
|
ReloadObjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void ButtonRemoveObject_Click()
|
||||||
|
{
|
||||||
|
if (frame.listStorage.getSelectedIndex() == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (JOptionPane.showConfirmDialog(null, "Delete object " + frame.listStorage.getSelectedValue() + "?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_tanksStorage.DelSet(frame.listStorage.getSelectedValue());
|
||||||
|
ReloadObjects();
|
||||||
|
}
|
||||||
|
|
||||||
protected void ButtonAddTank_Click()
|
protected void ButtonAddTank_Click()
|
||||||
{
|
{
|
||||||
|
if (frame.listStorage.getSelectedIndex() == -1)
|
||||||
|
return;
|
||||||
|
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||||||
|
if (obj == null)
|
||||||
|
return;
|
||||||
BaseFrame form = new BaseFrame();
|
BaseFrame form = new BaseFrame();
|
||||||
form.setVisible(true);
|
form.setVisible(true);
|
||||||
form.buttonSelect.addActionListener(
|
form.buttonSelect.addActionListener(
|
||||||
new ActionListener() {
|
new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent e){
|
public void actionPerformed(ActionEvent e){
|
||||||
if (_tanks.plus(form.Gasoline.GetSelectedCar()) != -1)
|
|
||||||
|
if (obj.plus(form.Gasoline.GetSelectedCar()) != -1)
|
||||||
{
|
{
|
||||||
form.dispose();
|
form.dispose();
|
||||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
@ -47,11 +112,23 @@ class CollectionFrame extends JComponent {
|
|||||||
|
|
||||||
protected void ButtonRemoveTank_Click(String text)
|
protected void ButtonRemoveTank_Click(String text)
|
||||||
{
|
{
|
||||||
|
if (frame.listStorage.getSelectedIndex() == -1)
|
||||||
|
return;
|
||||||
|
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||||||
|
if (obj == null)
|
||||||
|
return;
|
||||||
if (JOptionPane.showConfirmDialog(null, "Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
|
if (JOptionPane.showConfirmDialog(null, "Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
|
||||||
{
|
{
|
||||||
if (_tanks.minus(Integer.parseInt(text)))
|
int index = -1;
|
||||||
|
DrawTanker deleted = null;
|
||||||
|
try {
|
||||||
|
deleted = obj.minus(Integer.parseInt(text));
|
||||||
|
} catch(NumberFormatException e) {
|
||||||
|
}
|
||||||
|
if (deleted != null)
|
||||||
{
|
{
|
||||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
removedTankers.add(deleted);
|
||||||
super.repaint();
|
super.repaint();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -64,6 +141,14 @@ class CollectionFrame extends JComponent {
|
|||||||
|
|
||||||
protected void ButtonUpdate_Click() {super.repaint();}
|
protected void ButtonUpdate_Click() {super.repaint();}
|
||||||
|
|
||||||
|
protected void ButtonCreateDeleted_Click()
|
||||||
|
{
|
||||||
|
if (removedTankers.isEmpty())
|
||||||
|
return;
|
||||||
|
var tank = removedTankers.poll();
|
||||||
|
BaseFrame f = new BaseFrame(tank);
|
||||||
|
f.setVisible(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GarageFrame extends JFrame {
|
class GarageFrame extends JFrame {
|
||||||
@ -75,6 +160,18 @@ class GarageFrame extends JFrame {
|
|||||||
protected static final int Width = 1000;
|
protected static final int Width = 1000;
|
||||||
protected static final int Height = 600;
|
protected static final int Height = 600;
|
||||||
CollectionFrame Collection;
|
CollectionFrame Collection;
|
||||||
|
|
||||||
|
JButton addTankerButton;
|
||||||
|
JTextField indexTankerField;
|
||||||
|
JButton deleteTankerButton;
|
||||||
|
JButton updateCollectionButton;
|
||||||
|
JList<String> listStorage;
|
||||||
|
DefaultListModel<String> listModel;
|
||||||
|
JButton addCollectionButton;
|
||||||
|
JTextField nameStorageField;
|
||||||
|
JButton deleteCollectionbutton;
|
||||||
|
JButton createDeletedButton;
|
||||||
|
|
||||||
private void initUI()
|
private void initUI()
|
||||||
{
|
{
|
||||||
setSize(Width, Height);
|
setSize(Width, Height);
|
||||||
@ -85,7 +182,7 @@ class GarageFrame extends JFrame {
|
|||||||
setResizable(false);
|
setResizable(false);
|
||||||
|
|
||||||
|
|
||||||
JButton addTankerButton = new JButton("Add tanker");
|
addTankerButton = new JButton("Add tanker");
|
||||||
addTankerButton.setLayout(null);
|
addTankerButton.setLayout(null);
|
||||||
addTankerButton.setBounds(Width-200, Height-550, 200, 30);
|
addTankerButton.setBounds(Width-200, Height-550, 200, 30);
|
||||||
add(addTankerButton);
|
add(addTankerButton);
|
||||||
@ -96,12 +193,12 @@ class GarageFrame extends JFrame {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
JTextField indexTankerField = new JTextField();
|
indexTankerField = new JTextField();
|
||||||
indexTankerField.setBounds(Width- 200, Height-500, 200, 30);
|
indexTankerField.setBounds(Width- 200, Height-500, 200, 30);
|
||||||
indexTankerField.setLayout(null);
|
indexTankerField.setLayout(null);
|
||||||
add(indexTankerField);
|
add(indexTankerField);
|
||||||
|
|
||||||
JButton deleteTankerButton = new JButton("Delete tanker");
|
deleteTankerButton = new JButton("Delete tanker");
|
||||||
deleteTankerButton.setLayout(null);
|
deleteTankerButton.setLayout(null);
|
||||||
deleteTankerButton.setBounds(Width-200, Height-450, 200, 30);
|
deleteTankerButton.setBounds(Width-200, Height-450, 200, 30);
|
||||||
add(deleteTankerButton);
|
add(deleteTankerButton);
|
||||||
@ -112,7 +209,7 @@ class GarageFrame extends JFrame {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
JButton updateCollectionButton = new JButton("Update collection");
|
updateCollectionButton = new JButton("Update collection");
|
||||||
updateCollectionButton.setLayout(null);
|
updateCollectionButton.setLayout(null);
|
||||||
updateCollectionButton.setBounds(Width-200, Height-400, 200, 30);
|
updateCollectionButton.setBounds(Width-200, Height-400, 200, 30);
|
||||||
add(updateCollectionButton);
|
add(updateCollectionButton);
|
||||||
@ -123,7 +220,59 @@ class GarageFrame extends JFrame {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Collection = new CollectionFrame(Width-200, Height);
|
listModel = new DefaultListModel<>();
|
||||||
|
listStorage = new JList<String>(listModel);
|
||||||
|
listStorage.setLayout(null);
|
||||||
|
listStorage.setBounds(Width - 190, Height - 250, 180, 130);
|
||||||
|
add(listStorage);
|
||||||
|
listStorage.addListSelectionListener(new ListSelectionListener() {
|
||||||
|
@Override
|
||||||
|
public void valueChanged(ListSelectionEvent e) {
|
||||||
|
Collection.repaint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
addCollectionButton = new JButton("Add Collection");
|
||||||
|
addCollectionButton.setLayout(null);
|
||||||
|
addCollectionButton.setBounds(Width-190, Height - 350, 180, 30);
|
||||||
|
add(addCollectionButton);
|
||||||
|
addCollectionButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Collection.ButtonAddObject_Click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
nameStorageField = new JTextField();
|
||||||
|
nameStorageField.setBounds(Width- 190, Height- 300, 180, 30);
|
||||||
|
nameStorageField.setLayout(null);
|
||||||
|
add(nameStorageField);
|
||||||
|
|
||||||
|
|
||||||
|
deleteCollectionbutton = new JButton("Remove Collection");
|
||||||
|
deleteCollectionbutton.setBounds(Width-190, Height - 100, 180, 30);
|
||||||
|
deleteCollectionbutton.setLayout(null);
|
||||||
|
add(deleteCollectionbutton);
|
||||||
|
deleteCollectionbutton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Collection.ButtonRemoveObject_Click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
createDeletedButton = new JButton("Create deleted");
|
||||||
|
createDeletedButton.setLayout(null);
|
||||||
|
createDeletedButton.setBounds(Width - 200, 10, 200, 30);
|
||||||
|
add(createDeletedButton);
|
||||||
|
createDeletedButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Collection.ButtonCreateDeleted_Click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Collection = new CollectionFrame(Width-200, Height, this);
|
||||||
Collection.setLayout(null);
|
Collection.setLayout(null);
|
||||||
Collection.setBounds(0, 0, Width-200, Height);
|
Collection.setBounds(0, 0, Width-200, Height);
|
||||||
add(Collection);
|
add(Collection);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//GarageFrame a = new GarageFrame();
|
GarageFrame a = new GarageFrame();
|
||||||
ComboFrame a = new ComboFrame();
|
//ComboFrame a = new ComboFrame();
|
||||||
a.setVisible(true);
|
a.setVisible(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,15 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
public class SetGeneric <T extends Object>{
|
public class SetGeneric <T extends Object>{
|
||||||
private final T[] _places;
|
private final ArrayList<T> _places;
|
||||||
public int Count() {return _places.length;}
|
public int Count() {return _places.size();}
|
||||||
|
private final int _maxCount;
|
||||||
public SetGeneric(int count)
|
public SetGeneric(int count)
|
||||||
{
|
{
|
||||||
_places = (T[]) new Object[count];
|
_maxCount = count;
|
||||||
|
_places = new ArrayList<>();
|
||||||
}
|
}
|
||||||
public int Insert(T tanker)
|
public int Insert(T tanker)
|
||||||
{
|
{
|
||||||
@ -11,37 +17,19 @@ public class SetGeneric <T extends Object>{
|
|||||||
}
|
}
|
||||||
public int Insert(T tanker, int position)
|
public int Insert(T tanker, int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= Count())
|
if (position < 0 || position >= _maxCount)
|
||||||
return -1;
|
return -1;
|
||||||
if (_places[position] == null)
|
_places.add(position, tanker);
|
||||||
{
|
|
||||||
_places[position] = tanker;
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
int index = -1;
|
|
||||||
for (int i = position; i < Count(); i++)
|
|
||||||
{
|
|
||||||
if (_places[i] == null)
|
|
||||||
{
|
|
||||||
index = i; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (index < 0)
|
|
||||||
return -1;
|
|
||||||
for (int i = index; i > position; i--)
|
|
||||||
{
|
|
||||||
_places[i] = _places[i - 1];
|
|
||||||
}
|
|
||||||
_places[position] = tanker;
|
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean Remove(int position)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position > Count())
|
if (position < 0 || position >= Count())
|
||||||
return false;
|
return null;
|
||||||
_places[position] = null;
|
var returning = _places.get(position);
|
||||||
return true;
|
_places.remove(position);
|
||||||
|
return returning;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Get(int position)
|
public T Get(int position)
|
||||||
@ -50,7 +38,37 @@ public class SetGeneric <T extends Object>{
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (T)_places[position];
|
return _places.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Iterable<T> GetTankers(final Integer maxTankers) {
|
||||||
|
return new Iterable<T>() {
|
||||||
|
@Override
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
return new Iterator<T>() {
|
||||||
|
private int currentIndex = 0;
|
||||||
|
private int count = 0;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return currentIndex < _places.size() && (maxTankers == null || count < maxTankers);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T next() {
|
||||||
|
if (hasNext()) {
|
||||||
|
count++;
|
||||||
|
return _places.get(currentIndex++);
|
||||||
|
}
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user