BankAccount-InstanceStatic – instance vs static member field & method
BankAccount-InstanceStatic
import java.util.*;
public class BankAccount {
private int balance;
private static int interestRate; // (%)
public BankAccount() {
balance = 0;
interestRate = 5;
}
public void deposit(int amount) {
balance += amount;
}
public void withdrawl(int amount) {
balance -= amount;
}
public void print() {
System.out.println("final balance=" +
(balance + balance * interestRate * 0.01) +
" balance=" +
balance +
" interest=" +
(balance * interestRate * 0.01));
printInterestRate();
}
public static void setInterestRate(int interestRate) {
BankAccount.interestRate = interestRate;
}
public static void printInterestRate() {
System.out.println("interestRate=" +
interestRate);
}
public int getUserInputInt() {
Scanner input = new Scanner(System.in);
System.out.print("Please enter Integer value: ");
int value = input.nextInt();
return value;
}
public static void main(String[] args) {
BankAccount b = new BankAccount();
b.deposit(2000);
b.withdrawl(1000);
b.print();
BankAccount.printInterestRate();
int amount = b.getUserInputInt();
b.deposit(amount);
b.print();
}
}