Лабораторная работа №4

This commit is contained in:
ENDORFIT 2024-06-08 23:51:02 +04:00
parent e7a0a32fd9
commit 78a8bd4fa1
10 changed files with 280 additions and 99 deletions

View File

@ -20,7 +20,7 @@ public abstract class AbstractCompany {
_pictureHeight = picHeight;
_collection = collection;
System.out.println(_pictureHeight+" "+_pictureWidth+" "+_placeSizeHeight+" "+_placeSizeWidth);
_collection.SetMaxCount(GetMaxCount(), (Class) DrawingMonorail.class);
_collection.SetMaxCount(GetMaxCount());
}
//Перегрузок нет
public DrawingMonorail GetRandomObject()

View File

@ -10,11 +10,11 @@ import java.lang.reflect.Array;
import java.util.Random;
public class AdditionalCollection <T extends EntityMonorail, U extends IDrawingWheels>{
public T[] _collectionEntity;
public U[] _collectionWheels;
public T[] CollectionEntity;
public U[] CollectionWheels;
public AdditionalCollection(int size, Class<T> type1, Class<T> type2) {
_collectionEntity = (T[]) Array.newInstance(type1, size);
_collectionWheels = (U[]) Array.newInstance(type2, size);
CollectionEntity = (T[]) Array.newInstance(type1, size);
CollectionWheels = (U[]) Array.newInstance(type2, size);
CountEntities = size;
CountWheels = size;
}
@ -23,9 +23,9 @@ public class AdditionalCollection <T extends EntityMonorail, U extends IDrawingW
public int Insert(T entity) {
int index = 0;
while (index < CountEntities) {
if (_collectionEntity[index] == null)
if (CollectionEntity[index] == null)
{
_collectionEntity[index] = entity;
CollectionEntity[index] = entity;
return index;
}
++index;
@ -35,9 +35,9 @@ public class AdditionalCollection <T extends EntityMonorail, U extends IDrawingW
public int Insert(U decks) {
int index = 0;
while (index < CountWheels) {
if (_collectionWheels[index] == null)
if (CollectionWheels[index] == null)
{
_collectionWheels[index] = decks;
CollectionWheels[index] = decks;
return index;
}
++index;
@ -46,9 +46,9 @@ public class AdditionalCollection <T extends EntityMonorail, U extends IDrawingW
}
public DrawingMonorail CreateAdditionalCollectionMonorail() {
Random random = new Random();
if (_collectionEntity == null || _collectionWheels == null) return null;
T entity = _collectionEntity[random.nextInt(CountEntities)];
U wheels = _collectionWheels[random.nextInt(CountWheels)];
if (CollectionEntity == null || CollectionWheels == null) return null;
T entity = CollectionEntity[random.nextInt(CountEntities)];
U wheels = CollectionWheels[random.nextInt(CountWheels)];
DrawingMonorail drawingBaseStormtrooper = null;
if (entity instanceof EntityModernMonorail) {
drawingBaseStormtrooper = new DrawingModernMonorail((EntityModernMonorail)entity, wheels);

View File

@ -0,0 +1,7 @@
package Scripts.CollectionGenericObjects;
public enum CollectionType {
None,
Massive,
List
}

View File

@ -18,7 +18,7 @@ public class DepotSharingService extends AbstractCompany {
graphics.setColor(Color.BLACK);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height + 1; ++j)
for (int j = 0; j < height; ++j)
{
graphics.drawLine(i * _offsetX + i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth + i * _offsetX + i * _placeSizeWidth, j * _placeSizeHeight);
graphics.drawLine(i * _offsetX + i * _placeSizeWidth, j * _placeSizeHeight, i * _offsetX + i * _placeSizeWidth, _placeSizeHeight + j * _placeSizeHeight);
@ -36,7 +36,7 @@ public class DepotSharingService extends AbstractCompany {
int currnetIndex = 0;
for (int j = height; j >= 0; j--) {
for (int j = height - 1; j >= 0; j--) {
for (int i = 0; i < width; i++, currnetIndex++) {
if (_collection.Get(currnetIndex) == null) continue;

View File

@ -3,9 +3,8 @@ package Scripts.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);
}

View File

@ -0,0 +1,48 @@
package Scripts.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;
}
}

View File

@ -1,21 +1,27 @@
package Scripts.CollectionGenericObjects;
import Scripts.Drawing.DrawingMonorail;
import java.lang.reflect.Array;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>
{
private T[] _collection;
private int Count;
public void SetMaxCount(int size, Class<T> type) {
public void SetMaxCount(int size) {
if (size > 0) {
_collection = (T[]) Array.newInstance(type, size);
Count = size;
if (_collection == null) {
_collection = (T[]) Array.newInstance((Class) DrawingMonorail.class, size);
Count = size;
}
}
}
@Override
public int getCount() {
return Count;
}
@Override
public int Insert(T obj) {
int index = 0;
@ -31,36 +37,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;

View File

@ -0,0 +1,41 @@
package Scripts.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 SetCollectionObject(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;
}
public T remove(String name, int position){
if(_storages.containsKey(name))
return _storages.get(name).Remove(position);
return null;
}
}

View File

@ -105,7 +105,7 @@ public class FormAdditionalCollection extends JFrame {
public void AddWheels() {
for (int i = 0; i < _additionalCollection.CountWheels; i++) {
random = new Random();
EntityMonorail entity = _additionalCollection._collectionEntity[i];
EntityMonorail entity = _additionalCollection.CollectionEntity[i];
DrawingWheels drawingWheels = null;
drawingWheels = new DrawingWheels();
drawingWheels.SetCountWheels((int) ((Math.random() * 3) + 1) * 2);
@ -119,12 +119,12 @@ public class FormAdditionalCollection extends JFrame {
this._company = company;
String[] data1 = new String[_additionalCollection.CountEntities];
for (int i = 0; i < _additionalCollection.CountEntities; i++) {
EntityMonorail entity = _additionalCollection._collectionEntity[i];
EntityMonorail entity = _additionalCollection.CollectionEntity[i];
data1[i] = ToString(entity);
}
String[] data2 = new String[_additionalCollection.CountWheels];
for (int i = 0; i < _additionalCollection.CountWheels; i++) {
DrawingWheels wheels = _additionalCollection._collectionWheels[i];
DrawingWheels wheels = _additionalCollection.CollectionWheels[i];
data2[i] = ToString(wheels);
}
listEntity.setListData(data1);

View File

@ -1,11 +1,11 @@
package Scripts.Forms;
import Scripts.CollectionGenericObjects.AbstractCompany;
import Scripts.CollectionGenericObjects.DepotSharingService;
import Scripts.CollectionGenericObjects.MassiveGenericObjects;
import Scripts.CollectionGenericObjects.*;
import Scripts.Drawing.DrawingModernMonorail;
import Scripts.Drawing.DrawingMonorail;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@ -18,9 +18,21 @@ public class FormMonorailCollection extends JFrame{
private Dimension dimension;
public static CanvasFormMonorailCollection<DrawingMonorail> _canvasMonorail = new CanvasFormMonorailCollection<DrawingMonorail>();
private static AbstractCompany _company = null;
private JButton CreateButton = new JButton("Создать современный монорельс");;
private JButton CreateShipButton = new JButton("Создать монорельс");
private JButton RemoveButton = new JButton("Удалить");
private Queue<DrawingMonorail> _collectionRemovedObjects = new LinkedList<DrawingMonorail>();
private StorageCollection<DrawingMonorail> _storageCollection = new StorageCollection<DrawingMonorail>();
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 createButton = new JButton("Создать современный монорельс");;
private JButton createModernButton = new JButton("Создать монорельс");
private JButton removeButton = new JButton("Удалить");
private JButton removeObjectsButton = new JButton("Удаленные объекты");
private JButton GoToCheckButton = new JButton("На проверку");
private JButton RandomButton = new JButton("Случайные");
private JButton RefreshButton = new JButton("Обновить");
@ -53,7 +65,7 @@ public class FormMonorailCollection extends JFrame{
break;
default: return;
}
if (_company._collection.Insert(drawingMonorail, 0) != -1) {
if (_company._collection.Insert(drawingMonorail) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
canvasShow();
}
@ -84,20 +96,20 @@ public class FormMonorailCollection extends JFrame{
}
});
CreateShipButton.addActionListener(new ActionListener() {
createModernButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingMonorail");
}
});
CreateButton.addActionListener(new ActionListener() {
createButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingModernMonorail");
}
});
RemoveButton.addActionListener(new ActionListener() {
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || TextField.getText() == null) {
@ -106,12 +118,14 @@ public class FormMonorailCollection extends JFrame{
int pos = parseInt(TextField.getText());
int resultConfirmDialog = JOptionPane.showConfirmDialog(null, "Удалить", "Удаление", JOptionPane.YES_NO_OPTION);
if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
if (_company._collection.Remove(pos) != null) {
System.out.println(pos);
DrawingMonorail obj = _storageCollection.remove(listBoxCollection.getSelectedValue().toString(), pos);
System.out.println(obj);
if (obj != null) {
JOptionPane.showMessageDialog(null, "Объект удален");
_collectionRemovedObjects.add(obj);
canvasShow();
}
else {
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
}
@ -171,41 +185,137 @@ public class FormMonorailCollection extends JFrame{
form.setCompany(_company);
}
});
_canvasMonorail.setBounds(0, 0, getWidth()-200, getHeight());
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
CreateButton.setBounds(getWidth()-190, 100, 150, 30);
RandomButton.setBounds(getWidth()-190, 140, 150, 30);
TextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
setSize(dimension.width,dimension.height);
setLayout(null);
add(_canvasMonorail);
add(ComboBoxCollections);
add(CreateShipButton);
add(CreateButton);
add(TextField);
add(RemoveButton);
add(GoToCheckButton);
add(RandomButton);
add(RefreshButton);
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
_canvasMonorail.setBounds(0, 0, getWidth()-200, getHeight()-70);
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
CreateButton.setBounds(getWidth()-190, 100, 150, 30);
TextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RandomButton.setBounds(getWidth()-190, 140, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
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.SetCollectionObject(textBoxCollection.getText(), collectionType);
RerfreshListBoxItems();
}
});
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());
RerfreshListBoxItems();
}
});
buttonCreateCompany.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) {
JOptionPane.showMessageDialog(null, "Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawingMonorail> collection =
_storageCollection.GetCollectionObject(listBoxCollection.getSelectedValue().toString());
if (collection == null) {
JOptionPane.showMessageDialog(null, "Коллекция не проинициализирована");
return;
}
switch (ComboBoxCollections.getSelectedItem().toString()) {
case "Хранилище":
_company = new DepotSharingService(getWidth() - 200, getHeight() - 110,
collection);
break;
}
RerfreshListBoxItems();
}
});
removeObjectsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_collectionRemovedObjects.isEmpty()) {
JOptionPane.showMessageDialog(null, "Коллекция пуста");
return;
}
DrawingMonorail drawingMonorail = null;
drawingMonorail = _collectionRemovedObjects.remove();
if (drawingMonorail == null) {
return;
}
FormModernMonorail form = new FormModernMonorail();
form.Init(drawingMonorail);
}
});
ButtonGroup radioButtonsGroup = new ButtonGroup();
JLabel labelCollectionName = new JLabel("Название коллекции");
radioButtonsGroup.add(radioButtonMassive);
radioButtonsGroup.add(radioButtonList);
_canvasMonorail.setBounds(0, 0, getWidth() - 200, getHeight());
labelCollectionName.setBounds(getWidth()-190, 10, 150, 20);
textBoxCollection.setBounds(getWidth()-190,35,150,25);
radioButtonMassive.setBounds(getWidth()-190, 60, 75, 20);
radioButtonList.setBounds(getWidth()-105, 60, 75, 20);
buttonAddCollection.setBounds(getWidth()-190, 85, 150, 20);
listBoxCollection.setBounds(getWidth()-190, 115, 150, 70);
buttonRemoveCollection.setBounds(getWidth()-190, 195, 150, 20);
ComboBoxCollections.setBounds(getWidth() - 190, 225, 150, 20);
buttonCreateCompany.setBounds(getWidth()-190, 255, 150, 20);
createModernButton.setBounds(getWidth() - 190, 285, 150, 30);
createButton.setBounds(getWidth() - 190, 325, 150, 30);
RandomButton.setBounds(getWidth() - 190, 365, 150, 30);
removeObjectsButton.setBounds(getWidth()-190, 505, 150, 30);
TextField.setBounds(getWidth() - 190, 545, 150, 30);
removeButton.setBounds(getWidth() - 190, 585, 150, 30);
GoToCheckButton.setBounds(getWidth() - 190, 625, 150, 30);
RefreshButton.setBounds(getWidth() - 190, 665, 150, 30);
setSize(dimension.width, dimension.height);
setLayout(null);
add(textBoxCollection);
add(radioButtonMassive);
add(radioButtonList);
add(buttonAddCollection);
add(listBoxCollection);
add(buttonRemoveCollection);
add(buttonCreateCompany);
add(labelCollectionName);
add(removeObjectsButton);
add(_canvasMonorail);
add(ComboBoxCollections);
add(createModernButton);
add(createButton);
add(TextField);
add(GoToCheckButton);
add(RandomButton);
add(removeButton);
add(RefreshButton);
setVisible(true);
}
private void RerfreshListBoxItems() {
DefaultListModel<String> list = new DefaultListModel<String>();
for (String name : _storageCollection.Keys()) {
if (name != "") {
list.addElement(name);
}
}
listBoxCollection.setModel(list);
}
}