Commit 9c242b34 authored by edmundszagars's avatar edmundszagars
Browse files

Implementing "Write to file feature"

parent 358c19bb
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -13,8 +13,7 @@ import javafx.stage.FileChooser;
import javafx.stage.Stage;
import sorting_algorithms.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -33,8 +32,8 @@ public class MainWindow extends Application {
TextField fileLocation = new TextField();
fileLocation.setMaxWidth(250);
Button selectFileButton = new Button("Select file");
Button selectFileButton = new Button("Select file");
selectFileButton.setOnAction(event -> {
fileChooser = new FileChooser();
fileChooser.setTitle("Select CSV file");
......@@ -47,6 +46,14 @@ public class MainWindow extends Application {
}
});
Button writeToFileButton = new Button("Write results to file");
writeToFileButton.setOnAction(event -> {
if (results.isEmpty()) {
new ErrorBox("Nothing to write", "There are no results to write in file");
} else
saveToFileDialog();
});
CheckBox bubbleSortCheckBox = new CheckBox();
bubbleSortCheckBox.setText("Bubble sort");
bubbleSortCheckBox.setSelected(false);
......@@ -56,7 +63,7 @@ public class MainWindow extends Application {
sortables.add(new BubbleSort());
System.out.println("Sortable size now: " + sortables.size());
} else if (!bubbleSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals(p.getSortTypeName()));
sortables.removeIf(p -> p.getSortTypeName().equals("Bubble sort"));
System.out.println("Sortable size now: " + sortables.size());
}
});
......@@ -70,7 +77,7 @@ public class MainWindow extends Application {
sortables.add(new SelectionSort());
System.out.println("Sortable size now: " + sortables.size());
} else if (!selectionSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals(p.getSortTypeName()));
sortables.removeIf(p -> p.getSortTypeName().equals("Selection sort"));
System.out.println("Sortable size now: " + sortables.size());
}
});
......@@ -85,7 +92,7 @@ public class MainWindow extends Application {
System.out.println("Sortable size now: " + sortables.size());
} else if (!insertionSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals(p.getSortTypeName()));
sortables.removeIf(p -> p.getSortTypeName().equals("Insertion sort"));
System.out.println("Sortable size now: " + sortables.size());
}
});
......@@ -100,7 +107,7 @@ public class MainWindow extends Application {
System.out.println("Sortable size now: " + sortables.size());
} else if (!quickSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals(p.getSortTypeName()));
sortables.removeIf(p -> p.getSortTypeName().equals("Quick sort"));
System.out.println("Sortable size now: " + sortables.size());
}
});
......@@ -115,7 +122,7 @@ public class MainWindow extends Application {
System.out.println("Sortable size now: " + sortables.size());
} else if (!mergeSortCheckBox.isSelected()) {
sortables.removeIf(p -> p.getSortTypeName().equals(p.getSortTypeName()));
sortables.removeIf(p -> p.getSortTypeName().equals("Merge sort"));
System.out.println("Sortable size now: " + sortables.size());
}
});
......@@ -171,6 +178,7 @@ public class MainWindow extends Application {
result.setSortTypeName(sortable.getSortTypeName());
result.setArraySize(ar);
table.getItems().add(result);
results.add(result);
}
}
});
......@@ -186,7 +194,8 @@ public class MainWindow extends Application {
quickSortCheckBox,
mergeSortCheckBox,
startButton,
table);
table,
writeToFileButton);
Scene mainScene = new Scene(layout, 700, 500);
......@@ -198,5 +207,37 @@ public class MainWindow extends Application {
private ObservableList<Result> getResults(ArrayList<Result> resultArrayList) {
return FXCollections.observableArrayList();
}
private void writeResultsToFile(String fileName, File file) throws IOException {
//FileOutputStream fileOutputStream = new FileOutputStream(fileName);
//DataOutputStream dataOutputStream = new DataOutputStream(file);
PrintWriter writer = new PrintWriter(file);
String result = "";
for (Result anResult : results) {
result = "Sort type: " + anResult.getSortTypeName() + " \t"
+ "Array size " + anResult.getArraySize() + " \t"
+ "Time(mills): " + anResult.getExecutionResultTime() + "\n";
writer.println(result);
}
writer.close();
}
private void saveToFileDialog() {
FileChooser saveFileChooser = new FileChooser();
FileChooser.ExtensionFilter extensionFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
saveFileChooser.getExtensionFilters().add(extensionFilter);
File file = saveFileChooser.showSaveDialog(new Stage());
if (file != null) {
try {
writeResultsToFile(saveFileChooser.getInitialFileName(), file);
} catch (IOException e) {
new ErrorBox("Sorry something went wrong!", "Could not write results to file");
e.printStackTrace();
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment