1 module common; 2 3 import std.conv; 4 import std.format; 5 import hunt.util.Common; 6 import hunt.util.Comparator; 7 8 9 class Price { 10 11 private string item; 12 private int price; 13 14 this(string itm, int pr) { 15 this.item = itm; 16 this.price = pr; 17 } 18 19 string getItem() { 20 return item; 21 } 22 23 void setItem(string item) { 24 this.item = item; 25 } 26 27 int getPrice() nothrow { 28 return price; 29 } 30 31 void setPrice(int price) { 32 this.price = price; 33 } 34 35 override size_t toHash() @trusted nothrow { 36 size_t hashcode = 0; 37 hashcode = price * 20; 38 hashcode += hashOf(item); 39 return hashcode; 40 } 41 42 override bool opEquals(Object obj) { 43 Price pp = cast(Price) obj; 44 if (pp is null) 45 return false; 46 return (pp.item == this.item && pp.price == this.price); 47 } 48 49 override string toString() { 50 return "item: " ~ item ~ " price: " ~ price.to!string(); 51 } 52 } 53 54 55 class Person : Comparable!Person { 56 57 string name; 58 int age; 59 60 this(string n, int a) { 61 name = n; 62 age = a; 63 } 64 65 override string toString() { 66 return format("Name is %s, Age is %d", name, age); 67 } 68 69 int opCmp(Person o) { 70 int nameComp = compare(this.name, o.name); 71 return (nameComp != 0 ? nameComp : compare(this.age, o.age)); 72 } 73 74 alias opCmp = Object.opCmp; 75 76 } 77 78 class ComparatorByPrice : Comparator!Price{ 79 80 int compare(Price v1, Price v2) nothrow { 81 return .compare(v1.getPrice, v2.getPrice); 82 } 83 84 }