Here my code.
Main SavingsDemo.java
public class SavingsDemo
{
public static void main(String[] args)
{
// Create a SavingsAccount object with a $100 balance,
// 3% interest rate, and a monthly service charge
// of $2.50.
SavingsAccount savings =
new SavingsAccount(100.0, 0.03, 2.50);
// Display what we've got.
System.out.printf("Balance: $%,.2f\n",
savings.getBalance());
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Make some deposits.
savings.deposit(25.00);
savings.deposit(10.00);
savings.deposit(35.00);
// Display what we've done so far.
System.out.printf("Balance: $%,.2f\n",
savings.getBalance());
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Make some withdrawals.
savings.withdraw(100.00);
savings.withdraw(50.00);
savings.withdraw(10.00);
savings.withdraw(1.00);
savings.withdraw(1.00);
// Display what we've done so far.
System.out.printf("Balance: $%,.2f\n",
savings.getBalance());
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
// Do the monthly processing.
savings.monthlyProcess();
// Display what we've done so far.
System.out.printf("Balance: $%,.2f\n",
savings.getBalance());
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
}
}
Superclass BankAccount.java
public class BankAccount
{
private double balance; //The balance in the account
private int numDeposits; //Number of deposits this month
private int numWithdrawals; //Number of withdrawals
private double interestRate; //Annual interest rate
private double monthlyServiceCharge; //Monthly service charge
/**
The constructor
Accept arguments for the balance and annual interest rate.
#param bal To hold the number of balance.
#param intRate To hold the number of annual interest rate.
#param mon To hold the number of monthly service charge.
*/
public BankAccount(double bal, double intRate, double mon)
{
balance = bal;
interestRate = intRate;
monthlyServiceCharge = mon;
}
/**
This method to hold the amount of deposit.
#param amount The amount of deposit.
*/
public void deposit(double amount)
{
balance = balance + amount;
numDeposits++;
}
/**
This method to hold the amount of withdrawal.
#param amount The amount of withdrawal.
*/
public void withdraw(double amount)
{
balance = balance - amount;
numWithdrawals++;
}
/**
This method to update the balance by calculating the monthly interest
earned by the account.
*/
private void calcInterest()
{
double monIntRate;
double monInt;
monIntRate = (interestRate / 12.0);
monInt = balance * monIntRate;
balance = balance + monInt;
}
/**
This method to calculate the monthly service charge.
*/
public void monthlyProcess()
{
balance = balance - monthlyServiceCharge;
calcInterest();
numWithdrawals = 0;
numDeposits = 0;
monthlyServiceCharge = 0;
}
/**
This method to hold the number of monthly service charge.
#param amount The number of monthly service charge.
*/
public void setMonthlyServiceCharges(double amount)
{
monthlyServiceCharge += amount;
}
/**
This method to return the amount of balance.
#return The amount of balance.
*/
public double getBalance()
{
return balance;
}
/**
This method to return the number of deposits.
#return The number of deposits.
*/
public int getNumDeposits()
{
return numDeposits;
}
/**
This method to return the number of withdrawals.
#return The number of withdrawals.
*/
public int getNumWithdrawals()
{
return numWithdrawals;
}
/**
This method to return the number of annual interest rate.
#return The number of annual interest rate.
*/
public double getInterestRate()
{
return interestRate;
}
/**
This method to return the amount of monthly service charge.
#return The amount of monthly service charge.
*/
public double getMonthlyServiceCharge()
{
return monthlyServiceCharge;
}
}
Subclass SavingsAccount.java
public class SavingsAccount extends BankAccount
{
private boolean status;
/**
The constructor
To accept the argument of balance, interest rate and monthly charge.
#param bal To hold the amount of balance.
#param intRate To hold the number of annual interest rate.
#param mon To hold the amount of monthly service charge.
*/
public SavingsAccount(double bal, double intRate, double mon)
{
super(bal, intRate, mon);
if (bal < 25)
{
status = false;
}
else
{
status = true;
}
}
/**
This method to determine whether the account is inactive before withdrawals
is made.
#param amount The amount of withdrawal.
*/
public void withdraw(double amount)
{
if (status = true)
{
super.withdraw(amount);
}
}
/**
This method to determine whether the account is inactive before a
deposit is made.
#param amount The amount of deposit.
*/
public void deposit(double amount)
{
if (status = true)
{
super.deposit(amount);
}
}
/**
This method to check the number of withdrawals.
*/
public void monthlyProcess()
{
if (getNumWithdrawals() > 4)
{
setMonthlyServiceCharges(getNumWithdrawals() - 4);
}
super.monthlyProcess();
if (getBalance() < 25)
{
status = false;
}
}
}
As the question above. I want to make only two withdraw because in the main program when I do 2 withdraw, the balance will become 20$ and it become inactive. But somehow my code did withdraw 5 times. So can someone give me an idea how to solve this problem. Thanks!
Change
public SavingsAccount(double bal, double intRate, double mon)
{
super(bal, intRate, mon);
if (bal < 25)
{
status = false;
}
else
{
status = true;
}
}
to
public void withdraw(double amount)
{
if (getBalance() > 25)
{
super.withdraw(amount);
}
}
and
public void withdraw(double amount)
{
if (status = true)
{
super.withdraw(amount);
}
}
to
public void withdraw(double amount)
{
if (getBalance() > 25)
{
super.withdraw(amount);
}
}
Related
I would like someones expert opinion on both of my account class and the test account interest class. The issue I am facing is that the code from the test account interest class just multiplies on from the previous 12 month compute interest when it is supposed to be used only once.
The issue is in the
public double computeInterest(int n)
{
balance=balance*(Math.pow((1+rate),n/12));
return balance;
}
It is in this method of where the problem is that I should not use the balance but to use a variable that will store the answer but I did not understand the person that very clearly and he was very vague by only stating a variable should be used.
public class Account
{
private double balance; //STATE
private double interestRate; //STATE
private double rate;//STATE
public Account()
{
balance = 0;
interestRate = 0;
}
public Account(double amount, double interestRate)
{
balance = amount;
rate = interestRate;
}
public void deposit(double amount)
{
balance=balance+amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public void setInterest(double rate)
{
balance = balance + balance * rate;
//this.setInterst = setInterest;
//setInterest = InterestRate / 12;
}
public double computeInterest(int n)
{
balance=balance*(Math.pow((1+rate),n/12));
return balance;
}
public double getsetInterest()
{
return rate;
}
public double getBalance()
{
return balance;
}
public void close()
{
balance =0;
}
}
This is my test account interest class:
public class TestAccountInterest
{
public static void main (String[] args)
{
Account acc1 = new Account(100, 0.1);//0.10);
Account acc2 = new Account(133, 0.2); //0.20);
/*************************************
ACC1 ACCOUNT BELOW
*************************************/
//acc1.deposit(100);
//acc1.withdraw(100);
System.out.println(acc1.computeInterest(12));
// //acc1.computeInterest(12);
// System.out.println(acc1.computeInterest(24));
/**************************************
ACC2 ACCOUNT BELOW
**************************************/
acc2.withdraw(100);
acc2.deposit(100);
//acc2.computeInterest(24);
System.out.println(acc2.computeInterest(24));
}
}
This is the final output:
110.00000000000001
191.51999999999998
As you can see for the second one the figure is multiplied by the 12 month compute interest with the 24 month compute interest this stems from the method in the account class:
public double computeInterest(int n)
{
balance=balance*(Math.pow((1+rate),n/12));
return balance;
}
If I take out the balance it still causes and error so I confused on this particular part.
Code,
public double computeInterest(int n) {
balance = balance * (Math.pow((1 + rate), n / 12));
return balance;
}
should be changed to
public double computeInterest(int n) {
return balance * Math.pow(1 + rate, n / 12);
}
You shouldn't change balance field while computing interest. You might like to have a separate method to update balance where you do , balance = balance + computed_interest or something like that.
Also, I have remove unnecessary parenthesis. That was not an error but simply making your code less readable.
Assignment:
Change the Account class so that funds can be moved form one account to another. Think of this as withdrawing money from one account and depositing it into another. Change the main method of Banking class to show this new service.
I am working on a bank account class that can deposit and withdraw money from bank account balances. I am working on the class part of the the assignment where you declare all the methods for the driver. My assignment wants me to make a method that will withdraw money from one account and deposit that money into another account. I already know how to withdraw and deposit, I just don't know how to transfer money from one account to another account. Here is my code for the transfer method so far:
import java.text.NumberFormat;
public class Account
{
private NumberFormat fmt = NumberFormat.getCurrencyInstance();
private final double RATE = 0.035; // interest rate of 3.5%
private long acctNumber;
private double balance;
private String name;
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account (String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
//-----------------------------------------------------------------
// Validates the transaction, then deposits the specified amount
// into the account. Returns the new balance.
//-----------------------------------------------------------------
public double deposit (double amount)
{
if (amount < 0) // deposit value is negative
{
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (acctNumber + " " + fmt.format(amount));
}
else
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Validates the transaction, then withdraws the specified amount
// from the account. Returns the new balance.
//-----------------------------------------------------------------
public double withdraw (double amount, double fee)
{
amount += fee;
if (amount < 0) // withdraw value is negative
{
System.out.println ();
System.out.println ("Error: Withdraw amount is invalid.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
}
else
if (amount > balance) // withdraw value exceeds balance
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else
balance = balance - amount;
return balance;
}
public double transfer (double amount, double fee)
{
amount += fee;
if (amount < 0) // withdraw value is negative
{
System.out.println ();
System.out.println ("Error: Withdraw amount is invalid.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
}
else
if (amount > balance) // withdraw value exceeds balance
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else
balance = balance - amount;
//What should I put here to deposit the amount into another account?
if (amount < 0) // deposit value is negative
{
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (acctNumber + " " + fmt.format(amount));
}
else
balance = balance + amount;
}
//-----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of the account.
//-----------------------------------------------------------------
public double getBalance ()
{
return balance;
}
//-----------------------------------------------------------------
// Returns the account number.
//-----------------------------------------------------------------
public long getAccountNumber ()
{
return acctNumber;
}
//-----------------------------------------------------------------
// Returns a one-line description of the account as a string.
//-----------------------------------------------------------------
public String toString ()
{
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
}
There is no purpose to your transfer method. Tranfer should be a method in your main class which instantiates the accounts. Example of a transfer would be.
public static void main(String[] args)
{
// This creates two different accounts :)
Account a = new Account("userA", 123, 200);
Account b = new Account("userB", 234, 500);
// Tranfer
a.withdraw(100, 5);
System.out.println(a.getBalance());
b.deposit(100);
System.out.println(b.getBalance());
}
and turning this into a method would be
public static void transfer (Account from, Account to, double amount, double fee)
{
from.withdraw(amount, fee);
to.deposit(amount);
}
EDIT
If i understood your second question correctly, you want to create a default account? If i misunderstood, can you provide more details? (link to the assignment or something)
You need to write 6 new methods for it, you don't already have them,
getOwner
setOwner
getAcctNumber
setAcctNumber
getBalance
setBalance
Account CLass
import java.text.NumberFormat;
public class Account
{
private NumberFormat fmt = NumberFormat.getCurrencyInstance();
private final double RATE = 0.035; // interest rate of 3.5%
private long acctNumber;
private double balance;
private String name;
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account (String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
public Account()
{
// This would be the default constructor and default account
name ="N/A";
acctNumber = 0;
balance = 0.0;
}
//-----------------------------------------------------------------
// Validates the transaction, then deposits the specified amount
// into the account. Returns the new balance.
//-----------------------------------------------------------------
public double deposit (double amount)
{
if (amount < 0) // deposit value is negative
{
System.out.println ();
System.out.println ("Error: Deposit amount is invalid.");
System.out.println (acctNumber + " " + fmt.format(amount));
}
else
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Validates the transaction, then withdraws the specified amount
// from the account. Returns the new balance.
//-----------------------------------------------------------------
public double withdraw (double amount, double fee)
{
amount += fee;
if (amount < 0) // withdraw value is negative
{
System.out.println ();
System.out.println ("Error: Withdraw amount is invalid.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
}
else
if (amount > balance) // withdraw value exceeds balance
{
System.out.println ();
System.out.println ("Error: Insufficient funds.");
System.out.println ("Account: " + acctNumber);
System.out.println ("Requested: " + fmt.format(amount));
System.out.println ("Available: " + fmt.format(balance));
}
else
balance = balance - amount;
return balance;
}
//-----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of the account.
//-----------------------------------------------------------------
public double getBalance ()
{
return balance;
}
//-----------------------------------------------------------------
// Returns the account number.
//-----------------------------------------------------------------
public long getAccountNumber ()
{
return acctNumber;
}
//-----------------------------------------------------------------
// Returns a one-line description of the account as a string.
//-----------------------------------------------------------------
public String toString ()
{
return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
}
}
Drvier Class
public class test
{
public static void main(String[] args)
{
// Created here
Account defaultAccount = new Account();
Account a = new Account("userA", 123, 200);
Account b = new Account("userB", 234, 500);
System.out.println(defaultAccount.getBalance());
// Tranfer
a.withdraw(100, 5);
System.out.println(a.getBalance());
b.deposit(100);
System.out.println(b.getBalance());
}
public static void transfer (Account from, Account to, double amount, double fee)
{
from.withdraw(amount, fee);
to.deposit(amount);
}
}
Create a method transfer(String ID, double amount) that utilises the deposit(String Id, double amt) and withdrawal(double amount) methods to transfer the funds.
Ok I am having problems creating a transfer to method to transfer "money" from one account to the next. After the transfer the amounts of each account would be printed out. I created the code but when I run it the amount transferred is set to the bank account amount. It should just deduct from one account and add to the next account. What am I doing wrong?
This is my class with contructors and methods:
public class Account {
// Instance variables
private double balance;
// Constructors
public Account(double initialBalance) {
balance = initialBalance;
}
public Account(int account, double balance) {
this.balance = balance;
}
public Account() {
balance = 0.0;
}
// Instance methods
public void setBalance() {
balance = Math.random() * 1000;
balance = Math.round((balance * 100.0)+.5) / 100.0;
}
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance - amount;
}
public double getBalance() {
balance = Math.round((balance * 100.0)+.5) / 100.0;
return balance;
}
public void close() {
balance = 0.0;
}
public void transferTo(Account bank, double x) {
if (x <= balance) {
withdraw(x);
bank.deposit(x);
System.out.println("\nTransfer succesful. Tansfered: $" + bank.getBalance());
} else if (x > balance) {
System.out.println("\nTransfer failed, not enough balance!");
}
}
}
This is my class with the main method
public class MyBank {
public static void main(String[] args) {
Account[] bank = { new Account(), new Account(), new Account(),
new Account() };
bank[0].setBalance();
bank[1].setBalance();
bank[2].setBalance();
bank[3].setBalance();
System.out.println("Accounts 1 balance is: $" + bank[0].getBalance());
System.out.println("Accounts 2 balance is: $" + bank[1].getBalance());
System.out.println("Accounts 3 balance is: $" + bank[2].getBalance());
System.out.println("Accounts 4 balance is: $" + bank[3].getBalance());
double x = Math.random()*100;
bank[0].transferTo(bank[1], x);
System.out.println("Remaining balance of Account 1: $" + bank[0].getBalance());
System.out.println("Remaining balance of Account 2: $" + bank[1].getBalance());
double y = (Math.random()*300);
bank[2].transferTo(bank[3], y);
System.out.println("Remaining balance of Account 3: $" + bank[2].getBalance());
System.out.println("Remaining balance of Account 4: $" + bank[3].getBalance());
}
}
One issue you did have, is that you were accidentally printing the BALANCE rather than the amount actually transferred, in your transferTo() method. I fixed this. On another note, if you're going to be printing these numbers out, look into printf() to make them appear as actual dollar amounts. I did it to one of the lines to show you an example.
PS - Try NOT using Math.random() for stuff when you're trying to debug.
public void transferTo(Account bank, double x) {
if (x <= this.balance) {
withdraw(x);
bank.deposit(x);
System.out.println("\nTransfer succesful. Tansfered: $" + x);
} else { //does not need to be else if, because if not <=, it MUST be >.
System.out.println("\nTransfer failed, not enough balance!");
}
}
I can verify that the method works perfectly fine, after these edits, on my machine. This is what I used to test:
public class MyBank {
public static void main(String[] args) {
Account[] bank = { new Account(), new Account(), new Account(),
new Account() };
bank[0].setBalance();
bank[1].setBalance();
bank[2].setBalance();
bank[3].setBalance();
double x = 100.00;
System.out.printf("Transferring $%.2f from Acc 1 to Acc 2.\n", x); //printf example
System.out.println("Acc 1 previous balance: " + bank[0].getBalance());
System.out.println("Acc 2 previous balance: " + bank[1].getBalance());
bank[0].transferTo(bank[1], x);
System.out.println("Acc 1 new balance: " + bank[0].getBalance());
System.out.println("Acc 2 new balance: " + bank[1].getBalance());
}
}
And lastly, my output:
Transferring $100.00 from Acc 1 to Acc 2.
Acc 1 previous balance: 106.76
Acc 2 previous balance: 266.18
Transfer succesful. Transfered: $100.0
Acc 1 new balance: 6.77
Acc 2 new balance: 366.19
System.out.println("\nTransfer succesful. Tansfered: $" + bank.getBalance());
This prints the other account's new balance, because that's what you told it to do.
If you want to print the amount transferred:
System.out.println("\nTransfer succesful. Tansfered: $" + x);
// Bank account class
class BankAccount {
constructor(fname, lname, account_number) {
const minBalance = 10000;
this.name = `${fname} ${lname}`;
this.balance = minBalance;
this.account_number = account_number;
}
details() {
console.log(
`Cleint name: ${this.name} \n Balance: ${this.balance} \n Account number: ${this.account_number}`
);
}
isPositive(amount) {
return amount > 0 ? true : false;
}
isVlaidAmount(amount) {
return isNaN(amount) || !this.isPositive(amount) ? false : true;
}
deposit(amount) {
if (this.isVlaidAmount(amount)) {
this.balance += amount;
} else {
console.log("Sorry, this transcation cannot be completed!");
}
}
withdraw(amount) {
if (this.isVlaidAmount(amount)) {
this.balance -= amount;
} else {
console.log("Sorry, you have insufficient funds.");
}
}
transfer(amount, reciever) {
if (this.isVlaidAmount(amount)) {
this.withdraw(amount);
reciever.deposit(amount);
}
}
}
// create objects
let acc1 = new BankAccount("George", "Omara", 422);
let acc2 = new BankAccount("Haward", "Walowets", 662);
I have a very basic BankAccount class and tester.
/** A bank account that has a balance that can be changed by depositing and withdrawals also deducts a fee for each withdrawal and deposit.
*
* #author lapenta1
*
*/
public class BankAccount
{
private double balance;
private double feeCharge;
/** Contructs a bank account with a balance of zero
*
*/
public BankAccount()
{
balance = 0;
feeCharge = 2.50;
}
/**Constructs a bank account with a given balance
*
* #param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
/** Deposits money into the bank account
*
* #param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount - feeCharge;
}
/**Withdrawals money from the bank account
*
* #param amount the amount to withdrawal
*/
public void withdraw(double amount)
{
balance = balance - amount - feeCharge;
}
/**Gets the current balance of the bank account
*
* #return the current balance
*/
public double getBalance()
{
return balance;
}
public void deductMonthlyCharge()
{
}
}
This is the tester class
public class BankAccountTester
{
public static void main(String[] args)
{
BankAccount myWallet = new BankAccount();
myWallet.deposit(200);
System.out.println(myWallet.getBalance());
BankAccount otherWallet = new BankAccount();
otherWallet.deposit(200);
System.out.println(otherWallet.getBalance());
}
}
now I am trying to make it work so there is basically a set amount of withdrawals/deposits that can be used before the 2.50 fee is taken away every month. The only hint I have is to use math.max(actual transaction count, free transaction count) I am lost! any help?
Something like this would do the trick. Just add another if condition(s) to make same for deposits etc.
private int noOfWithdrawals = 0;
private int tresholdOfWithdrawals = 5; //Change this according to your requirement
public void withdraw(double amount)
{
if(noOfWithdrawals <= tresholdOfWithdrawals) {
balance = balance - amount;
noOfWithdrawals++;
}
else {
balance = balance - amount - feeCharge;
}
}
private double balance;
private double feeCharge;
private double freeCharge; // mutator for how many times it will be free
private double trCount = 0; //should increment after every transaction
public double deductMonthlyCharge(){
double monthlyCharge = (Math.max(trCount, freeCharge) - freeCharge) * feeCharge;
balance -= monthlyCharge;
trCount = 0;
return balance;
}
public class AccountDriver {
public static void main(String[] args) {
// ID, Balance, Annual Interest Rate
Account number1 = new Account();
Account number2 = new Account(1122, 20000.00, 0.045);
// Default account
System.out.println("The Account ID is: " + number1.getId());
System.out.println("The Account Balance is: " + number1.getBalance());
// System.out.println("The Account Balance is: "+
// number1.getMontlyInterest());
System.out.println("");
// Ask to withdraw 2500
System.out.println("The Account ID is: " + number2.getId());
number2.withdraw(2500.00);
number2.deposit(3000.00);
System.out.println("Account Balance is " + number2.getBalance());
// System.out.println("The montly interest is : "+
// number2.getMontlyInterest());
System.out.println("");
}
}
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
public Account(int id, double balance, double annualInterestRate) {
this.setId(id);
this.setBalance(this.balance);
this.setBalance(annualInterestRate);
}
public Account() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMontlyInterest(double montlyInterest) {
// Given Formula
// double MontlyInterest= this.balance * get.MontlyInterestRate();
return montlyInterest;
}
public double getMontlyInterestRate(double montlyInterestRate) {
// Given Formula
montlyInterestRate = this.annualInterestRate / 12;
return montlyInterestRate;
}
double withdraw(double amount) {
return balance -= amount;
}
double deposit(double amount) {
return balance += amount;
}
}
I am getting error
The Account ID is: 0
The Account Balance is: 0.0
The Account ID is: 1122
Account Balance is 20500.0
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getMontlyInterestRate(double) in the type Account is not applicable for the arguments ()
at Account.getMontlyInterest(Account.java:41)
at AccountDriver.main(AccountDriver.java:21)
You did 2 small mistakes in your code.
In your constructor these 2 lines
this.setBalance(this.balance); // this.balance is the instance variable and not the parameter passed
^^^^ - this is not required, just use the balance parameter passed.
this.setBalance(annualInterestRate); // you are re-writing the balance with interest rate
^^^^^^^^^^ - You need to set annual interest rate and not the balance here.
should be
this.setBalance(balance); // sets the balance passed to the instance variable balance
this.setAnnualInterestRate(annualInterestRate); // sets the annual interest rate
Now since the annualInterestRate is set, you can get the monthly interest rate by modifying getMontlyInterestRate method like this.
public double getMontlyInterestRate() {
// Given Formula
return this.annualInterestRate / 12;
}
And you can print your monthly interest rate by uncommenting your System.out.println code.
System.out.println("The montly interest is : "+ number2.getMontlyInterestRate());
And the monthly interest method would look like this:
public double getMontlyInterest() { // no parameter required
// Given Formula
double MontlyInterest = this.balance * getMontlyInterestRate(); // balance multiplied by monthly interest rate
return MontlyInterest; // return the value
}
System.out.println("The montly interest is : "+ number2.getMontlyInterest());