From 9527b75935e294c81c80f88ac5a14413c9359ae2 Mon Sep 17 00:00:00 2001 From: Abbas Al-younis Date: Thu, 2 Nov 2023 21:56:47 -0600 Subject: [PATCH] Refactor string and integer comparisons in Task class, and add test data for different task titles. --- Task.java | 17 +++++++++-------- Week9.java | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Task.java b/Task.java index 9f52da4..1a58c3c 100644 --- a/Task.java +++ b/Task.java @@ -63,16 +63,17 @@ public class Task { int length = Math.min(tokens1.length, tokens2.length); for (int i = 0; i < length; i++) { - if (Character.isLetter(tokens1[i].charAt(0)) && Character.isLetter(tokens2[i].charAt(0))) { - int stringCompare = tokens1[i].compareTo(tokens2[i]); - - if (stringCompare != 0) - return stringCompare; - } else { + if (Character.isDigit(tokens1[i].charAt(0)) && Character.isDigit(tokens2[i].charAt(0))) { int intCompare = Integer.compare(Integer.parseInt(tokens1[i]), Integer.parseInt(tokens2[i])); - - if (intCompare != 0) + + if (intCompare != 0) return intCompare; + } else { + + int stringCompare = tokens1[i].compareTo(tokens2[i]); + + if (stringCompare != 0) + return stringCompare; } } diff --git a/Week9.java b/Week9.java index fe76193..f1b60ce 100644 --- a/Week9.java +++ b/Week9.java @@ -154,6 +154,7 @@ public class Week9 { // Test tasks, not read from a file and are not intended to be saved to a file. private void testData() { 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--) tasks.add(new Task("Title " + i, "Description " + i, "Due Date " + i)); }