By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Help with Program

P: 13
I have the following 2 programs Account and Banking. I have to change the Account class so the funds can be moved from 1 account to another (like withdrawing money from one account and putting it in another). I also have to change the main method of Banking to show this new service. How would I do this?

// Account.java

import java.text.NumberFormat;

public class Account
{
private NumberFormat fmt = NumberFormat.getCurrencyInstance();

private final double RATE = 0.035; // interest rate of 3.5%

private int acctNumber;
private double balance;
private String name;

//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account (String owner, int account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}

//-----------------------------------------------------------------
// Validates the transaction, then deposits the specified amount
// into the account. Returns the new balance.
//-----------------------------------------------------------------
public double deposit (double amount)
{
if (amount < 0) // deposit value is negative
{
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (acctNumber + " " + fmt.format(amount));
}
else
balance = balance + amount;
return balance;
}

//-----------------------------------------------------------------
// Validates the transaction, then withdraws the specified amount
// from the account. Returns the new balance.
//-----------------------------------------------------------------
public double withdraw (double amount, double fee)
{
amount += fee;

if (amount < 0) // withdraw value is negative
{
System.out.println ();
System.out.println ("Error: Withdraw amount is invalid.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
}
else
if (amount > balance) // withdraw value exceeds balance
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else
balance = balance - amount;

return balance;
}

//-----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}

//-----------------------------------------------------------------
// Returns the current balance of the account.
//-----------------------------------------------------------------
public double getBalance ()
{
return balance;
}

//-----------------------------------------------------------------
// Returns the account number.
//-----------------------------------------------------------------
public int getAccountNumber ()
{
return acctNumber;
}

//-----------------------------------------------------------------
// Returns a one-line description of the account as a string.
//-----------------------------------------------------------------
public String toString ()
{
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}

}



public class Banking
{
//-----------------------------------------------------------------
// Creates some bank accounts and requests various services.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Account acct1 = new Account ("Ted Murphy", 72354, 102.56);
Account acct2 = new Account ("Anita Gomez", 69713, 40.00);
Account acct3 = new Account ("Sanchit Reddy", 93757, 759.32);

acct1.deposit (25.85);

double gomezBalance = acct2.deposit (500.00);
System.out.println ("Gomez balance after deposit: " +
gomezBalance);

System.out.println ("Gomez balance after withdrawal: " +
acct2.withdraw (430.75, 1.50));

acct3.withdraw (800.00, 0.0); // exceeds balance

acct1.addInterest();
acct2.addInterest();
acct3.addInterest();

System.out.println ();
System.out.println (acct1);
System.out.println (acct2);
System.out.println (acct3);
}
}
Dec 6 '06 #1
Share this Question
Share on Google+
1 Reply


Ganon11
Expert 2.5K+
P: 3,652
I noticed one problem with your existing code:

Expand|Select|Wrap|Line Numbers
  1. //-----------------------------------------------------------------
  2.    //  Validates the transaction, then withdraws the specified amount
  3.    //  from the account. Returns the new balance.
  4.    //-----------------------------------------------------------------
  5.        public double withdraw (double amount, double fee)
  6.       {
  7.          amount += fee; // Problem
  8.  
  9.          if (amount < 0)  // withdraw value is negative
  10.          {
  11.             System.out.println ();
  12.             System.out.println ("Error: Withdraw amount is invalid.");
  13.             System.out.println ("Account: " + acctNumber);
  14.             System.out.println ("Requested: " + fmt.format(amount));
  15.          }
  16.          else
  17.             if (amount > balance)  // withdraw value exceeds balance
  18.             {
  19.                System.out.println ();
  20.                System.out.println ("Error: Insufficient funds.");
  21.                System.out.println ("Account: " + acctNumber);
  22.                System.out.println ("Requested: " + fmt.format(amount));
  23.                System.out.println ("Available: " + fmt.format(balance));
  24.             }
  25.             else
  26.                balance = balance - amount;
  27.  
  28.          return balance;
  29.       }
You add the fee to the amount to start with, and then check if that amount is negative. What if the user tried to withdraw a negative amount, but the fee was large enough to make the amount positive? For instance,

Expand|Select|Wrap|Line Numbers
  1. myAccount.withdraw(-3, 5); // Obviously an error - withdrawing -3 dollars?
However, going through your function, the amount would be increased by 5 to produce a positive 2 - which is considered OK by your program. You should only add fee to amount if amount is greater than or equal to 0.

For the necessary additions, what are your ideas? What code have you written?
Dec 6 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.