简体   繁体   中英

How to use a static variable from a class to update a non-static instance variable in another class?

Hello I am working on this homework and it's almost done but one thing doesn't work well. I created these two classes "SavingAccount" and "TimeAccount", subclasses of "Account" and they have different type of interest calculations that includes current time and time that account last updated. I have a time variable which is modeled as months and it's declared as zero in my Test class. And whenever i deposit, withdraw or transfer money it should update the accounts lastTimeUpdated variable and make it equal to currentMonth. Here's my Test class:

public class Test {

    public static int month = 0;

    static void click(){
        month++;
        System.out.println("Current month is " + month);
    }

    static void click(int x){
        month += x;
        System.out.println("Current month is " + month);
    }


    public static void main(String[] args) {

        Customer ali = new Customer("Ali");
        Customer veli = new Customer("Veli");
        Customer elif = new Customer("Elif");

        click();

        Account savingAccount1 = new SavingAccount(ali, garanti, 3);
        Account timeAccount1 = new TimeAccount(elif, akbank);

        click();

        savingAccount1.deposit(500);
        timeAccount1.deposit(400);

        click(5);

        System.out.println(savingAccount1.getLastUpdate());
        System.out.println(timeAccount1.getLastUpdate());


    }


}

And in output it says their last time updated is still 1 although i called click() method a couple of times and deposited some money in them.

This is my deposit method and it should change its lastUpdated variable to currentTime but it doesn't.

public abstract class Account {

    protected int currentTime = Test.month;
    protected int timeUpdated;

    public abstract double getBalance();

    public void deposit(double amount){
        balance = this.getBalance();
        balance += amount;
        timeUpdated = currentTime;
    }
}

This is getBalance() method for each subclasses, since they have different kind of interest calculations:

public class SavingAccount extends Account {

    private final int term;
    private static int number = 1;
    private final double interestRate = 0.2;

    public SavingAccount(Customer c, Bank b, int t){
        super(c, b, 0, number);
        term = t;
        number++;   
    }

    @Override
    public double getBalance() {
        double a = (1+term*interestRate/12);
        double b = (currentTime-timeUpdated)/term;
        balance = balance*Math.pow(a,b);
        return balance;
    }

}

and

public class TimeAccount extends Account {

    private static int number = 1;
    private final double interestRate = 0.1;

    public TimeAccount(Customer c, Bank b){
        super(c, b, 1, number);
        number++;   
    }

    public double getBalance(){
        double a = 1+interestRate/12;
        double b = currentTime-timeUpdated;
        balance = balance*Math.pow(a,b);
        return balance;
    }

}

I know this is long but i tried to make it clear to find where the problem is and my other question is marked as "duplicate" but i couldn't find the answer.

So my program doesn't update the lastUpdated time, that's why interests won't work. Thanks in advance.

Assigning a static variable doesn't continuously update.

protected int currentTime = Test.month;

This sets the value of currentTime to whatever Test.month was when the Account was created. If you want it to change, you have to update it.

看来您只是在初始化currentTime时才设置它,因此一旦创建了Account对象,它的currentTime就固定了,并且不会随着Test.month更改而更新。

You are only assigning currentTime once (at instantiation). It will not automatically update when you update Test.month. You need a mechanism to update the time in each of your instances of Account and call it in each place you call click. Really, the Test.month member is not particularly useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM