Hello,

I'm trying to create a driver for the money class. I need to create and array of 5 money objects. Also I need to use a loop to determine which money object has the most amount of money using "compareMoney()"

class:

public class Money {

    private int dollars;
    private int cents;

    public Money( int dollars, int cents ) {

        dollars = dollars;
        cents = cents;
        adjustMoney();
    }

    public Money( String m ) {

        if( m.charAt(0) == '$' ) 
            dollars = Integer.parseInt( m.substring( 1, m.indexOf('.') ) );
        else
            dollars = Integer.parseInt( m.substring( 0, m.indexOf('.') ) ); 

        cents = Integer.parseInt( m.substring( m.indexOf('.') + 1 ) );
    }

    public Money() {

        dollars = 0;
        cents = 0;
    }

    private void adjustMoney() {

        if( cents >= 100 ) {

            dollars++;
            cents -= 100;
        }

        if( cents < 0 ) {

            dollars--;
            cents += 100;
        }
    }

    // getter methods.
    public int getDollars() {

        return dollars;
    }

    public int getCents() {

        return cents;
    }

    // instance methods

    public Money addMoney( Money otherMoney ) {

        int newd = dollars + otherMoney.getDollars();
        int newc = cents + otherMoney.getCents();

        return new Money( newd, newc );

    }

    public Money subtractMoney( Money otherMoney ) {

        int newd = dollars - otherMoney.getDollars();
        int newc = cents - otherMoney.getCents();

        return new Money( newd, newc );
    }

    public int compareMoney( Money otherMoney ) {

        if( dollars > otherMoney.getDollars() )
            return 1;
        else 
            if( dollars < otherMoney.getDollars() )
                return -1;
            else
                if( cents > otherMoney.getCents() )
                    return 1;
                else
                    if( cents < otherMoney.getCents() )
                        return -1;
                    else
                        return 0;       
    }

    public String toString() {

        if( cents < 10 )
            return "$" + dollars + ".0" + cents;
        else
            return "$" + dollars + "." + cents;
    }
}

Here is my driver:

public class MoneyTester {
    public static void main(String[] args) {
        // declaring an array of type money with 5 element
        Money[] moneyObject = new Money[5];

        // initialize
        moneyObject[0] = new Money(5,50);

        Money max = moneyObject[0];

        /** money object in the array
        for (int i = 1; i < moneyObject.length; ++i )
            if ( moneyObject[i].compareMoney( max ) == 1 )
            max = moneyObject[i];
        OR BELOW
        **/
        for( int k=0; k<moneyObject.length; k++ ) {
            moneyObject[k] = new Money();
            }

        Our.dollars = dollars;
        Our.cents = cents;

        System.out.println( max );
    }
}

Recommended Answers

All 9 Replies

and what exactly is your question, if you have any?

My MOneyDriver is not working right that's where I need help.

thanks

let me guess.... he can't find

Our.dollars = dollars;
Our.cents = cents;

if your compiler doesn't crash on that, I assume you haven't shown us all your code.
also, if that's not your problem: "is not working right" is not really a helpfull description of events, I'm afraid.

" is not working right " tells us nothing. If you have a compiler or runtime error message post the complete message plus the actual code it refers to. If your code is giving an incorrect result explain what result you get and what the correct result should be.

I declare my variables;

public static void main(String[] args) {

        int dollars = 0;
        int cents = 0;

I'm getting this error in the compiler;

Cannot use this in a static context

Cannot use this in a static context

" post the complete message plus the actual code it refers to"

public static void main(String[] args) {
        int dollars = 0;
        int cents = 0;

that is not the code you showed before. and with this, still, there is no 'Ours' anywhere.
any way, I don't see you use the keyword this, not in a static, nor in any other context, so please show the exact (correct) code, and the complete error message.

I do not have any other extra code is not hidden at all I'm making changes as I find the errors in compiler because the programm crash. The class and driver is at the top my drivers is the one I need to build to make the class work. So there is no reason not to posted here if I asking for help it wouldn't be making any sense.
Thank you

@toldav:-
set your instance variables from the value passed in constructor

this.dollars = dollars;//line 8
this.cents = cents;//line 9

You cannot use Our.dollar.
Call the contructor of superclass and by that you can send value to Money() class.(you cannot directly call dollars as its scope is hidden)

 dollars = max.getDollars();//line 21
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.