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

Add view command for monthly calendar view #87

Merged
merged 18 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
92cb2cd
Planner: Initialize weekType for each Day
dingheng4448 Mar 7, 2019
1a98449
Planner: Fix Codacy issue
dingheng4448 Mar 7, 2019
45f7915
Planner: Update code for initialisation of semester
dingheng4448 Mar 8, 2019
2c631df
Planner: Fix Codacy and checkstyle issues
dingheng4448 Mar 9, 2019
20b4abb
Merge branch 'master' into develop
dingheng4448 Mar 9, 2019
5ac6ca9
Planner: Fix build error
dingheng4448 Mar 9, 2019
7e5b58c
Planner: Dynamic generation of semester from date
dingheng4448 Mar 16, 2019
60d1cda
PlannerTest: Add JUnit test for generateSemester
dingheng4448 Mar 16, 2019
93b40ed
Merge remote-tracking branch 'origin/develop' into develop
dingheng4448 Mar 16, 2019
a8d57fe
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/mai…
dingheng4448 Mar 17, 2019
a002fdc
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/mai…
dingheng4448 Mar 17, 2019
dbcf1fd
Update documentation for view command
dingheng4448 Mar 17, 2019
f3ce309
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/mai…
dingheng4448 Mar 17, 2019
8d35cfb
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/mai…
dingheng4448 Mar 17, 2019
aa42b48
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/mai…
dingheng4448 Mar 17, 2019
f0d916b
Add view command for monthly calendar view
dingheng4448 Mar 18, 2019
132bb01
Merge branch 'master' of https://github.com/CS2113-AY1819S2-T08-3/mai…
dingheng4448 Mar 18, 2019
1eded38
Fix Codacy issue
dingheng4448 Mar 18, 2019
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
137 changes: 137 additions & 0 deletions src/planmysem/commands/ViewCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package planmysem.commands;

import java.time.LocalDate;
import java.util.HashMap;

import planmysem.data.semester.Day;
import planmysem.data.semester.Semester;

/**
* Adds a person to the address book.
*/
public class ViewCommand extends Command {

public static final String COMMAND_WORD = "view";
public static final String COMMAND_WORD_SHORT = "v";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": View month/week/day view or all details of planner."
+ "\n\tParameters: "
+ "\n\t\tMandatory: [viewType] [specifier]"
+ "\n\tExample 1: " + COMMAND_WORD
+ " month"
+ "\n\tExample 2: " + COMMAND_WORD
+ " week 7"
+ "\n\tExample 3: " + COMMAND_WORD
+ " week recess"
+ "\n\tExample 4: " + COMMAND_WORD
+ " day 01/03/2019"
+ "\n\tExample 5: " + COMMAND_WORD
+ " all";

private final String viewArgs;

public ViewCommand(String viewArgs) {
this.viewArgs = viewArgs;
}

@Override
public CommandResult execute() {
String viewType;
//String viewSpecifier;
final Semester currentSemester = planner.getSemester();
String output = null;

if ("all".equals(viewArgs)) {
//TODO: print all planner details
output = "all";
} else if ("month".equals(viewArgs)) {
output = displayMonthView(currentSemester);
} else {
viewType = viewArgs.split(" ")[0];
//viewSpecifier = viewArgs.split(" ")[1];

switch (viewType) {
case "month":
//TODO: month view
break;
case "week":
//TODO: week view
break;
case "day":
//TODO: day view
break;
default:
break;
}
}

return new CommandResult(output);
}

/**
* Display all months for the semester.
*/
private String displayMonthView(Semester currentSemester) {
HashMap<LocalDate, Day> allDays = currentSemester.getDays();
LocalDate semesterStartDate = currentSemester.getStartDate();
LocalDate semesterEndDate = currentSemester.getEndDate();
int year = semesterStartDate.getYear();
LocalDate firstDayOfMonth = semesterStartDate.withDayOfMonth(1);
int spaces = firstDayOfMonth.getDayOfWeek().getValue();
int lastMonthOfSem = semesterEndDate.getMonthValue();
StringBuilder sb = new StringBuilder();

String[] months = {"", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};

int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

for (int m = 1; m <= lastMonthOfSem; m++) {
// Set number of days in February to 29 if it is a leap year.
if ((((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) && m == 2) {
days[m] = 29;
}

// Print calendar header.
sb.append(" " + months[m] + " " + year + "\n");
sb.append("_____________________________________\n");
sb.append(" Sun Mon Tue Wed Thu Fri Sat\n");

// Print spaces required for the start of a month.
spaces = (days[m - 1] + spaces) % 7;
for (int i = 0; i < spaces; i++) {
sb.append(" ");
}
// Print the days in the month.
for (int i = 1; i <= days[m]; i++) {
sb.append(String.format(" %3d", i));
if (((i + spaces) % 7 == 0)) {
Day tempDay = allDays.get(LocalDate.of(year, m, i));
String weekType = "";
if (tempDay != null) {
weekType = tempDay.getType();
}
sb.append(" | " + weekType + "\n");
}
if (i == days[m]) {
LocalDate tempDate = LocalDate.of(year, m, i);
Day tempDay = allDays.get(tempDate);
String weekType = "";
int extraSpaces = 6 - (tempDate.getDayOfWeek().getValue() % 7);
for (int j = 0; j < extraSpaces; j++) {
sb.append(" ");
}
if (tempDay != null) {
weekType = tempDay.getType();
}
sb.append(" | " + weekType + "\n");
}
}

sb.append("\n");
}

return sb.toString();
}

}
48 changes: 25 additions & 23 deletions src/planmysem/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import planmysem.commands.HelpCommand;
import planmysem.commands.IncorrectCommand;
import planmysem.commands.ListCommand;
import planmysem.commands.ViewCommand;
import planmysem.common.Utils;
import planmysem.data.exception.IllegalValueException;

Expand Down Expand Up @@ -95,6 +96,10 @@ public Command parseCommand(String userInput) {
case ListCommand.COMMAND_WORD_SHORT:
return prepareList(arguments);

case ViewCommand.COMMAND_WORD:
case ViewCommand.COMMAND_WORD_SHORT:
return prepareView(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();

Expand Down Expand Up @@ -344,31 +349,28 @@ private Command prepareList(String args) {
* @param args full command args string
* @return the prepared command
*/
// private Command prepareView(String args) {
//
// try {
// final int targetIndex = parseArgsAsDisplayedIndex(args);
// return new ViewCommand(targetIndex);
// } catch (ParseException | NumberFormatException e) {
// return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
// ViewCommand.MESSAGE_USAGE));
// }
// }
private Command prepareView(String args) {
if (args == null || args.trim().isEmpty()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE));
}

/**
* Parses the given arguments string as a single index number.
*
* @param args arguments string to parse as index number
* @return the parsed index number
* @throws ParseException if no region of the args string could be found for the index
* @throws NumberFormatException the args string region is not a valid number
*/
private int parseArgsAsDisplayedIndex(String args) throws ParseException, NumberFormatException {
final Matcher matcher = PERSON_INDEX_ARGS_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
throw new ParseException("Could not find index number to parse");
String[] viewArgs = args.split(" ");
if ("all".equals(viewArgs[1]) && viewArgs.length == 2) {
return new ViewCommand(viewArgs[1]);
} else if ("month".equals(viewArgs[1]) && viewArgs.length == 2) {
return new ViewCommand(viewArgs[1]);
} else if ("month".equals(viewArgs[1]) && viewArgs.length == 3) {
//TODO: ensure month arguments
return new ViewCommand(viewArgs[1] + " " + viewArgs[2]);
} else if ("week".equals(viewArgs[1]) && viewArgs.length == 3) {
//TODO: ensure week arguments
return new ViewCommand(viewArgs[1] + " " + viewArgs[2]);
} else if ("day".equals(viewArgs[1]) && viewArgs.length == 3) {
//TODO: ensure day arguments
return new ViewCommand(viewArgs[1] + " " + viewArgs[2]);
}
return Integer.parseInt(matcher.group("targetIndex"));

return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/planmysem/ui/DarkTheme.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.text-area {
-fx-background-color: black;
-fx-control-inner-background: black;
-fx-font-family: "Segoe UI Semibold";
-fx-font-family: "Lucida Console";
-fx-font-size: 10pt;
-fx-padding: 5 5 5 5;
}