-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourse.cs
More file actions
116 lines (102 loc) · 4.73 KB
/
Course.cs
File metadata and controls
116 lines (102 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_1
{
public enum Stream { Java = 1, CSharp }
public enum Type { Full = 1, Part }
class Course
{
// Member variable, counts the number of succesfull constructed objects
private static ushort counter = 0;
// Properties
public string Title { get; set; }
public Stream Stream { get; set; } // Java, C#
public Type Type { get; set; } // Full, Part
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
// Default Constructor
public Course()
{
counter++; // Increases automatically every time a new object is created
Console.WriteLine($"\nCourse #{counter}");
}
// Constructor with parameters | Used for initializing the Synthetic data at run-time
public Course(string title, Stream stream, Type type, DateTime startDate, DateTime endDate)
{
Title = title;
Stream = stream;
Type = type;
StartDate = startDate;
EndDate = endDate;
}
// Method of type Course | The user can input data at run-time
public Course InputCourseData()
{
Console.Write("Title: ");
var title = Console.ReadLine();
Console.Write("Stream (1 for Java)|2 for C#): ");
var stream = (Stream)Enum.Parse(typeof(Stream), Console.ReadLine());
Console.Write("Type (1 for Full-Time|2 for Part-Time): ");
var type = (Type)Enum.Parse(typeof(Type), Console.ReadLine());
Console.Write("Start Date (yyyy/mm/dd): ");
var startDate = Convert.ToDateTime(Console.ReadLine());
Console.Write("End Date (yyyy/mm/dd): ");
var endDate = Convert.ToDateTime(Console.ReadLine());
Title = title;
Stream = stream;
Type = type;
StartDate = startDate;
EndDate = endDate;
// Initialize a variable of type Course using the Constructor with parameters
var course = new Course(title, stream, type, startDate, endDate);
return course;
}
// Create courses on-demand and store them in an accessible dictionary.
public static Dictionary<short, Course> CreateCourses(short numberToCreateCourses)
{
var newCoursesDictionary = new Dictionary<short, Course>(); // Used to store the recently created course(s)
Course newCourse = null; // Used to create a new course every time
short primaryKey = 0; // Auto increment Primary Key (ID) to uniquely identify a recently created course
for (ushort i = 0; i < numberToCreateCourses; i++)
{
newCourse = new Course();
primaryKey++;
newCoursesDictionary.Add(primaryKey, newCourse.InputCourseData()); // TValue is initialized via method
}
return newCoursesDictionary;
}
// Method to copy the contents <TKey, TValue> of a dictionary to another (similar with the List.AddRange).
// In our case, the AddRangeDictionary() copies the contents of the recently created courses dictionary
// (source) into the courses dictionary (destination), which is declared in the Main Program.
public static Dictionary<short, Course> AddRangeDictionary(Dictionary<short, Course> sourceDictionary, Dictionary<short, Course> destinationDictionary)
{
foreach (KeyValuePair<short, Course> pair in sourceDictionary) // KeyValuePair defines a pair to be set
{
destinationDictionary.Add(pair.Key, pair.Value);
}
return destinationDictionary;
}
// Prints the courses data contained in the corresponding dictionary
public static void PrintCourses(Dictionary<short, Course> dictionaryOfCoursesToPrint)
{
Console.Clear();
Console.WriteLine("\n***A LIST OF ALL THE COURSES OF THE PRIVATE SCHOOL***\n");
foreach (var course in dictionaryOfCoursesToPrint)
{
Console.WriteLine($"{course.ToString()}");
}
Console.Write("\n\nPress any key to continue...");
Console.ReadKey();
Console.Clear();
}
// Override method ToString() to print the contents of a collection (courses dictionary)
public override string ToString()
{
return ($"Title: {Title}, Stream: {Stream}, Type: {Type}, Start Date: {StartDate.ToShortDateString()}, " +
$"End Date {EndDate.ToShortDateString()}");
}
}
}