-1

I am totally lost, and I am not able to recover or get past this.

What I'm trying to do is to have a banking app where I should have SavingsAccount and CreditAccount that should inherit from Account class. When my subclasses try to getBalance/getIntereset I don't get correct value and instead getting Null at some times.

Can you help me point out the problem and see what needs to be sorted out to get my withdraw() and deleteCustomer() methods working?

Big thanks!

logic class


import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class BankLogic {

        //Deklarera global lista med kunder
    private ArrayList<Customer> customers = new ArrayList<>();
    private ArrayList<Transaction> transactions = new ArrayList<>();
    Date date;

    public ArrayList<String> getAllCustomers() {
        ArrayList<String> customersStr = new ArrayList<>();
        if (customers == null) {
            customersStr.add("");
            return customersStr;
        } else
            for (Customer customer : customers) {
                customersStr.add(customer.getpNo() + " " + customer.getName() + " " + customer.getSurname());
            }
        return customersStr;
    }

  
    public boolean createCustomer(String name, String surname, String pNo) {
        try {
            for (int i = 0; i < customers.size(); i++) {
                if (customers.get(i).getpNo().equals(pNo)) {
                    return false;
                }
            }
            if (!name.equals("") || !surname.equals("")) {
                Customer customer = new Customer(name, surname, pNo);
                customers.add(customer);
                return true;
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return false;
    }


   
    public ArrayList<String> getCustomer(String pNo) {
        ArrayList<String> presentCustomerArr = new ArrayList<>();

        try {
            for (int i = 0; i < customers.size(); i++) {
                if (customers.get(i).getpNo().equals(pNo)) {
                    presentCustomerArr.add(customers.get(i).getpNo() + " " + customers.get(i).getName() + " " + customers.get(i).getSurname());
                    ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
                    if (customers.get(i).getCustomerAccounts() != null) {
                        for (int j = 0; j < accountList.size(); j++) {
                            presentCustomerArr.add(accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + accountList.get(j).getInterestRate() + " %");
                        }
                    }
                    return presentCustomerArr;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

   
    public boolean changeCustomerName(String name, String surname, String pNo) {

        for (int i = 0; i < customers.size(); i++) {
            if (customers.get(i).getpNo().equals(pNo)) {
                if (!name.equals("") && !surname.equals("")) {
                    customers.get(i).setName(name);
                    customers.get(i).setSurname(surname);
                    return true;
                }
                if (name.equals("") && !surname.equals("")) {
                    customers.get(i).setName(customers.get(i).getName());
                    customers.get(i).setSurname(surname);
                    return true;
                }
                if (!name.equals("") && surname.equals("")) {
                    customers.get(i).setName(name);
                    customers.get(i).setSurname(customers.get(i).getSurname());
                    return true;
                }
            }
        }

        return false;
    }

    
    public int createSavingsAccount(String pNo) {

        try {
            for (int i = 0; i < customers.size(); i++) {
                if (customers.get(i).getpNo().equals(pNo)) {
                    ArrayList<Account> accountList = new ArrayList<>();

                    if (customers.get(i).getCustomerAccounts() != null) {
                        accountList = customers.get(i).getCustomerAccounts();
                    }
                    SavingsAccount savingsAccount = new SavingsAccount();
                    accountList.add(savingsAccount);
                    customers.get(i).setCustomerAccounts(accountList);
                    int accNr = -1;
                    for (int j = 0; j < accountList.size(); j++) {
                        accNr = savingsAccount.getAccountNumber();
                    }
                    return accNr;
                }
            }

        } catch (Exception e) {

        }

        return -1;
    }

    
    public String getAccount(String pNo, int accountId) {

        for (int i = 0; i < customers.size(); i++) {
            if (customers.get(i).getpNo().equals(pNo)) {

                if (customers.get(i).getCustomerAccounts() != null) {
                    ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
                    for (int j = 0; j < accountList.size(); j++) {
                        if (accountList.get(j).getAccountNumber() == accountId) {
                            String accPresentation = accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + accountList.get(j).getInterestRate() + " %";
                            return accPresentation;
                        }
                    }
                }
            }

        }
        return null;
    }

    
    public boolean deposit(String pNo, int accountId, int amount) {
        if (amount > 0) {
            for (int i = 0; i < customers.size(); i++) {
                if (customers.get(i).getpNo().equals(pNo)) {
                    if (customers.get(i).getCustomerAccounts() != null || customers.get(i).getCustomerAccounts() != null) {
                        ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
                        for (int j = 0; j < accountList.size(); j++) {
                            if (accountList.get(j).getAccountNumber() == accountId) {
                                accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() + amount));
                                transactions = addTransaction(new Date(), amount, accountList.get(j).getBalance(), accountList.get(j).getAccountNumber(), false);
                                customers.get(j).setAccountTransactions(transactions);
                                return true;
                            }
                        }
                    }
                }

            }
        }
        return false;
    }

    
    private ArrayList<Transaction> addTransaction(Date date, int amount, Double balance, int accountnumber, boolean isAWithdrawal) {
        Transaction transaction = new Transaction(new Date(), (double) amount, balance, accountnumber, isAWithdrawal);
        transactions.add(transaction);

        return transactions;
    }

    
    public boolean withdraw(String pNo, int accountId, int amount) {

        Boolean isUpdated = false;
        if (amount > 0) {

            for (int i = 0; i < customers.size(); i++) {
                if (customers.get(i).getpNo().equals(pNo)) {
                    if (customers.get(i).getCustomerAccounts() != null) {
                        ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
                        for (int j = 0; j < accountList.size(); j++) {

                            if (accountList.get(j).getAccountNumber() == accountId) {
                                isUpdated = accountList.get(j).withdraw(accountList, amount, customers, transactions);
                                if (isUpdated){
                                    return false;
                                }

                                //transactions = accountList.get(j).addTransaction(transactions, date, amount, Double.valueOf(accountList.get(j).getBalance()), accountList.get(j).getAccountNumber(), true);
                                customers.get(j).setAccountTransactions(transactions);
                                return true;

                            }
                        }
                    }
                }

            }
        }
        return false;
    }

    public void updateTransactions(ArrayList<Transaction> transactions) {
        this.transactions = transactions;
    }

    
    private boolean balanceAfterWithdrawIsOk(int amount, Double balance) {
        if (balance - amount < -5000) {
            return false;
        } else return true;
    }

    
    public String closeAccount(String pNr, int accountId) {
        for (int i = 0; i < customers.size(); i++) {
            if (customers.get(i).getpNo().equals(pNr)) {
                if (customers.get(i).getCustomerAccounts() != null) {
                    ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
                    for (int j = 0; j < accountList.size(); j++) {
                        if (accountList.get(j).getAccountNumber() == accountId) {
                            String closedAcc = accountList.get(j).calculateClosedAccountBalance(customers.get(i).getCustomerAccounts().get(j));
                            customers.get(i).getCustomerAccounts().remove(accountList.get(j));
                            return closedAcc;
                        }
                    }
                }
            }

        }
        return null;
    }

    
    public ArrayList<String> deleteCustomer(String pNo) {

        ArrayList<String> deletedCustomerArr = new ArrayList<>();

        try {
            for (int i = 0; i < customers.size(); i++) {
                if (customers.get(i).getpNo().equals(pNo)) {
                    int accNr = -1;
                    deletedCustomerArr.add(customers.get(i).getpNo() + " " + customers.get(i).getName() + " " + customers.get(i).getSurname());

                    ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
                    if (customers.get(i).getCustomerAccounts() != null) {
                        for (int j = 0; j < accountList.size(); j++) {

                            //String hej = stringFormatted(accountList.get(j).getInterest());
                            try {
                            String addString = accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + stringFormatted(accountList.get(j).getInterest());
                            deletedCustomerArr.add(addString);

                            }
                            catch (Exception e) {
                                System.out.println(e);
                            }

                        }
                        accountList.clear();
                    }
                    customers.remove(i);
                    return deletedCustomerArr;
                }

            }
        } catch (Exception e) {
            System.out.println(e);
        }


        return null;
    }

    
    public int createCreditAccount(String pNo) {

        try {
            for (int i = 0; i < customers.size(); i++) {
                if (customers.get(i).getpNo().equals(pNo)) {
                    ArrayList<Account> accountList = new ArrayList<>();
                    if (customers.get(i).getCustomerAccounts() != null) {
                        accountList = customers.get(i).getCustomerAccounts();
                    }
                    for (int j = 0; j < accountList.size(); j++) {
                    }
                    CreditAccount creditAccount = new CreditAccount((double) 0, 0.5, "Kreditkonto");
                    accountList.add(creditAccount);
                    customers.get(i).setCustomerAccounts(accountList);
                    int accNr = -1;
                    for (int j = 0; j < accountList.size(); j++) {
                        accNr = creditAccount.getAccountNumber();
                    }
                    return accNr;
                }
            }

        } catch (Exception e) {
        }

        return -1;
    }

    
    public ArrayList<String> getTransactions(String pNo, int accountId) {
        ArrayList<String> arrList = new ArrayList<>();
        try {
            for (Customer customer : customers) {
                if (customer.getpNo() == pNo) {
                    for (Transaction transaction : customer.getAccountTransactions()) {
                        if (accountId == transaction.getAccountNumber()) {
                            transactions.get(this.transactions.indexOf(transaction));
                            arrList.add(simpleDateFormatter(transactions.get(this.transactions.indexOf(transaction)).getDateAndTime()) + " " + (transactions.get(this.transactions.indexOf(transaction)).isAWithdrawal() == true ? "−" : "").concat(stringFormatted(transactions.get(this.transactions.indexOf(transaction)).getAmount())) + " Saldo: " + stringFormatted(transactions.get(this.transactions.indexOf(transaction)).getBalanceAfterTransaction()));
                        }


                    }
                }
            }
        } catch (NullPointerException e) {
            for (Customer customer : customers) {
                for (int i = 0; i < customer.getCustomerAccounts().size(); i++) {
                    if (customer.getCustomerAccounts().get(i).getAccountNumber() == accountId && customer.getAccountTransactions() == null) {
                        return arrList;
                    }
                }
                if (customer.getAccountTransactions() == null) {
                    return null;
                }
            }
            return null;
        }
        return arrList;
    }

    // Custom methods

    
    private String stringFormatted(Double balance) {
        balance = Double.valueOf(balance);
        String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
        return balanceStr;
    }

    
    private String simpleDateFormatter(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        return strDate;
    }

}

Account class


import java.util.ArrayList;
import java.util.Date;

public abstract class Account {

 

    private Double balance;
    private Double interestRate;
    private Double interest;
    private int accountNumber;
    private String accountType;
    private int numberOfWithdraws = 0;
    private static int lastAssignedNumber = 1000;

    public Account() {
        lastAssignedNumber++;
        this.balance = Double.valueOf(0);
        this.accountNumber = lastAssignedNumber;
    }

    public Account(Double balance, Double interestRate, String accountType, int lastAssignedNumber) {
    }

    public Account(Double balance, Double interestRate, String accountType) {
        lastAssignedNumber++;
        this.accountNumber = lastAssignedNumber;
        this.balance = balance;
        this.interestRate = interestRate;
        this.accountType = accountType;
    }

    public Account(Double balance, Double interestRate, Double interest, int accountNumber, String accountType, int numberOfWithdraws) {
        this.balance = balance;
        this.interestRate = interestRate;
        this.interest = interest;
        this.accountNumber = accountNumber;
        this.accountType = accountType;
        this.numberOfWithdraws = numberOfWithdraws;
    }

    public Double getBalance() {

        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    public Double getInterestRate() {
        return interestRate;
    }

    public void setInterestRate(Double interestRate) {
        this.interestRate = interestRate;
    }

    public Double getInterest() {

        return interest;
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public String getAccountType() {
        return accountType;
    }

    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }

    public int getNumberOfWithdraws() {
        return numberOfWithdraws;
    }

    public void setNumberOfWithdraws(int numberOfWithdraws) {
        this.numberOfWithdraws = numberOfWithdraws;
    }

    /
    public abstract String calculateClosedAccountBalance(Account customerAccounts);

    
    public ArrayList<Transaction> addTransaction(ArrayList<Transaction> transactions, Date date, int amount, Double balance, int accountnumber, boolean isAWithdrawal) {
        Transaction transaction = new Transaction(new Date(), (double) amount, balance, accountnumber, isAWithdrawal);
        transactions.add(transaction);

        return transactions;
    }

    public abstract boolean withdraw(ArrayList<Account> customerAccounts, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions);

    public abstract String stringFormatted(Double balance);
}

CreditAccount


import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;

public class CreditAccount extends Account {

    

    private String accountType = "Kreditkonto";
    private Double interestRate;
    private Double creditLimit = Double.valueOf(5000);
    private Double interest;
    private Double balance;


    public CreditAccount(String accountType, Double interestRate, Double creditLimit) {
        this.accountType = accountType;
        this.interestRate = interestRate;
        this.creditLimit = creditLimit;
    }

    public CreditAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber, String accountType1, Double interestRate1, Double creditLimit) {
        super(balance, interestRate, accountType, lastAssignedNumber);
        this.accountType = accountType1;
        this.interestRate = interestRate1;
        this.creditLimit = creditLimit;
    }

    public CreditAccount(Double balance, Double interestRate, String accountType, String accountType1, Double interestRate1, Double creditLimit) {
        super(balance, interestRate, accountType);
        this.accountType = accountType1;
        this.interestRate = interestRate1;
        this.creditLimit = creditLimit;
    }

    public CreditAccount(Double balance, Double interestRate, String accountType) {
        super(balance, interestRate, accountType);

    }

    public CreditAccount(Double balance, Double interestRate, Double interest, int accountNumber, String accountType, int numberOfWithdraws) {
        super(balance, interestRate, interest, accountNumber, accountType, numberOfWithdraws);
    }

    public CreditAccount() {
    }

    public CreditAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber) {
        super(balance, interestRate, accountType, lastAssignedNumber);
    }

    public Double getCreditLimit() {
        return creditLimit;
    }

    
    @Override
    public String calculateClosedAccountBalance(Account customerAccounts) {
        String closedAccStr = "";

        closedAccStr = customerAccounts.getAccountNumber() + " " + stringFormatted(customerAccounts.getBalance()) + " " + customerAccounts.getAccountType() + " " + stringFormatted(customerAccounts.getInterest());


        return closedAccStr;
    }

    @Override
    public String stringFormatted(Double balance) {
        String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
        return balanceStr;
    }

    @Override
    public String getAccountType() {
        return accountType;
    }

    @Override
    public Double getInterestRate() {
        if (this.getBalance() < 0) {
            this.interestRate = Double.valueOf(7);
        } else this.interestRate = Double.valueOf(0.5);
        return interestRate;
    }
    @Override
    public Double getInterest() {

        if (getBalance() < 0) {
            interest = getBalance() * -7 / 100;
        }

        return interest;
    }

    @Override
    public boolean withdraw(ArrayList<Account> accountList, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions) {
        for (int j = 0; j < accountList.size(); j++) {
            if (accountList.get(j).getAccountType().equals("Kreditkonto")) {
                if (balanceAfterWithdrawIsOk(amount, accountList.get(j).getBalance())) {
                    accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() + (-amount)));
                    if (accountList.get(j).getBalance() < 0) {
                        accountList.get(j).setInterestRate(Double.valueOf(7));
                    }
                    transactions = addTransaction(transactions, new Date(), amount, Double.valueOf(accountList.get(j).getBalance()), accountList.get(j).getAccountNumber(), true);
                    BankLogic bankLogic = new BankLogic();
                    bankLogic.updateTransactions(transactions);
                    customers.get(j).setAccountTransactions(transactions);
                    return true;
                }
            }
        }
        return false;
    }

    
    private boolean balanceAfterWithdrawIsOk(int amount, Double balance) {
        if (balance - amount < -5000) {
            return false;
        } else return true;
    }

}

SavingsAccount


import java.text.NumberFormat;
import java.util.ArrayList;

public class SavingsAccount extends Account {


    private String accountType = "Sparkonto";
    private Double interestRate = 1.2;
    private Double withdrawalFeePercentage = Double.valueOf(2);
    private Double interest;


    public SavingsAccount(String accountType, Double interestRate, Double withdrawalFeePercentage) {
        this.accountType = accountType;
        this.interestRate = interestRate;
        this.withdrawalFeePercentage = withdrawalFeePercentage;
    }

    public SavingsAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber, String accountType1, Double interestRate1, Double withdrawalFeePercentage) {
        super(balance, interestRate, accountType, lastAssignedNumber);
        this.accountType = accountType1;
        this.interestRate = interestRate1;
        this.withdrawalFeePercentage = withdrawalFeePercentage;
    }

    public SavingsAccount(Double balance, Double interestRate, String accountType, String accountType1, Double interestRate1, Double withdrawalFeePercentage) {
        super(balance, interestRate, accountType);
        this.accountType = accountType1;
        this.interestRate = interestRate1;
        this.withdrawalFeePercentage = withdrawalFeePercentage;
    }

    public SavingsAccount(Double balance, Double interestRate, String accountType, int accountNumber, int numberOfWithdraws, double withdrawalFeePercentage) {
        super(balance, interestRate, accountType, accountNumber);
        this.withdrawalFeePercentage = Double.valueOf(2);
    }

    public SavingsAccount() {

    }

    
    @Override
    public String calculateClosedAccountBalance(Account customerAccounts) {
        String closedAccStr = "";

            closedAccStr = customerAccounts.getAccountNumber() + " " + stringFormatted(customerAccounts.getBalance()) + " " + customerAccounts.getAccountType() + " " + stringFormatted(customerAccounts.getInterest());


        return closedAccStr;
    }

    @Override
    public String stringFormatted(Double balance) {
        String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
        return balanceStr;
    }

    public Double getWithdrawalFeePercentage() {
        return withdrawalFeePercentage;
    }

    @Override
    public String getAccountType() {
        return accountType;
    }

    @Override
    public Double getInterestRate() {
        return interestRate;
    }

    @Override
    public Double getInterest() {
        interest = getBalance() * 1.2 / 100;
        return interest;
    }

    @Override
    public boolean withdraw(ArrayList<Account> accountList, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions) {
        for (int j = 0; j < accountList.size(); j++) {
            if (accountList.get(j).getAccountType().equals("Sparkonto")) {
                //accountList.get(j).withdrawMoney();
                if (accountList.get(j).getNumberOfWithdraws() > 0) {
                    amount = (int) (amount * 1.02);
                }
                if (accountList.get(j).getBalance() >= amount) {
                    accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() - amount));
                    accountList.get(j).setNumberOfWithdraws(+1);
                    BankLogic bankLogic = new BankLogic();
                    bankLogic.updateTransactions(transactions);
                    return true;
                }
            }
        }
        return false;
    }
}

CompileNow
  • 79
  • 14
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Yoshikage Kira Dec 01 '21 at 20:10
  • 2
    Remove from subclasses everything that you already have in the parent class (Account). Notice that you are repeating `balance`, `interest`, `interestRate`. There shouldn't be any repetitions of this type. I haven't gone through your entire code as it's so much (use builder instead of so many Constructors), but that would for sure cause some issues. – Mirek Pluta Dec 01 '21 at 20:10
  • Thanks, I will go through check that I have initialized balance and interest. But what if I want different behaviour from each subclass when getting the Interest? I need to set different interest depending on the account balance. Should I keep the interest in the subclasses and remove from parent? – CompileNow Dec 02 '21 at 06:46

1 Answers1

1

If you are getting null values when you getBalance/getInterest you must not have initialized them yet. I would suggest tracing through your code and using a debugger/print statements to find when you assign/update those values since your inheritance structure seems correct.

jp1
  • 34
  • 3
  • 3
    "I would suggest tracing through your code and using print statements". Using a debugger is much better here. Also, this answer should be comment. – Yoshikage Kira Dec 01 '21 at 20:10