Bank1:
package banking1; //账户 public class Account { private double balance;// 账户余额 public Account(double init_balance) { balance = init_balance; } public double getBlance() { return balance; } // 存钱 public void deposit(double amt) {// amt 要存的额度 balance += amt; } // 取钱 public void withdraw(double amt) {// amt:要取得额度 if (balance >= amt) { balance -= amt; } else { System.out.println("余额不足"); } } }
TestBanking1:
/* * This class creates the program to test the banking classes. * It creates a new Bank, sets the Customer (with an initial balance), * and performs a series of transactions with the Account object. */ package TestBanking; import banking1.Account; public class TestBanking1 { public static void main(String[] args) { Account account; // Create an account that can has a 500.00 balance. account = new Account(500.00); System.out.println("Creating an account with a 500.00 balance."); // code account.withdraw(150); System.out.println("Withdraw 150.00"); // code account.deposit(22.50); System.out.println("Deposit 22.50"); // code account.withdraw(47.62); System.out.println("Withdraw 47.62"); // code // Print out the final account balance System.out.println("The account has a balance of " + account.getBlance()); } }
输出结果:
Creating an account with a 500.00 balance.
Withdraw 150.00
Deposit 22.50
Withdraw 47.62
The account has a balance of 324.88