money
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | unit money; {$mode objfpc}{$H+} interface uses Classes, SysUtils; type TMoney = class; TMoneyBag = class; IMoney = interface ['{2E0160F6-312C-D911-8DE5-DD8AC3E7C6F4}'] function add(m: IMoney): IMoney; function addMoney(m: TMoney): IMoney; function addMoneyBag(mb: TMoneyBag): IMoney; function isZero: boolean; function multiply(factor: integer): IMoney; function negate: IMoney; function subtract(m: IMoney): IMoney; procedure appendTo(m: TMoneyBag); function toString: String; function equals(m: IMoney): boolean; function Count: integer; function _Self: TObject; end; ISingleCurrencyMoney = interface(IMoney) ['{D6D97717-E52D-D911-83C4-8233402A6B6C}'] function GetCurrencyUnit: string; function GetAmount: int64; property Amount: int64 read GetAmount; property CurrencyUnit: string read GetCurrencyUnit; end; TMoney = class(TInterfacedObject, IMoney, ISingleCurrencyMoney) private FAmount: int64; FCurrencyUnit: String; function GetAmount: int64; function GetCurrencyUnit: string; public constructor Create(aAmount: int64; aCurrencyUnit: String); function add(m: IMoney): IMoney; function addMoney(m: TMoney): IMoney; function addMoneyBag(mb: TMoneyBag): IMoney; function isZero: Boolean; function multiply(factor: Integer): IMoney; function negate: IMoney; function subtract(m: IMoney): IMoney; procedure appendTo(m: TMoneyBag); function toString: String; function equals(m: IMoney): boolean; property Amount: int64 read GetAmount; property CurrencyUnit: string read GetCurrencyUnit; function Count: integer; function _Self: TObject; end; TMoneyBag = class(TInterfacedObject, IMoney) private FMonies: TFPList; function AddToMoniesList(const Item: IInterface): Integer; function RemoveFromMoniesList(const Item: IInterface): Integer; function FindMoney(aCurrencyUnit: string): ISingleCurrencyMoney; function Contains(m: ISingleCurrencyMoney): boolean; public constructor Create; class function CreateWith(m1: IMoney; m2: IMoney): IMoney; destructor Destroy; override; function Simplify: IMoney; function add(m: IMoney): IMoney; function addMoney(m: TMoney): IMoney; function addMoneyBag(mb: TMoneyBag): IMoney; procedure appendBag(aBag: TMoneyBag); procedure appendMoney(aMoney: ISingleCurrencyMoney); function isZero: boolean; function multiply(factor: integer): IMoney; function negate: IMoney; function subtract(m: IMoney): IMoney; procedure appendTo(m: TMoneyBag); function toString: String; function equals(m: IMoney): boolean; function Count: integer; function _Self: TObject; end; Operator + (c: IMoney; c1: IMoney) c2: IMoney; Operator - (c: IMoney; c1: IMoney) c2: IMoney; Operator * (c: IMoney; i: integer) c2: IMoney; implementation Operator + (c: IMoney; c1: IMoney) c2: IMoney; begin c2 := c.add(c1); end; Operator - (c: IMoney; c1: IMoney) c2: IMoney; begin c2 := c.subtract(c1); end; Operator * (c: IMoney; i: integer) c2: IMoney; begin c2 := c.multiply(i); end; function TMoneyBag.AddToMoniesList(const Item: IInterface): Integer; begin Result := FMonies.Add(nil); IInterface(FMonies.List^[Result]) := Item;end; function TMoneyBag.RemoveFromMoniesList(const Item: IInterface): Integer; begin Result := FMonies.IndexOf(Pointer(Item)); if Result > -1 then begin IInterface(FMonies.List^[Result]) := nil; FMonies.Delete(Result); end;end; function TMoneyBag.FindMoney(aCurrencyUnit: string): ISingleCurrencyMoney; var i: Integer;begin for i := 0 to FMonies.Count - 1 do if ISingleCurrencyMoney(FMonies.items[i]).CurrencyUnit = aCurrencyUnit then begin Result := ISingleCurrencyMoney(FMonies.items[i]); Exit; end; end; function TMoneyBag.Contains(m: ISingleCurrencyMoney): boolean;var found: ISingleCurrencyMoney; begin found := FindMoney(m.CurrencyUnit); if found = nil then begin Result := false; Exit; end; Result := (found.Amount = m.amount);end; class function TMoneyBag.CreateWith(m1: IMoney; m2: IMoney): IMoney; var mb: IMoney;begin mb := TMoneyBag.Create; m1.AppendTo(TMoneyBag(mb._Self)); m2.AppendTo(TMoneyBag(mb._Self)); Result := TMoneyBag(mb._Self).Simplify;end; constructor TMoneyBag.Create; begin FMonies := TFPList.Create;end; destructor TMoneyBag.Destroy; var i: integer;begin for i := 0 to FMonies.Count - 1 do IInterface(FMonies.items[i])._release; FMonies.Free; inherited Destroy;end; function TMoneyBag.Simplify: IMoney; begin if FMonies.Count = 1 then Result := IMoney(FMonies.items[0]) else Result := Self; end; function TMoneyBag.add(m: IMoney): IMoney; begin Result := m.AddMoneyBag(Self); end; function TMoneyBag.addMoney(m: TMoney): IMoney; begin Result := TMoneyBag.CreateWith(m, Self); end; function TMoneyBag.addMoneyBag(mb: TMoneyBag): IMoney; begin Result := TMoneyBag.CreateWith(mb, Self); end; procedure TMoneyBag.appendBag(aBag: TMoneyBag); var i: integer; begin for i := 0 to aBag.FMonies.Count - 1 do appendMoney(ISingleCurrencyMoney(aBag.FMonies.Items[i])); end; procedure TMoneyBag.appendMoney(aMoney: ISingleCurrencyMoney); var old: IMoney; sum: IMoney; begin if aMoney.isZero then Exit; old := findMoney(aMoney.CurrencyUnit); if old = nil then begin AddToMoniesList(aMoney); Exit; end; sum := old.Add(aMoney); RemoveFromMoniesList(old); if sum.isZero then Exit; AddToMoniesList(sum); end; function TMoneyBag.isZero: boolean; begin Result := FMonies.Count = 0; end; function TMoneyBag.multiply(factor: integer): IMoney; var i: Integer; begin Result := TMoneyBag.Create; if factor <> 0 then for i := 0 to FMonies.Count - 1 do begin TMoneyBag(Result._Self).appendMoney(ISingleCurrencyMoney( ISingleCurrencyMoney(FMonies.items[i]).Multiply(factor))); end; end; function TMoneyBag.negate: IMoney;var i: integer; begin Result := TMoneyBag.Create; for i := 0 to FMonies.Count - 1 do begin TMoneyBag(Result._Self).appendMoney(ISingleCurrencyMoney( ISingleCurrencyMoney(FMonies.items[i]).negate)); end; end; function TMoneyBag.subtract(m: IMoney): IMoney; begin Result := Add(m.negate); end; procedure TMoneyBag.appendTo(m: TMoneyBag); begin m.AppendBag(Self); end; function TMoneyBag.toString: String; var i: integer; begin Result := '{'; for i := 0 to FMonies.Count - 1 do Result := Result + ISingleCurrencyMoney(FMonies.items[i]).ToString; Result := Result + '}'; end; function TMoneyBag.equals(m: IMoney): boolean; var aMoneyBag: TMoneyBag; i: integer; ism: ISingleCurrencyMoney; begin if m = nil then begin Result := false; Exit; end; if isZero then begin Result := m.isZero; Exit; end; if m._Self.ClassType = TMoneyBag then begin aMoneyBag := TMoneyBag(m._Self); if aMoneyBag.FMonies.count <> FMonies.Count then begin Result := false; Exit; end; for i := 0 to FMonies.Count - 1 do begin ism := ISingleCurrencyMoney(FMonies.items[i]); if not aMoneyBag.Contains(ism) then begin Result := false; Exit; end; end; Result := true; Exit; end; Result := false; end; function TMoneyBag.Count: integer; begin Result := FMonies.Count; end; function TMoneyBag._Self: TObject; begin Result := Self; end; { TMoney } function TMoney.GetCurrencyUnit: string; begin Result := FCurrencyUnit; end; function TMoney.GetAmount: int64; begin Result := FAmount; end; constructor TMoney.Create(aAmount: int64; aCurrencyUnit: string); begin FAmount := aAmount; FCurrencyUnit := aCurrencyUnit; end; function TMoney.add(m: IMoney): IMoney;begin Result := m.AddMoney(Self); end; function TMoney.addMoney(m: TMoney): IMoney;begin if (m.CurrencyUnit = Self.CurrencyUnit) then Result := TMoney.Create(Self.Amount + m.Amount, Self.CurrencyUnit) else Result := TMoneyBag.CreateWith(Self, M);end; function TMoney.addMoneyBag(mb: TMoneyBag): IMoney; begin Result := mb.AddMoney(Self);end; function TMoney.isZero: Boolean; begin Result := Amount = 0;end; function TMoney.multiply(factor: Integer): IMoney; begin Result := TMoney.Create(Amount * factor, CurrencyUnit);end; function TMoney.negate: IMoney; begin Result := TMoney.Create(- Amount, CurrencyUnit);end; function TMoney.subtract(m: IMoney): IMoney; begin Result := Add(m.negate);end; procedure TMoney.appendTo(m: TMoneyBag); begin m.AppendMoney(Self);end; function TMoney.toString: String; begin Result := '[' + IntToStr(FAmount) + ' '+ FCurrencyUnit + ']';end; function TMoney.equals(m: IMoney): boolean; var ism: ISingleCurrencyMoney;begin if Assigned(m) then begin if isZero then Result := m.isZero; if m._Self.ClassType = TMoney then begin ism := ISingleCurrencyMoney(m); Result := (ism.Amount = Amount) and (ism.CurrencyUnit = CurrencyUnit) end else Result := false; end else Result := false; end; function TMoney.Count: integer; begin Result := 1; end; function TMoney._Self: TObject; begin Result := Self; end; end. |
¿Has encontrado algún error? ¿Tienes la solución? Dejame tu correción ;-)
Antes de comentar: Gran parte de los ejercicios propuestos no tienen librerías debido a que Wordpress las eliminó al verlas como etiquetas HTML. Si sabes/tienes/conoces las librerías que hacen falta, déjalo en los comentarios. Y lo mas importante: Todos los ejemplos fueron realizados por personas con únicamente conocimiento básico del lenguaje, no de programación.
Otro punto importante: Si vas a sugerir un segmento de código en algún lenguaje debes hacerlo así:
- Si es lenguaje C <code lang="c">Código en C</code>
- Si es lenguaje Pascal <code lang="pascal">Aquí dentro el código de Pascal</code>.
De esta manera el código coloreas el código.
Otro punto importante para muchos que crees que te he ignorado: Todos los comentarios los reviso y en su debido momento los apruebo, pero ojo con el con lo siguiente:Me reservo el derecho de alterar, publicar o no los comentarios así como también cambiar mis condiciones en el momento que yo lo requiera.
¿Si estas de acuerdo? Adelante! que ya te he quitado bastante tiempo leyendo esta basura :)