jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes help for a newbie that's out of her depth! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help for a newbie that Watch "help for a newbie that New topic
Author

help for a newbie that's out of her depth!

jo robinson
Greenhorn

Joined: Nov 02, 2006
Posts: 3
Hi,

Im hoping someone can help because I rather stuck! I think the solution must be obvious but I only started to study Java 7 weeks ago (and proramming).

when I run the following (in conjunction with a pre-written file from my tutor) its not working correctly. Its a currency converter. when I run the first while loop I am reducing the value of totalDollars. if there are enough coins/notes in the machine it should output the dollars. However, it seems from the log that even though it knows there are enough dollars available and how may to push out, it is not getting the value of total dollars to zero (hence returning the insufficient dollars message & returning sterling). I have been staring at the problem for days and may only thought is it culd be due to decimal formatting. But dont now how to fix it.

any help would be wonderful.

Jo


public void convert() {

totaldollar100Count = 10 - dollar100Count;
totaldollar20Count = 10 - dollar20Count;
totaldollar10Count = 10 - dollar10Count;
totaldollar5Count = 10 - dollar5Count;
totaldollar1Count = 10 - dollar1Count;
totalquarterCount = 10 - quarterCount;
totaldimeCount = 10 - dimeCount;
totalnickelCount = 10 - nickelCount;
totalcentCount = 10 - centCount;

totalDollarsDisplay = allSterling * 1.82; /** maintain full value to display to the user */
totalDollars = allSterling * 1.82; /** calculated total amount of dollars owed*/
df.format(totalDollars);
df.format(totalDollarsDisplay);
//* while the total number of dollars owed is greater than $100,
//*keep track of the number of $100 to be dispensed (for later) and subtract $100 from the amount owed
//* continue until the amount owed is less than $100 and move to the next while statement*/

if (totalDollarsDisplay == 0) {

controlPanel.setDisplay("No Sterling to be dispensed" + "\n" + "Please enter Sterling.");
}
else
if (totalDollars != 0);
{
{

while (totalDollars >= 100 && totaldollar100Count > dollar100Count) {
dollar100Count++;
totalDollars = totalDollars - 100;
}

while (totalDollars >= 20 && totaldollar20Count > dollar20Count) {
dollar20Count++;
totalDollars = totalDollars - 20;
}
while (totalDollars >= 10 && totaldollar10Count > dollar10Count) {
dollar10Count++;
totalDollars = totalDollars - 10;
}
while (totalDollars >= 1 && totaldollar1Count > dollar1Count) {
dollar1Count++;
totalDollars = totalDollars - 1;

}
while (totalDollars >= 0.25 && totalquarterCount > quarterCount) {
quarterCount++;
totalDollars = totalDollars - 0.25;

}
while (totalDollars >= 0.10 && totaldimeCount > dimeCount) {
dimeCount++;
totalDollars = totalDollars - 0.10;
}

while (totalDollars >= 0.05 && totalnickelCount > nickelCount) {
nickelCount++;
totalDollars = totalDollars - 0.05;
}

while (totalDollars >= 0.01 && totalcentCount > centCount) {
centCount++;
totalDollars = totalDollars - 0.01;}
}


if (totalDollars != 0)
{
while (no_of_�1 >0) {
controlPanel.giveUKCoin(4);
no_of_�1--;
total�1=0;}

while (no_of_50p >0) {
controlPanel.giveUKCoin(3);
no_of_50p--;
total50p=0;}

while (no_of_20p >0) {
controlPanel.giveUKCoin(2);
no_of_20p--;
total20p=0;}

while (no_of_10p >0) {
controlPanel.giveUKCoin(1);
no_of_10p--;
total10p=0;}

while (no_of_�5 >0) {
controlPanel.giveUKNote(5);
no_of_�5--;
total�5=0;}

while (no_of_�10 >0) {
controlPanel.giveUKNote(10);
no_of_�10--;
total�10=0;}

controlPanel.setDisplay ("Dollars due: " + df.format(totalDollarsDisplay) + "\n"+"Insufficient dollars available. (i have the following left over"+ totalDollars+" Sterling returned. I think I should give out: dollar" + dollar1Count + "quar"+quarterCount + "nick"+nickelCount + "dime"+dimeCount+ "cent"+centCount +" i have available in the machine" + totaldollar1Count + totalquarterCount + totalnickelCount + totaldimeCount +totalcentCount);
total�1=0;
total�5=0;
total�10=0;
total�20=0;
total50p=0;
total20p=0;
total10p=0;
allSterling=0;
totalDollarsDisplay = 0;
totalDollars = 0;}


//* if the right number of dollar notes and coins are available dispense the correct number to the user and show what is dispensed in the control panel.
else
if (totalDollars == 0)
{
controlPanel.setDisplay ("total dollars owed: $" + df.format(totalDollarsDisplay) + ". Dispensed in: " + dollar100Count + "X $100 notes, " + "\n" + dollar20Count + " X $20 bills, " + dollar10Count + " X $10 notes, " + dollar1Count + " X $1 notes, " + quarterCount + " X Quarters, "+ dimeCount + " X Dimes, "+ centCount + " X Cents.");

while (dollar100Count >0) {
controlPanel.giveDollarNote(100);
dollar100Count--;}

while (dollar20Count >0) {
controlPanel.giveDollarNote(20);
dollar20Count--;}

while (dollar10Count >0) {
controlPanel.giveDollarNote(10);
dollar10Count--;}

while (dollar1Count >0) {
controlPanel.giveDollarNote(1);
dollar1Count--;}

while (quarterCount >0) {
controlPanel.giveDollarCoin(4);
quarterCount--;}

while (dimeCount >0) {
controlPanel.giveDollarCoin(3);
dimeCount--;}

while (nickelCount >0) {
controlPanel.giveDollarCoin(2);
nickelCount--;}

while (centCount >0) {
controlPanel.giveDollarCoin(1);
centCount--;}

total�1=0;
total�5=0;
total�10=0;
total�20=0;
total50p=0;
total20p=0;
total10p=0;
allSterling=0;
totalDollarsDisplay = 0;
totalDollars = 0;
}}

}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

One problem that jumped out at me is the line

if (totalDollars != 0);

That semicolon at the end is bad -- the Java compiler sees that as an empty statement -- it's what it done in that "if" case. So the program will actually do the money-handing-out code whether or not this if statetement passes. So remove that semicolon -- although the immediate result may be to make things worse, not better. After that, c'mon back and let us know what happens.


[Jess in Action][AskingGoodQuestions]
jo robinson
Greenhorn

Joined: Nov 02, 2006
Posts: 3
thank you for your reply. I have removed the ;, recompiled etc. I'm still having the same issue, but at least one of my dogy bits of code have been fixed!

The log is giving the following info. again, it show that I've got the code right to some extent but I'm just missing something.

NEW LOG PRINT *************************
in> 10p
<out Text> 10p has been entered into the machine
Total value of sterling added � 0.10
in> �1
<out Text> �1 has been entered into the machine
Total value of sterling added � 1.10
in> �20
<out Text> �20 has been entered into the machine
Total value of sterling added � 21.10
in> 20p
<out Text> 20p has been entered into the machine
Total value of sterling added � 21.30
in> �5
<out Text> �5 has been entered into the machine
Total value of sterling added � 26.30
in> 50p
<out Text> 50p has been entered into the machine
Total value of sterling added � 26.80
in> �10
<out Text> �10 has been entered into the machine
Total value of sterling added � 36.80
in> Conversion Request
<out> Pound
<out> 50p
<out> 20p
<out> 10p
<out> �5
<out> �10
<out Text> Dollars due: 66.98
Insufficient dollars available. (i have the following left over0.005999999999999077 Sterling returned. I think I should give out: dollar6quar3nick0dime2cent2 i have available in the machine1010101010
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, I won't pretend to have got it straight in my mind what you're doing here, exactly, but in the relevant part of the program, I see

if (totalDollars != 0)
{
// Some lines skipped
controlPanel.setDisplay ("Dollars due: " + df.format(totalDollarsDisplay) + "\n"+"Insufficient dollars available.

In other words, if totalDollars is not zero, then this "Insufficient dollars" message will always be printed. That doesn't sound right to me -- but of course, only you know what exactly "totalDollars" is meant to represent, and what would amount to "sufficient dollars." Still, I think this is probably what you want to look at.
jo robinson
Greenhorn

Joined: Nov 02, 2006
Posts: 3
yes, I see what you are saying. I'm certainly not the most efficient code writer. lol.

Basically, totalDollars is the number of dollars owed to the user after sterling has been converted.

The while loop (in bold) is calculates the number of notes and coins I return to the user, ensuring that I do not try to dispense more than available. It is doing this correctly. Then, every time I allocate a $ coin or note to be dispensed, I subtract the value of that from the totalDollars value. In theory (or at least in my head), if I have enough notes and coins in the machine, the totalDollars value should become 0 and the dollars are dispensed (based on the count intiger rather than the totalDollars double). The problem seems to be that even if I do have enough notes and coins, the totalDollar value is not exactly 0 (e.g in the log example it thinks the totalDollar value is 0.00599999.....etc.)

I wonder if this is because the calculation is not being done using decimal format and only the value being displayed in the print.out is in df.

God, writing this out makes me realise how long winded I am. lol
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help for a newbie that's out of her depth!
 
Similar Threads
My program won't return the correct ammount of change
Program: Change Due/Tendered
almost there!
Variable Problem, I think.
programming help