I am working on a small app to deposit and withdraw funds from a bank account. The program is running mostly fine, but I am getting strange output from some of the transactions with a lot of trailing decimals. I am randomly generating doubles that should only have 2 decimals, so I'm not sure where they are coming from.
C:\Users\Jon\Documents\Class\Java>java SavingsAccountTest
Balance: 62.0
Deposit Count: 0
Withdraw Count: 0
Annual Interest Rate: 0.09
Service Charges: 0.0
Active: false
Withdraw first, then deposit using
randomly generated amounts.
Balance: 62.0
Withrdaw: Deposit:
8.76 35.43
Your account is inactive due to: Insufficient Funds
***Unable to complete withdrawal.***
Balance: 97.43
Withrdaw: Deposit:
16.83 3.98
Balance: 84.58000000000001
Withrdaw: Deposit:
99.44 35.2
Your account is inactive due to: Insufficient Funds
***Unable to complete withdrawal.***
Press enter to continue.
C:\Users\Jon\Documents\Class\Java>
The issue is with the second transaction, beginning the third set of transactions with a balance of 84.58000000000001.
Here is the code for my main class.
import java.util.Random;
import java.util.Scanner;
public class SavingsAccountTest
{
public static void main(String[] args)
{
Random rand = new Random();
double[] depositArray = {(double)rand.nextInt(10001)/100,
(double)rand.nextInt(10001)/100,
(double)rand.nextInt(10001)/100};
double[] withdrawArray = {(double)rand.nextInt(10001)/100,
(double)rand.nextInt(10001)/100,
(double)rand.nextInt(10001)/100};
SavingsAccount account = new SavingsAccount((double)rand.nextInt(101),
(double)(rand.nextInt(10)+1)/100.0);
System.out.print(account.toString());
System.out.println("\n\n\tWithdraw first, then deposit using\n\trandomly generated amounts.");
for (int i = 0; i < 3; i++)
{
System.out.println("\nBalance: " + account.getBalance());
System.out.print("Withrdaw:\t\tDeposit:\n" +
withdrawArray[i] + "\t\t\t" + depositArray[i] + "\n\n");
account.withdraw(withdrawArray[i]);
account.deposit(depositArray[i]);
}
pause();
}
private static void pause()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("\nPress enter to continue.");
keyboard.nextLine();
System.out.print("\n");
}
}
Here are the deposit and withdraw methods from SavingsAccount
public void withdraw(double amount)
{
if (this.active && amount <= super.balance)
super.withdraw(amount);
else
System.out.print("Your account is inactive due to: " +
"Insufficient Funds" + "\n***Unable to complete " +
"withdrawal.***\n");
}
public void deposit(double amount)
{
if (super.balance + amount >= 25.00)
{
this.active = true;
super.deposit(amount);
}
else
super.deposit(amount);
}
Here are the deposit and withdraw methods from the BankAccount super class:
public void withdraw(double withdraw)
{
this.balance -= withdraw;
withdrawCount++;
}
public void deposit(double deposit)
{
this.balance += deposit;
depositCount++;
}