当前位置:首页 > java bank项目
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(\ //code
System.out.println(\ //code
System.out.println(\ //code
System.out.println(\ //code
// Print out the final account balance
System.out.println(\ } }
Java 基础实战—Bank 项目
实验题目 2:
扩展银行项目,添加一个 Customer 类。Customer 类将包含一个 Account对象。 实验目的:
使用引用类型的成员变量。 提 示:
1. 在banking包下的创建Customer类。该类必须实现上面的UML图表中的模型。 a. 声明三个私有对象属性:firstName、lastName 和 account。
b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f 和 l)
c. 声明两个公有存取器来访问该对象属性,方法 getFirstName 和 getLastName 返
回相应的属性。
d. 声明 setAccount 方法来对 account 属性赋值。
e. 声明 getAccount 方法以获取 account 属性。
2. 在 exercise2 主目录里,编译运行这个 TestBanking 程序。应该看到如下输出结果:
Creating the customer Jane Smith.
Creating her account with a 500.00 balance. Withdraw 150.00 Deposit 22.50 Withdraw 47.62
Customer [Smith, Jane] 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. */
import banking.*;
public class TestBanking {
public static void main(String[] args) { Customer customer; Account account;
// Create an account that can has a 500.00 balance.
System.out.println(\ //code
System.out.println(\ //code
System.out.println(\ //code
System.out.println(\ //code
System.out.println(\ //code
// Print out the final account balance
System.out.println(\ + \
+ \ } }
Java 基础实战—Bank 项目
实验题目 3:
修改 withdraw 方法以返回一个布尔值,指示交易是否成功。 实验目的:
使用有返回值的方法。 提 示:
1. 修改 Account 类
a. 修改 deposit 方法返回 true(意味所有存款是成功的)。
b. 修改 withdraw 方法来检查提款数目是否大于余额。如果amt小于 balance, 则从余额
共分享92篇相关文档