diff --git a/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/Program.cs b/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/Program.cs index c49de90..65a24dd 100644 --- a/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/Program.cs +++ b/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/Program.cs @@ -1,4 +1,6 @@ using System; +using OOP_Opdracht_CSharp_Alfa_2022.classes; +using System.Collections.Generic; namespace OOP_Opdracht_CSharp_Alfa_2022 { @@ -7,6 +9,20 @@ namespace OOP_Opdracht_CSharp_Alfa_2022 static void Main(string[] args) { Console.WriteLine("Hello World!"); + Opdracht2(); + } + static void Opdracht2() + { + Game game = new classes.Game(); + var someRecord = game.find(1); + Console.WriteLine($"Record 1 is equal to {someRecord}"); + var games = game.getTableData(); + Console.WriteLine("Outputting Table Contents:"); + foreach (KeyValuePair entry in games) + { + Console.WriteLine($"Row with id {entry.Key} has value {entry.Value}"); + } + Console.WriteLine($"Trying to fetch KeyValue with invalid id 5 {game.find(5)}"); } } } diff --git a/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/classes/Game.cs b/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/classes/Game.cs index f3cea30..336daa6 100644 --- a/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/classes/Game.cs +++ b/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/classes/Game.cs @@ -15,22 +15,52 @@ namespace OOP_Opdracht_CSharp_Alfa_2022.classes // Define methods + // Define Getters and Setters + + // Table data get/set methods + public Dictionary getTableData() + { + return this.table_data; + } + public void setTableData(Dictionary table_data) + { + this.table_data = table_data; + return; + } + // Record get/set methods + public void setRecord(int key, string value) + { + var record = new KeyValuePair(key, value); + return; + } + // Overload in case of previously instantiated KeyValuePair as parameter + public void setRecord(KeyValuePair keyValuePair) + { + this.setRecord(keyValuePair.Key, keyValuePair.Value); + return; + } + public KeyValuePair getRecord() + { + return this.record; + } + // Get KeyValuePair by id, returns [-1, "Invalid Record"] when out of bounds public KeyValuePair find(int id = 1) { - KeyValuePair record = new KeyValuePair(0, null); - if (table_data.ContainsKey(id)) + KeyValuePair record = new KeyValuePair(-1, "Invalid Record"); + if (this.table_data.ContainsKey(id)) { - record = new KeyValuePair(id, table_data.GetValueOrDefault(id)); + record = new KeyValuePair(id, this.table_data.GetValueOrDefault(id)); } this.record = record; return record; } - + // Game class constructor public Game() { - table_data.Add(1, "Battlefield 2042"); - table_data.Add(2, "Counter-Strike: Global Offensive"); - table_data.Add(3, "Rust"); + if(this.table_data == null) table_data = new Dictionary(); + this.table_data.Add(1, "Battlefield 2042"); + this.table_data.Add(2, "Counter-Strike: Global Offensive"); + this.table_data.Add(3, "Rust"); }