Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please do FILL ME IN part. Bank Account : You will write a class called BankAcco

ID: 3786043 • Letter: P

Question

Please do FILL ME IN part.

Bank Account : You will write a class called BankAccount, with three fields and five methods. These are stubbed out for you in the template.

#Fields

accountNumber – this field holds a String representing a unique account number

balance – this field holds an int which represents the current balance in the account. Assume that we only work with whole dollar amounts. For this lab, you can ignore overflow of ints (a real bank ought to not do that).

transactionLog – this field holds a String which represents a history of the transactions for the account. This is described more below.

#Methods

deposit – this method takes an int, and returns void. It adds the given amount to the account's balance. If the amount is negative, the method should return without making any changes. If successful, it should also add a line to the end of the transaction log (by appending to the string) that says "Deposit: $<num> ", where <num> is the amount of the deposit (note that " " is the newline character, which makes the log print well).

withdraw – this method takes an int, and returns void. It subtracts the given amount from the account's balance. If the amount is negative, or the account doesn't have enough money, the method should return without making any changes. If successful, it should also add a line to the end of the transaction log that says "Withdrawal: $<num> ", where <num> is the amount of the withdrawal.

report – this method takes no arguments, and returns a String which contains the current transaction log, followed by "Balance: $<num>", where <num> is the current balance.

getBalance – this method takes no arguments, and returns the current balance of the account

transfer – this method takes two arguments, one int representing an amount to transfer, and one BankAccount from which money should be transferred. If the amount is negative or the fromAccount doesn't have a high enough balance, it should return without doing anything. Otherwise, it should "move" the requested amount from fromAccount to this account, by subtracting from the former and adding to the latter. It should also add a line to both transaction logs. For the account referenced by fromAccount, it should add "Transfer to <id>: $<num> ", where <id> is the account number of this, and <num> is the amount transferred. For the account referenced by this, it should add "Transfer from <id>: $<num> ", where <id> is the account number of fromAccount and <num> is the amount transferred.

#Advice

Implement deposit and getBalance first without the transaction log. Then add the updates to the log to deposit, then implement report report. Next, implement withdraw, then do transfer last. Make sure each method works before moving onto the next

////////////////Main.java////////////////////////////

public class Main {

public static void main(String[] args) {
// Feel free to add more to this to test things out.
// You will be graded by the automatic tests you get from "Run Tests"
  
BankAccount b1 = new BankAccount("abc");
BankAccount b2 = new BankAccount("123");
b1.deposit(300);
b2.deposit(400);
b1.transfer(150, b2); // should remove 150 from b2 and put it in b1
  
// Should show a deposit, a move from b1, and a final balance of 450
System.out.println(b1.report());
// Should show a deposit, a move to b1, and a final balance of 250
System.out.println(b2.report());
}
}

///////////////BankAccount.java////////////////////////

public class BankAccount {
public int balance;
public final String accountNumber;
public String transactionLog;

public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0;
this.transactionLog = "";
}

public String report() {
// FILL ME IN!
int deposit = deposit(balance);
int withdrawl = withdraw(balance);

transactionLog= "Deposit: $" + deposit + " Withdrawl: $" + withdraw(balance)
+ "Transfer from " + accountNumber +": $" +this.balance + " Balance: $" + this.balance;
  
return transactionLog;
}

public int getBalance() {
// FILL ME IN!
return balance;
}

public void transfer(int transferAmount, BankAccount fromAccount) {
// FILL ME IN!
this.balance = this.balance - transferAmount;
fromAccount.balance = fromAccount.balance + transferAmount;
  
}

public void withdraw(int withdrawalAmount) {
// FILL ME IN!
this.balance -=withdrawalAmount;
}

public void deposit(int depositAmount) {
// FILL ME IN!
this.balance +=depositAmount;
}

}

Explanation / Answer

Hi buddy, I've added comments for your better understanding.

////////////////Main.java////////////////////////////
public class Main {

public static void main(String[] args) {
// Feel free to add more to this to test things out.
// You will be graded by the automatic tests you get from "Run Tests"

BankAccount b1 = new BankAccount("abc");
BankAccount b2 = new BankAccount("123");
b1.deposit(300);
b2.deposit(400);
b1.transfer(150, b2); // should remove 150 from b2 and put it in b1

// Should show a deposit, a move from b1, and a final balance of 450
System.out.println(b1.report());
// Should show a deposit, a move to b1, and a final balance of 250
System.out.println(b2.report());
}
}

///////////////BankAccount.java////////////////////////
public class BankAccount {

public int balance;
public final String accountNumber;
public String transactionLog;

public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0;
this.transactionLog = "";
}

public String report() {
String report = transactionLog;
report = report + "Balance$:" + balance + " ";
return report;
}

public int getBalance() {
// FILL ME IN!
return balance;
}

public void transfer(int transferAmount, BankAccount fromAccount) {
// FILL ME IN!
//If the fromAccount doesn't have sufficient balance , do nothing.
if (transferAmount<0||fromAccount.balance < transferAmount) {
return;
}
//Balance of this account increases
this.balance = this.balance + transferAmount;
//Balance of the from account decreases
fromAccount.balance = fromAccount.balance - transferAmount;
fromAccount.transactionLog = fromAccount.transactionLog + "Transfer to " + this.accountNumber + ":$" + transferAmount + " ";
this.transactionLog = this.transactionLog + "Transfer from " + this.accountNumber + ":$" + transferAmount + " ";

}

public void withdraw(int withdrawalAmount) {
// FILL ME IN!
//If the balance is less than the amount to be withdrawn
if (withdrawalAmount < 0 || withdrawalAmount > this.balance) {
return;
}
this.balance -= withdrawalAmount;
this.transactionLog = this.transactionLog + "Withdrawal: $" + withdrawalAmount + " ";
}

public void deposit(int depositAmount) {
// FILL ME IN!

//If the balance is negative, method should return without making any changes.
if (this.balance < 0) {
return;
}
this.balance += depositAmount;
this.transactionLog = this.transactionLog + "Deposit: $" + depositAmount + " ";
}
}