Skip to content

Commit

Permalink
Finish A-MoreOOP
Browse files Browse the repository at this point in the history
  • Loading branch information
DerenC committed Feb 9, 2023
1 parent 388ae95 commit 4a61459
Show file tree
Hide file tree
Showing 27 changed files with 803 additions and 427 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ src/main/resources/docs/
*.iml
bin/


/test-ui-test/
text-ui-test/EXPECTED.TXT
/text-ui-test/ACTUAL.TXT
text-ui-test/EXPECTED-UNIX.TXT
text-ui-test/data/

*.class

/src/main/java/checkstyle-8.2-all.jar
/src/main/java/cs2030_checks.xml

data/savedTasks.txt
Event flow.txt
src/main/java/exceptions/All wrong cmds.txt
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
id 'java'
id 'application'
id 'checkstyle'
id 'com.github.johnrengelman.shadow' version '5.1.0'
}

Expand Down
26 changes: 25 additions & 1 deletion src/main/java/CmdType.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

public enum CmdType {
LIST("list"),
BYE("bye"),
Expand All @@ -7,15 +11,35 @@ public enum CmdType {
DEADLINE("deadline"),
EVENT("event"),
DELETE("delete"),

CLEAR("clear");

private final String cmd;
private static final Map<String, CmdType> COMMANDS = new HashMap<>();

CmdType(String str) {
this.cmd = str;
}

static {
COMMANDS.put("bye", CmdType.BYE);
COMMANDS.put("list", CmdType.LIST);
COMMANDS.put("todo", CmdType.TODO);
COMMANDS.put("deadline", CmdType.DEADLINE);
COMMANDS.put("event", CmdType.EVENT);
COMMANDS.put("mark", CmdType.MARK);
COMMANDS.put("unmark", CmdType.UNMARK);
COMMANDS.put("delete", CmdType.DELETE);
COMMANDS.put("clear", CmdType.CLEAR);
}

public static CmdType getCmdType(String str) {
return COMMANDS.get(str);
}

public static Set<String> getKeys() {
return COMMANDS.keySet();
}

@Override
public String toString() {
return cmd;
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/Exceptions/MissingInputException.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/main/java/Exceptions/MissingSpacingException.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/main/java/Exceptions/UnspecifiedTimeException.java

This file was deleted.

123 changes: 123 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import exceptions.int_exceptions.NotPositiveIntegerException;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

public class Parser {
static CmdType getCmd(String userInput) {
int idx = userInput.indexOf(' ');
if (idx == -1) {
idx = userInput.length();
}
return CmdType.getCmdType(userInput.substring(0, idx));
}
// for (String str : CmdType.getKeys()) {
// if (userInput.substring(0, str.length()).equals(str)) {
// return CmdType.getCmdType(str);
// }
// }
// return null;
// }



// Only for Deadline, Event & ToDo commands
static String[] getTaskComponents(String userInput, CmdType cmd) {
String byStr = "/by";
String fromStr = "/from";
String toStr = "/to";
int firstIdx;
int secondIdx;
String description = userInput.substring(cmd.len() + 1);
switch (cmd) {
case TODO:
return new String[]{description};
case DEADLINE:
firstIdx = description.indexOf(byStr);
return new String[]{removeSpacePadding(description.substring(0, firstIdx)),
removeSpacePadding(description.substring(firstIdx + byStr.length()))};
case EVENT:
firstIdx = description.indexOf(fromStr);
secondIdx = description.indexOf(toStr);
return new String[]{removeSpacePadding(description.substring(0, firstIdx)),
removeSpacePadding(description.substring(firstIdx + fromStr.length(), secondIdx)),
removeSpacePadding(description.substring(secondIdx + toStr.length()))};
}
return new String[0];
}

public static LocalDateTime parseDateTime(String str) throws DateTimeParseException {
str = removeSpacePadding(str);
if (count(str, ':') == 2) {
return LocalDateTime.parse(str);
}
int n = str.length();
if (n <= 10) {
return LocalDateTime.parse(parseDate(str) + "T12:34:56");
}
int idx = 10;
if (str.charAt(9) == ' ') {
idx = 9;
} else if (str.charAt(8) == ' ') {
idx = 8;
}
if (str.charAt(idx + 3) == ':') {
return LocalDateTime.parse(parseDate(str.substring(0, idx)) + "T" + str.substring(idx + 1) + ":00");
}
if (str.charAt(idx + 3) == '.') {
return LocalDateTime.parse(parseDate(str.substring(0, idx)) + "T" + str.substring(idx + 1, idx + 3) + ":" + str.substring(idx + 4) + ":00");
}
return LocalDateTime.parse(parseDate(str.substring(0, idx)) + "T" + str.substring(idx + 1, idx + 3) + ":" + str.substring(idx + 3) + ":00");
}

static int count(String str, char target) {
int num = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == target) {
num++;
}
}
return num;
}

// Used in parseDateTime
static String parseDate(String str) throws DateTimeParseException {
try {
String[] components = str.split("-", 3);
if (str.contains("/")) {
components = str.split("/", 3);
}
for (int i = 0; i < components.length; i++) {
if (components[i].length() == 1) {
components[i] = "0" + components[i];
}
}
if (components[0].length() == 4) {
return components[0] + "-" + components[1] + "-" + components[2];
}
return components[2] + "-" + components[1] + "-" + components[0];
} catch (ArrayIndexOutOfBoundsException ex) {
throw new DateTimeParseException("", str, 0);
}
}

public static int parseInt(String userInput, CmdType cmd) throws NotPositiveIntegerException {
int num = Integer.parseInt(removeSpacePadding(userInput.substring(cmd.len())));
if (num <= 0) {
throw new NotPositiveIntegerException();
}
return num;
}

// HELPER FUNC
public static String removeSpacePadding(String str) {
int start = 0;
while (str.charAt(start) == ' ') {
start++;
}
int end = str.length() - 1;
while (str.charAt(end) == ' ') {
end--;
}
return str.substring(start, end + 1);
}
}
76 changes: 76 additions & 0 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import task.Deadline;
import task.Event;
import task.ToDo;
import task.Task;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Storage {
private final String folderPath;
private final String fileName;

public Storage(String filePath) {
int lastIdx = filePath.lastIndexOf('/');
if (lastIdx == -1) {
this.folderPath = "";
this.fileName = filePath;
} else {
this.folderPath = filePath.substring(0, lastIdx);
this.fileName = filePath.substring(lastIdx + 1); // Need to add a slash
}
}

public Storage() {
this("data/savedTasks.txt");
}

String getFullPath() {
return folderPath + "/" + fileName;
}

List<Task> load() throws SecurityException, IOException {
List<Task> tasks = new ArrayList<Task>();
File savedFile = new File(folderPath + "/" + fileName);
if (savedFile.exists()) {
Scanner sc = new Scanner(savedFile);
while (sc.hasNextLine()) {
String[] taskComponents = sc.nextLine().split("~%~");
boolean isDone = taskComponents[1].charAt(0) == '1';
switch (taskComponents[0].charAt(0)) {
case 'T':
tasks.add(new ToDo(taskComponents[2], isDone));
break;
case 'D':
tasks.add(new Deadline(taskComponents[2], Parser.parseDateTime(taskComponents[3]), isDone));
break;
case 'E':
tasks.add(new Event(taskComponents[2], Parser.parseDateTime(taskComponents[3]), Parser.parseDateTime(taskComponents[4]), isDone));
break;
}
}
}
return tasks;
}

void save(String tasksAsStr) throws IOException, SecurityException {
// CREATE FOLDERS
Path path = Paths.get(folderPath);
Files.createDirectories(path);

// CREATE FILE
File file = new File(getFullPath());
file.createNewFile();

// WRITE FILE
FileWriter fw = new FileWriter(getFullPath());
fw.write(tasksAsStr);
fw.close();
}
}

0 comments on commit 4a61459

Please sign in to comment.