Completed assignment 2

This commit is contained in:
Andreas 2022-12-08 13:24:24 +01:00
parent 6823d35f81
commit 2bdb786e8b
2 changed files with 53 additions and 7 deletions

View File

@ -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<int, string> 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)}");
}
}
}

View File

@ -15,22 +15,52 @@ namespace OOP_Opdracht_CSharp_Alfa_2022.classes
// Define methods
// Define Getters and Setters
// Table data get/set methods
public Dictionary<int, string> getTableData()
{
return this.table_data;
}
public void setTableData(Dictionary<int, string> table_data)
{
this.table_data = table_data;
return;
}
// Record get/set methods
public void setRecord(int key, string value)
{
var record = new KeyValuePair<int, string>(key, value);
return;
}
// Overload in case of previously instantiated KeyValuePair as parameter
public void setRecord(KeyValuePair<int, string> keyValuePair)
{
this.setRecord(keyValuePair.Key, keyValuePair.Value);
return;
}
public KeyValuePair<int, string> getRecord()
{
return this.record;
}
// Get KeyValuePair by id, returns [-1, "Invalid Record"] when out of bounds
public KeyValuePair<int, string> find(int id = 1)
{
KeyValuePair<int, string> record = new KeyValuePair<int, string>(0, null);
if (table_data.ContainsKey(id))
KeyValuePair<int, string> record = new KeyValuePair<int, string>(-1, "Invalid Record");
if (this.table_data.ContainsKey(id))
{
record = new KeyValuePair<int, string>(id, table_data.GetValueOrDefault(id));
record = new KeyValuePair<int, string>(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<int, string>();
this.table_data.Add(1, "Battlefield 2042");
this.table_data.Add(2, "Counter-Strike: Global Offensive");
this.table_data.Add(3, "Rust");
}