PIbd-11 Karakozov_AK LabWork04 Hard #4
@ -18,7 +18,7 @@ public abstract class AbstractCompany {
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = collection;
|
||||
_collection.SetMaxCount(GetMaxCount(), DrawingBulldozer.class);
|
||||
_collection.SetMaxCount(GetMaxCount());
|
||||
}
|
||||
//перегрузка операторов в джаве невозможна
|
||||
public DrawingBulldozer GetRandomObject()
|
||||
|
7
src/CollectionGenericObjects/CollectionType.java
Normal file
7
src/CollectionGenericObjects/CollectionType.java
Normal file
@ -0,0 +1,7 @@
|
||||
package CollectionGenericObjects;
|
||||
|
||||
public enum CollectionType {
|
||||
None,
|
||||
Massive,
|
||||
List
|
||||
}
|
@ -3,9 +3,8 @@ package CollectionGenericObjects;
|
||||
public interface ICollectionGenericObjects<T>
|
||||
{
|
||||
int getCount();
|
||||
void SetMaxCount(int count, Class<T> type);
|
||||
void SetMaxCount(int count);
|
||||
int Insert(T obj);
|
||||
int Insert(T obj, int position);
|
||||
T Remove(int position);
|
||||
T Get(int position);
|
||||
}
|
||||
|
41
src/CollectionGenericObjects/ListGenericObjects.java
Normal file
41
src/CollectionGenericObjects/ListGenericObjects.java
Normal file
@ -0,0 +1,41 @@
|
||||
package CollectionGenericObjects;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
|
||||
private List<T> _collection;
|
||||
private int _maxCount;
|
||||
public int getCount() {
|
||||
return _collection.size();
|
||||
}
|
||||
@Override
|
||||
public void SetMaxCount(int size) {
|
||||
if (size > 0) {
|
||||
_maxCount = size;
|
||||
}
|
||||
}
|
||||
public ListGenericObjects() {
|
||||
_collection = new ArrayList<T>();
|
||||
}
|
||||
@Override
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position >= getCount() || position < 0) return null;
|
||||
return _collection.get(position);
|
||||
}
|
||||
@Override
|
||||
public int Insert(T obj)
|
||||
{
|
||||
if (getCount() == _maxCount) return -1;
|
||||
_collection.add(obj);
|
||||
return getCount();
|
||||
}
|
||||
@Override
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position >= getCount() || position < 0) return null;
|
||||
T obj = _collection.get(position);
|
||||
_collection.remove(position);
|
||||
return obj;
|
||||
}
|
||||
}
|
@ -1,14 +1,18 @@
|
||||
package CollectionGenericObjects;
|
||||
|
||||
import Drawings.DrawingBulldozer;
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
|
||||
private T[] _collection;
|
||||
private T[] _collection = null;
|
||||
private int Count;
|
||||
public void SetMaxCount(int size, Class<T> type) {
|
||||
@Override
|
||||
public void SetMaxCount(int size) {
|
||||
if (size > 0) {
|
||||
_collection = (T[]) Array.newInstance(type, size);
|
||||
Count = size;
|
||||
if (_collection == null){
|
||||
_collection = (T[]) Array.newInstance((Class) DrawingBulldozer.class, size);
|
||||
Count = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
@ -30,36 +34,6 @@ public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
|
||||
return -1;
|
||||
}
|
||||
@Override
|
||||
public int Insert(T obj, int position) {
|
||||
if (position >= getCount() || position < 0)
|
||||
return -1;
|
||||
if (_collection[position] == null) {
|
||||
_collection[position] = obj;
|
||||
return position;
|
||||
}
|
||||
int index = position + 1;
|
||||
while (index < getCount())
|
||||
{
|
||||
if (_collection[index] == null)
|
||||
{
|
||||
_collection[index] = obj;
|
||||
return index;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
index = position - 1;
|
||||
while (index >= 0)
|
||||
{
|
||||
if (_collection[index] == null)
|
||||
{
|
||||
_collection[index] = obj;
|
||||
return index;
|
||||
}
|
||||
--index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@Override
|
||||
public T Remove(int position) {
|
||||
if (position >= getCount() || position < 0)
|
||||
return null;
|
||||
|
41
src/CollectionGenericObjects/StorageCollection.java
Normal file
41
src/CollectionGenericObjects/StorageCollection.java
Normal file
@ -0,0 +1,41 @@
|
||||
package CollectionGenericObjects;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class StorageCollection<T> {
|
||||
private Map<String, ICollectionGenericObjects<T>> _storages;
|
||||
public StorageCollection()
|
||||
{
|
||||
_storages = new HashMap<String, ICollectionGenericObjects<T>>();
|
||||
}
|
||||
public Set<String> Keys() {
|
||||
Set<String> keys = _storages.keySet();
|
||||
return keys;
|
||||
}
|
||||
public void AddCollection(String name, CollectionType collectionType)
|
||||
{
|
||||
if (_storages.containsKey(name)) return;
|
||||
if (collectionType == CollectionType.None) return;
|
||||
else if (collectionType == CollectionType.Massive)
|
||||
_storages.put(name, new MassiveGenericObjects<T>());
|
||||
else if (collectionType == CollectionType.List)
|
||||
_storages.put(name, new ListGenericObjects<T>());
|
||||
}
|
||||
public void DelCollection(String name)
|
||||
{
|
||||
if (_storages.containsKey(name))
|
||||
_storages.remove(name);
|
||||
}
|
||||
// в джаве отсутствуют индикаторы
|
||||
public ICollectionGenericObjects<T> getCollectionObject(String name) {
|
||||
if (_storages.containsKey(name))
|
||||
return _storages.get(name);
|
||||
return null;
|
||||
}
|
||||
//дополнительное задание 1
|
||||
public T Get(String name, int position){
|
||||
if(_storages.containsKey(name))
|
||||
return _storages.get(name).Remove(position);
|
||||
return null;
|
||||
}
|
||||
}
|
@ -22,7 +22,8 @@ public class FormAdditionalCollection extends JFrame {
|
||||
private CanvasExcavator canvasExc = new CanvasExcavator();
|
||||
private AdditionalCollections<EntityBulldozer, IDifferentRollers> additionalCollection = null;
|
||||
private Random random = new Random();
|
||||
private JButton buttonGenerate = new JButton("Создать");
|
||||
private JButton buttonCreate = new JButton("Создать");
|
||||
private JButton buttonAdd = new JButton("Добавить");
|
||||
private JList<String> listEntity = new JList<String>();
|
||||
private JList<String> listRollers = new JList<String>();
|
||||
public FormAdditionalCollection() {
|
||||
@ -31,39 +32,49 @@ public class FormAdditionalCollection extends JFrame {
|
||||
additionalCollection = new AdditionalCollections<EntityBulldozer, IDifferentRollers>(3, (Class) EntityBulldozer.class, (Class) IDifferentRollers.class);
|
||||
AddEntities();
|
||||
AddRollers();
|
||||
buttonGenerate.addActionListener(new ActionListener() {
|
||||
|
||||
buttonCreate.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
drawingBulldozer = additionalCollection.CreateAdditionalCollectionBulldozer();
|
||||
drawingBulldozer.SetPictureSize(getWidth(), getHeight());
|
||||
drawingBulldozer.SetPosition(50,50);
|
||||
drawingBulldozer.SetPosition(50, 50);
|
||||
canvasExc._drawingBulldozer = drawingBulldozer;
|
||||
canvasExc.repaint();
|
||||
DrawingBulldozer copyBulldozer;
|
||||
if (drawingBulldozer instanceof DrawingExcavator)
|
||||
copyBulldozer = new DrawingExcavator((EntityExcavator) drawingBulldozer.EntityBulldozer, drawingBulldozer.drawingRollers);
|
||||
else
|
||||
copyBulldozer = new DrawingBulldozer(drawingBulldozer.EntityBulldozer, drawingBulldozer.drawingRollers);
|
||||
company._collection.Insert(copyBulldozer);
|
||||
FormBulldozerCollection.canvasShow();
|
||||
|
||||
|
||||
String[] data1 = new String[additionalCollection.CountEntities];
|
||||
for (int i = 0; i < additionalCollection.CountEntities; i++) {
|
||||
EntityBulldozer entity = additionalCollection._collectionEntity[i];
|
||||
data1[i] = ToString(entity);
|
||||
}
|
||||
String[] data2 = new String[additionalCollection.CountRollers];
|
||||
for (int i = 0; i < additionalCollection.CountRollers; i++) {
|
||||
IDifferentRollers rollers = additionalCollection._collectionRollers[i];
|
||||
data2[i] = ToString(rollers);
|
||||
}
|
||||
listEntity.setListData(data1);
|
||||
listRollers.setListData(data2);
|
||||
for (int i = 0; i < additionalCollection.CountEntities; i++) {
|
||||
EntityBulldozer entity = additionalCollection._collectionEntity[i];
|
||||
data1[i] = ToString(entity);
|
||||
}
|
||||
String[] data2 = new String[additionalCollection.CountRollers];
|
||||
for (int i = 0; i < additionalCollection.CountRollers; i++) {
|
||||
IDifferentRollers rollers = additionalCollection._collectionRollers[i];
|
||||
data2[i] = ToString(rollers);
|
||||
}
|
||||
listEntity.setListData(data1);
|
||||
listRollers.setListData(data2);
|
||||
}
|
||||
});
|
||||
buttonGenerate.setBounds(450, 10, 100, 50);
|
||||
add(buttonGenerate);
|
||||
|
||||
buttonAdd.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (drawingBulldozer != null) {
|
||||
DrawingBulldozer copyBulldozer;
|
||||
if (drawingBulldozer instanceof DrawingExcavator)
|
||||
copyBulldozer = new DrawingExcavator((EntityExcavator) drawingBulldozer.EntityBulldozer, drawingBulldozer.drawingRollers);
|
||||
else
|
||||
copyBulldozer = new DrawingBulldozer(drawingBulldozer.EntityBulldozer, drawingBulldozer.drawingRollers);
|
||||
company._collection.Insert(copyBulldozer);
|
||||
FormBulldozerCollection.canvasShow();
|
||||
}
|
||||
}
|
||||
});
|
||||
buttonCreate.setBounds(300, 10, 100, 50);
|
||||
buttonAdd.setBounds(450, 10, 100, 50);
|
||||
add(buttonCreate);
|
||||
add(buttonAdd);
|
||||
listEntity.setBounds(10,200,300,60);
|
||||
listRollers.setBounds(320,200,300,60);
|
||||
add(listEntity);
|
||||
|
@ -1,6 +1,4 @@
|
||||
import CollectionGenericObjects.AbstractCompany;
|
||||
import CollectionGenericObjects.BulldozerSharingService;
|
||||
import CollectionGenericObjects.MassiveGenericObjects;
|
||||
import CollectionGenericObjects.*;
|
||||
import Drawings.CanvasFormBulldozerCollection;
|
||||
import Drawings.DrawingBulldozer;
|
||||
import Drawings.DrawingExcavator;
|
||||
@ -12,6 +10,7 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.text.ParseException;
|
||||
import java.util.Random;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import static java.lang.Integer.parseInt;
|
||||
|
||||
@ -20,11 +19,21 @@ public class FormBulldozerCollection extends JFrame{
|
||||
private Dimension dimension;
|
||||
public static CanvasFormBulldozerCollection<DrawingBulldozer> _canvasExcavator = new CanvasFormBulldozerCollection<DrawingBulldozer>();
|
||||
private static AbstractCompany _company = null;
|
||||
private JButton CreateExcButton = new JButton("Создать экскаватор");
|
||||
private LinkedList<DrawingBulldozer> _collectionRemoveObjects = new LinkedList<DrawingBulldozer>();
|
||||
private StorageCollection<DrawingBulldozer> _storageCollection = new StorageCollection<DrawingBulldozer>();
|
||||
private JTextField textBoxCollection = new JTextField();
|
||||
private JRadioButton radioButtonMassive = new JRadioButton("Массив");
|
||||
private JRadioButton radioButtonList = new JRadioButton("Список");
|
||||
private JButton buttonAddCollection = new JButton("Добавить");
|
||||
private JList listBoxCollection = new JList();
|
||||
private JButton buttonRemoveCollection = new JButton("Удалить");
|
||||
private JButton buttonCreateCompany = new JButton("Создать компанию");
|
||||
private JButton CreateExcButton = new JButton("Создать экскаватор");;
|
||||
private JButton CreateBullButton = new JButton("Создать бульдозер");
|
||||
private JButton RemoveButton = new JButton("Удалить");
|
||||
private JButton GoToCheckButton = new JButton("Тест");
|
||||
private JButton RandomButton = new JButton("Случайный объект");
|
||||
private JButton RemoveObjectsButton = new JButton("Показать удаленный");
|
||||
private JButton RefreshButton = new JButton("Обновить");
|
||||
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
|
||||
private JFormattedTextField MaskedTextField;
|
||||
@ -55,7 +64,7 @@ public class FormBulldozerCollection extends JFrame{
|
||||
break;
|
||||
default: return;
|
||||
}
|
||||
if (_company._collection.Insert(drawingBulldozer, 0) != -1) {
|
||||
if (_company._collection.Insert(drawingBulldozer) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
canvasShow();
|
||||
}
|
||||
@ -83,17 +92,6 @@ public class FormBulldozerCollection extends JFrame{
|
||||
|
||||
MaskedTextField = new JFormattedTextField(mask);
|
||||
|
||||
ComboBoxCollections.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
switch (ComboBoxCollections.getSelectedItem().toString()) {
|
||||
case "Хранилище":
|
||||
_company = new BulldozerSharingService(getWidth()-200, getHeight()-70, new MassiveGenericObjects<DrawingBulldozer>());
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
CreateBullButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@ -110,7 +108,7 @@ public class FormBulldozerCollection extends JFrame{
|
||||
RemoveButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_company == null || MaskedTextField.getText() == null) {
|
||||
if (_company == null || MaskedTextField.getText() == null || listBoxCollection.getSelectedValue().toString() == null) {
|
||||
return;
|
||||
}
|
||||
int pos = parseInt(MaskedTextField.getText());
|
||||
@ -118,8 +116,10 @@ public class FormBulldozerCollection extends JFrame{
|
||||
"Удалить", "Удаление",
|
||||
JOptionPane.YES_NO_OPTION);
|
||||
if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
|
||||
if (_company._collection.Remove(pos) != null) {
|
||||
DrawingBulldozer obj = _storageCollection.Get(listBoxCollection.getSelectedValue().toString(), pos);
|
||||
if (obj != null) {
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
_collectionRemoveObjects.push(obj);
|
||||
canvasShow();
|
||||
}
|
||||
else {
|
||||
@ -164,6 +164,7 @@ public class FormBulldozerCollection extends JFrame{
|
||||
}
|
||||
FormAdditionalCollection form = new FormAdditionalCollection();
|
||||
form.setCompany(_company);
|
||||
form.setLocationRelativeTo(null);
|
||||
}
|
||||
});
|
||||
|
||||
@ -178,27 +179,141 @@ public class FormBulldozerCollection extends JFrame{
|
||||
}
|
||||
});
|
||||
|
||||
buttonAddCollection.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (textBoxCollection.getText().isEmpty() || (!radioButtonMassive.isSelected()
|
||||
&& !radioButtonList.isSelected())) {
|
||||
JOptionPane.showMessageDialog(null, "Не все данные заполнены");
|
||||
return;
|
||||
}
|
||||
CollectionType collectionType = CollectionType.None;
|
||||
if (radioButtonMassive.isSelected())
|
||||
{
|
||||
collectionType = CollectionType.Massive;
|
||||
}
|
||||
else if (radioButtonList.isSelected())
|
||||
{
|
||||
collectionType = CollectionType.List;
|
||||
}
|
||||
_storageCollection.AddCollection(textBoxCollection.getText(), collectionType);
|
||||
RefreshListBoxItems();
|
||||
}
|
||||
});
|
||||
|
||||
buttonRemoveCollection.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) {
|
||||
JOptionPane.showMessageDialog(null, "Коллекция не выбрана");
|
||||
return;
|
||||
}
|
||||
int resultConfirmDialog = JOptionPane.showConfirmDialog(null,
|
||||
"Удалить", "Удаление",
|
||||
JOptionPane.YES_NO_OPTION);
|
||||
if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
|
||||
_storageCollection.DelCollection(listBoxCollection.getSelectedValue().toString());
|
||||
RefreshListBoxItems();
|
||||
}
|
||||
});
|
||||
|
||||
buttonCreateCompany.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) {
|
||||
JOptionPane.showMessageDialog(null, "Коллекция не выбрана");
|
||||
return;
|
||||
}
|
||||
ICollectionGenericObjects<DrawingBulldozer> collection =
|
||||
_storageCollection.getCollectionObject(listBoxCollection.getSelectedValue().toString());
|
||||
if (collection == null) {
|
||||
JOptionPane.showMessageDialog(null, "Коллекция не проинициализирована");
|
||||
return;
|
||||
}
|
||||
switch (ComboBoxCollections.getSelectedItem().toString()) {
|
||||
case "Хранилище":
|
||||
_company = new BulldozerSharingService(getWidth()-200, getHeight()-70,
|
||||
collection);
|
||||
break;
|
||||
}
|
||||
RefreshListBoxItems();
|
||||
}
|
||||
});
|
||||
|
||||
RemoveObjectsButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_collectionRemoveObjects.isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
DrawingBulldozer bulldozer = null;
|
||||
bulldozer = _collectionRemoveObjects.pop();
|
||||
if (bulldozer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FormExcavator form = new FormExcavator("Экскаватор", new Dimension(900,565));
|
||||
form.Init(bulldozer);
|
||||
}
|
||||
});
|
||||
|
||||
JLabel labelCollectionName = new JLabel("Название коллекции");
|
||||
ButtonGroup radioButtonsGroup = new ButtonGroup();
|
||||
radioButtonsGroup.add(radioButtonMassive);
|
||||
radioButtonsGroup.add(radioButtonList);
|
||||
|
||||
_canvasExcavator.setBounds(0, 0, getWidth()-200, getHeight());
|
||||
labelCollectionName.setBounds(getWidth()-190, 30, 150, 20);
|
||||
textBoxCollection.setBounds(getWidth()-190, 52, 150, 25);
|
||||
radioButtonMassive.setBounds(getWidth()-190, 80, 75, 20);
|
||||
radioButtonList.setBounds(getWidth()-105, 80, 75, 20);
|
||||
buttonAddCollection.setBounds(getWidth()-190, 105, 150, 20);
|
||||
listBoxCollection.setBounds(getWidth()-190, 135, 150, 70);
|
||||
buttonRemoveCollection.setBounds(getWidth()-190, 210, 150, 20);
|
||||
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
|
||||
CreateBullButton.setBounds(getWidth()-190, 60, 150, 30);
|
||||
CreateExcButton.setBounds(getWidth()-190, 100, 150, 30);
|
||||
MaskedTextField.setBounds(getWidth()-190,200,150,30);
|
||||
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
|
||||
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
|
||||
RandomButton.setBounds(getWidth()-190, 320, 150, 30);
|
||||
buttonCreateCompany.setBounds(getWidth()-190, 240, 150, 20);
|
||||
CreateBullButton.setBounds(getWidth()-190, 280, 150, 30);
|
||||
CreateExcButton.setBounds(getWidth()-190, 320, 150, 30);
|
||||
MaskedTextField.setBounds(getWidth()-190,360,150,30);
|
||||
RemoveButton.setBounds(getWidth()-190, 400, 150, 30);
|
||||
GoToCheckButton.setBounds(getWidth()-190, 440, 150, 30);
|
||||
RandomButton.setBounds(getWidth()-190, 480, 150, 30);
|
||||
RemoveObjectsButton.setBounds(getWidth()-190, 520, 150, 30);
|
||||
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
|
||||
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
add(_canvasExcavator);
|
||||
add(labelCollectionName);
|
||||
add(textBoxCollection);
|
||||
add(radioButtonMassive);
|
||||
add(radioButtonList);
|
||||
add(buttonAddCollection);
|
||||
add(listBoxCollection);
|
||||
add(buttonRemoveCollection);
|
||||
add(ComboBoxCollections);
|
||||
add(buttonCreateCompany);
|
||||
add(CreateBullButton);
|
||||
add(CreateExcButton);
|
||||
add(MaskedTextField);
|
||||
add(RemoveButton);
|
||||
add(GoToCheckButton);
|
||||
add(RandomButton);
|
||||
add(RemoveObjectsButton);
|
||||
add(RefreshButton);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void RefreshListBoxItems()
|
||||
{
|
||||
DefaultListModel<String> list = new DefaultListModel<String>();
|
||||
for (String name : _storageCollection.Keys()) {
|
||||
if (name != "")
|
||||
{
|
||||
list.addElement(name);
|
||||
}
|
||||
}
|
||||
listBoxCollection.setModel(list);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user