c# - How to access value out of dictionary whose key is complex type? -
in writing insurance premium calculator basic scheme follows: points assigned predetermined number of attributes, such car-value, num-incidents-in-past, years-experience-driving etc. hene, if car worth $3800, lies within 3001 4000 range warrants 30 points in premium calculation. if num-incidents-in-past 3 or below, warrants 0 points. if 4-5 num-inc points warranted 5. if years-exp between 1-5, warrants 12 points. idea arbitrary value being assigned range of values given number of attributes. premium calculations tallying points warranted each attribute category , multiplying factor i.e 2.5. trying use b. liskov's power of abstractions , srp neatly assign responsiblities design calculator extensible , designed.
based on answer provided drharris here is there c# type representing integer range?
how access value out of following dictionary key generic type range defined drharris?
//************************abstractions************************ public abstract class absperson { public virtual abspolicy apolicy { get; set; } public virtual string id { get; set; } public string name { get; set; } public int age { get; set; } public virtual string address { get; set; } } public abstract class abspolicy { public virtual string personid { get; set; } //fk public virtual int propvalue { get; set; } public virtual int insgroup { get; set; } } public abstract class absvaluecategorycalculator: ievaluatepolicy { //data public abstract void initrange(); //reference drharris poster generic type signature - public class range<t> t : icomparable<t> public abstract dictionary<range<int>, int> valuerange {get; set;} public abstract int tally { get; set; } //behaviour public virtual void evaluatepolicydetails(absperson person) { } } public interface ievaluatepolicy { void evaluatepolicydetails(absperson person); } //*************************concretions************************** public class carvaluecategorycalculator : absvaluecategorycalculator { public carvaluecategorycalculator() {//ctor initrange(); } public override void initrange() { this.valuerange = new dictionary<range<int>, int>(); this.valuerange.add(new range<int>() { minimum = 1000, maximum = 2000 }, 10); this.valuerange.add(new range<int>() { minimum = 2001, maximum = 3000 }, 20); this.valuerange.add(new range<int>() { minimum = 3001, maximum = 4000 }, 30); this.valuerange.add(new range<int>() { minimum = 4001, maximum = 5000 }, 40); this.valuerange.add(new range<int>() { minimum = 5001, maximum = 6000 }, 50); this.valuerange.add(new range<int>() { minimum = 6001, maximum = 7000 }, 60); } public override dictionary<range<int>, int> valuerange { get; set; } public override void evaluatepolicydetails(absperson person) { //i trying tally value given wether cars worth lies within range if (this.valuerange.containskey(new range<int>() { maximum = person.apolicy.propvalue, minimum = person.apolicy.propvalue })) { this.tally = } console.writeline("good"); } public override int tally { get;set; } }//end class
(as noted in comments, sam's answer points out dictionary isn't what's wanted here - finds equal keys, whereas op trying find range key contains single value. hash tables aren't geared that.)
you need either override gethashcode
, equals
in range<t>
(which sensible - ideally implementing iequatable<range<t>>
@ same time) or create separate type implements iequalitycomparer<range<t>>
, pass dictionary constructor.
i on range type, this:
public sealed class range<t> : iequatable<range<t>> t : icomparable<t>, iequatable<t> { ... public override int gethashcode() { int hash = 23; hash = hash * 31 + equalitycomparer.default<t>.gethashcode(minimum); hash = hash * 31 + equalitycomparer.default<t>.gethashcode(maximum); return hash; } public override bool equals(object other) { return equals(other range<t>); } public bool equals(range<t> other) { if (referenceequals(other, this)) { return true; } if (referenceequals(other, null)) { return false; } return equalitycomparer<t>.default.equals(minimum, other.minimum) && equalitycomparer<t>.default.equals(maximum, other.maximum); } }
note range<t>
type mutable, - that's bad idea dictionary keys. idea make @ least "shallow-immutable" - there's not lot can if
Comments
Post a Comment