-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrainer.cs
More file actions
102 lines (89 loc) · 4.02 KB
/
Trainer.cs
File metadata and controls
102 lines (89 loc) · 4.02 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_1
{
class Trainer
{
// Member variable, counts the number of succesfull constructed objects
private static ushort counter = 0;
// Properties
public string FirstName { get; set; }
public string LastName { get; set; }
public string Subject { get; set; }
// Default Constructor
public Trainer()
{
counter++; // Increases automatically every time a new object is created
Console.WriteLine($"\nTrainer #{counter}");
}
// Constructor with Parameters | Used for initializing the Synthetic data at run-time
public Trainer(string firstName, string lastName, string subject)
{
FirstName = firstName;
LastName = lastName;
Subject = subject;
}
// Method of type Trainer | The user can input data at run-time
public Trainer InputTrainerData()
{
Console.Write("FirstName: ");
var firstName = Console.ReadLine();
Console.Write("LastName: ");
var lastName = Console.ReadLine();
Console.Write("Subject: ");
var subject = Console.ReadLine();
FirstName = firstName;
LastName = lastName;
Subject = subject;
// Initialize a variable of type Trainer using the Constructor with parameters
var trainer = new Trainer(firstName, lastName, subject);
return trainer;
}
// Create trainers on-demand and store them in an accessible dictionary.
public static Dictionary<short, Trainer> CreateTrainers(short numberToCreateTrainers)
{
var newTrainersDictionary = new Dictionary<short, Trainer>(); // Used to store the recently created trainer(s)
Trainer newTrainer = null; // Used to create a new trainer every time
short primaryKey = 0; // Auto increment Primary Key (ID) to uniquely identify a recently created trainer
for (ushort i = 0; i < numberToCreateTrainers; i++)
{
newTrainer = new Trainer();
primaryKey++;
newTrainersDictionary.Add(primaryKey, newTrainer.InputTrainerData()); // TValue is initialized via method
}
return newTrainersDictionary;
}
// 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 trainers dictionary
// (source) into the trainers dictionary (destination), which is declared in the Main Program.
public static Dictionary<short, Trainer> AddRangeDictionary(Dictionary<short, Trainer> sourceDictionary, Dictionary<short, Trainer> destinationDictionary)
{
foreach (KeyValuePair<short, Trainer> pair in sourceDictionary) // KeyValuePair defines a pair to be set
{
destinationDictionary.Add(pair.Key, pair.Value);
}
return destinationDictionary;
}
// Prints the trainers data contained in the corresponding dictionary
public static void PrintTrainers(Dictionary<short, Trainer> dictionaryOfTrainersToPrint)
{
Console.Clear();
Console.WriteLine("\n***A LIST OF ALL THE TRAINERS OF THE PRIVATE SCHOOL***\n");
foreach (var trainer in dictionaryOfTrainersToPrint)
{
Console.WriteLine($"{trainer.ToString()}");
}
Console.Write("\n\nPress any key to continue...");
Console.ReadKey();
Console.Clear();
}
// Override method ToString() to print the contents of a collection (trainers dictionary)
public override string ToString()
{
return ($"FirstName: {FirstName}, LastName: {LastName}, Subject: {Subject}");
}
}
}