Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Currency (Using with)

Part of OperatorOverloadingCategory

Description

A currency object featuring some operator overloading (uses with).

Example

/*
    Compile with the "TEST" version set, to output more information to the console during run.
*/

import std.string;
import std.math;


char[] leadingZeros(byte num, byte digits)
{ 
    char[] buffer;
    byte diff;
    
    buffer = toString(num);
    if (buffer.length < digits) 
    {
        diff = digits - buffer.length;
        for(int i=0; i < diff; i++)
          buffer = "0" ~ buffer;
    } 
    return buffer;
}


char[] toStringFromDouble(double d)
{
    return toStringFromDouble(d, 2); /* defaults to 2 decimal places */ 
}

char[] toStringFromDouble(double d, int decPlaces)
{
    return toStringFromDouble(d, decPlaces, "."); /* separator defaults to period */ 
}

char[] toStringFromDouble(double i, int decPlaces, char[] sep)
{  
    int whole;
    int dec;
    double decPart;
    
    whole = cast(int) i;
    version(TEST) printf("whole: %d\n", whole);

    decPart = (i - whole) * pow(cast(real) 10, cast(uint) decPlaces);
    version(TEST) printf("decPart: %lf\n", decPart);
    
    dec = cast(int) (decPart);
    version(TEST) printf("dec: %d\n", dec);
    return toString(i) ~ sep ~ leadingZeros(dec, decPlaces);
}


class Currency
{   
    /* Read property ("get-ter") */
    double value() {return intValue;}
    
    /* Write property ("set-ter") */
    double value(double v) {return intValue = v;}

    
    this(double v)
    {
        version(TEST) printf("An instance of the Currency object is created.\n\n");
        intValue = v;
        intSeparator = ".";
        intDecPlaces = 2;
    }

    this()
    {
        this(0);
    }

    char[] toString() { return toStringFromDouble(intValue, intDecPlaces, intSeparator);}


    /* operator overloading */

    Currency opAdd(Currency m, Currency n)
    {  
        Currency t;
        t.value = m.value + n.value;
        return t;
    }

    Currency opAddAssign(Currency m)
    {  
        intValue += m.value;
        return this;
    }


    private 
    {
        double intValue;
        double intDecPlaces;
        char[] intSeparator; /* decimal, period (U.S.), comma (Some non-U.S. locations) */
    }
}


void main()
{ 
    Currency c; // = new Currency();  

    with(c = new Currency())
    { 
        printf("\n\n\t\t\t\t\t\t%.*s\n", toString);
    
        printf("Change the value\t\t\t\t");
        value = 100.75;
        printf("%.*s\n", toString);
    }
    
    Currency c2;    
    with(c2 = new Currency(26.12))
    { 
        printf("\t\t\t\t\t\t%.*s\n", toString);
    
        printf("Change the value\t\t\t\t");
        value = 6.25;
        printf("%.*s\n", toString);
    
        printf("Change the value (operator overloading)\t\t");
        c2 += c; /* requires operator overloading */

        printf("%.*s\n\n", toString);
    }
}

Source

Link http://jcc_7.tripod.com/d/tutor/with.html
Author jcc7
Date 2004/01/03