Harvest per type task complete

This commit is contained in:
2022-06-02 16:07:05 +03:00
parent e080a37e3f
commit 0eff9f6b46
3 changed files with 77 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
import javax.swing.*; import javax.swing.*;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import java.awt.*; import java.awt.*;
import java.util.*;
import java.util.List;
public class DataTableModel extends AbstractTableModel { public class DataTableModel extends AbstractTableModel {
@@ -200,6 +202,7 @@ public class DataTableModel extends AbstractTableModel {
double harvest = 0; double harvest = 0;
int counter = 0; int counter = 0;
// Go through all cells in table
for (int row = 0; row < getRowCount(); row++) { for (int row = 0; row < getRowCount(); row++) {
for (int col = 0; col < getColumnCount(); col++) { for (int col = 0; col < getColumnCount(); col++) {
Data cell = rowData[row][col]; Data cell = rowData[row][col];
@@ -219,4 +222,49 @@ public class DataTableModel extends AbstractTableModel {
return harvest; return harvest;
} }
public List<Data> getTreeHarvestRating() {
// First, group trees by name
HashMap<String, Double> groupHarvests = new HashMap<>();
// 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];
// Skip cells without tree names
if (cell.getTree().equals("")) {
continue;
}
// Increase group harvest by harvest of this tree
if (!groupHarvests.containsKey(cell.getTree())) {
// If this is first time encountering this tree, create new group
groupHarvests.put(cell.getTree(), cell.getHarvest());
} else {
Double newValue = groupHarvests.get(cell.getTree()) + cell.getHarvest();
groupHarvests.put(cell.getTree(), newValue);
}
}
}
// Second, use Data class to store the results
List<Data> harvestData = new ArrayList<>();
for (Map.Entry<String, Double> entry : groupHarvests.entrySet()) {
Data newEntry = new Data(entry.getKey(), entry.getValue());
harvestData.add(newEntry);
}
// Third, sort the resulting list
harvestData.sort(new Comparator<Data>() {
@Override
public int compare(Data o1, Data o2) {
double h1 = o1.getHarvest();
double h2 = o2.getHarvest();
return Double.compare(h1, h2);
}
});
Collections.reverse(harvestData);
return harvestData;
}
} }

View File

@@ -0,0 +1,25 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class MenuActionGetHarvestPerType implements ActionListener {
private DataTableModel tableModel;
public MenuActionGetHarvestPerType(DataTableModel tableModel) {
this.tableModel = tableModel;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Get harvest per type");
List<Data> harvestData = tableModel.getTreeHarvestRating();
StringBuilder result = new StringBuilder();
for (Data entry : harvestData) {
result.append(String.format("%s - %.3f", entry.getTree(), entry.getHarvest())).append("\n");
}
JOptionPane.showMessageDialog(null, result);
}
}

View File

@@ -58,7 +58,10 @@ public class MenuBar extends JMenuBar {
plumHarvestAvgItem.addActionListener(new MenuActionGetPlumHarvestAvg(tableModel)); plumHarvestAvgItem.addActionListener(new MenuActionGetPlumHarvestAvg(tableModel));
menu.add(plumHarvestAvgItem); menu.add(plumHarvestAvgItem);
menu.add(createMockMenuItem("Общий урожай")); JMenuItem harvestPerTypeItem = new JMenuItem("Общий урожай");
harvestPerTypeItem.addActionListener(new MenuActionGetHarvestPerType(tableModel));
menu.add(harvestPerTypeItem);
menu.add(createMockMenuItem("Lin Spec")); menu.add(createMockMenuItem("Lin Spec"));
menu.add(createMockMenuItem("Сектор К деревьев")); menu.add(createMockMenuItem("Сектор К деревьев"));
this.add(menu); this.add(menu);