Lin Spec task ready

This commit is contained in:
2022-06-02 16:32:01 +03:00
parent 0eff9f6b46
commit 2f296d5cf4
5 changed files with 74 additions and 3 deletions

11
Copaci.in Normal file
View File

@@ -0,0 +1,11 @@
10 4
Яблоко Яблоко Яблоко Яблоко
Яблоко Груша Вишня Персик
Яблоко Абрикос Вишня Слива
Яблоко Айва Слива Слива
Яблоко Слива Слива Слива
Яблоко Слива Слива Слива
Яблоко Слива Слива Слива
Яблоко Слива Слива Слива
Яблоко Слива Слива Слива
Яблоко Слива Слива Слива

1
LinSpec.txt Normal file
View File

@@ -0,0 +1 @@
Яблоко Яблоко Яблоко Яблоко

View File

@@ -13,11 +13,14 @@ public class MenuActionGetHarvestPerType implements ActionListener {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
System.out.println("Get harvest per type"); System.out.println("Get harvest per type");
// Get sorted list of harvest per tree type
List<Data> harvestData = tableModel.getTreeHarvestRating(); List<Data> harvestData = tableModel.getTreeHarvestRating();
StringBuilder result = new StringBuilder();
// Create a string where each element is at a separate line
StringBuilder result = new StringBuilder();
for (Data entry : harvestData) { for (Data entry : harvestData) {
result.append(String.format("%s - %.3f", entry.getTree(), entry.getHarvest())).append("\n"); result.append(String.format("%s - %.3f", entry.getTree(), entry.getHarvest()));
result.append("\n");
} }
JOptionPane.showMessageDialog(null, result); JOptionPane.showMessageDialog(null, result);

View File

@@ -0,0 +1,53 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public class MenuActionLinSpec implements ActionListener {
public void actionPerformed(ActionEvent e) {
DataReader dataReader = new DataReader();
String[][] treesGrid = dataReader.readTrees("Copaci.in");
int rowCount = treesGrid.length;
int colCount = treesGrid[0].length;
// Go through each row of the grid
StringBuilder outputGrid = new StringBuilder();
for (int row = 0; row < rowCount; row++) {
// Get first tree in the row. It will be compared to other elements to define if row is homogenous
String treeName = treesGrid[row][0];
boolean isHomogenous = true;
StringBuilder outputRow = new StringBuilder();
for (int col = 0; col < colCount; col++) {
// Stop execution if a tree of different type was discovered
if (!treesGrid[row][col].equals(treeName)) {
isHomogenous = false;
break;
}
// Add tree to output row
outputRow.append(treesGrid[row][col]);
outputRow.append(" ");
}
if (isHomogenous) {
outputGrid.append(outputRow);
outputGrid.append("\n");
}
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("LinSpec.txt"));
writer.write(outputGrid.toString());
writer.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
JOptionPane.showMessageDialog(null, "Копирование успешно");
}
}

View File

@@ -62,7 +62,10 @@ public class MenuBar extends JMenuBar {
harvestPerTypeItem.addActionListener(new MenuActionGetHarvestPerType(tableModel)); harvestPerTypeItem.addActionListener(new MenuActionGetHarvestPerType(tableModel));
menu.add(harvestPerTypeItem); menu.add(harvestPerTypeItem);
menu.add(createMockMenuItem("Lin Spec")); JMenuItem linSpecItem = new JMenuItem("Lin Spec");
linSpecItem.addActionListener(new MenuActionLinSpec());
menu.add(linSpecItem);
menu.add(createMockMenuItem("Сектор К деревьев")); menu.add(createMockMenuItem("Сектор К деревьев"));
this.add(menu); this.add(menu);
} }