Help with Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sallyk57
    New Member
    • Oct 2006
    • 13

    #1

    Help with Program

    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.Numbe rFormat;

    public class Account
    {
    private NumberFormat fmt = NumberFormat.ge tCurrencyInstan ce();

    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.prin tln ();
    System.out.prin tln ("Error: Deposit amount is invalid.");
    System.out.prin tln (acctNumber + " " + fmt.format(amou nt));
    }
    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.prin tln ();
    System.out.prin tln ("Error: Withdraw amount is invalid.");
    System.out.prin tln ("Account: " + acctNumber);
    System.out.prin tln ("Requested: " + fmt.format(amou nt));
    }
    else
    if (amount > balance) // withdraw value exceeds balance
    {
    System.out.prin tln ();
    System.out.prin tln ("Error: Insufficient funds.");
    System.out.prin tln ("Account: " + acctNumber);
    System.out.prin tln ("Requested: " + fmt.format(amou nt));
    System.out.prin tln ("Available: " + fmt.format(bala nce));
    }
    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 getAccountNumbe r ()
    {
    return acctNumber;
    }

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

    }



    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.prin tln ("Gomez balance after deposit: " +
    gomezBalance);

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

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

    acct1.addIntere st();
    acct2.addIntere st();
    acct3.addIntere st();

    System.out.prin tln ();
    System.out.prin tln (acct1);
    System.out.prin tln (acct2);
    System.out.prin tln (acct3);
    }
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I noticed one problem with your existing code:

    Originally posted by sallyk57
    Code:
    //-----------------------------------------------------------------
       //  Validates the transaction, then withdraws the specified amount
       //  from the account. Returns the new balance.
       //-----------------------------------------------------------------
           public double withdraw (double amount, double fee)
          {
             amount += fee; // Problem
          
             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;
          }
    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,

    Code:
    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?

    Comment

    Working...