The str method of the Bank class returns a string containing the accounts in ran
ID: 3717123 • Letter: T
Question
The str method of the Bank class returns a string containing the accounts in random order. Design and implement a change that causes the accounts to be placed in the string by order of name. (Hint: You will also have to define some methods in the SavingsAccount class.)
(I think this is the Bank class below)
(saving account methods below)
Bank METHOD WHAT IT DOES b = Bank() Returns a bank. b.add(account) Adds the given account to the bank. b.remove(pin) Removes the account with the given pin from the bank and returns the account. If the pin is not in the bank, returns None. b.get(pin) Returns the account associated with the pin if the PIN is in the bank. Otherwise, returns None. b.computeInterest() Computes the interest on each account, deposits it in that account, and returns the total interest. __str__(b) Same as str(b). Returns a string representation of the bank (all the accounts).Explanation / Answer
//Bank.java
package com.src;
import java.util.ArrayList;
import java.util.Random;
public class Bank {
private String account;
private int pin;
private double balance;
ArrayList<Bank> accountsList = new ArrayList<>();
public Bank() {
}
public void add(String account) {
Bank b = new Bank();
b.account = account;
Random rand = new Random();
b.pin = rand.nextInt();
b.balance = 0;
accountsList.add(b);
}
public String remove(int pin) {
for(int i=0;i<accountsList.size();i++) {
Bank b = accountsList.get(i);
if(b.pin == pin) {
accountsList.remove(i);
return b.account;
}
}
return "NONE";
}
public String get(int pin) {
for(int i=0;i<accountsList.size();i++) {
Bank b = accountsList.get(i);
if(b.pin == pin) {
return b.account;
}
}
return "NONE";
}
public double computeInterest() {
double totalInterest=0;
for(int i=0;i<accountsList.size();i++) {
Bank b = accountsList.get(i);
double interest = balance*0.01;
totalInterest += interest;
b.balance += interest;
}
return totalInterest;
}
private String __str__() {
String str="";
for(int i=0;i<accountsList.size();i++) {
Bank b = accountsList.get(i);
str = str + b.account + " ";
}
return str;
}
}
//SavingAccount.java
package com.src;
public class SavingsAccount {
private String name;
private int pin;
private double balance;
public SavingsAccount(String name, int pin, double balance) {
this.name = name;
this.pin = pin;
this.balance = balance;
}
public void deposit(double amount) {
this.balance += amount;
}
public void withdraw(double amount) {
if(amount <= this.balance)
balance -= amount;
}
public double getBalance() {
return this.balance;
}
public String getName() {
return this.name;
}
public int getPin() {
return this.pin;
}
public void computeInterest() {
double interest = this.balance*0.1; // please use interest rate as per your
this.deposit(interest);
}
public String __str__(SavingsAccount a) {
return a.name + " " + a.pin + " " + a.balance;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.