Question: Coding in java You are to incorporate all requirements of the three exercises into the basic class definition provided in PART A description. In addition,
Coding in java

You are to incorporate all requirements of the three exercises into the basic class definition provided in PART A description. In addition, you must check the values of deposit, withdrawal, and transfer amounts to ensure they are greater than 0. For two of the three methods, deposit and withdraw, the methods are to return a boolean value to indicate if the transaction was completed. For method transfer, the method is to RETurn the allowed amount transferred, refer to PART C for details for a partial transfer. In addition to the specified class methods, provide a method name() that returns a String reference to the objects account name.
File BankAccount.java has the class definition shown in the text as your starting point; note that methods deposit and withdraw are incomplete! You must define all required methods in this file.
Here is the BankAccount.java
public class BankAccount
{
// Public Interface
// 1. Constructors
// Add your Constructor(s) here......
// 2. Mutator Methods
// A. boolean deposit(double)
// Note: This method enforces the class invariant that stipulates
// that a deposit amount MUST be > $0.00
public boolean deposit(double depositAmount)
{
boolean depositMade = false;
// Insert your code here. Remember, you MUST enforce the invariant condition!!!
return depositMade;
}
//
// B. boolean withdraw(double)
// Note: This method enforces the class invariant that stipulates
// that the ending account balance MUST be >= $0.00
public boolean withdraw(double withdrawalAmount)
{
boolean withdrawalMade = false;
// Insert your code here. Remember, you MUST enforce the invariant condition!!!
return withdrawalMade;
}
//
// C. double transfer(BankAccount,double)
// RETurns the amount transferred: 0.0 - No transfer occurred
public double transfer(BankAccount otherAccount, double withdrawalAmount)
{
double amountTransfered = 0.0;
// Create your method here
return amountTransfered;
}
//
//
// 3. Observer Methods
public String name()
{
return name;
}
//
//
// 4. Virtual (late binding) Methods: Extensions of class "Object"
// A. toString()
// Constructs Description "String" of the object
public String toString()
{
String str = "";
// Insert your code here.....
return str;
}
// Class Private, non-static Data Members
// Private Class Data Members
// 1. Variable Declarations
private String name;
private double balance;
// Add any additional static and non-static data members
// here...
} // End Class Definition: BankAccount
To start this project, create an Eclipse project and copy the two supplied source code files, Project_BankAccount.java and BankAccount.java into the projects src folder. File BankAccount.java has the class definition shown in the text as your starting point; note that methods deposit and withdraw are incomplete! You must define all required methods in this file. You should not modify the Project_BankAccount.java file as it provides a means to test your code. I will utilize this same test program to verify the functionality of the BankAccount class source code file you submit. Your code must work as shown with my test program! A sample Console output from this program is shown below. Code method toString() to produce the output shown for the class objects in the following Sample Console Output.
Here is project_BankAccount.java code
public class Project_BankAccount
{
public static void main(String[] args)
{
// Constant "final" Value Declarations
final double BEGINNING_BALANCE1 = 23.67;
final double BEGINNING_BALANCE2 = 34.57;
final double TRANSACTION_FEE = 2.50;
// Create a "BankAccount" object, specifying just the account holder's
// name. The beginning balance and withdrawal fee will be set to $0.0.
BankAccount myAccount = new BankAccount("John Doe");
// Create arrays of deposit, withdrawal, and transfer amounts.
double[] deposits = new double[] { 51.75, 14.78, -4.59 };
double[] withdrawals = new double[] { 7.96, 45.79, -3.57, 12.78, .01, 75.86 };
double[] transfers = new double[] { 41.38, -8.79, 78.67, 94.74, .01, 150.00 };
// Uninitialized Variable Declarations
double deposit, withdrawal, transfer;
// Inform the User of the Project Solution
introduction();
// Example #1: Account name only specified to Constructor
System.out.println(" Example #1: Account object instantiated with name only");
// Display the beginning account balance
System.out.println(" Beginning account:" +
" " + myAccount);
// Make a deposit to the account object, then "display" the object
// via the overloaded "toString()" class method.
deposit = deposits[0];
System.out.println(" Depositing: $" + deposit);
makeDeposit(myAccount, deposit);
// Now make a withdrawal
withdrawal = withdrawals[0];
System.out.println(" Withdrawing: $" + withdrawal);
makeWithdrawal(myAccount, withdrawal);
// Then another deposit
deposit = deposits[1];
System.out.println(" Depositing: $" + deposit);
makeDeposit(myAccount, deposit);
// Then another deposit that is a negative value
deposit = deposits[2];
System.out.println(" Depositing: $" + deposit);
makeDeposit(myAccount, deposit);
// Another withdrawal
withdrawal = withdrawals[1];
System.out.println(" Withdrawing: $" + withdrawal);
makeWithdrawal(myAccount, withdrawal);
// Another withdrawal that is a negative value
withdrawal = withdrawals[2];
System.out.println(" Withdrawing: $" + withdrawal);
makeWithdrawal(myAccount, withdrawal);
// Another withdrawal that will result in a balance of $0.00.
withdrawal = withdrawals[3];
System.out.println(" Withdrawing: $" + withdrawal);
makeWithdrawal(myAccount, withdrawal);
// Finally, a withdrawal the will exceed the current account balance
withdrawal = withdrawals[4];
System.out.println(" Withdrawing: $" + withdrawal);
makeWithdrawal(myAccount, withdrawal);
// Example #2: Account name and beginning balance specified to the Constructor
System.out.println(" Example #2: Account object instantiated with name and" +
" beginning balance: $" + BEGINNING_BALANCE1);
// Create a "BankAccount" object, specifying the account holder's name
// and beginning balance. The withdrawal fee will be set to $0.00.
// Note: This is an example of the Java feature of "garbage" collection.
// The REFERENCE "account" is assigned to a new "BankAccount" class
// object. Java will detect that no other reference refers to the
// previous object (Name: John Doe) and will automatically return
// the memory allocated for that object to the Heap Memory Manager.
myAccount = new BankAccount("Sarah Jones", BEGINNING_BALANCE1);
// Display the beginning account balance
System.out.println(" Beginning account:" +
" " + myAccount);
// Make a deposit to the account object, then "display" the object
// via the overloaded "toString()" class method.
deposit = deposits[0];
System.out.println(" Depositing: $" + deposit);
makeDeposit(myAccount, deposit);
// Now make a withdrawal
withdrawal = withdrawals[0];
System.out.println(" Withdrawing: $" + withdrawal);
makeWithdrawal(myAccount, withdrawal);
// Example #3: Account name, beginning balance, and transaction fee are
// specified to the Constructor
System.out.println(" Example #3: Account object instantiated with name," +
" beginning balance: $" + BEGINNING_BALANCE2 +
", and transaction fee: $" + String.format("%.2f", TRANSACTION_FEE));
// Create a "BankAccount" object, specifying the account holder's name,
// the beginning balance, and the transaction fee.
BankAccount yourAccount =
new BankAccount("Samuel Johnson", BEGINNING_BALANCE2, TRANSACTION_FEE);
// Display the beginning account balance
System.out.println(" Beginning account:" +
" " + yourAccount);
// Make a deposit to the account object, then "display" the object
// via the overloaded "toString()" class method.
deposit = deposits[0];
System.out.println(" Depositing: $" + deposit);
makeDeposit(yourAccount, deposit);
// Now make a withdrawal
withdrawal = withdrawals[0];
System.out.println(" Withdrawing: $" + withdrawal);
makeWithdrawal(yourAccount, withdrawal);
// Now make a withdrawal of the exact remaining balance
// Note: The fee + withdrawal will exceed the remaining balance!
withdrawal = withdrawals[5];
System.out.println(" Withdrawing: $" + withdrawal);
makeWithdrawal(yourAccount, withdrawal);
// Example #4: Transfer between accounts
System.out.println(" Example #4: Transfers between two accounts");
System.out.println( " myAccount: " + myAccount +
" yourAccount: " + yourAccount);
// Make a valid transfer from "myAccount" to "yourAccount"
transfer = transfers[0];
System.out.println(" Transfering $" + transfer + " from " + myAccount.name() +
"'s account to " + yourAccount.name() + "'s account");
transfer(myAccount, yourAccount, transfer);
// Make an invalid transfer (negative amount) from "myAccount" to "yourAccount"
transfer = transfers[1];
System.out.println(" Transfering $" + transfer + " from " + myAccount.name() +
"'s account to " + yourAccount.name() + "'s account");
transfer(myAccount, yourAccount, transfer);
// Make a valid transfer from "yourAccount" to "myAccount"
transfer = transfers[2];
System.out.println(" Transfering $" + transfer + " from " + yourAccount.name() +
"'s account to " + myAccount.name() + "'s account");
transfer(yourAccount, myAccount, transfer);
// Make a second valid transfer from "yourAccount" to "myAccount" that results
// in a balance of $0.01 in "myAccount"
transfer = transfers[3];
System.out.println(" Transfering $" + transfer + " from " + myAccount.name() +
"'s account to " + yourAccount.name() + "'s account");
transfer(myAccount, yourAccount, transfer);
// Make an invalid transfer (insufficient funds) from "myAccount" to "yourAccount"
transfer = transfers[4];
System.out.println(" Transfering $" + transfer + " from " + myAccount.name() +
"'s account to " + yourAccount.name() + "'s account");
transfer(myAccount, yourAccount, transfer);
// Make an invalid transfer (insufficient funds) from "yourAccount" to "myAccount"
transfer = transfers[5];
System.out.println(" Transfering $" + transfer + " from " + yourAccount.name() +
"'s account to " + myAccount.name() + "'s account");
transfer(yourAccount, myAccount, transfer);
} // End static method: main(String[])
private static void introduction()
{
System.out.println(" Project #8 Solution" +
" Chapter 8, Exercise #11, page 568 ");
}
private static void makeDeposit(BankAccount account, double deposit)
{
// First, attempt to make the deposit
if (account.deposit(deposit) )
{ // A deposit amount >0 was specified, continue...
// Now "display" the object to the User. By specifying the REFERENCE
// to the object as an argument in the "println" method, the compiler
// will generate a call to the overloaded class method, "toString()".
System.out.println(" " + account);
}
else
{ // Otherwise, an invalid deposit amount was specified
System.out.println(" Unable to deposit $" +
String.format("%.2f", deposit));
}
}
private static void makeWithdrawal(BankAccount account, double withdrawal)
{
// Attempt to make the withdrawal. The class method, "withdrawal" will
// RETurn a "boolean" value; "true": Withdrawal made,
// "false": Withdrawal (+ fee, if applicable) will exceed current
// balance.
boolean status = account.withdraw(withdrawal);
if (status)
{ // Withdrawal was successful, display the remaining balance.
System.out.println(" " + account);
}
else
{ // Otherwise, inform the User that withdrawal was NOT made.
System.out.println(" Unable to withdraw $" +
String.format("%.2f", withdrawal));
}
// Note: A "return" statement is unnecessary for a method that "returns"
// "void", but is allowed for completeness.
return;
}
private static void transfer(BankAccount source, BankAccount destination,
double transferAmount)
{
// Attempt to make the transfer. The class method, "transfer" will
// RETurn a "boolean" value; "true": Transfer was made,
// "false": Transfer + fee will exceed current balance of the
// source account.
double amountTransferred = source.transfer(destination, transferAmount);
if (amountTransferred > 0.0)
{ // Transfer was successful, display the two accounts.
System.out.printf(" Source Account: %s" +
" Amount Transferred: %.2f" +
" Destination Account: %s ",
source, amountTransferred, destination);
}
else
{ // Otherwise, inform the User that withdrawal was NOT made.
System.out.println(" Unable to transfer $" +
String.format("%.2f", transferAmount));
}
}
} // End class definition: Project_BankAccount
A sample Console output from this program is shown below. Code method toString() to produce the output shown for the class objects in the following Sample Console Output.
Sample Console Output:
Sample Console Output:
Example #1: Account object instantiated with name only
Beginning account:
John Doe, $0.00, Total fees: $0.00
Depositing: $51.75
John Doe, $51.75, Total fees: $0.00
Withdrawing: $7.96
John Doe, $43.79, Total fees: $0.00
Depositing: $14.78
John Doe, $58.57, Total fees: $0.00
Depositing: $-4.59
Unable to deposit $-4.59
Withdrawing: $45.79
John Doe, $12.78, Total fees: $0.00
Withdrawing: $-3.57
Unable to withdraw $-3.57
Withdrawing: $12.78
John Doe, $0.00, Total fees: $0.00
Withdrawing: $0.01
Unable to withdraw $0.01
Example #2: Account object instantiated with name and
beginning balance: $23.67
Beginning account:
Sarah Jones, $23.67, Total fees: $0.00
Depositing: $51.75
Sarah Jones, $75.42, Total fees: $0.00
Withdrawing: $7.96
Sarah Jones, $67.46, Total fees: $0.00
Example #3: Account object instantiated with name,
beginning balance: $34.57, and transaction fee: $2.50
Beginning account:
Samuel Johnson, $34.57, Total fees: $0.00
Depositing: $51.75
Samuel Johnson, $86.32, Total fees: $0.00
Withdrawing: $7.96
Samuel Johnson, $75.86, Total fees: $2.50
Withdrawing: $75.86
Unable to withdraw $75.86
Example #4: Transfers between two accounts
myAccount: Sarah Jones, $67.46, Total fees: $0.00
yourAccount: Samuel Johnson, $75.86, Total fees: $2.50
Transfering $41.38 from Sarah Jones's account to Samuel Johnson's account
Source Account: Sarah Jones, $21.08, Total fees: $5.00
Amount Transferred: 41.38
Destination Account: Samuel Johnson, $117.24, Total fees: $2.50
Transfering $-8.79 from Sarah Jones's account to Samuel Johnson's account
Unable to transfer $-8.79
Transfering $78.67 from Samuel Johnson's account to Sarah Jones's account
Source Account: Samuel Johnson, $33.57, Total fees: $7.50
Amount Transferred: 78.67
Destination Account: Sarah Jones, $99.75, Total fees: $5.00
Transfering $94.74 from Sarah Jones's account to Samuel Johnson's account
Source Account: Sarah Jones, $0.01, Total fees: $10.00
Amount Transferred: 94.74
Destination Account: Samuel Johnson, $128.31, Total fees: $7.50
Transfering $0.01 from Sarah Jones's account to Samuel Johnson's account
Unable to transfer $0.01
Transfering $150.0 from Samuel Johnson's account to Sarah Jones's account
Source Account: Samuel Johnson, $0.00, Total fees: $12.50
Amount Transferred: 123.31
Destination Account: Sarah Jones, $123.32, Total fees: $10.00
A. Suppose the following BankAccount class has been created: 1 Each BankAccount object represents one user s account 2 I information including name and balance of money 3 public class BankAccount ( String name; double balance; public void deposit(double amount) balance balance + amount; public void withdraw (double amount) ( balance balance- amount; 12 13 14 Add a field to the BankAccount class named transactionFee for a real number representing an amount of money to deduct every time the user withdraws money. The default value is $0.00, but the client can change the value. Deduct the transaction fee money during every withdraw call (but not from deposits). Make sure that the balance cannot go negative during a withdrawal. If the withdrawal (amount plus transaction fee) would cause it to become negative, don't modify the balance at all. nt class from the previous exercise. Your method should return a string that Add a toString method to the B contains the account's name and balance separated by a comma and space. For example, if an account object named yana has the name Yana" and a balance of 3.03, the call yana.tostring() should return the string "Yana, $3.03" Add a transfer method to the BankAccount class from the previous exercises. Your method should move money from the current bank account to another account. The method accepts two parameters: a second BankAccount to accept the money, and a real number for the amount of money to transfer. There is a $5.00 fee for transferring money, so this much must be deducted from the current account's balance before any transfer. The method should modify the two BankAccount objects such that "this" current object has its balance decreased by the given amount plus the S5 fee, and the other account's balance is increased by the given amount. If this account object does not have enough money to make the full transfer, transfer whatever money is left after the $5 fee is deducted. If this account has under $5 or the amount is 0 or less, no transfer should occur and neither account's state should be modified. The following are some example calls to the method: BankAccount ben = new BankAccount(); ben.deposit (80.00) BankAccount hal = new BankAccount(); hal.deposit (20.00); ben.transfer (hal, 20.00 ben $55, hal $40 (ben-25, hal +$20) ben.transfer (hal, 10.00 ben $40, hal $50 (ben-15, hal +$10) hal.transfer (ben, 60.00) / ben $85, hal 0 (ben +$45, hal-$50)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
