ja-vic
asked on
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());
}
}
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());
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
Thanks