Skip to content

Commit

Permalink
Fixed Dates and Times
Browse files Browse the repository at this point in the history
  • Loading branch information
nicljr committed Feb 1, 2023
1 parent 8836d32 commit 45f7698
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 29 deletions.
1 change: 0 additions & 1 deletion DukeData/tasks.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
T|0|borrow book
29 changes: 23 additions & 6 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import duke.command.*;

import java.time.LocalDate;
import java.sql.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Parser {

public static DateTimeFormatter strFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
public static DateTimeFormatter ldtFormatter = DateTimeFormatter.ofPattern("MMM dd yyyy HHmm");


public static Integer stringToInt(String index) {
return Integer.parseInt(index);
}
Expand All @@ -29,12 +34,24 @@ public static Command stringToCommand(String command) {
}
}

public static LocalDate stringToDate(String duration) {
LocalDate localDate = LocalDate.parse(duration);
return localDate;
public static String[] deadlineSplit(String deadline) {
return deadline.split(" ", 2);
}

public static String[] eventSplit(String event) {
return event.split(" ", 6);
}

public static LocalDateTime stringToDateTime(String duration) {
LocalDateTime localDateTime = LocalDateTime.parse(duration, strFormatter);
return localDateTime;
}

public static String dateTimeToString(LocalDateTime localDateTime) {
return localDateTime.format(ldtFormatter);
}

public static String dateToString(LocalDate localDate) {
return localDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy"));
public static String dateTimeSaving(LocalDateTime localDateTime) {
return localDateTime.format(strFormatter);
}
}
1 change: 1 addition & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void loadTasks(TaskList taskList) throws IOException {
break;
case ("E"):
curr = new Events(text[2], text[3], text[4]);
break;
default:
System.out.println("Error while Loading up the file");
break;
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/duke/TaskAssigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,38 @@ public Task assignToDo(String command) {
public Task assignDeadline(String command) throws DukeException {
String[] splitTiming = command.split("/by ");
if (splitTiming.length != 2) {
throw new DukeException("Improper Deadline Format! deadline {desc} /by {date}\n");
throw new DukeException("Improper Deadline Format! deadline {desc} /by yyyy-mm-dd hhmm\n");
}

try {
int d_index = command.indexOf("/by ") + 4;
String deadline = command.substring(d_index);
System.out.println(deadline);
String updated_deadline = TimeChecker.updateTime(deadline);
String d_desc = command.substring(9, d_index - 4);
return new Deadline(d_desc, deadline);
return new Deadline(d_desc, updated_deadline);
} catch (DateTimeParseException e) {
throw new DukeException("Date Incorrect Time Format! Follow: yyyy-mm-dd\n");
throw new DukeException("Improper Deadline Format! deadline {desc} /by yyyy-mm-dd hhmm\n");
}
}

public Task assignEvent(String command) throws DukeException {
String[] splitTimings = command.split("/from | /to");
String[] splitTimings = command.split("/from | /to ");
if (splitTimings.length != 3) {
throw new DukeException("Improper Event Format! Event {desc} /from {date} /to {date}\n");
throw new DukeException("Improper Event Format! Follow:\n" +
"event {desc} /from yyyy-mm-dd hhmm /to yyyy-mm-dd hhmm\n");
}
try {
int s_index = command.indexOf("/from") + 6;
int e_index = command.indexOf("/to") + 4;
String start_date = command.substring(s_index, e_index - 5);
String end_date = command.substring(e_index);
String start_date = TimeChecker.updateTime(command.substring(s_index, e_index - 5));
String end_date = TimeChecker.updateTime(command.substring(e_index));
String e_desc = command.substring(6, s_index - 6);
return new Events(e_desc, start_date, end_date);
} catch (DateTimeParseException e) {
throw new DukeException("Date Incorrect Format! Follow: yyyy-mm-dd\n");
throw new DukeException("Improper Event Format! Follow:\n" +
"event {desc} /from yyyy-mm-dd hhmm /to yyyy-mm-dd hhmm\n");

}
}
}
20 changes: 20 additions & 0 deletions src/main/java/duke/TimeChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package duke;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class TimeChecker {

public static String DEFAULT_TIME = "2359";
private static DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmm");

public static String updateTime(String timeline) {
String[] split = timeline.split(" ");
if (split.length == 2) {
return timeline;
} else {
return timeline + " " + DEFAULT_TIME;
}
}
}
23 changes: 17 additions & 6 deletions src/main/java/duke/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@

import duke.Parser;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class Deadline extends Task {
protected LocalDate by;

static protected String DEFAULT_TIME = "2359";
protected LocalDateTime by;

public Deadline(String description, String by) {
super(description);
this.by = Parser.stringToDate(by);
super(description);
this.by = Parser.stringToDateTime(by);
}

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

public String getBy() {
return Parser.dateTimeToString(by);
}

@Override
public String getDescription() {
return super.getDescription() + " (by: " + this.by + ")";
return super.getDescription() + " (by: " + this.getBy() + ")";
}

public String parseBySaving() {
return Parser.dateTimeSaving(by);
}

@Override
public String saveString() {
return String.format("D|%s|%s|%s", super.saveString(), super.description, this.by);
return String.format("D|%s|%s|%s", super.saveString(), super.description, this.parseBySaving());
}
}
36 changes: 28 additions & 8 deletions src/main/java/duke/tasks/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,52 @@

import duke.Parser;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class Events extends Task {
protected LocalDate startDate;
protected LocalDate endDate;

static protected String DEFAULT_TIME = "2359";

protected LocalDateTime startDate;
protected LocalDateTime endDate;

public Events(String description, String startDate, String endDate) {
super(description);
this.startDate = Parser.stringToDate(startDate);
this.endDate = Parser.stringToDate(endDate);
this.startDate = Parser.stringToDateTime(startDate);
this.endDate = Parser.stringToDateTime(endDate);
}

public String getStart() {
return Parser.dateTimeToString(startDate);
}

public String getEnd() {
return Parser.dateTimeToString(endDate);
}


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

@Override
public String getDescription() {
return super.getDescription() + " (from: " + this.startDate +
" to: " + this.endDate + ")";
return super.getDescription() + " (from: " + this.getStart() +
" to: " + this.getEnd() + ")";
}

public String parseStartSaving() {
return Parser.dateTimeSaving(startDate);
}

public String parseEndSaving() {
return Parser.dateTimeSaving(startDate);
}

@Override
public String saveString() {
return String.format("E|%s|%s|%s|%s", super.saveString(), super.description,
this.startDate, this.endDate);
this.parseStartSaving(), this.parseEndSaving());
}
}

0 comments on commit 45f7698

Please sign in to comment.