home *** CD-ROM | disk | FTP | other *** search
- /*
- this is an example of a class to handle money as an abstract type
- first, define a Money class with a variable to hold an
- integer number of cents; this will be converted into
- dollars and cents whenever the Money variable is converted to a String
- */
- inherit(Int,Money,[]);
-
- /*
- set the number of cents in self
- NOTE: self must be passed by address for this to work !
- */
- method Money::setCents(*self,c)
- {
- replace(parent(*self),c);
- }
-
- /*
- return the number of dollars in self
- */
- method Money::dollars(self)
- {
- return(self/100);
- }
-
- /*
- return the number of cents in self
- */
- method Money::cents(self)
- {
- return(self - (dollars(self)*100));
- }
-
- /*
- the "asString" method allows a Money variable to be converted
- to a String and/or printed
- */
- method Money::asString(self)
- {
- return("$ "+asString(dollars(self))+"."+format(cents(self),"%02d"));
- }
-
- method Int::asMoney(self)
- {
- return(new(Money,self));
- }
-
- /*
- test case
- */
- m = new(Money,100);
- setCents(&m,104);
- ? m;
- ? asMoney(m+1);
- ? asMoney(m*10);
-