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

Newest version of the project.

parent 7ad1d4a5
using System;
using System.Collections.Generic;
using static System.Console;
using System.Linq;
[Serializable]
public class Calendar
......@@ -15,25 +15,24 @@ public class Calendar
public Calendar()
{
this.Days = new List<Day>();
this.DefaultWorkingHours = 8;
this.DefaultWorkingHoursInterval = (9, 17);
Days.Add(new Day(DateTime.Now, new List<Task>(), DefaultWorkingHoursInterval, DefaultWorkingHours));
this.CurrentDate = DateTime.Now; // format dd.mm.yyyy hh:mm:ss
}
public Calendar(List<Day> days)
public Calendar(List<Day> days, (int, int) workingHoursInterval, int workingHours)
{
this.Days = days;
this.DefaultWorkingHoursInterval = workingHoursInterval;
this.DefaultWorkingHours = workingHours;
this.CurrentDate = DateTime.Now; // format dd.mm.yyyy hh:mm:ss
addDaysUpToDate(DateTime.Now, -1);
}
private static int numberOfDaysInRange(DateTime startingDate, DateTime endDate) => (endDate.Date - startingDate.Date).Days + 1;
private bool isDateValid(DateTime startingDate, DateTime endDate) => numberOfDaysInRange(startingDate, endDate) > 0;
public static bool isDateValid(string date)
{
DateTime val;
return (DateTime.TryParse(date, out val)) && (numberOfDaysInRange(DateTime.Now, val) > 0) ? true : false;
}
public static bool isDateValid(DateTime startingDate, DateTime endDate) => numberOfDaysInRange(startingDate, endDate) > 0;
public Day getDayByDate(DateTime date)
{
......@@ -47,7 +46,7 @@ public class Calendar
return null;
}
private bool shouldAddDay(Task task)
private int shouldAddDay(Task task)
{
int hours = task.Duration;
Day day = Days[Days.Count - 1];
......@@ -56,23 +55,14 @@ public class Calendar
hours += day.hoursToShift;
day = day.PrevDay;
}
return hours > 0;
return hours;
}
public bool addDaysUpToDate(DateTime date, int duration)
public void addDaysUpToDate(DateTime date, int duration)
{
int daysToAdd;
if (Days.Count == 0 || numberOfDaysInRange(Days[Days.Count - 1].Date, DateTime.Now) > 1)
{
daysToAdd = numberOfDaysInRange(date, DateTime.Now);
Days.Add(new Day(DateTime.Now, new List<Task>(), DefaultWorkingHoursInterval, DefaultWorkingHours));
duration -= Days[0].WorkingHours;
daysToAdd--;
}
else
{
daysToAdd = numberOfDaysInRange(Days[Days.Count - 1].Date, date) - 1;
}
daysToAdd = numberOfDaysInRange(Days[Days.Count - 1].Date, date) - 1;
for(int i = 0; i < daysToAdd; ++i)
{
if(duration == 0 || !isDateValid(Days[Days.Count - 1].Date.AddDays(1), date))
......@@ -86,47 +76,34 @@ public class Calendar
Days.Add(newDay);
duration = duration >= newDay.WorkingHours || duration < 0 ? (duration - newDay.WorkingHours) : 0;
}
return duration <= 0;
if(duration > 0)
{
throw new NoSpaceForTaskExeption("No space to add task");
}
}
public List<Day> getRangeOfDaysForTask(Task task)
{
List<Day> validDays = new List<Day>();
Day newDay;
if (Days.Count == 0)
{
newDay = new Day(CurrentDate, new List<Task>(), DefaultWorkingHoursInterval, DefaultWorkingHours);
Days.Add(newDay);
validDays.Add(newDay);
}
if (shouldAddDay(task))
int shouldAddDayHours = shouldAddDay(task);
if (shouldAddDayHours > 0)
{
Day day = isDateValid(Days[Days.Count - 1].Date, DateTime.Now) ? Days[Days.Count - 1] : Days[0];
int durationLeft = task.Duration;
while (day.PrevDay != null && !day.isDayFull(0))
{
durationLeft += day.hoursToShift;
day = day.PrevDay;
}
if(!addDaysUpToDate(task.Deadline, durationLeft))
{
throw new NoSpaceForTaskExeption("No space to add task");
}
addDaysUpToDate(task.Deadline, shouldAddDayHours);
}
int numberOfValidDaysInRange = numberOfDaysInRange(CurrentDate, task.Deadline);
// start from current Date
int startIndex = numberOfDaysInRange(Days[0].Date, CurrentDate) - 1;
for (int i = 0; i < numberOfValidDaysInRange; ++i)
{
// TODO: First Condition?
if (Days.Count - 1 < startIndex + i)
{
break;
}
validDays.Add(Days[startIndex + i]);
}
......@@ -147,25 +124,24 @@ public class Calendar
}
else if(task.Type == Type.FIXED)
{
addFixedTask(task);
_addFixedTask(task);
}
}
catch (NoSpaceForTaskExeption exception)
{
Day day = getDayByDate(task.Deadline);
day.removeTask(task);
deleteReorderCalendar(exception.Day.NextDay, day.hoursToShift * -1, day);
ErrorInTaskParameters.Invoke();
if(exception.Day != null)
{
Day day = getDayByDate(task.Deadline);
day.removeTask(task);
deleteReorderCalendar(exception.Day.NextDay, day.hoursToShift * -1, day);
}
ErrorInTaskParameters?.Invoke();
}
}
public void addFixedTask(Task task)
public void _addFixedTask(Task task)
{
if (Days.Count == 0)
{
Days.Add(new Day(CurrentDate, new List<Task>(), DefaultWorkingHoursInterval, DefaultWorkingHours));
}
Day day;
if (numberOfDaysInRange(task.Deadline, Days[Days.Count - 1].Date) > 0){
day = getDayByDate(task.Deadline);
......@@ -189,15 +165,7 @@ public class Calendar
public void _addTask(Task task)
{
List<Day> validDays;
try
{
validDays = getRangeOfDaysForTask(task);
}
catch(NoSpaceForTaskExeption e)
{
ErrorInTaskParameters?.Invoke();
return;
}
validDays = getRangeOfDaysForTask(task);
foreach (Day day in validDays)
{
......@@ -223,7 +191,6 @@ public class Calendar
return;
}
}
}
private void reorderCalendar(Day day, int hours)
......@@ -244,7 +211,6 @@ public class Calendar
if (!isDateValid(dirDay.Date, curTask.Deadline))
{
foreach (Task task in tasks) { day.addTask(task, day.Tasks.Count); }
throw new NoSpaceForTaskExeption("There is no space to add the Task, error occured during shifting the Tasks", day, curTask, hours);
}
......@@ -279,7 +245,7 @@ public class Calendar
}
hours -= curTaskHours;
day.removeTask(curTask);
dirDay.addTask(curTask, 0);
}
}
}
......@@ -355,7 +321,7 @@ public class Calendar
deleteReorderCalendar(Days[Days.Count - 1], task.SplitTaskPtr.Duration, day.NextDay);
}
while (day.PrevDay != null && day.PrevDay.Tasks.Count != 0 && day.PrevDay.Tasks[day.PrevDay.Tasks.Count - 1].IsSplit)
while (day.PrevDay != null && day.PrevDay.Tasks.Count != 0 && (day.PrevDay.Tasks[day.PrevDay.Tasks.Count - 1].IsSplit && day.PrevDay.Tasks[day.PrevDay.Tasks.Count - 1].Name == task.Name))
{
hours += day.PrevDay.Tasks[day.PrevDay.Tasks.Count - 1].Duration;
deleteTaskHelper(day.PrevDay, day.PrevDay.Tasks[day.PrevDay.Tasks.Count - 1]);
......
......@@ -378,7 +378,7 @@ namespace Scheduler.Forms
private void PerformSearch(DateTime date)
{
CurrentDay = Settings.MyCalendar.getDayByDate(date);
CurrentDay = Settings.MyCalendar.getDayByDate(date.Date);
this.CurrentDateLabel.Text = date.ToShortDateString();
LoadTasks();
}
......@@ -395,7 +395,8 @@ namespace Scheduler.Forms
});
createTask.ShowDialog();
PerformSearch(DateTime.Parse(CurrentDateLabel.Text));
DateTime d = DateTime.Parse(CurrentDateLabel.Text);
PerformSearch(d.Date);
}
private void leftButtonClick(object sender, EventArgs e)
......
......@@ -242,7 +242,12 @@ namespace Scheduler.Forms
private bool AllFieldsCorrect()
{
return this.TaskName.Text.Length > 0 && Calendar.isDateValid(DatePickerDeadline.GetDate().ToString()) && Duration.Text.Length > 0;
DateTime val;
if(DateTime.TryParse(DatePickerDeadline.GetDate().ToString(), out val))
{
return this.TaskName.Text.Length > 0 && Calendar.isDateValid(DateTime.Now, val) && Duration.Text.Length > 0;
}
return false;
}
private void FormButton_Click(object sender, EventArgs e)
......
using System;
using System.Collections.Generic;
using static System.Console;
using System.Windows.Forms;
static class Program
{
private const int totalTests = 9;
private static int totalPassed = 0;
static void assert(bool b)
{
if (!b)
throw new Exception("assertion failed");
totalPassed++;
}
static void test()
{
Calendar cal = new Calendar();
Settings.MyCalendar = cal;
Task t = new Task("mow the lawn", DateTime.Today.AddDays(3), 1, Type.NORMAL, false);
cal.addTask(t);
Day current = cal.getDayByDate(DateTime.Today);
assert(current.Tasks.Count == 1);
assert(current.Tasks[0] == t);
cal.deleteTask(current, t);
assert(current.Tasks.Count == 0);
t = new Task("a long task", DateTime.Today.AddDays(30), 100, Type.NORMAL, false);
cal.addTask(t);
cal.deleteTask(current, t);
// Error - No space in the day
t = new Task("error", DateTime.Now, 20, Type.NORMAL, false);
// Error
t = new Task("buy groceries", DateTime.Today, 5, Type.NORMAL, false);
cal.addTask(t);
Task t1 = new Task("clean room", DateTime.Today.AddDays(2), 5, Type.NORMAL, false);
cal.addTask(t1);
// will throw error
Task t2 = new Task("do homework", DateTime.Today, 5, Type.NORMAL, false);
cal.addTask(t2);
assert(current.Tasks.Count == 2);
assert(current.NextDay.Tasks.Count == 1);
t2 = new Task("do homework", DateTime.Today.AddDays(1), 5, Type.NORMAL, false);
cal.addTask(t2);
// delete split task
cal.deleteTask(cal.getDayByDate(DateTime.Now), t2);
assert(current.Tasks.Count == 2);
assert(current.NextDay.Tasks.Count == 1);
t2 = new Task("do homework", DateTime.Today.AddDays(2), 7, Type.NORMAL, false);
cal.addTask(t2);
Task t3 = new Task("make lunch", DateTime.Today.AddDays(2), 2, Type.NORMAL, false);
cal.addTask(t3);
Task t4 = new Task("go to the mall", DateTime.Today.AddDays(4), 4, Type.NORMAL, false);
cal.addTask(t4);
// will throw error since there is no space while reordering
Task t5 = new Task("fix bike", DateTime.Today.AddDays(2), 6, Type.NORMAL, false);
cal.addTask(t5);
assert(current.Tasks.Count == 2);
assert(current.NextDay.Tasks.Count == 2);
assert(current.NextDay.NextDay.Tasks.Count == 3);
}
[STAThread]
static void Main()
{
Settings.Load();
if (Environment.OSVersion.Version.Major >= 6)
SetProcessDPIAware();
try
{
Settings.Load();
test();
if (Environment.OSVersion.Version.Major >= 6)
SetProcessDPIAware();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Scheduler.Forms.CalendarView());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Scheduler.Forms.CalendarView());
}
catch(Exception e)
{
WriteLine($"{totalPassed}/{totalTests} Passed!\n" +
$"{totalTests - totalPassed} remain unexecuted!\n" +
$"Failed at assert number: {totalPassed + 1}");
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
}
}
\ No newline at end of file
......@@ -16,11 +16,6 @@ public class ProgramData
public Calendar loadCalendar()
{
Calendar MyCalendar = new Calendar();
MyCalendar.Days = Days;
MyCalendar.DefaultWorkingHours = DefaultWorkingHours;
MyCalendar.DefaultWorkingHoursInterval = DefaultWorkingHoursInterval;
return MyCalendar;
return new Calendar(Days, DefaultWorkingHoursInterval, DefaultWorkingHours);
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1F6FE665-3863-440A-97E8-12B5F87E0078}</ProjectGuid>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<RootNamespace>Scheduler</RootNamespace>
<AssemblyName>Scheduler</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
......@@ -31,6 +31,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
......
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