Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347193
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: Java

2015-07-16 21:22:27

实验题目 1 创建一个简单的银行程序包

实验目的

Java 语言中面向对象的封装性及构造器的创建和使用。

实验说明 在这个练习里创建一个简单版本的 Account 类。将这个源文件放入 banking 序包中。在创建单个帐户的默认程序包中已编写了一个测试程序 TestBanking 这个测试程序初始化帐户余额并可执行几种简单的事物处理。最后该测试程 序 显示该帐户的最终余额。

提示

1创建 banking

2banking 包下创建 Account 类。该类必须实现上述 UML 框图中的模型。

a. 声明一个私有对象属性balance这个属性保留了银行帐户的当前或 即余额。

b. 声明一个带有一个参数 init_balance 的公有构造器 这个参数为

balance 属性赋值。

c. 声明一个公有方法 getBalance该方法用于获取账户余额。

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


点击(此处)折叠或打开

  1. package com.troubleshooting.bank1;

  2. //定义一个账户
  3. public class Account {
  4.     // 账户余额
  5.     private double balance;

  6.     public Account(double init_balance) {
  7.         balance = init_balance;
  8.     }

  9.     // 获取账户余额
  10.     public double getBlance() {
  11.         return balance;
  12.     }

  13.     public void setBalance(double balance) {
  14.         this.balance = balance;
  15.     }

  16.     // 存钱
  17.     // amt存钱的额度
  18.     public void deposit(double amt) {
  19.         balance += amt;
  20.     }

  21.     // 取钱
  22.     // amt取钱的额度
  23.     public void withdraw(double amt) {
  24.         if (balance >= amt) {
  25.             balance -= amt;
  26.         } else {
  27.             System.out.println("余额不足!");
  28.         }
  29.     }
  30. }

  31. /*
  32.  * This class creates the program to test the banking classes.
  33.  * It creates a new Bank, sets the Customer (with an initial balance),
  34.  * and performs a series of transactions with the Account object.
  35.  */
  36. package com.troubleshooting.testbank1;

  37. import com.troubleshooting.bank1.Account;

  38. public class TestBanking1 {

  39.     public static void main(String[] args) {
  40.         Account account;

  41.         // Create an account that can has a 500.00 balance.
  42.         System.out.println("Creating an account with a 500.00 balance.");
  43.         // code
  44.         account = new Account(500);
  45.         System.out.println("Withdraw 150.00");

  46.         // code
  47.         account.withdraw(150);
  48.         System.out.println("Deposit 22.50");

  49.         // code
  50.         account.deposit(22.50);
  51.         System.out.println("Withdraw 47.62");
  52.         // code
  53.         account.withdraw(47.62);
  54.         // Print out the final account balance
  55.         System.out.println("The account has a balance of " + account.getBlance());
  56.     }
  57. }
显示结果:
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

阅读(2049) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~