Harvest per type task complete
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
public class DataTableModel extends AbstractTableModel {
|
||||
|
||||
@@ -200,6 +202,7 @@ public class DataTableModel extends AbstractTableModel {
|
||||
double harvest = 0;
|
||||
int counter = 0;
|
||||
|
||||
// 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];
|
||||
@@ -219,4 +222,49 @@ public class DataTableModel extends AbstractTableModel {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
25
src/MenuActionGetHarvestPerType.java
Normal file
25
src/MenuActionGetHarvestPerType.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,10 @@ public class MenuBar extends JMenuBar {
|
||||
plumHarvestAvgItem.addActionListener(new MenuActionGetPlumHarvestAvg(tableModel));
|
||||
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("Сектор К деревьев"));
|
||||
this.add(menu);
|
||||
|
||||
Reference in New Issue
Block a user