Java class add method and do calculation on fields

Description

Java class add method and do calculation on fields

class Account {/*  w  w  w  .j a  va2  s  .com*/
    String name;
    String address;
    double balance;

    public void display() {
       System.out.print(name);
       System.out.print(" (");
       System.out.print(address);
       System.out.print(") has $");
       System.out.print(balance);
    }

    public double getInterest(double percentageRate) {
        return balance * percentageRate / 100.00;
    }
}

public class Main {

    public static void main(String args[]) {
        Account myAccount = new Account();
        Account yourAccount = new Account();

        myAccount.name = "Java";
        myAccount.address = "222 Main Lane";
        myAccount.balance = 24.02;

        yourAccount.name = "Javascript";
        yourAccount.address = "111 Main Street";
        yourAccount.balance = 42.42;

        myAccount.display();
        
        System.out.print(" plus $");
        System.out.print(myAccount.getInterest(5.00));
        System.out.println(" interest ");

        yourAccount.display();
        
        double yourInterestRate = 7.00;         
        System.out.print(" plus $");
        double yourInterestAmount = yourAccount.getInterest(yourInterestRate);
        System.out.print(yourInterestAmount);
        System.out.println(" interest ");
    }
}



PreviousNext

Related