Compare commits

..

2 Commits

Author SHA1 Message Date
a180fa3b0a labwork04 UP! 2024-06-05 01:35:49 +04:00
48da449924 закончил labwork03 2024-06-05 01:32:46 +04:00
6 changed files with 275 additions and 25 deletions

View File

@ -0,0 +1,17 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects;
public enum CollectionType
{
None(0),
Massive (1),
List (2);
private final int Value;
CollectionType(int value) {
Value = value;
}
public int GetCountDeck() {
return Value;
}
}

View File

@ -0,0 +1,50 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects;
import java.util.ArrayList;
import java.util.List;
public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
private final List<T> collection;
private int maxCount;
public int getCount() {
return collection.size();
}
public void setMaxCount(int value) {
if (value > 0) {
maxCount = value;
}
}
public ListGenericObjects() {
collection = new ArrayList<>();
}
public T get(int position) {
if (position < 0 || position >= collection.size()) {
return null;
}
return collection.get(position);
}
public int insert(T obj) {
return insert(obj, collection.size());
}
public int insert(T obj, int position) {
if (maxCount == collection.size() || position < 0 || position > collection.size()) {
return -1;
}
collection.add(position, obj);
return collection.size();
}
public T remove(int position) {
if (position < 0 || position >= collection.size()) {
return null;
}
return collection.remove(position);
}
}

View File

@ -1,8 +1,9 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects; package ProjectElectricLocomotive.src.CollectionGenericObjects;
import java.util.Optional; import java.util.Optional;
import java.util.Arrays;
public class MassiveGenericObjects<T extends Object> implements ICollectionGenericObjects<T> public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>
{ {
private T[] _collection; private T[] _collection;
@ -12,14 +13,24 @@ public class MassiveGenericObjects<T extends Object> implements ICollectionGener
} }
@Override @Override
public void setMaxCount(int maxCount) { public void setMaxCount(int value) {
if (maxCount > 0) { if (value > 0) {
_collection = (T[]) new Object[maxCount]; if (_collection.length > 0) {
if (value > _collection.length) {
T[] newArray = Arrays.copyOf(_collection, value);
_collection = newArray;
} else if (value < _collection.length) {
T[] newArray = Arrays.copyOf(_collection, value);
_collection = newArray;
}
} else {
_collection = (T[]) new Object[value];
}
} }
} }
public MassiveGenericObjects() { public MassiveGenericObjects() {
_collection = (T[]) new Object[0]; // Create an empty array of type T _collection = (T[]) new Object[0];
} }
@Override @Override
public int insert(T obj) { public int insert(T obj) {

View File

@ -0,0 +1,43 @@
package ProjectElectricLocomotive.src.CollectionGenericObjects;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StorageCollection<T extends Object> {
private final Map<String, ICollectionGenericObjects<T>> storage;
public List<String> getKeys() {
return new ArrayList<>(storage.keySet());
}
public StorageCollection() {
storage = new HashMap<>();
}
public void addCollection(String name, CollectionType collectionType) {
if (name == null || storage.containsKey(name)) return;
if (collectionType == CollectionType.Massive) {
storage.put(name, new MassiveGenericObjects<>());
} else if (collectionType == CollectionType.List) {
storage.put(name, new ListGenericObjects<>());
}
}
public T GetShip(String name, int ind){
if (name == null || !storage.containsKey(name)) return null;
return storage.get(name).get(ind);
}
public void delCollection(String name) {
if(!storage.containsKey(name)) return;
storage.remove(name);
}
public ICollectionGenericObjects<T> get(String name) {
return storage.get(name);
}
}

View File

@ -1,10 +1,10 @@
package ProjectElectricLocomotive.src.Forms; package ProjectElectricLocomotive.src.Forms;
import ProjectElectricLocomotive.src.CollectionGenericObjects.AbstractCompany; import ProjectElectricLocomotive.src.CollectionGenericObjects.*;
import ProjectElectricLocomotive.src.CollectionGenericObjects.LocomotiveDepotService;
import ProjectElectricLocomotive.src.CollectionGenericObjects.MassiveGenericObjects;
import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive; import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive;
import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
import java.util.Queue;
import java.util.Stack;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -15,17 +15,18 @@ public class DrawingLocomotiveCollection extends JPanel
private final FormLocomotiveCollection field; private final FormLocomotiveCollection field;
private AbstractCompany _company = null; private AbstractCompany _company = null;
private Stack<DrawingLocomotive> delLocomotive;
private final StorageCollection<DrawingLocomotive> storageCollection;
public DrawingLocomotiveCollection(FormLocomotiveCollection field) { public DrawingLocomotiveCollection(FormLocomotiveCollection field) {
this.field = field; this.field = field;
storageCollection = new StorageCollection<DrawingLocomotive>();
delLocomotive = new Stack<DrawingLocomotive>();
} }
void comboBoxSelectorCompany_SelectedIndexChanged(String text) void comboBoxSelectorCompany_SelectedIndexChanged(String text)
{ {
switch (text) field.manageBox.setEnabled(false);
{
case "Хранилище":
_company = new LocomotiveDepotService(field.pictureBox.getWidth(), field.pictureBox.getHeight(), new MassiveGenericObjects<DrawingLocomotive>());
break;
}
} }
public void insertNewLocomotive(DrawingLocomotive _drawingLocomotive) public void insertNewLocomotive(DrawingLocomotive _drawingLocomotive)
@ -92,7 +93,11 @@ public class DrawingLocomotiveCollection extends JPanel
int pos = Integer.parseInt(maskedTextField.getText()); int pos = Integer.parseInt(maskedTextField.getText());
int choice = JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); int choice = JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (choice == JOptionPane.NO_OPTION) return; if (choice == JOptionPane.NO_OPTION) return;
if (_company.remove(_company, pos) != null) { DrawingLocomotive obj = _company.remove(_company, pos);
if (obj != null) {
delLocomotive.add(obj);
JOptionPane.showMessageDialog(null, "Объект удален"); JOptionPane.showMessageDialog(null, "Объект удален");
field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox))); field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox)));
} else { } else {
@ -119,5 +124,63 @@ public class DrawingLocomotiveCollection extends JPanel
if (_company == null) return; if (_company == null) return;
field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox))); field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox)));
} }
public void ButtonCollectionAdd_Click() {
if (field.collectionTextBox.getText().isEmpty() || (!field.radioButtonList.isSelected() && !field.radioButtonMassive.isSelected()) || (field.radioButtonList.isSelected() && field.radioButtonMassive.isSelected())) {
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
CollectionType collectionType = field.radioButtonList.isSelected() ? CollectionType.List : CollectionType.Massive;
storageCollection.addCollection(field.collectionTextBox.getText(), collectionType);
refreshListBoxItems();
}
public void ButtonCollectionRemove_Click()
{
if (field.listBoxCollection.getSelectedIndex() < 0) {
JOptionPane.showMessageDialog(null, "Коллекция не выбрана", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
int dialogResult = JOptionPane.showConfirmDialog(null, "Удалить коллекцию?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (dialogResult == JOptionPane.YES_OPTION) {
storageCollection.delCollection(field.listBoxCollection.getSelectedValue());
refreshListBoxItems();
}
}
private void refreshListBoxItems() {
DefaultListModel<String> listModel = new DefaultListModel<>();
for (String key : storageCollection.getKeys()) {
listModel.addElement(key);
}
field.listBoxCollection.setModel(listModel);
}
public void ButtonCreateCompany_Click() {
if (field.listBoxCollection.getSelectedIndex() < 0) {
JOptionPane.showMessageDialog(null, "Коллекция не выбрана", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
ICollectionGenericObjects<DrawingLocomotive> collection = storageCollection.get(field.listBoxCollection.getSelectedValue());
if (collection == null) {
JOptionPane.showMessageDialog(null, "Коллекция не проинициализирована", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
switch (field.comboBoxSelectorCompany.getSelectedItem().toString()) {
case "Хранилище":
_company = new LocomotiveDepotService(field.pictureBox.getWidth(), field.pictureBox.getHeight(), collection);
break;
}
field.setPanelEnabled(true);
refreshListBoxItems();
}
public void ButtonCheckDel()
{
if(delLocomotive.isEmpty()) return;
FormElectricLocomotive form = new FormElectricLocomotive(delLocomotive.peek());
delLocomotive.pop();
}
} }

View File

@ -9,16 +9,29 @@ import java.text.ParseException;
public class FormLocomotiveCollection extends JFrame { public class FormLocomotiveCollection extends JFrame {
protected int Width; protected int Width;
private int Height; private int Height;
private JPanel storageBox;
protected JPanel manageBox;
private JPanel groupBoxTools; private JPanel groupBoxTools;
private JButton buttonAddElectricLocomotive; private JButton buttonAddElectricLocomotive;
private JButton buttonAddLocomotive; private JButton buttonAddLocomotive;
private JComboBox<String> comboBoxSelectorCompany; protected JComboBox<String> comboBoxSelectorCompany;
private JButton buttonRefresh; private JButton buttonRefresh;
private JButton buttonGoToCheck; private JButton buttonGoToCheck;
private JButton buttonRemoveLocomotive; private JButton buttonRemoveLocomotive;
private JButton buttonCreateSpecialLocomotive; private JButton buttonCreateSpecialLocomotive;
private JFormattedTextField maskedTextBoxPosition; private JFormattedTextField maskedTextBoxPosition;
protected JTextField collectionTextBox;
protected JLabel pictureBox; protected JLabel pictureBox;
protected JLabel nameCollection;
protected JButton buttonAddCollection;
protected JButton ButtonGoToCheckRemoved;
protected JList<String> listBoxCollection;
protected JButton buttonRemoveCollection;
protected JButton buttonAddCompany;
protected JRadioButton radioButtonList;
protected JRadioButton radioButtonMassive;
DrawingLocomotiveCollection field = new DrawingLocomotiveCollection(this); DrawingLocomotiveCollection field = new DrawingLocomotiveCollection(this);
@ -35,15 +48,26 @@ public class FormLocomotiveCollection extends JFrame {
private void initComponents() { private void initComponents() {
groupBoxTools = new JPanel(); groupBoxTools = new JPanel();
storageBox = new JPanel();
manageBox = new JPanel();
buttonAddElectricLocomotive = new JButton("Добавление электровоза"); buttonAddElectricLocomotive = new JButton("Добавление электровоза");
buttonAddLocomotive = new JButton("Добавление локомотива"); buttonAddLocomotive = new JButton("Добавление локомотива");
comboBoxSelectorCompany = new JComboBox<>(); comboBoxSelectorCompany = new JComboBox<>();
buttonAddCollection = new JButton("Добавить коллекцию");
buttonRefresh = new JButton("Обновить"); buttonRefresh = new JButton("Обновить");
buttonGoToCheck = new JButton("Передать на тесты"); buttonGoToCheck = new JButton("Передать на тесты");
ButtonGoToCheckRemoved = new JButton("Тест локомотива из удаленных");
buttonRemoveLocomotive = new JButton("Удалить локомотив"); buttonRemoveLocomotive = new JButton("Удалить локомотив");
buttonCreateSpecialLocomotive = new JButton("Сгенерировать локомотив"); buttonCreateSpecialLocomotive = new JButton("Сгенерировать локомотив");
maskedTextBoxPosition = createMaskedTextField(); maskedTextBoxPosition = createMaskedTextField();
pictureBox = new JLabel(); pictureBox = new JLabel();
nameCollection = new JLabel("Название коллекции");
collectionTextBox = new JTextField();
listBoxCollection = new JList<>();
buttonRemoveCollection = new JButton("Удалить коллекцию");
radioButtonList = new JRadioButton("Список");
radioButtonMassive = new JRadioButton("Массив");
buttonAddCompany = new JButton("Создать компанию");
comboBoxSelectorCompany.addActionListener(e -> { comboBoxSelectorCompany.addActionListener(e -> {
field.comboBoxSelectorCompany_SelectedIndexChanged((String) comboBoxSelectorCompany.getSelectedItem()); field.comboBoxSelectorCompany_SelectedIndexChanged((String) comboBoxSelectorCompany.getSelectedItem());
@ -76,19 +100,55 @@ public class FormLocomotiveCollection extends JFrame {
field.ButtonCreateSpecialLocomotive_Click(); field.ButtonCreateSpecialLocomotive_Click();
repaint(); repaint();
}); });
buttonAddCollection.addActionListener(e->
{
field.ButtonCollectionAdd_Click();
repaint();
});
buttonRemoveCollection.addActionListener(e->
{
field.ButtonCollectionRemove_Click();
repaint();
});
buttonAddCompany.addActionListener(e->
{
field.ButtonCreateCompany_Click();
repaint();
});
ButtonGoToCheckRemoved.addActionListener(e->
{
field.ButtonCheckDel();
repaint();
});
storageBox.setLayout(new GridLayout(9, 1));
storageBox.add(nameCollection);
storageBox.add(radioButtonList);
storageBox.add(radioButtonMassive);
storageBox.add(collectionTextBox);
storageBox.add(buttonAddCollection);
storageBox.add(listBoxCollection);
storageBox.add(buttonRemoveCollection);
storageBox.add(comboBoxSelectorCompany);
storageBox.add(buttonAddCompany);
groupBoxTools.setBorder(BorderFactory.createTitledBorder("Инструменты")); groupBoxTools.setBorder(BorderFactory.createTitledBorder("Инструменты"));
groupBoxTools.setLayout(new GridLayout(8, 1)); groupBoxTools.setLayout(new GridLayout(2, 1));
groupBoxTools.add(comboBoxSelectorCompany);
groupBoxTools.add(buttonAddLocomotive);
groupBoxTools.add(buttonAddElectricLocomotive);
groupBoxTools.add(maskedTextBoxPosition);
groupBoxTools.add(buttonRemoveLocomotive);
groupBoxTools.add(buttonGoToCheck);
groupBoxTools.add(buttonCreateSpecialLocomotive);
groupBoxTools.add(buttonRefresh); groupBoxTools.add(storageBox);
manageBox.setLayout(new GridLayout(8, 1));
manageBox.add(buttonAddLocomotive);
manageBox.add(buttonAddElectricLocomotive);
manageBox.add(maskedTextBoxPosition);
manageBox.add(buttonRemoveLocomotive);
manageBox.add(ButtonGoToCheckRemoved);
manageBox.add(buttonGoToCheck);
manageBox.add(buttonCreateSpecialLocomotive);
manageBox.add(buttonRefresh);
groupBoxTools.add(manageBox);
setPanelEnabled(false);
add(groupBoxTools); add(groupBoxTools);
add(pictureBox); add(pictureBox);
@ -109,6 +169,12 @@ public class FormLocomotiveCollection extends JFrame {
} }
public void setPanelEnabled(boolean enabled) {
for (Component component : manageBox.getComponents()) {
component.setEnabled(enabled);
}
}
private JFormattedTextField createMaskedTextField() { private JFormattedTextField createMaskedTextField() {
try { try {
MaskFormatter formatter = new MaskFormatter("##"); MaskFormatter formatter = new MaskFormatter("##");