Skip to content

Commit 2e2b19d

Browse files
committed
Use switch case instead of elseif cascade for algorithm selection
1 parent f96b45f commit 2e2b19d

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

src/main/java/de/moritzf/sorting/gui/windows/AlgorithmSelection.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.swing.JLabel;
1616
import javax.swing.JOptionPane;
1717
import javax.swing.JPanel;
18+
1819
import de.moritzf.sorting.logic.sorting.*;
1920
import org.jdesktop.swingx.JXTextField;
2021

@@ -209,8 +210,6 @@ public void actionPerformed(ActionEvent e) {
209210
new RandomArrayGeneratorWindow(this);
210211
} else if (e.getSource().equals(aboutButton)) {
211212
new AboutWindow(this);
212-
} else if (e.getSource().equals(algorithmSelectionBox)) {
213-
214213
}
215214

216215
}
@@ -241,27 +240,39 @@ private void handleStart() {
241240
+ "<br>just tried to cause a software crash ;)</html>",
242241
"Error: Are you serious?", JOptionPane.INFORMATION_MESSAGE);
243242
} else {
244-
if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.BUBBLE_SORT)) {
245-
new SortingWindow(this, new BubbleSort(input));
246-
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.HEAP_SORT_MAX)) {
247-
new HeapWindow(this, new HeapSort(input, false));
248-
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.HEAP_SORT_MIN)) {
249-
new HeapWindow(this, new HeapSort(input, true));
250-
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.QUICK_SORT)) {
251-
new SortingWindow(this, new QuickSort(input));
252-
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.RADIX_SORT)) {
253-
new SortingWindow(this, new RadixSort(input));
254-
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.SELECTION_SORT)) {
255-
new SortingWindow(this, new SelectionSort(input));
256-
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.INSERTION_SORT)) {
257-
new SortingWindow(this, new InsertionSort(input));
258-
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.SHELL_SORT_2N)) {
259-
new SortingWindow(this, new ShellSort(input));
260-
} else {
261-
JOptionPane.showMessageDialog(this,
262-
this.algorithmSelectionBox.getSelectedItem()
263-
+ " should start now. This algorithm has however not been implemented yet.",
264-
"Developer was lazy", JOptionPane.INFORMATION_MESSAGE);
243+
SelectionItem selection = (SelectionItem) this.algorithmSelectionBox.getSelectedItem();
244+
245+
switch (selection) {
246+
case BUBBLE_SORT:
247+
new SortingWindow(this, new BubbleSort(input));
248+
break;
249+
case HEAP_SORT_MAX:
250+
new HeapWindow(this, new HeapSort(input, false));
251+
break;
252+
case HEAP_SORT_MIN:
253+
new HeapWindow(this, new HeapSort(input, true));
254+
break;
255+
case QUICK_SORT:
256+
new SortingWindow(this, new QuickSort(input));
257+
break;
258+
case RADIX_SORT:
259+
new SortingWindow(this, new RadixSort(input));
260+
break;
261+
case SELECTION_SORT:
262+
new SortingWindow(this, new SelectionSort(input));
263+
break;
264+
case INSERTION_SORT:
265+
new SortingWindow(this, new InsertionSort(input));
266+
break;
267+
case SHELL_SORT_2N:
268+
new SortingWindow(this, new ShellSort(input));
269+
break;
270+
default:
271+
JOptionPane.showMessageDialog(this,
272+
this.algorithmSelectionBox.getSelectedItem()
273+
+ " should start now. This algorithm has however not been implemented yet.",
274+
"Developer was lazy", JOptionPane.INFORMATION_MESSAGE);
275+
break;
265276
}
266277
}
267278
}
@@ -305,7 +316,7 @@ public void keyTyped(KeyEvent e) {
305316
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
306317
*/
307318
public void keyPressed(KeyEvent e) {
308-
// TODO Auto-generated method stub
319+
// nothing to do here
309320

310321
}
311322

@@ -315,7 +326,7 @@ public void keyPressed(KeyEvent e) {
315326
* @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
316327
*/
317328
public void keyReleased(KeyEvent e) {
318-
// TODO Auto-generated method stub
329+
// nothing to do here
319330

320331
}
321332

@@ -329,8 +340,7 @@ private enum SelectionItem {
329340
HEAP_SORT_MIN("Heapsort (Minheap)"),
330341
HEAP_SORT_MAX("Heapsort (Maxheap)"),
331342
INSERTION_SORT("Insertionsort"),
332-
SHELL_SORT_2N("Shellsort ( steplengths=[... 8,4,2,1] )"),
333-
SHELL_SORT_CUSTOM("Shellsort (custom steplengths)");
343+
SHELL_SORT_2N("Shellsort ( steplengths=[... 8,4,2,1] )");
334344

335345
private final String selectionValue;
336346

src/main/resources/de/moritzf/sorting/logic/sorting/shellsort.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ \chapter*{Shellsort}
3030

3131
\texttt{array}: array with numbers that are to be sorted
3232

33-
\texttt{steplengths}: array with steplengths that are to be used. In our definition, steplengths are arranged in descending order (eg. steplengths = [8,4,2,1]). A practical steplengths-array must further fulfill the condition $steplengths[1] < array.length$.
33+
\texttt{steplengths}: array with steplengths that are to be used. In our definition, steplengths are arranged in descending order (eg. steplengths = [8,4,2,1]). A practical steplengths-array must further fulfill the conditions $steplengths[1] < array.length$ and $steplengths[steplengths.length] == 1$.
3434

3535

3636
\begin{lstlisting}

0 commit comments

Comments
 (0)