Refactor string and integer comparisons in Task class, and add test data for different task titles.

This commit is contained in:
Abbas Al-younis
2023-11-02 21:56:47 -06:00
parent f9cfe8fbd2
commit 9527b75935
2 changed files with 10 additions and 8 deletions
+7 -6
View File
@@ -63,16 +63,17 @@ public class Task {
int length = Math.min(tokens1.length, tokens2.length); int length = Math.min(tokens1.length, tokens2.length);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (Character.isLetter(tokens1[i].charAt(0)) && Character.isLetter(tokens2[i].charAt(0))) { if (Character.isDigit(tokens1[i].charAt(0)) && Character.isDigit(tokens2[i].charAt(0))) {
int stringCompare = tokens1[i].compareTo(tokens2[i]);
if (stringCompare != 0)
return stringCompare;
} else {
int intCompare = Integer.compare(Integer.parseInt(tokens1[i]), Integer.parseInt(tokens2[i])); int intCompare = Integer.compare(Integer.parseInt(tokens1[i]), Integer.parseInt(tokens2[i]));
if (intCompare != 0) if (intCompare != 0)
return intCompare; return intCompare;
} else {
int stringCompare = tokens1[i].compareTo(tokens2[i]);
if (stringCompare != 0)
return stringCompare;
} }
} }
+1
View File
@@ -154,6 +154,7 @@ public class Week9 {
// Test tasks, not read from a file and are not intended to be saved to a file. // Test tasks, not read from a file and are not intended to be saved to a file.
private void testData() { private void testData() {
tasks.add(new Task("Title 1000", "Description ", "Due Date ")); tasks.add(new Task("Title 1000", "Description ", "Due Date "));
tasks.add(new Task("100 Title", "Description ", "Due Date "));
for (int i = 20; i > 0; i--) for (int i = 20; i > 0; i--)
tasks.add(new Task("Title " + i, "Description " + i, "Due Date " + i)); tasks.add(new Task("Title " + i, "Description " + i, "Due Date " + i));
} }