Changed Contract to be a struct because if the public fields are all comparable then you can just ask for equality. There is probably a better way to return the contract string though.
using System; using System.Collections.Generic; using System.Linq; public enum Quality { SD, HD, UHD } public class TV { private struct Contract { public static Contract Premium = new Contract(4, Quality.UHD); public static Contract Standard = new Contract(2, Quality.HD); public static Contract Essential = new Contract(1, Quality.SD); public int Number; public Quality Quality; public Contract(int number, Quality quality) { Number = number; Quality = quality; } } public static string GetContract(int numDevices, Quality quality) { var desiredContract = new Contract(numDevices, quality); return desiredContract.Equals(Contract.Premium) ? "PREMIUM" : desiredContract.Equals(Contract.Standard) ? "STANDARD" : desiredContract.Equals(Contract.Essential) ? "ESSENTIAL" : "INVALID"; } }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public enum Quality { SD, HD, UHD }
- public class TV
- {
private class Contract- private struct Contract
- {
public string Type;public Func<int, Quality, bool> Condition;public static readonly IList<Contract> Types =new List<Contract>{new Contract { Type = "PREMIUM", Condition = (numDevices, quality) => numDevices == 4 && quality == Quality.UHD },new Contract { Type = "STANDARD", Condition = (numDevices, quality) => numDevices == 2 && quality == Quality.HD },new Contract { Type = "ESSENTIAL", Condition = (numDevices, quality) => numDevices == 1 && quality == Quality.SD },new Contract { Type = "INVALID", Condition = (numDevices, quality) => true }};- public static Contract Premium = new Contract(4, Quality.UHD);
- public static Contract Standard = new Contract(2, Quality.HD);
- public static Contract Essential = new Contract(1, Quality.SD);
- public int Number;
- public Quality Quality;
- public Contract(int number, Quality quality) { Number = number; Quality = quality; }
- }
- public static string GetContract(int numDevices, Quality quality)
- {
return Contract.Types.First(contract => contract.Condition(numDevices, quality)).Type;- var desiredContract = new Contract(numDevices, quality);
- return desiredContract.Equals(Contract.Premium) ? "PREMIUM" :
- desiredContract.Equals(Contract.Standard) ? "STANDARD" :
- desiredContract.Equals(Contract.Essential) ? "ESSENTIAL" :
- "INVALID";
- }
- }