Java 基础实战 —Bank 项目
实验题目 1:
创建一个简单的银行程序包
实验目的:
Java 语言中面向对象的封装性及构造器的创建和使用。
实验说明:
在这个练习里,创建一个简单版本的 Account 类。将这个源文件放入 banking 程序包中。
在创建单个帐户的默认程序包中, 已编写了一个测试程序 TestBanking 。这个测试程序初始
化帐户余额,并可执行几种简单的事物处理。最后,该测试程 序显示该帐户的最终余额。
提示:
1.创建 banking 包
2 . 在 banking 包下创建 Account 类。该类必须实现上述 UML 框图中的模型。
a. 声明一个私有对象属性: balance ,这个属性保留了银行帐户的当前(或 即时)余额。
b. 声明一个带有一个参数 (init_balance )的公有构造器, 这个参数为 balance 属性赋值。
c. 声明一个公有方法 geBalance ,该方法用于获取经常余额。
d. 声明一个公有方法 deposit, 该方法向当前余额增加金额。
e. 声明一个公有方法 withdraw 从当前余额中减去金额。
3 .打开 TestBanking.java 文件,按提示完成编写,并编译 TestBanking.java 文件。
4 . 运行 TestBanking 类。可以看到下列输出结果:
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
//TestBanking.java 文件
/*
* 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 test;
import banking.*;
public class TestBanking {
public static void main(String[] args) {
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating an account with a 500.00 balance.");
//code
System.out.println("Withdraw 150.00");
//code
System.out.println("Deposit 22.50");
//code
System.out.println("Withdraw 47.62");
//code
// Print out the final account balance
System.out.println("The account has a balance of " + account.getBalance());
}
}
Java 基础实战 —Bank 项目
实验题目 2 :
扩展银行项目,添加一个 Customer 类。 Customer 类将包含一个 Account 对象。
实验目的:
使用引用类型的成员变量。
提 示:
1. 在 banking 包下的创建 Customer 类。该类必须实现上面的