Skip to content

Commit

Permalink
Update view command (#124)
Browse files Browse the repository at this point in the history
> 
> 
> ![Codacy](https://camo.githubusercontent.com/b72992711a855bd9a03bfbdfc5ef7fcf528482f5/68747470733a2f2f6170702e636f646163792e636f6d2f6173736574732f696d616765732f66617669636f6e2e706e67) Here is an overview of what got changed by this pull request:
> 
> ```diff
> Complexity increasing per file
> ==============================
> - src/planmysem/logic/parser/ViewCommandParser.java  4
>          
> ```
> 
> See the complete overview on [Codacy](https://app.codacy.com/app/CS2113T-T08-3/main/pullRequest?prid=3354301&bid=12090529)

To be expected.
  • Loading branch information
dingheng4448 authored and seanieyap committed Mar 30, 2019
1 parent 054da6a commit b28443a
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/DeveloperGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ FindCommand will be enhanced by using Levenshtein Distance to generate an ordere
** Cons: Large amount of additional operations.

==== Future Implementation

// to be edited
The month/week/day view mechanism is facilitated by `PlannerView`.
It extends `Planner` with different calendar views, stored internally as `monthView`, `weekView` and `dayView`.
Additionally, it implements the following operations:
Expand Down
12 changes: 7 additions & 5 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -344,24 +344,26 @@ image::Find_Command_Output_1.png[width="790"]
View the planner in a month/week/day view.
1. View the monthly calendar view of the current academic semester. +
Format: `view month [MONTH]`
Format: `view month`
2. View the weekly calendar view of the current academic week. +
Format: `view week [WEEK]`
3. View the day view of a particular day in the academic semester. +
Format: `view day [DATE]`
Format: `view day [DATE_OR_DAY_OF_WEEK]`
4. View all the details in the planner. +
Format: `view all`
[TIP]
You may omit `[MONTH]`/`[WEEK]`/`[DATE]` to view the format chosen in respect to the current day! +
E.g. `view month` will allow you to view the current month and `view day` will allow you to view the current day!
You may omit `[WEEK]`/`[DATE_OR_DAY_OF_WEEK]` to view the calendar with respect to the current date! +
E.g. `view week` will allow you to view the current week and `view day` will allow you to view the current day!
Examples:
* `view day` +
View planner for the current date.
* `view day 01/03/2019` +
* `view day 01-03-2019` +
View planner for the first of March.
* `view day monday` +
View planner for the upcoming Monday.
* `view week 7` +
View planner for week 7 of the academic calendar.
* `view week recess` +
Expand Down
2 changes: 1 addition & 1 deletion src/planmysem/logic/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class HelpCommand extends Command {
+ "\n\n" + DeleteCommand.MESSAGE_USAGE
+ "\n\n" + ListCommand.MESSAGE_USAGE
+ "\n\n" + FindCommand.MESSAGE_USAGE
// + "\n\n" + ViewCommand.MESSAGE_USAGE
+ "\n\n" + ViewCommand.MESSAGE_USAGE
// + "\n\n" + ViewAllCommand.MESSAGE_USAGE
+ "\n\n" + ClearCommand.MESSAGE_USAGE
+ "\n\n" + HelpCommand.MESSAGE_USAGE
Expand Down
166 changes: 155 additions & 11 deletions src/planmysem/logic/commands/ViewCommand.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
package planmysem.logic.commands;

import static planmysem.common.Utils.getNearestDayOfWeek;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import planmysem.common.Clock;
import planmysem.common.Utils;
import planmysem.logic.CommandHistory;
import planmysem.model.Model;
import planmysem.model.semester.Day;
import planmysem.model.semester.Semester;
import planmysem.model.slot.Slot;

/**
* Adds a person to the address book.
* View the planner.
*/
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\tFormat: view [viewType] [specifier]"
+ "\n\tParameters:"
+ "\n\t\tMandatory: [viewType]"
+ "\n\t\tOptional: [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"
+ " week"
+ "\n\tExample 5: " + COMMAND_WORD
+ " day 01/03/2019"
+ "\n\tExample 6: " + COMMAND_WORD
+ " day monday"
+ "\n\tExample 7: " + COMMAND_WORD
+ " day"
+ "\n\tExample 8: " + COMMAND_WORD
+ " all";

private final String viewArgs;
Expand All @@ -39,7 +59,7 @@ public ViewCommand(String viewArgs) {
@Override
public CommandResult execute(Model model, CommandHistory commandHistory) {
String viewType;
//String viewSpecifier;
String viewSpecifier;
final Semester currentSemester = model.getPlanner().getSemester();
String output = null;

Expand All @@ -48,19 +68,20 @@ public CommandResult execute(Model model, CommandHistory commandHistory) {
output = "all";
} else if ("month".equals(viewArgs)) {
output = displayMonthView(currentSemester);
} else if ("week".equals(viewArgs)) {
output = displayWeekView(currentSemester, null);
} else if ("day".equals(viewArgs)) {
output = displayDayView(currentSemester, null);
} else {
viewType = viewArgs.split(" ")[0];
//viewSpecifier = viewArgs.split(" ")[1];
viewSpecifier = viewArgs.split(" ")[1];

switch (viewType) {
case "month":
//TODO: month view
break;
case "week":
//TODO: week view
output = displayWeekView(currentSemester, viewSpecifier);
break;
case "day":
//TODO: day view
output = displayDayView(currentSemester, viewSpecifier);
break;
default:
break;
Expand Down Expand Up @@ -136,4 +157,127 @@ private String displayMonthView(Semester currentSemester) {
return sb.toString();
}

/**
* Display all slots for a given week.
*/
private String displayWeekView(Semester currentSemester, String week) {
HashMap<LocalDate, Day> allDays = currentSemester.getDays();
StringBuilder sb = new StringBuilder();

if (week == null) {
System.out.println(allDays.get(LocalDate.now()).getType());

} else {
HashMap<Integer, String> acadCal = currentSemester.getAcadCal();
//System.out.println(currentSemester.getAcadCal());
String key;
int[] weekOfYear = {0, 0};
List<LocalDate> datesList;

if ("Recess".equals(week) || "Reading".equals(week) || "Examination".equals(week)
|| "Orientation".equals(week)) {
key = week + " Week" + "_" + currentSemester.getName();
sb.append(week + "Week" + " of " + currentSemester.getName() + "\n");
} else {
key = "Week " + week + "_" + currentSemester.getName();
sb.append("Week " + week + " of " + currentSemester.getName() + "\n");
}
//System.out.println(key);

for (Map.Entry<Integer, String> entry: acadCal.entrySet()) {
if (key.equals(entry.getValue())) {
if (weekOfYear[0] == 0) {
weekOfYear[0] = entry.getKey();
} else {
weekOfYear[1] = entry.getKey();
}
}
}
//System.out.println(weekOfYear[0] + ", " + weekOfYear[1]);

LocalDate weekStart = LocalDate.now().with(WeekFields.ISO.weekOfWeekBasedYear(), weekOfYear[0]);
weekStart = weekStart.with(WeekFields.ISO.dayOfWeek(), 1);
LocalDate weekEnd = weekStart.with(WeekFields.ISO.weekOfWeekBasedYear(), weekOfYear[0] + 1);
datesList = weekStart.datesUntil(weekEnd).collect(Collectors.toList());
//System.out.println(datesList);

sb.append("__________________________________________________________________________\n\n");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
for (LocalDate date : datesList) {
sb.append(displayDayView(currentSemester, date.format(formatter)));
sb.append("__________________________________________________________________________\n\n");
}

// get acad cal map
// swap key and value
// find week of year from acad week
// get all dates for week of year
// get all slots for all dates found
// format and print out all slots
}

return sb.toString();
}

/**
* Display all slots for a given day/date.
*/
private String displayDayView(Semester currentSemester, String dateOrDay) {
HashMap<LocalDate, Day> allDays = currentSemester.getDays();
StringBuilder sb = new StringBuilder();

// Parse different formats of given day/date.
LocalDate givenDate;
if (dateOrDay == null) {
givenDate = LocalDate.now();
} else {
int day = -1;
givenDate = Utils.parseDate(dateOrDay);
if (givenDate == null) {
day = Utils.parseDay(dateOrDay);
}
if (day == -1 && givenDate == null) {
return MESSAGE_USAGE;
}
if (day != -1) {
givenDate = getNearestDayOfWeek(LocalDate.now(Clock.get()), day);
}
}
sb.append(givenDate.getDayOfWeek().name() + " , " + givenDate + "\n\n");

// Retrieve all slots for given day/date in sorted order.
ArrayList<Slot> allSlotsInDay = allDays.get(givenDate).getSlots();
Comparator<Slot> comparator = new Comparator<Slot>() {
@Override
public int compare(final Slot o1, final Slot o2) {
return o1.getStartTime().compareTo(o2.getStartTime());
}
};
allSlotsInDay.sort(comparator);

// Print each slot.
for (Slot slot : allSlotsInDay) {
sb.append("* " + slot.getStartTime());
sb.append(" to ");
sb.append(Utils.getEndTime(slot.getStartTime(), slot.getDuration()));

sb.append("\n\t" + slot.getName() + "\n");
sb.append("\t" + "Location: " + slot.getLocation() + "\n");
sb.append("\t" + "Description: " + slot.getDescription() + "\n");
sb.append("\n\tTags: \n");

int count = 1;
for (String tag : slot.getTags()) {
sb.append("\t");
sb.append(count);
sb.append(". ");
sb.append(tag);
count++;
}

sb.append("\n\n");
}

return sb.toString();
}
}
4 changes: 4 additions & 0 deletions src/planmysem/logic/parser/ViewCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public ViewCommand parse(String args) throws ParseException {
return new ViewCommand(viewArgs[1]);
} else if ("month".equals(viewArgs[1]) && viewArgs.length == 2) {
return new ViewCommand(viewArgs[1]);
} else if ("week".equals(viewArgs[1]) && viewArgs.length == 2) {
return new ViewCommand(viewArgs[1]);
} else if ("day".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]);
Expand Down
1 change: 1 addition & 0 deletions src/planmysem/model/semester/ReadOnlySemester.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface ReadOnlySemester {
LocalDate getStartDate();
LocalDate getEndDate();
int getNoOfWeeks();
HashMap<Integer, String> getAcadCal();

// These variables aid in making searches more effective
Set<LocalDate> getRecessDays();
Expand Down
18 changes: 13 additions & 5 deletions src/planmysem/model/semester/Semester.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @see Day#equals(Object)
*/
public class Semester implements ReadOnlySemester {
private static HashMap<Integer, String> acadCal = new HashMap<>();
private final String name;
private final String academicYear;
private final HashMap<LocalDate, Day> days = new HashMap<>();
Expand Down Expand Up @@ -107,6 +108,7 @@ public static Semester generateSemester(LocalDate currentDate) {
Set<LocalDate> examDays = new HashSet<>();

acadCalMap = generateAcadCalMap(currentDate);
acadCal = acadCalMap;
semesterDetails = getSemesterDetails(currentDate, acadCalMap);
acadSem = semesterDetails[1];
acadYear = semesterDetails[2];
Expand Down Expand Up @@ -242,6 +244,7 @@ private static HashMap<Integer, String> generateAcadCalMap(LocalDate date) {
acadCalMap.put(vacationWeekNo++, "Vacation_Sem 2");
}

//System.out.println(acadCalMap); //why does this print twice?
return acadCalMap;
}

Expand Down Expand Up @@ -467,11 +470,6 @@ public void setDays(HashMap<LocalDate, Day> days) {
this.days.putAll(days);
}

@Override
public HashMap<LocalDate, Day> getDays() {
return days;
}

@Override
public LocalDate getStartDate() {
return startDate;
Expand Down Expand Up @@ -507,6 +505,16 @@ public Set<LocalDate> getExamDays() {
return examDays;
}

@Override
public HashMap<LocalDate, Day> getDays() {
return days;
}

@Override
public HashMap<Integer, String> getAcadCal() {
return acadCal;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down

0 comments on commit b28443a

Please sign in to comment.