1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
public interface IOperator
{
decimal A { get; }
decimal B { get; }
decimal C { get; }
}
public interface ISurface : IOperator
{
decimal Total { get; }
}
public abstract class Operator : IOperator
{
ISurface _operande;
public ISurface Operande { get { return this._operande; } }
public decimal A
{
get
{
return Compute(_operande.A);
}
}
public decimal B
{
get
{
return Compute(_operande.B);
}
}
public decimal C
{
get
{
return Compute(_operande.C);
}
}
public Operator(ISurface operande)
{
this._operande = operande;
}
public abstract decimal Compute(decimal value);
}
public class Pourcent : Operator
{
public Pourcent(ISurface operande) : base(operande)
{
}
public override decimal Compute(decimal value)
{
return Operande.Total == 0 ? 0 : value * 100 / Operande.Total;
}
}
public class PourcentWithoutA : Operator
{
public decimal TotalWithoutA
{
get
{
return this.Operande.B + this.Operande.C;
}
}
public PourcentWithoutA(ISurface operande) : base(operande)
{
}
public override decimal Compute(decimal value)
{
return this.TotalWithoutA == 0 ? 0 : (value * 100) / this.TotalWithoutA;
}
}
public class Surface : ISurface
{
private decimal _a;
private decimal _b;
private decimal _c;
private readonly Pourcent _pourcentage;
private readonly PourcentWithoutA _pourcentageSansA;
public decimal A
{
get
{
return _a;
}
set
{
_a = value;
}
}
public decimal B
{
get
{
return _b;
}
set
{
_b = value;
}
}
public decimal C
{
get
{
return _c;
}
set
{
_c = value;
}
}
public Pourcent Pourcentage
{
get { return this._pourcentage; }
}
public PourcentWithoutA PourcentageSansA
{
get { return this._pourcentageSansA; }
}
public decimal Total
{
get
{
return this.A + this.B + this.C;
}
}
public Surface()
{
_a = 0;
_b = 0;
_c = 0;
this._pourcentage = new Pourcent(this);
this._pourcentageSansA = new PourcentWithoutA(this);
}
public Surface(decimal a, decimal b, decimal c) : this()
{
this._a = a;
this._b = b;
this._c = c;
}
}
void Main()
{
Surface s = new Surface(1, 2, 3);
Console.WriteLine("s.A = " + s.A);
Console.WriteLine("s.B = " + s.B);
Console.WriteLine("s.C = " + s.C);
Console.WriteLine("s.Pourcentage.A = " + s.Pourcentage.A);
Console.WriteLine("s.Pourcentage.B = " + s.Pourcentage.B);
Console.WriteLine("s.Pourcentage.C = " + s.Pourcentage.C);
Console.WriteLine("s.PourcentageSansA.A = " + s.PourcentageSansA.A);
Console.WriteLine("s.PourcentageSansA.B = " + s.PourcentageSansA.B);
Console.WriteLine("s.PourcentageSansA.C = " + s.PourcentageSansA.C);
} |
Partager