home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / do1beta.zip / MONEY.DO < prev    next >
Text File  |  1991-07-08  |  1KB  |  56 lines

  1. /*
  2.     this is an example of a class to handle money as an abstract type
  3.     first, define a Money class with a variable to hold an
  4.     integer number of cents; this will be converted into
  5.     dollars and cents whenever the Money variable is converted to a String
  6. */
  7. inherit(Int,Money,[]);
  8.  
  9. /*
  10.     set the number of cents in self
  11.     NOTE: self must be passed by address for this to work !
  12. */
  13. method Money::setCents(*self,c) 
  14. {
  15.     replace(parent(*self),c);
  16. }
  17.  
  18. /*
  19.     return the number of dollars in self
  20. */
  21. method Money::dollars(self)
  22. {
  23.     return(self/100);
  24. }
  25.  
  26. /*
  27.     return the number of cents in self
  28. */
  29. method Money::cents(self)
  30. {
  31.     return(self - (dollars(self)*100));
  32. }
  33.  
  34. /*
  35.     the "asString" method allows a Money variable to be converted 
  36.     to a String and/or printed
  37. */
  38. method Money::asString(self)
  39. {
  40.     return("$ "+asString(dollars(self))+"."+format(cents(self),"%02d"));
  41. }
  42.  
  43. method Int::asMoney(self)
  44. {
  45.     return(new(Money,self));
  46. }
  47.  
  48. /*
  49.     test case
  50. */
  51. m = new(Money,100);
  52. setCents(&m,104);
  53. ? m;
  54. ? asMoney(m+1);
  55. ? asMoney(m*10);
  56.