Highest yielding tree search task ready

This commit is contained in:
2022-06-02 10:53:01 +03:00
parent 58c3711ad0
commit 7a3fc9d7d4
5 changed files with 85 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
import javax.swing.*; import javax.swing.*;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import java.awt.*;
public class DataTableModel extends AbstractTableModel { public class DataTableModel extends AbstractTableModel {
@@ -130,6 +131,23 @@ public class DataTableModel extends AbstractTableModel {
this.fireTableStructureChanged(); this.fireTableStructureChanged();
} }
public Point getMostProductiveCellPos(String treeName) {
double maxHarvest = -1;
Point targetPos = new Point(-1, -1);
// Go through all cells in table
for (int row = 0; row < getRowCount(); row++) {
for (int col = 0; col < getColumnCount(); col++) {
Data cell = rowData[row][col];
// 1) Check if cell contains target tree
// 2) Compare tree output with max, save if higher
if (cell.getTree().equals(treeName) && cell.getHarvest() > maxHarvest) {
maxHarvest = cell.getHarvest();
targetPos.setLocation(col, row);
}
}
}
return targetPos;
}
public String[] getColumnNames() { public String[] getColumnNames() {
return columnNames; return columnNames;

View File

@@ -34,7 +34,7 @@ public class MenuActionDeleteColumn implements ActionListener {
); );
// Canceled by user // Canceled by user
if (result == JOptionPane.CLOSED_OPTION || result == 2) { if (result == JOptionPane.CLOSED_OPTION || result == 1) {
return; return;
} }

View File

@@ -34,7 +34,7 @@ public class MenuActionDeleteRow implements ActionListener {
); );
// Canceled by user // Canceled by user
if (result == JOptionPane.CLOSED_OPTION || result == 2) { if (result == JOptionPane.CLOSED_OPTION || result == 1) {
return; return;
} }

View File

@@ -0,0 +1,58 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class MenuActionFindHighestHarvest implements ActionListener {
private DataTableModel tableModel;
public MenuActionFindHighestHarvest(DataTableModel tableModel) {
this.tableModel = tableModel;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Find highest harvest");
Object[] options = {"Поиск", "Отмена"};
JPanel panel = new JPanel();
panel.add(new JLabel("Введите название дерева"));
JTextField textField = new JTextField(20);
panel.add(textField);
int result = JOptionPane.showOptionDialog(
null,
panel,
"Введите название дерева",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
null
);
// Canceled by user
if (result == JOptionPane.CLOSED_OPTION || result == 1) {
return;
}
// Process user response
Point highestProductiveCellPos = tableModel.getMostProductiveCellPos(textField.getText());
// Check if search was successful
String text;
if (highestProductiveCellPos.x != -1 && highestProductiveCellPos.y != -1) {
// Success
text = String.format("Строка: %d\nСтолбец: %d", highestProductiveCellPos.y + 1, highestProductiveCellPos.x + 1);
} else {
// Fail
text = "Не удалось найти деревья с таким названием";
}
// Display the result
JOptionPane.showMessageDialog(null, text);
}
}

View File

@@ -45,11 +45,15 @@ public class MenuBar extends JMenuBar {
// Create main menu // Create main menu
JMenu menu = new JMenu("Меню"); JMenu menu = new JMenu("Меню");
menu.add(addMenu); menu.add(addMenu);
menu.add(deleteMenu); menu.add(deleteMenu);
// menu.add(createAddRowColItem());
// menu.add(createRemoveRowColItem()); JMenuItem highestHarvestItem = new JMenuItem("Макс урожай");
menu.add(createMockMenuItem("Макс Урожай")); highestHarvestItem.addActionListener(new MenuActionFindHighestHarvest(tableModel));
menu.add(highestHarvestItem);
menu.add(createMockMenuItem("Средн Урожай")); menu.add(createMockMenuItem("Средн Урожай"));
menu.add(createMockMenuItem("Общий урожай")); menu.add(createMockMenuItem("Общий урожай"));
menu.add(createMockMenuItem("Lin Spec")); menu.add(createMockMenuItem("Lin Spec"));