0

I'm making a banking system based on an UML that I got recently. I'm having problems finishing some methods, whereas I tried several things to complete it myself.

It's the following. My method "addInterest()" isn't adding any sort of interest to the balance that one account has. May it be checking account or savings account. it just doesn't add it.

And one more question, in the requirements it's being said that after every new customer has been made, 2 accounts are being made. I hope i've done it correctly, and some correction would be highly appreciated! I know that the code isn't 100% complete yet, but i'm doing it bit by bit.

Account.java

package com.company;

public class Account {
    public static Double interest = 0.042;
    private static Long number = 0L;
    private Double balance = 0.0;

    public Account(Double interest, Long number, Double balance) {

        number = Account.number;
        balance = this.balance;
    }


    public void deposit(Integer amount) {

        balance = balance + amount;

    }

    public double addInterest() {
        return balance += balance * interest;
    }

    public double getBalance() {

        return balance;

    }

    public static void main(String[] args) {
        Account checkingaccount = new Account(interest, 1L, 0.0);
        Account savingsaccount = new Account(interest, 1L, 0.0);

        Customer customer = new Customer(1L, "John Doe", savingsaccount, checkingaccount);


        checkingaccount.deposit(500);
        savingsaccount.deposit(100);


        checkingaccount.addInterest();
        savingsaccount.addInterest();

        System.out.println("Has a balance of " + checkingaccount.getBalance());

        System.out.println("Has a balance of " + savingsaccount.getBalance());

        System.out.println("Total balance is " + customer.totalBalance());



    }
}

Customer.java

class Customer {
    private static Long lastNumber;
    private String name;
    private Account savingsAccount;
    private Account checkingAccount;


public Customer(Long lastNumber, String name, Account savingsAccount, Account checkingAccount){
    //add lastnumber
    this.name = name;
    this.savingsAccount = savingsAccount;
    this.checkingAccount = checkingAccount;
}

public String getName(){
    return this.name;
}

public Account getCheckingaccount(Account checkingaccount){
    return checkingaccount;
}

//public Long getUniqueNumber(){
//
//}

public Account getSavingsaccount(Account savingsaccount){
    // return savingsAccount info
    return savingsaccount;
}

public double totalBalance(){
    // return totalbalance
    return savingsAccount.getBalance() + checkingAccount.getBalance();
}

















}
  • FYI floating-point types (i.e. `double`) aren't precise and will lead to errors when you need absolute precision, such as when handling money. [This question](https://stackoverflow.com/q/3730019/113632) goes into more detail. For practice or a homework assignment it's totally fine, but it's worth knowing that floating-point really isn't safe for this sort of task. – dimo414 Feb 13 '20 at 17:33
  • Be sure to initialize your balance. Right now you just declare the variable without actually putting it at 0.0 upon creation. – Tim Hunter Feb 13 '20 at 17:34
  • @dimo414 it's for homework specifically –  Feb 13 '20 at 17:34
  • @TimHunter i finished the Account constructor and added the initial balance for both accounts. –  Feb 13 '20 at 17:35
  • @Programmingboi123 yep, just sharing some knowledge :) – dimo414 Feb 13 '20 at 17:37
  • @Programmingboi123 Ah, you didn't put that in your posted code so I didn't see that. Another note for your `addInterest()` method, you can shorten the added interest and return it in the same line with something like `return balance += balance * interest;`. – Tim Hunter Feb 13 '20 at 17:44

1 Answers1

1

You don't appear to be calling addInterest() in your main() method or elsewhere. You might want to call it inside .deposit() or after both .deposit() calls in main(). It depends on how you want the Account to behave (e.g. most banks add interest on a given timeline, such as once a month).

dimo414
  • 47,227
  • 18
  • 148
  • 244