fork download
  1. #lang racket
  2.  
  3. (define Message%
  4. (class object%
  5. (init aString)
  6. (field (text aString))
  7. (super-new)
  8. (define/public (printIt)
  9. (println text))))
  10.  
  11. (define Square%
  12. (class object%
  13. (init side)
  14. (field (this-side side))
  15. (super-new)
  16. (define/public (calculateArea)
  17. (expt this-side 2))))
  18.  
  19. (define Circle%
  20. (class object%
  21. (init radius)
  22. (field (this-radius radius))
  23. (super-new)
  24. (define/public (calculateArea)
  25. (* pi (expt this-radius 2)))))
  26.  
  27. (define BalanceError "現在の口座残高は~aしかありません。")
  28.  
  29. (define BankAccount%
  30. (class object%
  31. (init initialAmount)
  32. (field (balance initialAmount))
  33. (printf "口座を開設しました。口座残高は~aです。~%" balance)
  34. (super-new)
  35. (define/public (deposit amount)
  36. (set! balance (+ balance amount)))
  37. (define/public (withdraw amount)
  38. (if (>= balance amount)
  39. (set! balance (- balance amount))
  40. (error 'BalanceError BalanceError balance)))
  41. (define/public (getBalance)
  42. balance)
  43. (define/public (transfer amount account)
  44. (with-handlers ((exn:fail?
  45. (lambda (exn)
  46. (printf BalanceError balance)
  47. (newline))))
  48. (withdraw amount)
  49. (send account deposit amount)))))
  50.  
  51. (define InterestAccount%
  52. (class BankAccount%
  53. (super-new)
  54. (inherit-field balance)
  55. (define/override (deposit amount)
  56. (super deposit amount)
  57. (set! balance (* balance 1.03)))))
  58.  
  59. (define ChargingAccount%
  60. (class BankAccount%
  61. (super-new)
  62. (init-field (fee 3))
  63. (define/override (withdraw amount)
  64. (super withdraw (+ amount fee)))))
  65.  
  66. (module+ main
  67.  
  68. (define m1 (new Message% (aString "Hello world")))
  69. (define m2 (new Message% (aString "楽しかったです。さようなら。")))
  70.  
  71. (define note `(,m1 ,m2))
  72. (for-each (lambda (msg)
  73. (send msg printIt)) note)
  74.  
  75. (define lst `(,(new Circle% (radius 5))
  76. ,(new Circle% (radius 7))
  77. ,(new Square% (side 9))
  78. ,(new Circle% (radius 3))
  79. ,(new Square% (side 12))))
  80.  
  81. (for-each (lambda (shape)
  82. (printf "面積は、~a~%" (send shape calculateArea))) lst)
  83.  
  84. ;; 標準のBankAccountのテスト
  85. (define a (new BankAccount% (initialAmount 500)))
  86. (define b (new BankAccount% (initialAmount 200)))
  87. (send a withdraw 100)
  88. ;; (send a withdraw 1000)
  89. (send a transfer 100 b)
  90. (printf "A=~a~%" (send a getBalance)) ; 300になる
  91. (printf "B=~a~%" (send b getBalance)) ; 300になる
  92.  
  93. ;; InterestAccount のテスト
  94. (define c (new InterestAccount% (initialAmount 1000)))
  95. (send c deposit 100)
  96. (printf "C=~a~%" (send c getBalance)) ; 1133になる
  97.  
  98. ;; ChargingAccountのテスト
  99. (define d (new ChargingAccount% (initialAmount 300)))
  100. (send d deposit 200)
  101. (printf "D=~a~%" (send d getBalance)) ; 500になる
  102. (send d withdraw 50)
  103. (printf "D=~a~%" (send d getBalance)) ; 447になる
  104. (send d transfer 100 a)
  105. (printf "A=~a~%" (send a getBalance)) ; 400になる
  106. (printf "D=~a~%" (send d getBalance)) ; 344になる
  107.  
  108. ;; 最後に、ChargingAccountからInterestAccountに振り込みする
  109. ;; ChagingAccountでは振り込み手数料が発生し、
  110. ;; InterestAccountでは利息が発生する
  111. (printf "C=~a~%" (send c getBalance)) ; 1133である
  112. (printf "D=~a~%" (send d getBalance)) ; 344である
  113. (send d transfer 20 c)
  114. (printf "C=~a~%" (send c getBalance)) ; 1187.59である
  115. (printf "D=~a~%" (send d getBalance)) ; 321になる
  116. )
Success #stdin #stdout 1.34s 119456KB
stdin
Standard input is empty
stdout
Standard output is empty