Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bingyuan] iP #230

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Empty file added duke.txt
Empty file.
22 changes: 22 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Deadline extends Task {
public String due;
public String[] info;
public String deadlineName;

public Deadline(String description) {
super(description);
this.info = this.description.split("/by", 2);
this.deadlineName = info[0];
this.due = info[1];
}

@Override
public String fileFormat() {
return (String.format("D|%b|%s by %s", super.isDone, this.deadlineName, this.due));
}

@Override
public String toString() {
return ("[D][" + super.getStatusIcon() + "] " + this.deadlineName) + " (by: " + this.due + ")";
}
}
147 changes: 146 additions & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,155 @@
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Duke {
public static void main(String[] args) {

private List<Task> taskList = new ArrayList<Task>();
private static String fileName = "duke.txt";

public void greet() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);

System.out.println("Hello! I'm Duke");
pullFileData();
System.out.println("What can I do for you?");
}

// public void addTask(String taskName){
// Task t = new Task(taskName);
// taskList.add(t);
// System.out.printf(
// "Got it. I've added this task:\n" +
// String.format("added: %s\n", taskName)

// );
// }

public void addEvent(String taskName) {
Event t = new Event(taskName);
taskList.add(t);
System.out.printf(
"Got it. I've added this task:\n" +
t.toString() +
String.format("\nNow you have %d tasks in the list.\n", taskList.size()));
}

public void addTodo(String taskName) {
Todo t = new Todo(taskName);
taskList.add(t);
System.out.printf(
"Got it. I've added this task:\n" +
t.toString() +
String.format("\nNow you have %d tasks in the list.\n", taskList.size()));
}

public void addDeadline(String taskName) {
Deadline t = new Deadline(taskName);
taskList.add(t);
System.out.printf(
"Got it. I've added this task:\n" +
t.toString() +
String.format("\nNow you have %d tasks in the list.\n", taskList.size()));
}

public void changeTaskState(boolean doneState, Integer index) {
if (index > taskList.size()) {
System.out.println("Please input valid task number!");
} else {
index--;
if (doneState) {
taskList.get(index).markAsDone();
} else {
taskList.get(index).markAsUndone();
}
}
}

public void delete(int index) {
if (index > taskList.size()) {
System.out.println("Please input valid task number!");
} else {
index--;
System.out.printf("Noted. I've removed this task:" +
taskList.get(index).toString());
taskList.remove(index);
System.out.println(String.format("\nNow you have %d tasks in the list.\n", taskList.size()));
}
}

public void list() {
if (taskList.size() == 0) {
System.out.println("Task list is empty.");
} else {
System.out.println("Here are the tasks in your list:");
Integer i = 0;
for (Task task : taskList) {
System.out.printf(String.format("%d.%s\n", i + 1, task.toString()));
i++;
}
}
}

private void pullFileData() {
File file = new File(fileName);
String data;
try {
if (!file.createNewFile()) {
Scanner fileData = new Scanner(file);
while (fileData.hasNext()) {
data = fileData.nextLine();
String[] inputArgs = data.split("|");
addFileData(inputArgs);
}
fileData.close();
}
} catch (IOException e) {
System.out.print("\nError getting file data");
}
System.out.println("These are the tasks from your file:\n");
list();
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try-catch should have a finally clause defined to mark the relevant section of code (in this case 117-118) that runs regardless of the outcome of try catch


private void addFileData(String[] inputArgs) {
Task newTask;
String command = inputArgs[0];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do consider renaming inputArgs to its full form for better readability (ie. inputArguments)

boolean taskStatus = Boolean.parseBoolean(inputArgs[1]);
switch (command) {
case "T":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case line should not be tabbed (ie. on the same level as switch)

newTask = new Todo(inputArgs[2]);
break;
case "D":
newTask = new Deadline(inputArgs[2]);
break;
case "E":
newTask = new Event(inputArgs[2]);
break;
default:
throw new IllegalStateException("File contents are invalid");
}
if (taskStatus) {
newTask.markAsDone();
}
taskList.add(newTask);
}

public void saveToFile() {
try {
FileWriter fWriter = new FileWriter(fileName);
for (Task task : taskList) {
fWriter.write(task.fileFormat());
}
fWriter.close();
} catch (IOException e) {
System.out.print("IOException Error: data not saved to file\n");
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Event extends Task {
public String startTime, endTime, eventName;
public String[] info;

public Event(String description) {
super(description);
this.info = this.description.split("/from", 2);
this.eventName = info[0];
this.startTime = info[1].split("/to", 2)[0];
this.endTime = info[1].split("/to", 2)[1];
}

@Override
public String fileFormat() {
return (String.format("E|%b|%s /from %s /to %s", super.isDone, this.eventName, this.startTime, this.endTime));
}

@Override
public String toString() {
return ("[E][" + super.getStatusIcon() + "] " + this.eventName) +
" (from: " + this.startTime + " to: " + this.endTime + ")";
}
}
47 changes: 47 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Duke Duke = new Duke();
Duke.greet();
Scanner userInput = new Scanner(System.in); // create Scanner object
String inputCommand; // read user input
String[] commandPhrase;
String command, phrase;
while (true) {
inputCommand = userInput.nextLine();
commandPhrase = inputCommand.split(" ", 2);
command = commandPhrase[0];
if (commandPhrase.length < 2) {
if (command.equals("bye")) {
break;
} else if (command.equals("list")) {
Duke.list();
} else {
System.out.println("Invalid command");
}
} else {
phrase = commandPhrase[1];
if (command.equals("mark")) {
Duke.changeTaskState(true, Integer.parseInt(phrase));
} else if (command.equals("unmark")) {
Duke.changeTaskState(false, Integer.parseInt(phrase));
} else if (command.equals("event")) {
Duke.addEvent(phrase);
} else if (command.equals("todo")) {
Duke.addTodo(phrase);
} else if (command.equals("deadline")) {
Duke.addDeadline(phrase);
} else if (command.equals("delete")) {
Duke.delete(Integer.parseInt(phrase));
} else {
System.out.println("Invalid command");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main seems to be overloaded with a lot of functionality, perhaps consider abstracting behaviour such as parsing command (like this block) into another method?

}
Duke.saveToFile();
System.out.println("------------------------------------");
}
userInput.close();
System.out.println("Bye. Hope to see you again soon!");
}
}
36 changes: 36 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public class Task {
protected String description;
protected boolean isDone;

public Task(String description) {
this.description = description;
this.isDone = false;
}

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}
Comment on lines +14 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do consider replacing such string literals with named constants, it will help in enhancing readability of your code


public void markAsDone() {
this.isDone = true;
System.out.println("Nice! I've marked this task as done:");
System.out.println(this.toString());
}

public void markAsUndone() {
this.isDone = false;
System.out.println("OK, I've marked this task as not done yet:");
System.out.println(this.toString());
}

public String fileFormat() {
return (String.format(" |%b|%s", this.isDone, this.description));
}

@Override
public String toString() {
return String.format("[%s] %s", this.getStatusIcon(), this.description);
}

// ...
}
15 changes: 15 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Todo extends Task {
public Todo(String description) {
super(description);
}

@Override
public String toString() {
return ("[T][" + super.getStatusIcon() + "] " + super.description);
}

@Override
public String fileFormat() {
return (String.format("T|%b|%s", super.isDone, this.description));
}
}