From 48da449924411ecf5087046c94ec03bf39498617 Mon Sep 17 00:00:00 2001 From: BoiledMilk123 Date: Wed, 5 Jun 2024 01:32:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B8?= =?UTF-8?q?=D0=BB=20labwork03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionType.java | 17 ++++ .../ListGenericObjects.java | 50 +++++++++++ .../MassiveGenericObjects.java | 21 +++-- .../StorageCollection.java | 37 ++++++++ .../Forms/DrawingLocomotiveCollection.java | 83 +++++++++++++++--- .../src/Forms/FormLocomotiveCollection.java | 86 ++++++++++++++++--- 6 files changed, 269 insertions(+), 25 deletions(-) create mode 100644 ProjectElectricLocomotive/src/CollectionGenericObjects/CollectionType.java create mode 100644 ProjectElectricLocomotive/src/CollectionGenericObjects/ListGenericObjects.java create mode 100644 ProjectElectricLocomotive/src/CollectionGenericObjects/StorageCollection.java diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/CollectionType.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/CollectionType.java new file mode 100644 index 0000000..deac31e --- /dev/null +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/CollectionType.java @@ -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; + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/ListGenericObjects.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/ListGenericObjects.java new file mode 100644 index 0000000..8115666 --- /dev/null +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/ListGenericObjects.java @@ -0,0 +1,50 @@ +package ProjectElectricLocomotive.src.CollectionGenericObjects; + +import java.util.ArrayList; +import java.util.List; + +public class ListGenericObjects implements ICollectionGenericObjects { + private final List 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); + } +} diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/MassiveGenericObjects.java index 11b7612..97efd73 100644 --- a/ProjectElectricLocomotive/src/CollectionGenericObjects/MassiveGenericObjects.java +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -1,8 +1,9 @@ package ProjectElectricLocomotive.src.CollectionGenericObjects; import java.util.Optional; +import java.util.Arrays; -public class MassiveGenericObjects implements ICollectionGenericObjects +public class MassiveGenericObjects implements ICollectionGenericObjects { private T[] _collection; @@ -12,14 +13,24 @@ public class MassiveGenericObjects implements ICollectionGener } @Override - public void setMaxCount(int maxCount) { - if (maxCount > 0) { - _collection = (T[]) new Object[maxCount]; + public void setMaxCount(int value) { + if (value > 0) { + 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() { - _collection = (T[]) new Object[0]; // Create an empty array of type T + _collection = (T[]) new Object[0]; } @Override public int insert(T obj) { diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/StorageCollection.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/StorageCollection.java new file mode 100644 index 0000000..573febc --- /dev/null +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/StorageCollection.java @@ -0,0 +1,37 @@ +package ProjectElectricLocomotive.src.CollectionGenericObjects; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class StorageCollection { + + private final Map> storage; + + + public List 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 void delCollection(String name) { + storage.remove(name); + } + + public ICollectionGenericObjects get(String name) { + return storage.get(name); + } +} diff --git a/ProjectElectricLocomotive/src/Forms/DrawingLocomotiveCollection.java b/ProjectElectricLocomotive/src/Forms/DrawingLocomotiveCollection.java index 3ecdbb8..ef0c6f3 100644 --- a/ProjectElectricLocomotive/src/Forms/DrawingLocomotiveCollection.java +++ b/ProjectElectricLocomotive/src/Forms/DrawingLocomotiveCollection.java @@ -1,10 +1,10 @@ package ProjectElectricLocomotive.src.Forms; -import ProjectElectricLocomotive.src.CollectionGenericObjects.AbstractCompany; -import ProjectElectricLocomotive.src.CollectionGenericObjects.LocomotiveDepotService; -import ProjectElectricLocomotive.src.CollectionGenericObjects.MassiveGenericObjects; +import ProjectElectricLocomotive.src.CollectionGenericObjects.*; import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive; import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; +import java.util.Queue; +import java.util.Stack; import javax.swing.*; import java.awt.*; @@ -15,17 +15,18 @@ public class DrawingLocomotiveCollection extends JPanel private final FormLocomotiveCollection field; private AbstractCompany _company = null; + private Stack delLocomotive; + private final StorageCollection storageCollection; + public DrawingLocomotiveCollection(FormLocomotiveCollection field) { this.field = field; + + storageCollection = new StorageCollection(); + delLocomotive = new Stack(); } void comboBoxSelectorCompany_SelectedIndexChanged(String text) { - switch (text) - { - case "Хранилище": - _company = new LocomotiveDepotService(field.pictureBox.getWidth(), field.pictureBox.getHeight(), new MassiveGenericObjects()); - break; - } + field.manageBox.setEnabled(false); } public void insertNewLocomotive(DrawingLocomotive _drawingLocomotive) @@ -92,7 +93,11 @@ public class DrawingLocomotiveCollection extends JPanel int pos = Integer.parseInt(maskedTextField.getText()); int choice = JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 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, "Объект удален"); field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox))); } else { @@ -119,5 +124,63 @@ public class DrawingLocomotiveCollection extends JPanel if (_company == null) return; 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 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 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(); + } + + } diff --git a/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java index 78341d9..352aac8 100644 --- a/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java +++ b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java @@ -9,16 +9,29 @@ import java.text.ParseException; public class FormLocomotiveCollection extends JFrame { protected int Width; private int Height; + private JPanel storageBox; + protected JPanel manageBox; private JPanel groupBoxTools; private JButton buttonAddElectricLocomotive; private JButton buttonAddLocomotive; - private JComboBox comboBoxSelectorCompany; + protected JComboBox comboBoxSelectorCompany; private JButton buttonRefresh; private JButton buttonGoToCheck; private JButton buttonRemoveLocomotive; private JButton buttonCreateSpecialLocomotive; private JFormattedTextField maskedTextBoxPosition; + + protected JTextField collectionTextBox; + protected JLabel pictureBox; + protected JLabel nameCollection; + protected JButton buttonAddCollection; + protected JButton ButtonGoToCheckRemoved; + protected JList listBoxCollection; + protected JButton buttonRemoveCollection; + protected JButton buttonAddCompany; + protected JRadioButton radioButtonList; + protected JRadioButton radioButtonMassive; DrawingLocomotiveCollection field = new DrawingLocomotiveCollection(this); @@ -35,15 +48,26 @@ public class FormLocomotiveCollection extends JFrame { private void initComponents() { groupBoxTools = new JPanel(); + storageBox = new JPanel(); + manageBox = new JPanel(); buttonAddElectricLocomotive = new JButton("Добавление электровоза"); buttonAddLocomotive = new JButton("Добавление локомотива"); comboBoxSelectorCompany = new JComboBox<>(); + buttonAddCollection = new JButton("Добавить коллекцию"); buttonRefresh = new JButton("Обновить"); buttonGoToCheck = new JButton("Передать на тесты"); + ButtonGoToCheckRemoved = new JButton("Тест локомотива из удаленных"); buttonRemoveLocomotive = new JButton("Удалить локомотив"); buttonCreateSpecialLocomotive = new JButton("Сгенерировать локомотив"); maskedTextBoxPosition = createMaskedTextField(); 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 -> { field.comboBoxSelectorCompany_SelectedIndexChanged((String) comboBoxSelectorCompany.getSelectedItem()); @@ -76,19 +100,55 @@ public class FormLocomotiveCollection extends JFrame { field.ButtonCreateSpecialLocomotive_Click(); 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.setLayout(new GridLayout(8, 1)); - groupBoxTools.add(comboBoxSelectorCompany); - groupBoxTools.add(buttonAddLocomotive); - groupBoxTools.add(buttonAddElectricLocomotive); - groupBoxTools.add(maskedTextBoxPosition); - groupBoxTools.add(buttonRemoveLocomotive); - groupBoxTools.add(buttonGoToCheck); - groupBoxTools.add(buttonCreateSpecialLocomotive); + groupBoxTools.setLayout(new GridLayout(2, 1)); - 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(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() { try { MaskFormatter formatter = new MaskFormatter("##");