Commit 2bccdc3e authored by edmundszagars's avatar edmundszagars
Browse files

Implementing Strategy pattern.

Adding listeners to check boxes
Check if user has selected sort algorithm
parent f9c5bb2d
......@@ -3,8 +3,6 @@ package gui;
import helper.GetDataFromCSV;
import helper.Result;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
......@@ -14,18 +12,23 @@ import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import sorting_algorithms.BubbleSort;
import sorting_algorithms.QuickSort;
import sorting_algorithms.Sortable;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
public class MainWindow extends Application {
private FileChooser fileChooser = new FileChooser();
private FileChooser fileChooser;
private String filePath;
private ArrayList list;
private TableView<Result> table;
private ArrayList<Result> results = new ArrayList<>();
private ArrayList<Sortable> sortables = new ArrayList<>();
@Override
public void start(Stage primaryStage) {
......@@ -36,6 +39,7 @@ public class MainWindow extends Application {
Button selectFileButton = new Button("Select file");
selectFileButton.setOnAction(event -> {
fileChooser = new FileChooser();
fileChooser.setTitle("Select CSV file");
FileChooser.ExtensionFilter csvFilter = new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv");
fileChooser.getExtensionFilters().add(csvFilter);
......@@ -48,27 +52,37 @@ public class MainWindow extends Application {
CheckBox bubbleSortCheckBox = new CheckBox();
bubbleSortCheckBox.setText("Bubble sort");
bubbleSortCheckBox.setSelected(true);
bubbleSortCheckBox.setSelected(false);
bubbleSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if(bubbleSortCheckBox.isSelected())
System.out.println("Bubble selected");
if (bubbleSortCheckBox.isSelected()) {
System.out.println("Bubble selected");
sortables.add(new BubbleSort());
}else if (!bubbleSortCheckBox.isSelected()){
System.out.println("BS unselected");
}
});
CheckBox selectionSortCheckBox = new CheckBox();
selectionSortCheckBox.setText("Selection sort");
selectionSortCheckBox.setSelected(true);
selectionSortCheckBox.setSelected(false);
CheckBox insertionSortCheckBox = new CheckBox();
insertionSortCheckBox.setText("Insertion sort");
insertionSortCheckBox.setSelected(true);
insertionSortCheckBox.setSelected(false);
CheckBox quickSortCheckBox = new CheckBox();
quickSortCheckBox.setText("Quick sort");
quickSortCheckBox.setSelected(true);
quickSortCheckBox.setSelected(false);
quickSortCheckBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (quickSortCheckBox.isSelected()) {
System.out.println("Quick sort selected");
sortables.add(new QuickSort());
}
});
CheckBox mergeSortCheckBox = new CheckBox();
mergeSortCheckBox.setText("Merge sort");
mergeSortCheckBox.setSelected(true);
mergeSortCheckBox.setSelected(false);
TableColumn<Result, Double> dataColumn = new TableColumn<>("Data");
dataColumn.setMinWidth(300);
......@@ -84,11 +98,15 @@ public class MainWindow extends Application {
table = new TableView<>();
table.setItems(getResults(results));
table.getColumns().addAll(dataColumn, resultDataColumn, timeColumn);
table.getColumns().addAll(Arrays.asList(dataColumn, resultDataColumn, timeColumn));
Button startButton = new Button("Run tests");
startButton.setOnAction(event -> {
if(fileLocation.getText().equals("")){
System.out.println("Displaying error box");
return;
}
GetDataFromCSV dataFromCSV = new GetDataFromCSV();
dataFromCSV.setnFileLocation(filePath);
try {
......@@ -96,21 +114,15 @@ public class MainWindow extends Application {
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (bubbleSortCheckBox.isSelected()) {
Result anResult = new Result();
BubbleSort bs = new BubbleSort();
double[] ar = dataFromCSV.getDoubleArrayFromListItem((ArrayList) list.get(0));
anResult.setStartDataString(ar);
double[] resultArray = bs.sort(ar);
long tree = bs.getExecutionStartTime();
long end = bs.getExecutionEndTime();
anResult.setResultDataString(resultArray);
long free = bs.getExecutionEndTime();
anResult.setExecutionResultTime(free);
long tt = anResult.getExecutionResultTime();
table.getItems().add(anResult);
for (Object aList : list) {
for (Sortable sortable : sortables) {
Result result = new Result();
double[] ar = dataFromCSV.getDoubleArrayFromListItem((ArrayList) aList);
sortable.sort(ar);
result.setExecutionResultTime(sortable.getExecutionEndTime());
table.getItems().add(result);
System.out.println(sortable.getClass() + "\n" + Arrays.toString(ar));
}
}
});
......@@ -133,7 +145,7 @@ public class MainWindow extends Application {
primaryStage.show();
}
public ObservableList<Result> getResults(ArrayList<Result> resultArrayList) {
private ObservableList<Result> getResults(ArrayList<Result> resultArrayList) {
return FXCollections.observableArrayList();
}
}
......
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