Created
July 26, 2019 11:33
-
-
Save mustafa-zidan/e1369bd20401bdc3cd6ccc418cb1d5ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hawkai; | |
import java.util.*; | |
/* | |
* Refactor following code to get clean, readable code without coding smells. | |
* | |
* Hints: | |
* There is nothing wrong with this code in terms of functionality. | |
* Think of yourself as contributing to the 'clean code' chapter of a | |
* book about programming. The code above is the bad example at the beginning. | |
* With your refactored example, you should show the reader the difference between | |
* code that is working and code that is clean, maintainable and unlikely to introduce bugs | |
*/ | |
public class SortArray { | |
private static final Integer INPUT_SIZE = 5; | |
public static void main(String[] args) { | |
List<Integer> nums = readInput(); | |
System.out.printf("Normal List: %s%n", nums); | |
/* | |
* There is no need to implement our own sorting specially | |
* for something as simple as integer. | |
* | |
* The sort method can be replaced simply by {@link Collections.sort} | |
* since it does check the list size and decide which type | |
* of sort is going to be used | |
*/ | |
System.out.printf("Sorted List: %s%n", sort(nums)); | |
} | |
/** | |
* Read Integer from console into {@link List<Integer>} | |
* | |
* @return Unsorted {@link List<Integer>} | |
*/ | |
private static List<Integer> readInput() { | |
Scanner in = new Scanner(System.in); | |
List<Integer> nums = new ArrayList<>(); | |
System.out.printf("Enter %d numbers:%n", INPUT_SIZE); | |
while (nums.size() < INPUT_SIZE) { | |
append(nums, in); | |
} | |
return nums; | |
} | |
/** | |
* Append next element in the list in case of non integer input found it will be ignored | |
* | |
* @param nums list of numbers to append the next input | |
* @param in input scanner to read the next integer from | |
*/ | |
private static void append(List<Integer> nums, Scanner in) { | |
try { | |
nums.add(in.nextInt()); | |
} catch (NoSuchElementException | IllegalStateException e) { | |
System.out.printf("Error while parsing Input %s%n please enter an integer:%n", e.getMessage()); | |
} | |
} | |
/** | |
* Insertion Sort implementation, not the best option but it works well | |
* smaller data sets | |
* | |
* @param nums @see {@link List<Integer>} input un sorted array | |
* @return Sorted @see {@link List<Integer>} | |
*/ | |
private static List<Integer> sort(List<Integer> nums) { | |
for (int i = 0; i < nums.size(); i++) { | |
for (int j = 0; j < nums.size() - 1; j++) { | |
if (nums.get(j).compareTo(nums.get(j + 1)) > 0) { | |
Collections.swap(nums, j, j + 1); | |
} | |
} | |
} | |
return nums; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment