Commit ee501bb9 authored by Stojcheska Teodora's avatar Stojcheska Teodora
Browse files

Fixed bugs for addig tasks

parent e40a7462
......@@ -15,18 +15,20 @@ public class Calendar
{
this.days = new List<Day>();
this.allTasks = new List<Task>();
currentDate = DateTime.Now; // format dd.mm.yyyy hh:mm:ss
this.currentDate = DateTime.Now; // format dd.mm.yyyy hh:mm:ss
}
public Calendar(List<Day> days, List<Task> allTasks)
{
this.days = days;
this.allTasks = allTasks;
currentDate = DateTime.Now; // format dd.mm.yyyy hh:mm:ss
this.currentDate = DateTime.Now; // format dd.mm.yyyy hh:mm:ss
}
// TODO: Check the case when we add a task for today since this function will be returning 0
private int numberOfDaysInRange(DateTime startingDate, DateTime endDate) => ((endDate.Date - startingDate.Date).Days);
private int numberOfDaysInRange(DateTime startingDate, DateTime endDate) => (endDate.Date - startingDate.Date).Days + 1;
private bool isDateValid(DateTime startingDate, DateTime endDate) => numberOfDaysInRange(startingDate, endDate) >= 0;
private bool shouldAddDay(Task task) => days[days.Count - 1].hoursToShift * -1 < task.duration;
......@@ -34,9 +36,7 @@ public class Calendar
{
List<Day> validDays = new List<Day>();
// if there are no days in the calendar add a new day and add the task there
// TODO: don't add just one day, but add days according to the duration of the task
// TODO: Should it be here?
Day newDay;
if (days.Count == 0) {
newDay = new Day(currentDate, new List<Task>(), defaultWorkingHoursInterval, defaultWorkingHoursInterval);
......@@ -47,29 +47,22 @@ public class Calendar
if (shouldAddDay(task)) {
newDay = new Day(days[days.Count - 1].date.AddDays(1), new List<Task>(), defaultWorkingHoursInterval, defaultWorkingHoursInterval);
newDay.prevDay = days[days.Count - 1];
days[days.Count - 1].nextDay = newDay;
days.Add(newDay);
}
int numberOfValidDaysInRange = numberOfDaysInRange(currentDate, task.deadline);
// TODO: what if there is exactly one day in the range?
if(numberOfValidDaysInRange == 0)
{
WriteLine("The date is not valid!");
}
for (int i = 0; i < numberOfValidDaysInRange; ++i)
{
// TODO: is it <= or just < ? : <=
// validDays.Count == days.Count
// should be checked after adding the task
// can validDays.Count be 0?
if (days.Count <= i)
{
// TODO: add the amount of days according to the duration of the task
// TODO: Check date.AddDays(1)
if(validDays[validDays.Count - 1].isDayFull(task.duration)) {
newDay = new Day(validDays[validDays.Count - 1].date.AddDays(1), new List<Task>(), defaultWorkingHoursInterval, defaultWorkingHoursInterval);
days[days.Count - 1].nextDay = newDay;
days.Add(newDay);
validDays.Add(newDay);
}
break;
}
validDays.Add(days[i]);
......@@ -85,7 +78,7 @@ public class Calendar
if (hours <= 0) { return true; }
for(int i = validDays.Count - 1; i >= 0; --i) {
for(int j = validDays[i].tasks.Count - 1; j >=0; --j) {
for(int j = validDays[i].tasks.Count - 1; j >= 0; --j) {
// start date end date
if(numberOfDaysInRange(task.deadline, validDays[i].tasks[j].deadline) > 0) {
hours -= task.duration;
......@@ -98,9 +91,36 @@ public class Calendar
return hours <= 0;
}
public bool temp(Task task, List<Day> validDays, ref Day d)
{
for (int i = 0; i < validDays.Count; ++i)
{
if (!validDays[i].isDayFull(0))
{
break;
}
d = validDays[i];
}
int hours = 0;
Day cur = d;
while (cur != null && validDays.Contains(cur))
{
hours += cur.hoursToShift * -1;
cur = cur.nextDay;
}
if (hours < task.duration)
{
WriteLine("No space to add task");
return false;
}
return true;
}
public void addTask(Task task)
{
// TODO: if we add a task with todays deadline
List<Day> validDays = getRangeOfDaysForTask(task);
if(!isThereEnoughSpace(validDays, task)) {
......@@ -108,19 +128,14 @@ public class Calendar
return;
}
// TODO: Check the commented if else
foreach (Day day in validDays)
{
// TODO: Handle it in a different way
//if (day.tasks.Count != 0) {
//foreach (Task curTask in day.tasks)
for (int i = 0; i < day.tasks.Count; ++i) {
// TODO: If equality optimize according to duration
// TODO: Decide what to do when the task with deadline x has to be added to a day where there are tasks with deadline x
// should it be just < ? just to avoid the options below?
// TODO: When will there be no space to add a task?
if (numberOfDaysInRange(day.tasks[i].deadline, task.deadline) <= 0 && day.tasks[i].type != Type.FIXED) {
day.addTask(task, i);
if (day.isDayFull(0)) {
reorderCalendar(day, day.hoursToShift);
......@@ -128,55 +143,36 @@ public class Calendar
return;
}
}
//}
//}
/*else {
day.addTask(task, 0);
return;
}*/
}
days[days.Count - 1].addTask(task, days[days.Count - 1].tasks.Count);
Day d = validDays[0];
temp(task, validDays, ref d);
d.addTask(task, d.tasks.Count);
if (d.isDayFull(0))
{
reorderCalendar(d, d.hoursToShift);
}
}
// when reordering the calendar take care of the dates and if you are sending a task to a date
// when the deadline would be finished, then send an error
// there must be space for reordering since that is handled in addTask
void reorderCalendar(Day day, int hours)
{
Day nextDay = day.nextDay;
if (nextDay == null) { return; }
List<Task> tasks = getTasksGivenHours(day, hours);
foreach(Task task in tasks)
{
nextDay.addTask(task, 0);
}
reorderCalendar(nextDay, nextDay.hoursToShift);
}
// get the tasks that need to be shifted and remove them from the day
// TODO: should the removing part be done in the reorder caledar function?
List<Task> getTasksGivenHours(Day day, int hours)
{
// I'm checking the task that needs to be added as well
// TODO: Think about it!
List<Task> tasks = new List<Task>();
for (int i = day.tasks.Count - 1; i >= 0; --i)
{
// this should not be handles here - if a task has the same deadline as some other task
// add the new task afterwards
if(day.tasks[i].deadline == day.date)
{
// then there is no space to add the task we are adding
/*
Options:
-Modify task
-Add hours to the day
-Add hours equally to each day from the current date up until the deadline of the task
*/
}
if (day.tasks[i].type != Type.FIXED)
{
// should never get a negative value?
if (hours == 0) { break; }
Task curTask = day.tasks[i];
......@@ -184,7 +180,7 @@ public class Calendar
if (curTask.duration > hours)
{
int additionalHours = 0;
// merge then split the task
if (curTask.isSplit)
{
additionalHours = curTask.splitTaskPtr.duration;
......@@ -192,28 +188,29 @@ public class Calendar
day.nextDay.removeTask(curTask.splitTaskPtr);
curTask.mergeTasks(curTask, curTask.splitTaskPtr);
}
// TODO: if curTask.duration - hours is 0 then the task should be removed from day and the merged task should be added to the next day
// hours is the value that needs to be removed, so, in that day we're left with curTask.duration - hours
int[] splitHours = { curTask.duration - hours - additionalHours, hours + additionalHours};
curTask.splitTask(splitHours, 0, curTask);
// TODO: should I pass day or day.nextDay to splitTask as a parameter
curTask.splitTask(splitHours, 0, curTask, day);
if (!isDateValid(day.nextDay.date, curTask.splitTaskPtr.deadline))
{
WriteLine("There is no space to add task");
return;
}
hours = 0;
//day.nextDay.addTask(curTask.splitTaskPtr, 0);
tasks.Add(curTask.splitTaskPtr);
}
else
{
// && hours -= curTask.duration == 0?
int curTaskHours = curTask.duration;
// oone special case: when the last task of the day is split and we need to shift the whole thing
if (curTask.isSplit) {
//day.nextDay.removeTask(curTask.splitTaskPtr);
// the curtask should be deleted and the curTask.splitTaskPtr should be merged in one
curTask.splitTaskPtr.duration += curTask.duration;
// P:TODO: is this correct?
//curTask.splitTaskPtr = curTask;
}
else
{
tasks.Add(curTask);
//day.nextDay.addTask(curTask, 0);
}
hours -= curTaskHours;
day.removeTask(curTask);
......@@ -221,7 +218,77 @@ public class Calendar
}
}
}
return tasks;
foreach(Task task in tasks)
{
if (!isDateValid(day.nextDay.date, task.deadline))
{
WriteLine("There is no space to shift the tasks");
// TODO: Throw exeption
// then there is no space to add the task we are adding
/*
Options:
-Modify task
-Add hours to the day
-Add hours equally to each day from the current date up until the deadline of the task
*/
return;
}
nextDay.addTask(task, 0);
}
reorderCalendar(nextDay, nextDay.hoursToShift);
}
void deleteTask(Day day, Task task)
{
for (int i = 0; i < day.tasks.Count; ++i)
{
if (day.tasks[i] == task)
{
day.removeTask(task);
// Iterate the calendar backwards and remove tasks from day x and add them to day x - 1
// until we have reached day "day"
// Idea
// First - instaead of a "linked list" make the data structure a doubly linked list
// Second - pass the "directon" of reordering the calendar as a parameter and use it both ways
// i.e. modify reorder calendar
}
}
}
void modifyTask<T>(Day day, Task task, string parameterType, string parameter)
{
// TODO: Ask Bane => if I iterate with foreach will the changes be saved?
// TODO: How to compare two tasks, should I overwrite compareTo method?
for(int i = 0; i < day.tasks.Count; ++i)
{
if(day.tasks[i] == task)
{
if (parameterType == "deadline")
{
day.removeTask(task);
task.deadline = DateTime.Parse(parameter);
addTask(task);
}
else if (parameterType == "duration")
{
// Option 1
task.duration = int.Parse(parameter);
reorderCalendar(day, int.Parse(parameter));
// Option 2
day.removeTask(task);
task.duration = int.Parse(parameter);
addTask(task);
}
else if (parameterType == "name")
{
task.name = parameter;
}
}
}
}
void loadFile()
......
......@@ -11,6 +11,7 @@ public class Day
public int workingHoursInterval { get; set; }
public int workingHours { get; set; }
public Day nextDay { get; set; }
public Day prevDay { get; set; }
public Day(DateTime date, List<Task> tasks, int workingHoursInterval, int workingHours)
{
......@@ -19,6 +20,7 @@ public class Day
this.workingHoursInterval = workingHoursInterval;
this.workingHours = workingHours;
this.nextDay = null;
this.prevDay = null;
}
public int totalHoursTasks() {
......
......@@ -9,31 +9,22 @@ static class Program
calendar.defaultWorkingHoursInterval = 12;
Task task = new Task("kdb", new DateTime(2021, 07, 08), 5, Type.NORMAL, false);
Task t = new Task("Test", new DateTime(2021, 07, 07), 3, Type.NORMAL, false);
// splitting tasks problem - try with task duration 4
Task t1 = new Task("Test1", new DateTime(2021, 06, 14), 5, Type.NORMAL, false);
Task t2 = new Task("Test2", new DateTime(2021, 08, 07), 2, Type.NORMAL, false);
Task t3 = new Task("Test3", new DateTime(2021, 07, 10), 6, Type.NORMAL, false);
// doesn't add task
// also try date 2021, 07, 07
Task t4 = new Task("Test4", new DateTime(2021, 07, 07), 2, Type.NORMAL, false);
Task t5 = new Task("Test5", new DateTime(2021, 07, 15), 7, Type.NORMAL, false);
// when t6 is 10 day1 has 15 hours
// TODO: Modify the error state
Task t6 = new Task("Test6", new DateTime(2021, 06, 15), 7, Type.NORMAL, false);
Task t2 = new Task("Test2", new DateTime(2021, 06, 15), 2, Type.NORMAL, false);
Task t3 = new Task("Test3", new DateTime(2021, 06, 15), 6, Type.NORMAL, false);
Task t4 = new Task("Test4", new DateTime(2021, 06, 15), 2, Type.NORMAL, false);
Task t5 = new Task("Test5", new DateTime(2021, 06, 15), 3, Type.NORMAL, false);
Task t6 = new Task("Test6", new DateTime(2021, 06, 14), 10, Type.NORMAL, false);
calendar.addTask(task);
calendar.addTask(t);
calendar.addTask(t1);
calendar.addTask(t2);
/*calendar.addTask(t2);
calendar.addTask(t3);
// TODO: Check what happens when you add this task
calendar.print();
calendar.addTask(t4);
calendar.addTask(t5);
calendar.print();
WriteLine();
calendar.addTask(t5);*/
calendar.addTask(t6);
calendar.print();
}
}
\ No newline at end of file
......@@ -50,11 +50,11 @@ public class Task
public Task(string name, DateTime deadline, Type type, bool isSplit) : this(name, deadline, 0, type, isSplit){}
// TODO: Problem
public void splitTask(int[] hours, int index, Task task)
public void splitTask(int[] hours, int index, Task task, Day day)
{
task.duration = hours[0];
task.isSplit = true;
// TODO: Shouldn't it be deadline.AddDay(1) ?
task.splitTaskPtr = new Task(name, deadline, hours[1], type, false);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment