adding up money using methods

I have this class money and i am trying to use the method add but it is not adding or setting the parameters into the instance variables.  What am i doing wrong, also i do not know if my instance variables are being set

public class Money

{
      private int dollars;
      private int cents;

      public void set (int dollarsIn, int centsIn)
      {
            if ((dollarsIn < 0 ) || (centsIn < 0 ))
            {
                  dollars = 0;
                  cents = 0;
            }
            else if ((dollarsIn >= 0) && (cents >= 0))
            {
                  dollars = dollarsIn;
                  cents = centsIn;
            }

      }

      public void add (Money moneyIn)
      {
            numDollars();
            numCents();
            dollars = this.dollars + moneyIn.dollars;
            cents = this.cents + moneyIn.cents;
            while (cents >= 100)
            {
                  dollars = dollars + 1;
                  cents = cents - 100;
            }


      }

      public void subtract (Money moneyIn)
      {
            this.dollars = dollars - moneyIn.dollars;
            this.cents = cents - moneyIn.cents;
            if (dollars < 0)
            {
                  dollars = 0;
                  cents = 0;
            }
            else
            {
                  while (cents < 0)
                  {
                        dollars = dollars - 1;
                        cents = cents * -1;
                  }
            }
      }

      public boolean equals (Money moneyIn)
      {
            if (moneyIn.dollars == moneyIn.cents)
                  return true;
            else
                  return false;
      }

      public boolean lessThan (Money moneyIn)
      {
            if ((dollars < moneyIn.dollars) && (cents < moneyIn.dollars))
                  return true;
            else
                  return false;
      }

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

      public int numDollars ()
      {
            return dollars;
      }

      public int numCents ()
      {
            return cents;
      }


}

thats the class

here is the main program

import java.util.*;

/*      Class: MoneyProg
      Contents:
            main
            getMoneyInput
            printChange
      Classes used:
            Scanner
            System
            Money
            Change
*/

public class MoneyProg
{
      /*      Method: main
            Description: The customer enters purchase amounts until a value of 0.0
            is entered.  Then the amount due is printed and the money received read
            until the amount received covers the amount due.  The amount of change
            is then printed.
      */

      public static void main (String [] args)
    {
            System.out.println ("Program to calculate a purchase and make change");
        System.out.println ();

        Money amountDue = new Money ();
        amountDue.set (0,0);

        Money purchase;
        purchase = getMoneyInput ("Make a purchase (enter 0.0 to stop)");

        // while there are new purchases
        Money zero = new Money();
        zero.set (0,0);
        while (!purchase.equals(zero))
        {
             amountDue.add(purchase);
             purchase = getMoneyInput ("Make a purchase (enter 0.0 to stop)");
        }

        System.out.println ("There is " + amountDue.toString() + " due");

        // get the money from the customer
        Money received;
        received = getMoneyInput ("Enter the amount paid: ");
            while (received.lessThan (amountDue))
            {
                  System.out.println ("Not enough money has been received");
                  received = getMoneyInput ("Enter the amount paid: ");
            }


            // subtract amount due and get change
            received.subtract (amountDue);
            Change custChange = new Change ();
            custChange.set (received);
            printChange (custChange);
    }

      /*
            Method: getMoneyInput
            Description: The prompt is printed and input (dollars and cents) is read
                  until non-negative values are input.  The input must be in the format
                  DD.CC.  Leading zeros are assumed for each value.  A Money object is
                  then created and returned.
            Preconditions: The String prompt is sent as an argument.
            Postconditions: The Money object is created with non-negative input and
                  returned.
      */


    static Money getMoneyInput (String prompt)
    {
            Scanner keyboard = new Scanner (System.in);
            String line;
            int dollars = 0;
            int cents = 0;
            String tempstr = "";
            boolean moreInput = true;
            int pos;

            while (moreInput)
            {
                  System.out.print (prompt);
                  line = keyboard.nextLine();
                  pos = 0;
                  while (line.charAt(pos) != '.')
                        pos++;
                  tempstr = line.substring (0,pos);
                  dollars = Integer.parseInt(tempstr);
                  tempstr = line.substring (pos + 1);
                  cents = Integer.parseInt(tempstr);
                  moreInput = dollars < 0 || cents < 0;
            }


            Money tempMoney = new Money();
            tempMoney.set (dollars, cents);
            return tempMoney;
      }

      /*
            Method: printChange
            Description: The amount of change is printed as dollars and
                  cents and the amount for each coin is printed.
            Precondition: A correctly formed Change object is passed
                  as a parameter (dollars and cents are correct).
            Postcondition: The output is printed to the screen.
      */

      static void printChange (Change coins)
      {
            System.out.println ("The change is " + coins.toMoney());
            System.out.println ("\tDollars: " + coins.numDollars());
            System.out.println ("\tQuarters: " + coins.numQuarters());
            System.out.println ("\tDimes: " + coins.numDimes());
            System.out.println ("\tNickels: " + coins.numNickels());
            System.out.println ("\tPennies: " + coins.numPennies());
      }

}

ja-vicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

suprapto45Commented:
Hi,

There is something not useful in your add method


     public void add (Money moneyIn)
     {
          numDollars();
          numCents();
          dollars = this.dollars + moneyIn.dollars;
          cents = this.cents + moneyIn.cents;
          while (cents >= 100)
          {
               dollars = dollars + 1;
               cents = cents - 100;
          }
     }

What do numDollars() and numCents() use for? You can remove that anyway.

Can you send me the sample of output that you have?

Regards
Dave
zzynxSr. Software engineerCommented:
If I add this main() method to your Money class:

     public static void main (String [] args) {
     
        Money amountDue = new Money ();
        amountDue.set(0,0);

        Money purchase = new Money();
        purchase.set(1, 85);

        amountDue.add(purchase);
        System.out.println ("There is " + amountDue.toString() + " due");
     }

I get the correct result:
There is $1.85 due

So, I don't see any problem.
CEHJCommented:
>>also i do not know if my instance variables are being set

You should get used to placing logging statements in your code then you can't fail to know what's being set:

public void set (int dollarsIn, int centsIn)
{
      if ((dollarsIn < 0 ) || (centsIn < 0 ))
      {
            dollars = 0;
            cents = 0;
            System.out.println("dollars and cents set to zero");
      }
      else if ((dollarsIn >= 0) && (cents >= 0))
      {
            dollars = dollarsIn;
            cents = centsIn;
            System.out.println("dollars set to " + dollars + ", cents set to " + cents);
      }

}

It would be better still to use the logging API or log4j

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

shivamohanCommented:
Should this...
public void set (int dollarsIn, int centsIn)
     {
          if ((dollarsIn < 0 ) || (centsIn < 0 ))
          {
               dollars = 0;
               cents = 0;
          }
          else if ((dollarsIn >= 0) && (cents >= 0))
          {
               dollars = dollarsIn;
               cents = centsIn;
          }

     }
be...
public void set (int dollarsIn, int centsIn)
     {
          if ((dollarsIn < 0 ) || (centsIn < 0 ))
          {
               dollars = 0;
               cents = 0;
          }
          else if ((dollarsIn >= 0) && (centsIn >= 0))
          {
               dollars = dollarsIn;
               cents = centsIn;
          }

     }
(centsIn as opposed to cents in else if condition)?
CEHJCommented:
:-)
zzynxSr. Software engineerCommented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.