Skip to content

Commit dfc33e4

Browse files
authored
Merge pull request #4 from YingxuH/branch-A-CodeQuality
Branch a code quality
2 parents f00cb45 + 94f9b8e commit dfc33e4

File tree

12 files changed

+170
-98
lines changed

12 files changed

+170
-98
lines changed

src/main/java/Duke.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
//package java;
22

3-
import exceptions.IllegalDateTimeFormatException;
43
import parser.Command;
54
import parser.ExitCommand;
65
import parser.Parser;
6+
7+
import exceptions.IllegalDateTimeFormatException;
78
import exceptions.InvalidStorageFilePathException;
89
import exceptions.NoCommandException;
910
import exceptions.StorageOperationException;
11+
import exceptions.NoDescriptionException;
12+
1013
import model.TaskList;
1114

12-
import exceptions.NoDescriptionException;
1315
import storage.Storage;
1416

1517
import java.io.IOException;
@@ -27,7 +29,7 @@ public class Duke {
2729
protected Ui ui;
2830

2931
public Duke() {
30-
this.userName = "";
32+
3133
}
3234

3335
/**
@@ -39,11 +41,11 @@ public Duke(String userName) {
3941
}
4042

4143
private void start() {
42-
this.ui = new Ui();
44+
ui = new Ui();
4345
try {
44-
this.parser = new Parser();
45-
this.storage = new Storage();
46-
this.taskList = storage.load();
46+
parser = new Parser();
47+
storage = new Storage();
48+
taskList = storage.load();
4749
ui.askForName();
4850
ui.greet();
4951
} catch (InvalidStorageFilePathException | IOException e) {
@@ -55,26 +57,17 @@ private void start() {
5557
}
5658

5759
/**
58-
* exit with status 0.
60+
* Listen to the user input and take actions.
5961
*/
60-
private void exit() {
61-
System.exit(0);
62-
}
63-
64-
/**
65-
* Listen to the user input and start interactions.
66-
*/
67-
private void run() {
62+
private void listen() {
6863
Command command = new Command();
69-
this.start();
7064

7165
while (!ExitCommand.isExit(command)) {
7266
try {
7367
String input = ui.getUserInput();
7468
command = parser.parseCommand(input);
75-
command.setTaskList(this.taskList);
76-
String commandResult = command.execute();
77-
ui.printCommandResult(commandResult);
69+
command.setTaskList(taskList);
70+
ui.printCommandResult(command.execute());
7871
storage.save(taskList);
7972
} catch (NoDescriptionException | NoCommandException | IllegalDateTimeFormatException e) {
8073
ui.printErrorMessage(e.getMessage());
@@ -83,7 +76,19 @@ private void run() {
8376
throw new RuntimeException(e);
8477
}
8578
}
86-
this.exit();
79+
}
80+
81+
/**
82+
* exit with status 0.
83+
*/
84+
private void exit() {
85+
System.exit(0);
86+
}
87+
88+
private void run() {
89+
start();
90+
listen();
91+
exit();
8792
}
8893

8994
public static void main(String[] args) {

src/main/java/exceptions/NoTimeException.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/model/DeadLineTask.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
* Presents the deadline task in the task list.
1010
*/
1111
public class DeadLineTask extends Task {
12+
public static final String TASK_TYPE_CHA = "D";
13+
1214
private static final String TASK_TYPE_STRING = "deadline task";
13-
private static final String TASK_TYPE_CHA = "D";
1415

1516
private LocalDateTime by;
1617

@@ -29,6 +30,18 @@ public DeadLineTask(String description, LocalDateTime by) throws NoDescriptionEx
2930
this.by = by;
3031
}
3132

33+
/**
34+
* Constructs an {@code DeadLineTask}.
35+
* @param description a not empty description.
36+
* @param by a valid date time object.
37+
* @param isDone A boolean indicating whether the task is done.
38+
* @throws NoDescriptionException if the description is empty.
39+
*/
40+
public DeadLineTask(String description, LocalDateTime by, boolean isDone) throws NoDescriptionException {
41+
super(description, TASK_TYPE_STRING, isDone);
42+
this.by = by;
43+
}
44+
3245
/**
3346
* get and return the single-character task type. E.g. "E", "T", or "D".
3447
* @return TASK_TYPE_CHA single-character task type.

src/main/java/model/EventTask.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
* Represents an event task in the inner-task list.
1212
*/
1313
public class EventTask extends Task {
14+
public static final String TASK_TYPE_CHA = "E";
15+
1416
private static final String TASK_TYPE_STRING = "event task";
15-
private static final String TASK_TYPE_CHA = "E";
1617

1718
private LocalDateTime at;
1819

@@ -31,6 +32,18 @@ public EventTask(String description, LocalDateTime at) throws NoDescriptionExcep
3132
this.at = at;
3233
}
3334

35+
/**
36+
* Constructs an {@code EventTask} with description, dateTime, and isDone status.
37+
* @param description A not empty description.
38+
* @param at A valid datetime object.
39+
* @param isDone A boolean indicating whether the task is done.
40+
* @throws NoDescriptionException if the description is empty.
41+
*/
42+
public EventTask(String description, LocalDateTime at, boolean isDone) throws NoDescriptionException {
43+
super(description, TASK_TYPE_STRING, isDone);
44+
this.at = at;
45+
}
46+
3447
/**
3548
* get and return the single-character task type. E.g. "E", "T", or "D".
3649
* @return TASK_TYPE_CHA single-character task type.

src/main/java/model/Task.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import exceptions.NoDescriptionException;
44

5+
import java.time.LocalDateTime;
56
import java.util.ArrayList;
67
import java.util.Arrays;
78
import java.time.format.DateTimeFormatter;
@@ -12,12 +13,14 @@
1213
*/
1314
public abstract class Task {
1415
protected String description;
15-
private String taskTypeString;
1616
protected boolean isDone;
17+
private String taskTypeString;
1718

18-
private static final String DEFAULT_TASK_TYPE_STRING = "task";
1919
static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
2020

21+
private static final String DEFAULT_TASK_TYPE_STRING = "task";
22+
private static final boolean DEFAULT_TASK_IS_DONE = false;
23+
2124
public Task() {
2225

2326
}
@@ -28,14 +31,7 @@ public Task() {
2831
* @throws NoDescriptionException If the description is empty.
2932
*/
3033
public Task(String description) throws NoDescriptionException {
31-
this.taskTypeString = DEFAULT_TASK_TYPE_STRING;
32-
if ("".equals(description)) {
33-
throw new NoDescriptionException("OOPS!!! The description of a "
34-
+ taskTypeString
35-
+ " cannot be empty.\n");
36-
}
37-
this.description = description;
38-
this.isDone = false;
34+
this(description, DEFAULT_TASK_TYPE_STRING, DEFAULT_TASK_IS_DONE);
3935
}
4036

4137
/**
@@ -45,14 +41,25 @@ public Task(String description) throws NoDescriptionException {
4541
* @throws NoDescriptionException If the description is empty.
4642
*/
4743
public Task(String description, String taskTypeString) throws NoDescriptionException {
44+
this(description, taskTypeString, DEFAULT_TASK_IS_DONE);
45+
}
46+
47+
/**
48+
* Constructs a {@code Task} with the input description, task type string, and task status.
49+
* @param description A not empty description.
50+
* @param taskTypeString A not empty task type string.
51+
* @param taskIsDone A boolean object indicating whether the task is done.
52+
* @throws NoDescriptionException If the description is empty.
53+
*/
54+
public Task(String description, String taskTypeString, boolean taskIsDone) throws NoDescriptionException {
4855
this.taskTypeString = taskTypeString;
4956
if ("".equals(description)) {
5057
throw new NoDescriptionException("OOPS!!! The description of a "
5158
+ this.taskTypeString
5259
+ " cannot be empty.\n");
5360
}
5461
this.description = description;
55-
this.isDone = false;
62+
this.isDone = taskIsDone;
5663
}
5764

5865
/**

src/main/java/model/TaskList.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class TaskList implements Iterable<Task> {
1616
private static final String ECHO_DELETE_TASK = "Noted. I have removed this task:\n";
1717
private static final String ECHO_VIEW_TASK_LIST = "Here are the tasks in your list:\n";
1818
private static final String ECHO_COMPLETE_TASK = "Nice! I've marked this task as done:\n";
19-
private final String ECHO_FIND_TASK = "Here are the matching tasks in your list:\n";
19+
private static final String ECHO_FIND_TASK = "Here are the matching tasks in your list:\n";
2020

2121
public TaskList() {
2222

@@ -113,18 +113,17 @@ public String findByKeyWord(String keyWord) {
113113
* @return string representation of the task list.
114114
*/
115115
public String toString() {
116-
String listOverView = ECHO_VIEW_TASK_LIST;
116+
StringBuilder listOverView = new StringBuilder(ECHO_VIEW_TASK_LIST);
117117
for (int i = 0; i < this.internalList.size(); i++) {
118118
if (this.internalList.get(i) == null) {
119119
continue;
120120
}
121-
listOverView = listOverView
122-
+ Integer.toString(i + 1)
123-
+ "."
124-
+ this.internalList.get(i).toString()
125-
+ "\n";
121+
listOverView.append(Integer.toString(i + 1));
122+
listOverView.append(".");
123+
listOverView.append(this.internalList.get(i).toString());
124+
listOverView.append("\n");
126125
}
127-
return listOverView;
126+
return listOverView.toString();
128127
}
129128

130129
/**

src/main/java/model/ToDoTask.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* Represents an todo task in the inner-task list.
77
*/
88
public class ToDoTask extends Task {
9+
public static final String TASK_TYPE_CHA = "T";
10+
911
private static final String TASK_TYPE_STRING = "todo task";
10-
private static final String TASK_TYPE_CHA = "T";
1112

1213
public ToDoTask() {
1314

@@ -22,6 +23,16 @@ public ToDoTask(String description) throws NoDescriptionException {
2223
super(description, TASK_TYPE_STRING);
2324
}
2425

26+
/**
27+
* Constructs an {@code ToDoTask} with input description and isDone status.
28+
* @param description A not empty description.
29+
* @param isDone A boolean indicating whether the task is done.
30+
* @throws NoDescriptionException if the description is empty.
31+
*/
32+
public ToDoTask(String description, boolean isDone) throws NoDescriptionException {
33+
super(description, TASK_TYPE_STRING, isDone);
34+
}
35+
2536
/**
2637
* Convert the details of the task, such as description, deadline to one arraylist
2738
* for encoder to encode in a string.

src/main/java/parser/Command.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
/**
77
* Abstract class implementing part of the command object.
88
*/
9-
public class Command {
9+
public abstract class Command {
1010
protected TaskList taskList;
1111

1212
public Command() {
1313

1414
}
1515

16-
public void setTaskList(TaskList taskList) {
16+
public Command(TaskList taskList) {
1717
this.taskList = taskList;
1818
}
1919

20-
public String execute() throws NoDescriptionException {
21-
return "";
20+
public void setTaskList(TaskList taskList) {
21+
this.taskList = taskList;
2222
}
23+
24+
public abstract String execute() throws NoDescriptionException;
2325
}

0 commit comments

Comments
 (0)