Skip to content

Commit

Permalink
Merge pull request #1 from DavidTan0527/branch-A-CodeQuality
Browse files Browse the repository at this point in the history
Adapt code to Code Quality standard
  • Loading branch information
DavidTan0527 authored Feb 12, 2022
2 parents af489a0 + deb09e3 commit b534a8d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 62 deletions.
35 changes: 20 additions & 15 deletions src/main/java/nikki/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public Storage(String filename) {
*/
private void createIfNotExist() throws IOException {
// Create file if it doesn't exist
if (!this.file.exists()) {
this.file.getParentFile().mkdirs();
this.file.createNewFile();
if (this.file.exists()) {
return;
}

this.file.getParentFile().mkdirs();
this.file.createNewFile();
}

/**
Expand Down Expand Up @@ -67,20 +69,23 @@ public TaskList loadTasks() throws IOException, NikkiException {

try {
Scanner fileReader = new Scanner(this.file);

TaskList taskList = new TaskList();
for (int lineCount = 1; fileReader.hasNextLine(); lineCount++) {
try {
Task task = Task.parseFileSaveFormat(fileReader.nextLine());
taskList.addTask(task);
} catch (NikkiException e) {
throw new NikkiException(String.format("Wrong format in line %d", lineCount));
}
}

return taskList;
return loadTaskListFromScanner(fileReader);
} catch (FileNotFoundException e) {
return new TaskList();
}
}

private TaskList loadTaskListFromScanner(Scanner scanner) throws NikkiException {
TaskList taskList = new TaskList();
for (int lineCount = 1; scanner.hasNextLine(); lineCount++) {
try {
Task task = Task.parseFileSaveFormat(scanner.nextLine());
taskList.addTask(task);
} catch (NikkiException e) {
throw new NikkiException(String.format("Wrong format in line %d", lineCount));
}
}

return taskList;
}
}
37 changes: 19 additions & 18 deletions src/main/java/nikki/command/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,29 @@ public static Command parseCommand(String cmd) throws NikkiException {
Pattern p = Pattern.compile(command[1]);
Matcher m = p.matcher(cmd);

if (m.matches()) {
if (m.groupCount() > 0) {
String arg = m.group(1);
HashMap<String, String> kwargs = new HashMap<>();

// kwargs start at 2,
// The last one is groupCount() - 1, its value being groupCount()
for (int i = 2; i < m.groupCount(); i += 2) {
kwargs.put(m.group(i), m.group(i + 1));
}

return new Command(cmdName, arg, kwargs);
} else {
return new Command(cmdName);
}
} else {
// Syntax error
// Syntax error
if (!m.matches()) {
throw new NikkiException(String.format(
"Woi, that's not how you do it...\n"
+ "\tUsage: %s",
+ "\tUsage: %s",
command[2]));
}

// No args or kwargs
if (m.groupCount() == 0) {
return new Command(cmdName);
}

// Match args
String arg = m.group(1);
HashMap<String, String> kwargs = new HashMap<>();

// Match kwargs
for (int i = 2; i < m.groupCount(); i += 2) {
kwargs.put(m.group(i), m.group(i + 1));
}

return new Command(cmdName, arg, kwargs);
}

throw new NikkiException("What are you trying to do??");
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/nikki/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ public Deadline(String name, LocalDate date, Boolean done) {
*/
@Override
public String nameWithStatus() {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd yyyy");

return String.format("%s (by: %s)",
super.nameWithStatus(),
this.date.format(fmt));
this.date.format(Task.DATE_FORMAT));
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/nikki/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ public Event(String name, LocalDate date, Boolean done) {
*/
@Override
public String nameWithStatus() {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd yyyy");

return String.format("%s (at: %s)",
super.nameWithStatus(),
this.date.format(fmt));
this.date.format(Task.DATE_FORMAT));
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/nikki/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import nikki.NikkiException;

import java.time.format.DateTimeFormatter;

/**
* Class to encapsulate basic properties and methods of a Task
*/
public class Task {
/** Date format for all Task object */
public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MMM dd yyyy");

/** Mark in checkbox of completed Task */
private static final char COMPLETED_MARK = 'X';

Expand Down Expand Up @@ -158,7 +163,7 @@ public static Task parseFileSaveFormat(String fmt) throws NikkiException {
throw new NikkiException("Wrong format");
}

// Extract relevant duke.task information
// Extract relevant task information
String taskTag = taskInfo[0];
Boolean taskStatus = taskInfo[1].equals("1");
String taskName = taskInfo[2];
Expand Down
36 changes: 14 additions & 22 deletions src/main/java/nikki/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ public Task removeTask(int index) throws NikkiException {
* @throws NikkiException index out of range
*/
private void checkIndex(int index) throws NikkiException {
if (index < 0 || index >= this.size()) {
throw new NikkiException(
this.size() == 0
? "List is empty"
: String.format("Please choose tasks from 1 to %d", this.size())
);
if (index >= 0 && index < this.size()) {
return;
}

throw new NikkiException(
this.size() == 0
? "List is empty"
: String.format("Please choose tasks from 1 to %d", this.size())
);
}

/**
Expand Down Expand Up @@ -121,12 +123,7 @@ public String listFileSaveFormat() {
String result = "";

for (int i = 0; i < this.size(); i++) {
try {
result += this.getTask(i).toFileSaveFormat();
} catch (NikkiException e) {
// This won't happen, given the bounds of the for-loop
assert false;
}
result += this.tasks.get(i).toFileSaveFormat();

if (i != this.size() - 1) {
result += "\n";
Expand Down Expand Up @@ -164,16 +161,11 @@ public String toString() {
String result = "";

for (int i = 0; i < this.size(); i++) {
try {
Task current = this.getTask(i);
result += String.format(
"%3d. %s",
i + 1,
current.nameWithStatus());
} catch (NikkiException e) {
// This won't happen, given the bounds of the for-loop
assert false;
}
Task current = this.tasks.get(i);
result += String.format(
"%3d. %s",
i + 1,
current.nameWithStatus());

if (i != this.size() - 1) {
result += "\n";
Expand Down

0 comments on commit b534a8d

Please sign in to comment.