Created
April 8, 2012 11:17
-
-
Save hahmed/2336677 to your computer and use it in GitHub Desktop.
Company, Model and its keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace dictionary | |
{ | |
public class CustomerModel | |
{ | |
public string Model { get; set; } | |
public string Key { get; set; } | |
} | |
public class CustomerList | |
{ | |
private readonly IDictionary<string, IList<CustomerModel>> Keys; | |
public CustomerList() | |
{ | |
//initialize dictionary - declare company plus all the models with its corresponding key | |
Keys = new Dictionary<string, IList<CustomerModel>>() | |
{ | |
{ "Acer", | |
new List<CustomerModel> | |
{ | |
new CustomerModel {Model = "Aspire 1300 Series", Key = "somekey343jnj43n" }, | |
new CustomerModel {Model = "Aspire 1350 Series", Key = "98s9djisdjihuhskey" }, | |
new CustomerModel {Model = "Aspire 1360 Series", Key = "98s9djisdjihuhskey" }, | |
new CustomerModel {Model = "Aspire 1501LMi", Key = "98s9djisdjihuhskey" }, | |
new CustomerModel {Model = "Aspire 1800 Series", Key = "98s9djisdjihuhskey" } | |
} | |
}, | |
{ "Sony", | |
new List<CustomerModel> | |
{ | |
new CustomerModel {Model = "VAIO SB 13 Series", Key = "98sa9du9asj9" }, | |
new CustomerModel {Model = "VAIO SE 15 Series", Key = "sadsad" }, | |
new CustomerModel {Model = "VAIO SA 19 Series", Key = "ajsoidj" } | |
} | |
}, | |
{ "Samsung", | |
new List<CustomerModel> | |
{ | |
new CustomerModel {Model = "300E5a", Key = "kewiiuh4" }, | |
new CustomerModel {Model = "300E5b", Key = "k4j5okn" }, | |
new CustomerModel {Model = "300E5c", Key = "kdmfkmds" } | |
} | |
} | |
}; | |
} | |
/// <summary> | |
/// get a list of all models by this customer | |
/// </summary> | |
/// <param name="company">case sensitive</param> | |
/// <returns></returns> | |
public IList<CustomerModel> GetAllModelsForCompany(string company) | |
{ | |
//we could not find the company... we can throw exception, but it is better to log the error or fail silently... | |
if (!Keys.ContainsKey(company)) | |
throw new KeyNotFoundException("company not declared"); | |
return Keys[company]; | |
} | |
/// <summary> | |
/// determine if company exists | |
/// </summary> | |
/// <param name="company"></param> | |
/// <returns></returns> | |
public bool DoesCompanyExist(string company) | |
{ | |
return Keys.ContainsKey(company); | |
} | |
/// <summary> | |
/// get a list of all models by this customer | |
/// </summary> | |
/// <param name="company">case sensitive- name of company</param> | |
/// <param name="model">case sensitive- name of model</param> | |
/// <returns>key for the model</returns> | |
public string GetKeyForCompany(string company, string model) | |
{ | |
//we could not find the company... we can throw exception, but it is better to log the error or fail silently... | |
if (!Keys.ContainsKey(company)) | |
throw new KeyNotFoundException("company not declared"); | |
//we can only have one model and one key (for a company), this will fail if we find more than one model for a company | |
return Keys[company].SingleOrDefault(x => x.Model == model).Key; | |
} | |
} | |
} | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using NUnit.Framework; | |
namespace dictionary | |
{ | |
[TestFixture] | |
public class TestCustomerKeys | |
{ | |
[Test] | |
public void CustomerSonyHasMoreThanOneModel() | |
{ | |
//initialize class | |
var customers = new CustomerList(); | |
var list = customers.GetAllModelsForCompany("Sony"); | |
Assert.Greater(list.Count, 1); | |
} | |
[Test] | |
public void CustomerSonyShouldExist() | |
{ | |
//initialize class | |
var customers = new CustomerList(); | |
Assert.IsTrue(customers.DoesCompanyExist("Sony")); | |
} | |
[Test] | |
public void CustomerTESTShouldNotExist() | |
{ | |
//initialize class | |
var customers = new CustomerList(); | |
Assert.IsFalse(customers.DoesCompanyExist("TEST")); | |
} | |
[Test] | |
public void ModelShouldExistForCompany() | |
{ | |
//initialize class | |
var customers = new CustomerList(); | |
var key = "98sa9du9asj9"; | |
var modelKey = customers.GetKeyForCompany("Sony", "VAIO SB 13 Series"); | |
Assert.AreEqual(modelKey,key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment