1. The following Java code provides the outline of a simple bank account class i
ID: 3748370 • Letter: 1
Question
1. The following Java code provides the outline of a simple bank account class import java.io.*; public class Account implements Comparable, Serializable ( protected int accnum; protected HolderDetails holder; protected List transactions; protected float balancei // Add a suitable constructor here // Add methods to make deposits / withdrawals // Method to print out account transaction summary // Add suitable attribute accessor methods // Add method to implement the Comparable interface a: Complete the implementation of the Account class, providing a suitable constructor, attribute accessor methods, methods for making a deposit or withdrawal, a method to print out a transaction summary related to a range of dates and an implementation method for the Comparable interface. Activate Go to Settin 7 MARKSExplanation / Answer
Solution:
Please find below the code Account.java . Please note the code could not be executed as the Holder and Transaction classes were not provided.
public class Account implements Comparable<Account>{
protected int accnum;
protected HolderDetails holder;
protected List<Transaction> transactions;
protected float balance;
public Account(int accnum, HolderDetails holder, List<Transaction> transactions, float balance) {
super();
this.accnum = accnum;
this.holder = holder;
this.transactions = transactions;
this.balance = balance;
}
public int getAccnum() {
return accnum;
}
public HolderDetails getHolder() {
return holder;
}
public List<Transaction> getTransactions() {
return transactions;
}
public float getBalance() {
return balance;
}
public boolean withdrawal(float amount) {
if (amount > getBalance()) {
System.out.println("Account balance is low!!");
return false;
}else {
float remainingBalance = getBalance() - amount;
this.balance = remainingBalance;
return true;
}
}
public void deposit(float amount) {
float balanceAfterDeposit = getBalance() + amount;
this.balance = balanceAfterDeposit;
}
public void printAccountDetails() {
System.out.println("Account No: " + getAccnum());
System.out.println("Balance: " + getBalance());
System.out.println("Holder Details: " + holder.toString());
System.out.println("Transaction details: ");
getTransactions().forEach(transaction -> System.out.println(transaction.toString()));
}
@Override
public int compareTo(Account arg0) {
return Integer.valueOf(this.getAccnum()).compareTo(Integer.valueOf(arg0.getAccnum()));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.