Home » Java » java – Any idea why I can not assign a unique number to my SavingsAccount?-Exceptionshub

java – Any idea why I can not assign a unique number to my SavingsAccount?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have one abstract class Account and one subclass SavingsAccount, but when I create SavingsAccount object it doesn’t assign a number like 1001, 1002, 1003 and so on. Any idea why?

import java.util.ArrayList;

public abstract class Account {
    private String accountType;
    private static double balance = 0;
    private static int accountId;
    private static int accountNumberCounter = 1000;
    private ArrayList<Account> accounts;

    public Account(String acType, int acNumber){
        accountType = acType;
        accountNumberCounter ++;
        accountId = accountNumberCounter;

    }

    public Account() {
        accountNumberCounter++;
        accountId = accountNumberCounter;
    }
    public void addAccounts(Account acc){
        accounts.add(acc);

    }
    public void deposit(double amount){
        balance += amount;
    }
    public abstract boolean withdraw(double value);


    public String getAccountInfo(){
        return "Account type: " + accountType + ", Account number: " + accountId;
    }

    public int getAccountNumber(){
        return accountId;
    }
    public String getAccount(){
        String accountInformation = "Account Number: : " + accountId + "\nAccount Type: " + accountType;
        return accountInformation;
    }
    public void closeCurrentAccount() {
        if (balance < 0) {
            System.out.println("Your balance: " + balance + "Close your debt");
        } else {
            System.out.println("Ending balance: " + balance);
        }
    }


}

And this is SavingsAccount

public class SavingsAccount extends Account {
    private static double balance = 0;
    private static final double RATE = 1.0;
    private static  String accountType = "Savings Account";
    private static int accountId;


    public SavingsAccount(){
        super();
    }

    public double getBalance(){
        return balance;
    }

    public void deposit(double amount){
        balance = balance + amount;
    }

    public boolean withdraw(double amount){
        if (balance<= amount){
            System.out.println("You have only" + amount + "left on your account.");
        return false;
        }
        else{
            balance -= amount;
            System.out.println("You put:" + amount);
            return true;
        }
    }

    public static String getAccountType(){
        return accountType;
    }

    public static double getRate(){
        return RATE;
    }
    public static double calculateRate(){
        return balance += (balance * RATE) / 100;
    }
    public String getAccount(){
        String accountInformation = "Account Number: : " + accountId + "\nAccount Type: " + accountType +
            "\nBalance: " + balance + "\nRate: " + RATE;
        return accountInformation;
    }
}

Maybe it’s not needed but here is Customer class as well

import java.util.ArrayList;

public class Customer {
    private String name;
    private String surname;
    private String personalNumber;
    private ArrayList<Account> accounts;

    public Customer(String customerName, String customerSurname, String customerPersonalNumber)
    {
        name = customerName;
        surname = customerSurname;
        personalNumber = customerPersonalNumber;
        this.accounts = new ArrayList<Account>();
    }
    public Customer(){

    }
    public String getName(){
        return name;
    }
    public String getSurname(){
        return surname;
    }
    public String getPersonalNumber(){
        return personalNumber;
    }
    public void setName(String aName){
        name = aName;
    }
    public void setSurname(String aSurname){
        surname = aSurname;
    }

    public void setPersonalNumber(String aPersonalNumber){
        personalNumber = aPersonalNumber;
    }

    public void addAccounts(Account acc){
        accounts.add(acc);
    }

    public String getCustomerInfo(){
        return  name + " " +  surname + " " + personalNumber;
    }

    public int getFirstAccountNumber(){
        return accounts.get(0).getAccountNumber();

    }
    public int getLastAccountNumber(){
        return accounts.get(accounts.size()-1).getAccountNumber();
    }

    public ArrayList<Account> getAllAccounts(){
        return accounts;
    }
}

When I do some tests this unique number doesn’t get assigned.

Is it something wrong with a constructor?

How to&Answers:

You marked accountId as static as well, so every instance of Account will acquire the same id, that is the latest you “generated”.

Just mark accountId as a normal instance variable (i.e., remove static).

As a side note, re-declaring accountId in SavingsAccount breaks encapsulation and is, frankly, weird. You inherited getAccountNumber() from Account. Use that instead of accessing accountId directly. You’re treating it as a read-only variable anyway.

Answer:

A static variable is not unique to each instance; rather, it is common to all instances of a class because static members are class members, not instance members. I can see that you have declared many static variables in Account and SavingsAccount whereas you need to declare them without static keyword.

Some more improvements are required:

A. Remove accountType and accountId from the class, SavingsAccount and declare them in Account as follows (check here for more information):

protected String accountType;
protected int accountId;
protected double balance;

B. Remove the following constructor from Account

public Account(String acType, int acNumber){
    accountType = acType;
    accountNumberCounter ++;
    accountId = accountNumberCounter;
}

C. Change the constructor, SavingsAccount() as follows:

public SavingsAccount() {
    accountType = "Savings Account";
    super();
}