Chinaunix首页 | 论坛 | 博客
  • 博客访问: 353418
  • 博文数量: 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:27:51

实验题目 3 修改 withdraw 方法以返回一个布尔值,指示交易是否成功。

实验目的 使用有返回值的方法。

提 示

1修改 Account

a. 修改 deposit 方法返回 true意味所有存款是成功的

b. 修改 withdraw 方法来检查提款数目是否大于余额。如果amt小于

balance, 则从余额中扣除提款数目并返回 true否则余额不变返回

false

2exercise3 主目录编译并运行 TestBanking 程序将看到下列输出;

Creating the customer Jane Smith.

Creating her account with a 500.00 balance.

Withdraw 150.00: true

Deposit 22.50: true

Withdraw 47.62: true

Withdraw 400.00: false

Customer [Smith, Jane] has a balance of 324.88

点击(此处)折叠或打开

  1. package com.troubleshooting.bank3;

  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 boolean deposit(double amt) {
  19.         balance += amt;
  20.         return true;
  21.     }

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

  34. package com.troubleshooting.bank3;

  35. public class Customer {

  36.     private String firstName;
  37.     private String lastName;
  38.     private Account account;

  39.     public Customer(String f, String l) {
  40.         firstName = f;
  41.         lastName = l;
  42.     }

  43.     public String getFirstName() {
  44.         return firstName;
  45.     }

  46.     public void setFirstName(String firstName) {
  47.         this.firstName = firstName;
  48.     }

  49.     public String getLastName() {
  50.         return lastName;
  51.     }

  52.     public void setLastName(String lastName) {
  53.         this.lastName = lastName;
  54.     }

  55.     public Account getAccount() {
  56.         return account;
  57.     }

  58.     public void setAccount(Account account) {
  59.         this.account = account;
  60.     }

  61. }

  62. package com.troubleshooting.testbank1;
  63. /*
  64.  * This class creates the program to test the banking classes.
  65.  * It creates a new Bank, sets the Customer (with an initial balance),
  66.  * and performs a series of transactions with the Account object.
  67.  */

  68. import com.troubleshooting.bank3.Customer;
  69. import com.troubleshooting.bank3.Account;

  70. public class TestBanking3 {

  71.   public static void main(String[] args) {
  72.     Customer customer;
  73.     Account account;

  74.     // Create an account that can has a 500.00 balance.
  75.     account = new Account(500.00);
  76.     System.out.println("Creating the customer Jane Smith.");
  77.     //code
  78.     customer = new Customer("Jane", "Smith");
  79.     System.out.println("Creating her account with a 500.00 balance.");
  80.     
  81.     //code
  82.     customer.setAccount(account);
  83.     account = customer.getAccount();
  84.     // Perform some account transactions
  85.     System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
  86.     System.out.println("Deposit 22.50: " + account.deposit(22.50));
  87.     System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
  88.     System.out.println("Withdraw 400.00: " + account.withdraw(400.00));

  89.     // Print out the final account balance
  90.     System.out.println("Customer [" + customer.getLastName()
  91.          + ", " + customer.getFirstName()
  92.          + "] has a balance of " + account.getBlance());
  93.   }
  94. }
显示结果:
Creating the customer Jane Smith.
Creating her account with a 500.00 balance.
Withdraw 150.00: true
Deposit 22.50: true
Withdraw 47.62: true
余额不足!
Withdraw 400.00: false
Customer [Smith, Jane] has a balance of 324.88


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