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

[Bian Rui] iP #60

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ bin/

/text-ui-test/ACTUAL.txt
text-ui-test/EXPECTED-UNIX.TXT
/text-ui-test/runtest.sh
3 changes: 3 additions & 0 deletions Saves/Tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
T | 1 | todo
D | 0 | deadline | sometime
E | 0 | event | now
4 changes: 4 additions & 0 deletions Saves/Tasks_edited
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
T | 1 | todo
D | 0 | deadline | sometime
E | 0 | event | now
T | 0 | kkk
2 changes: 2 additions & 0 deletions src/main/java/CorruptedDataFileException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class CorruptedDataFileException extends Exception{
}
61 changes: 61 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
public class Deadline extends Task implements FormatChecker {
private static final String DEADLINE_MARK = "[D]";
private static final String taskType = "D";
private String time;

/**
* Default constructor for Deadline instance
*
* @param description description of Deadline
* @param time time of deadline
*/
public Deadline(String description, String time) {
super(description);
this.time = time;
}

/**
* Constructor for a raw string input of deadline
*
* @param complexString raw string containing description and deadline
*/
public Deadline(String complexString) throws WrongCommandFormatException {
super();
try {
int identifierIndex = FormatChecker.findIdentifierIndex(DEADLINE_IDENTIFIER, complexString);
this.description = complexString.substring(0, identifierIndex);
FormatChecker.checkNullString(this.description);
this.time = complexString.substring(identifierIndex + TIME_IDENTIFIER_LENGTH);
FormatChecker.checkNullString(this.time);
this.isDone = false;
} catch (WrongCommandFormatException e) {
throw new WrongCommandFormatException();
}
}

public Deadline(String description, Boolean isDone, String time) {
this.description = description;
this.isDone = isDone;
this.time = time;
}

@Override
public String getStatusIcon() {
return DEADLINE_MARK + super.getStatusIcon();
}

@Override
public String getTaskType() {
return taskType;
}

@Override
public String getTime() {
return this.time;

Choose a reason for hiding this comment

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

Perhaps you can avoid the unnecessary use of this.

Suggested change
return this.time;
return time;

https://se-education.org/guides/conventions/java/index.html#variables

}

@Override
public String toString() {
return String.format("%s %s(by: %s)", this.getStatusIcon(), description, this.time);
}
}
72 changes: 66 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,70 @@
import java.util.Scanner;

public class Duke {

private static final String COMMAND_BYE = "bye";
private static final String BYE_MESSAGE = "Bye! Always there waiting for you!";
private static final String GREET_MESSAGE_1 = "Hello! I'm Rae.";
private static final String GREET_MESSAGE_2 = "How does everything progress now?";
private static final int LINE_SEPARATOR_LENGTH = 60;
private static final String LINE_SEPARATOR = "_";

public static void separateLine() {
System.out.println(LINE_SEPARATOR.repeat(LINE_SEPARATOR_LENGTH));
}

/**
* greet prints greet messages
*/
public static void greet() {
separateLine();
System.out.println(GREET_MESSAGE_1);
System.out.println(GREET_MESSAGE_2);
}

/**
* bye prints farewell messages
*/
public static void bye() {
separateLine();
System.out.println(BYE_MESSAGE);
separateLine();
}

/**
* echo print input string with line breaker after it
*
* @param line the line of string input
*/
public static void echo(String line) {
separateLine();
System.out.println(line);
separateLine();
}


public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
greet();
Storage storage = new Storage();
storage.loadSave();
separateLine();
Scanner in = new Scanner(System.in);
String line = in.nextLine();

while (!line.equalsIgnoreCase(COMMAND_BYE)) {
separateLine();
try {
storage.execute(line);
} catch (UnknownCommandException e) {
System.out.format("Exception: Unknown command%n" +
"%s is not a valid command%n", line.split(" ")[0]);
} catch (NullCommandException e) {
System.out.format("Exception: Null command%n" +
"No command is entered%n");
}
separateLine();
line = in.nextLine();
}
bye();
}
}
61 changes: 61 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
public class Event extends Task implements FormatChecker {
private static final String EVENT_MARK = "[E]";
private static final String taskType = "E";
private String time;

/**
* Default constructor for Event instance
*
* @param description description of Event
* @param time time of event
*/
public Event(String description, String time) {
super(description);
this.time = time;
}

/**
* Event constructor for a raw string input of event
*
* @param complexString raw string input containing time and description of event
*/
public Event(String complexString) throws WrongCommandFormatException {
super();
try {
int identifierIndex = FormatChecker.findIdentifierIndex(EVENT_IDENTIFIER, complexString);
this.description = complexString.substring(0, identifierIndex);
FormatChecker.checkNullString(this.description);
this.time = complexString.substring(identifierIndex + TIME_IDENTIFIER_LENGTH);
FormatChecker.checkNullString(this.time);
this.isDone = false;
} catch (WrongCommandFormatException e) {
throw new WrongCommandFormatException();
}
}

public Event(String description, Boolean isDone, String time) {
this.description = description;
this.isDone = isDone;
this.time = time;
}

@Override
public String getStatusIcon() {
return EVENT_MARK + super.getStatusIcon();
}

@Override
public String getTaskType() {
return taskType;
}

@Override
public String getTime() {
return this.time;
}

@Override
public String toString() {
return String.format("%s %s(at: %s)", this.getStatusIcon(), description, this.time);
}
}
2 changes: 2 additions & 0 deletions src/main/java/ExcessArgumentException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class ExcessArgumentException extends Exception {
}
59 changes: 59 additions & 0 deletions src/main/java/FileIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;

public interface FileIO {

static ArrayList<Task> loadFile() throws CorruptedDataFileException, FileNotFoundException {
File file = new File("/Users/bromine/ip/Saves/Tasks");
Scanner sc = new Scanner(file);

ArrayList<Task> tasks = new ArrayList<>();
while (sc.hasNext()) {
String line = sc.nextLine();

String[] args = line.split(" | ", 0);

String taskType = args[0];
String description = args[4];
String time = (args.length > 5 ? args[6] : "");
Boolean isDone = (Integer.parseInt(args[2]) == 1);

switch (taskType) {
case "T":
tasks.add(new Todo(description, isDone));
break;
case "D":
tasks.add(new Deadline(description, isDone, time));
break;
case "E":
tasks.add(new Event(description, isDone, time));
break;
default:
throw new CorruptedDataFileException();
}
}

return tasks;
}

static void writeFile(ArrayList<Task> Tasks) throws IOException {
FileWriter fw = new FileWriter("/Users/bromine/ip/Saves/Tasks");

Choose a reason for hiding this comment

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

Seems the Path is repeated twice, could it be better as a constant?

StringBuilder taskFileFormat = new StringBuilder();

for (Task task : Tasks) {
taskFileFormat.append(task.getTaskType()).append(" | ")
.append(task.getIsDone() ? "1" : "0").append(" | ").append(task.getDescription());
if (!task.getTaskType().equals("T")) {
taskFileFormat.append(" | ").append(task.getTime());
}
taskFileFormat.append(String.format("%n"));
}
fw.write(taskFileFormat.toString());
fw.close();
}

}
25 changes: 25 additions & 0 deletions src/main/java/FormatChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public interface FormatChecker {

int TIME_IDENTIFIER_LENGTH = 5;

static void checkNullString(String string) throws WrongCommandFormatException {
if (string.replaceAll("\\s+", "").equals("")) {
throw new WrongCommandFormatException();
}
}

static int findIdentifierIndex(String identifier, String complexString) throws WrongCommandFormatException {
int separatorIndex = complexString.indexOf(identifier);
if (separatorIndex == -1) {
throw new WrongCommandFormatException();
}
return separatorIndex;
}

static void checkExcessArgument(String cmd) throws ExcessArgumentException {
if (cmd.split(" ").length > 2) {
throw new ExcessArgumentException();
}
}

}
2 changes: 2 additions & 0 deletions src/main/java/NullArgumentException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class NullArgumentException extends Exception {
}
2 changes: 2 additions & 0 deletions src/main/java/NullCommandException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class NullCommandException extends Exception{
}
Loading