KaiquanMah's picture
Create 04. Define attributes
89c5012 verified
In the program, the class BankAccount is defined.
Define the following attributes for the class:
String-type accountNumber and owner
double type balance and interest
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
System.out.println("Testing bank account creation...");
BankAccount pt = new BankAccount("1234-567","Randy Rich", 1000000000, 10.5);
BankAccount pt2 = new BankAccount("555-444","Pat Poor", 7.50, 0.5);
System.out.println("Bank account 1: " + pt);
System.out.println("Bank account 2: " + pt2);
}
}
class BankAccount {
// ADD ATTRIBUTES HERE
String accountNumber;
String owner;
double balance;
double interest;
// CONSTRUCTOR
public BankAccount(String tn, String o, double s, double k) {
accountNumber = tn;
owner = o;
balance = s;
interest = k;
}
@Override
public String toString() {
return "Bank account [account number=" + accountNumber + ", owner=" + owner + ", balance=" + balance + ", interest="
+ interest + "]";
}
}
Testing bank account creation...
Bank account 1: Bank account [account number=1234-567, owner=Randy Rich, balance=1.0E9, interest=10.5]
Bank account 2: Bank account [account number=555-444, owner=Pat Poor, balance=7.5, interest=0.5]