Java programming help plz...

Recommended Videos

StickManRampage

New member
Sep 18, 2008
172
0
0
Hey, I am currently taking a java course at school and i have to write a program that acts like an atm. I finally got my "do while" loop to work, but now it is doing some freaky arithmetic. Just wondering if anyone can help me in figuring out what kind of math is going on here...

CODE:
public class MiniTeller
{
public static void main(String[] arg)
{
TextReader keyboard = new TextReader();
BankAccount currentAccount = new BankAccount("Jackson", 100.00);
System.out.println("Initial Balance for Jackson is " + currentAccount.getBalance());
String hmm;

do
{
System.out.print("W)ithdraw, D)eposit, B)alance, or Q)uit: ");
String whatToDo = keyboard.readLine();
hmm = whatToDo;

if(whatToDo.equalsIgnoreCase("w"))
{
System.out.print("Enter withdrawal amount: ");
double withdraw = keyboard.readDouble();
currentAccount.withdraw(withdraw);
//System.out.println();

if(withdraw > currentAccount.getBalance())
System.out.println("Insufficient Funds...");
}

else
{

if(whatToDo.equalsIgnoreCase("d"))
{
System.out.print("Enter deposit amount: ");
double deposit = keyboard.readDouble();
currentAccount.deposit(deposit);
//System.out.println();
}

else
{
if(whatToDo.equalsIgnoreCase("b"))
{
System.out.println("Current Balance: " + currentAccount.getBalance());
// System.out.println();
}

else
{
if(whatToDo.equalsIgnoreCase("q"))
System.out.println("Have a nice whatever : )");
}
}
}
}
while(!(hmm.equalsIgnoreCase("q")));
}
}


OUTPUT:

--------------------Configuration: --------------------
Initial Balance for Jackson is 100.0
W)ithdraw, D)eposit, B)alance, or Q)uit: b
Current Balance: 100.0
W)ithdraw, D)eposit, B)alance, or Q)uit: w
Enter withdrawal amount: 98.91
Insufficient Funds...
W)ithdraw, D)eposit, B)alance, or Q)uit: b
Current Balance: 1.0900000000000034
W)ithdraw, D)eposit, B)alance, or Q)uit:
 

Alex_P

All I really do is threadcrap
Mar 27, 2008
2,712
0
0
Let me stress that "1.0900000000000034" will surprise you but it's not an error. IEEE floating-point numbers can't represent all every number perfectly -- they have to round to this binary-plus-exponents kind of thing. That tiny bit of stuff at the end is the result of there not being a double that perfectly represents 1.09. Fortunately for us, the way the numbers are defined ensures a pretty consistent number of significant figures regardless of whether the value itself is high or low.

For your application, as Johnny said, you can format the thing down to two decimal places when you print.

-- Alex