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);
}
}
// 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);
}
}
Comment