JAVA PLEASE PART 1 For some types of accounts, it is important to keep track of
ID: 3888711 • Letter: J
Question
JAVA PLEASE
PART 1
For some types of accounts, it is important to keep track of the minimum balance over a period.
Also, some accounts have adjustments made on a periodic basis: monthly fees, interest earned
To implement tracking the minimum balance in BankAccount, make the following changes to the
class.
Instance field minBalance
A ‘getter’ for minBalance: getMinBalance
Method resetMinBalance
No parameters
Sets minBalance to the value of balance
Method updateMinBalance
No parameters
If minBalance is greater than balance, sets minBalance to the value of balance
Any constructor will call resetMinBalance at the end
deposit and withdraw both call updateMinBalance at the end
PART 2
Some accounts have adjustments made on a periodic basis: monthly fees, interest earned. A new type of account is introduced
Add a method to carry out tasks that must be taken care of at the end of each month
Method month_end_process
public, can be used externally
Calls resetMinBalance. Using this method at the end of each month means that the min balance is always the minimum balance during the past month
Create a new class SavingsAccount that extends BankAccount
There is an instance field, annualInterestRate
Provide constructors
The first constructor takes two parameters: the account id and the annual interest rate
The second constructor takes three parameters: the account id, the initial balance and the annual interest rate
Getter and setter for the annual interest rate
Override the monthly processing method
Add the interest earned that month to the account
The amount of interest earned in a month is the mininum balance during the month times the monthly interest rate
The monthly interest rate is the annual interest rate divided by 12
After adding interest to the account, call the monthly processing method in BankAccount.
PART 3
This part of the lab uses ArrayList to let customers of a bank have direct access to their own accounts. This part requires additions to the AccountOwner class.
Add a new instance field accountsOwned that is an ArrayList
Each constructor should initialize accountsOwned to an empty list
Add a ‘getter’ for accountsOwned
Add a method named totalBalanceOfAccountsOwned that takes no parameters and returns the total balance of all accounts owned by that person
Add a method named ownerOfAllAccountsOwned that returns true if all the accounts in accountsOwned actually specify this Person as the owner. Otherwise, the method returns false.
EXISTING CODE TO BE MODIFIED (sorry i left this off)
public class BankAccount {
private double balance; // account balance
private String accountId; // unique account identifier
private AccountOwner owner; // owner of the account
/**
* Initializes the account id to the parameter value and initializes
* the balance to 0.
* The owner of the account is set to null.
*
* @param initialAccountId Should be a valid account id. (can't check it at this time)
*/
public BankAccount(String initialAccountId) {
accountId = initialAccountId;
balance = 0.0; // not necessary since balance will default to 0.0 if nothing else is done
// to initialize it.
}
/**
* Initializes the account id and balance from the parameter values.
* The owneer of the account is set to null;
*
* @param initialAccountId Should be a valid account id. (can't check it at this time)
* @param initialBalance Should be positive
*/
public BankAccount(String initialAccountId, double initialBalance) {
accountId = initialAccountId;
if(initialBalance <= 0) {
// error
System.err.println("Initial balance for an account should be positive: " + initialBalance);
System.exit(1);
} else {
balance = initialBalance;
}
}
public BankAccount(String initialAccountid, double initialBalance, AccountOwner initialOwner) {
accountId = initialAccountid;
owner = initialOwner;
if(initialBalance <= 0) {
// error
System.err.println("Initial balance for an account should be positive: " + initialBalance);
System.exit(1);
} else {
balance = initialBalance;
}
}
public String getAccountId() {
return accountId;
}
public double getBalance() {
return balance;
}
/**
* Deposit the given amount in this bank account.
* This will add amount to the balance.
*
* @param amount Should be a positive number, that is, greater than 0
*/
public void deposit(double amount) {
if(amount > 0) {
// go ahead and process the deposit
balance += amount;
} else {
// report an error
System.err.println("Amount of deposit should be positive: " + amount);
System.exit(1);
}
}
/**
* Withdraw the given amount from this bank account.
* The amount will be deducted from the account balance.
*
* @param amount Should be a positive value.
* Should be no bigger than the current balance.
*/
public void withdraw(double amount) {
if(amount <= 0) {
// error: amount should be positive
System.err.println("Amount of withdrawal should be positive: " + amount);
System.exit(1);
} else if(amount > balance) {
// error: amount should be no bigger than the balance
System.err.println("Amount of withdrawal should be no bigger than the balance: " + amount + " " + balance);
System.exit(1);
} else {
// amount is ok, process the withdrawal
balance -= amount;
}
}
public void setOwner(AccountOwner newOwner) {
owner = newOwner;
}
public AccountOwner getOwner() {
return owner;
}
}
------------------------------------------------------------------------------------
public class AccountOwner {
private String name;
private String accountOwnerId;
private String e_mail;
/**
* Initialize the properties of this object to the given parameter values.
* If a value is not known, use null.
*/
public AccountOwner(String name, String accountOwnerId, String e_mail) {
this.name = name;
this.accountOwnerId = accountOwnerId;
this.e_mail = e_mail;
}
public AccountOwner(String accountOwnerId) {
this.accountOwnerId = accountOwnerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public String getAccountOwnerIdId() {
return accountOwnerId;
}
}
Explanation / Answer
See below the modified code. as per problem description code is modified and inline comments are added to better understand the code.
if you like my work, then pls give me a feedback and rate my ans. it will help me to answer your questions.
Thanks.
Code:
Class BankAccount:
public class BankAccount
{
private double balance; // account balance
private String accountId; // unique account identifier
private AccountOwner owner; // owner of the account
private double minBalance;//minimum balance
/**
* Initializes the account id to the parameter value and initializes
* the balance to 0.
* The owner of the account is set to null.
*
* @param initialAccountId Should be a valid account id. (can't check it at this time)
*/
public BankAccount(String initialAccountId) {
accountId = initialAccountId;
balance = 0.0; // not necessary since balance will default to 0.0 if nothing else is done
// to initialize it.
//call to reset minimum balance
resetMinBalance();
}
/**
* Initializes the account id and balance from the parameter values.
* The owneer of the account is set to null;
*
* @param initialAccountId Should be a valid account id. (can't check it at this time)
* @param initialBalance Should be positive
*/
public BankAccount(String initialAccountId, double initialBalance)
{
accountId = initialAccountId;
if(initialBalance <= 0) {
// error
System.err.println("Initial balance for an account should be positive: " + initialBalance);
System.exit(1);
} else {
balance = initialBalance;
}
//call to reset minimum balance
resetMinBalance();
}
public BankAccount(String initialAccountid, double initialBalance, AccountOwner initialOwner)
{
accountId = initialAccountid;
owner = initialOwner;
if(initialBalance <= 0) {
// error
System.err.println("Initial balance for an account should be positive: " + initialBalance);
System.exit(1);
} else {
balance = initialBalance;
}
//call to reset minimum balance
resetMinBalance();
}
public String getAccountId() {
return accountId;
}
public double getBalance() {
return balance;
}
/**
* Deposit the given amount in this bank account.
* This will add amount to the balance.
*
* @param amount Should be a positive number, that is, greater than 0
*/
public void deposit(double amount)
{
if(amount > 0) {
// go ahead and process the deposit
balance += amount;
} else {
// report an error
System.err.println("Amount of deposit should be positive: " + amount);
System.exit(1);
}
//call to update minimum balance
updateMinBalance();
}
/**
* Withdraw the given amount from this bank account.
* The amount will be deducted from the account balance.
*
* @param amount Should be a positive value.
* Should be no bigger than the current balance.
*/
public void withdraw(double amount)
{
if(amount <= 0) {
// error: amount should be positive
System.err.println("Amount of withdrawal should be positive: " + amount);
System.exit(1);
} else if(amount > balance) {
// error: amount should be no bigger than the balance
System.err.println("Amount of withdrawal should be no bigger than the balance: " + amount + " " + balance);
System.exit(1);
} else {
// amount is ok, process the withdrawal
balance -= amount;
}
//call to update minimum balance
updateMinBalance();
}
public void setOwner(AccountOwner newOwner) {
owner = newOwner;
}
public AccountOwner getOwner() {
return owner;
}
//getter method for minmum balance
public double getMinBalance()
{
return minBalance;
}
//reset method which will reset minimum balance.
public void resetMinBalance()
{
this.minBalance=this.balance;
}
public void updateMinBalance()
{
if(minBalance>balance)
{
minBalance=balance;
}
}
//method which will carry out all the activities at the end of month
public void month_end_process()
{
resetMinBalance();
}
}
Class AccountOwner
import java.util.ArrayList;
public class AccountOwner
{
private String name;
private String accountOwnerId;
private String e_mail;
private ArrayList<BankAccount> accountsOwned;
/**
* Initialize the properties of this object to the given parameter values.
* If a value is not known, use null.
*/
public AccountOwner(String name, String accountOwnerId, String e_mail)
{
this.name = name;
this.accountOwnerId = accountOwnerId;
this.e_mail = e_mail;
//initialise accountsOwned to emptyList
accountsOwned=new ArrayList<BankAccount>();
}
public AccountOwner(String accountOwnerId) {
this.accountOwnerId = accountOwnerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public String getAccountOwnerIdId() {
return accountOwnerId;
}
public ArrayList getAccountsOwned() {
return accountsOwned;
}
public void setAccountsOwned(ArrayList accountsOwned) {
this.accountsOwned = accountsOwned;
}
//adds up all balances in his all account to show , the total balance
public double totalBalanceOfAccountsOwned()
{
double totalBalance=0.0;
for(BankAccount account : accountsOwned)
{
totalBalance=totalBalance+account.getBalance();
}
return totalBalance;
}
//method checks the each account hold by owner has a owner id same that of his own.
public boolean ownerOfAllAccountsOwned()
{
for(BankAccount account : accountsOwned)
{
String ownerID=account.getOwner().getAccountOwnerIdId();
if(!ownerID.equals(this.getAccountOwnerIdId()))
{
return false;
}
}
return true;
}
}
Class SavingsAccount:
public class SavingsAccount extends BankAccount
{
private double annualInterestRate;
public SavingsAccount(String initialAccountId)
{
super(initialAccountId);
// TODO Auto-generated constructor stub
}
public SavingsAccount(String initialAccountId,double initialBalance)
{
super(initialAccountId,initialBalance);
// TODO Auto-generated constructor stub
}
public SavingsAccount(String initialAccountId,double initialBalance,AccountOwner owner)
{
super(initialAccountId,initialBalance,owner);
// TODO Auto-generated constructor stub
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//overriding the end of month process method
@Override
public void month_end_process()
{
//calculating interest
double interest=this.getMinBalance()*(annualInterestRate/12);
//adding interest earned in month.
deposit(interest);
//calling the end of month process of bank account.
super.month_end_process();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.