forked from nus-cs2103-AY2223S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
803 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.