练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功。



任务

1.修改Account类

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

b.修改withdraw方法来检查提款数目是否大于余额。如果amt小于balance,则从余额中扣除提款数目并返回true,否则余额不变返回false。

2.在exercise2主目录编译并运行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

//Account类

package banking;

public class Account {

private double balance;

public Account(double i)

{

balance=i;

}
public double getBalance()
{ return balance;
}
public boolean deposit(double i)
{
balance+=i;
System.out.print("Deposit "+i);
return true;
}
public boolean withdraw(double i)
{
if(balance>=i)
{
balance-=i;
System.out.print("Withdraw "+i);
return true;
}
else
{
System.out.print("余额不足");
return false;
}
}

}

//Testbanking类

package banking;

public class TestBanking {

public static void main(String[] args) {
Account a=new Account(500.00);
System.out.println("Creating an account with a "+a.getBalance()+"balance");
a.withdraw(150.00);
a.deposit(22.50);
a.withdraw(47.62);
System.out.println("The account has a balance of "+a.getBalance());
Customer c=new Customer("Jane", "Smith");
Account b=new Account(500.00);
c.setAccount(b);
a=c.getAccount();
System.out.println("Creating her account with a "+a.getBalance()+"balance");
System.out.println(":"+a.withdraw(150.00));
System.out.println(":"+a.deposit(22.50));
System.out.println(":"+a.withdraw(47.62));
System.out.println("Customer ["+c.getFirstName()+","+c.getLastName()+"] has a balance of "+a.getBalance()); }

}

//运行

Creating an account with a 500.0balance

Withdraw 150.0Deposit 22.5Withdraw 47.62The account has a balance of 324.88

Creating the customer Jane Smith

Creating her account with a 500.0balance

Withdraw 150.0:true

Deposit 22.5:true

Withdraw 47.62:true

Customer [Jane,Smith] has a balance of 324.88

练习3:修改withdraw 方法的更多相关文章

  1. 修改withdraw 方法

    修改withdraw 方法 练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功. 任务 1. 修改Account类 修改deposit 方法返回tr ...

  2. 练习3:修改withdraw 方法 练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功。

    boolean withdraw(double get){ if(get<=balance) { System.out.println("取钱"+get+"元,当余 ...

  3. Spring Aop 修改目标方法参数和返回值

    一.新建注解 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Document ...

  4. Django的内置登录、退出、修改密码方法

    Django中内置的登录.退出.修改密码方法. 1.url.py中使用django.contrib.auth中的views函数,django.views.generic中的TemplateView函数 ...

  5. ecshop2.73修改密码方法|ecshop2.73修改密码方法

    ecshop2.73修改密码方法|ecshop2.73修改密码方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2012-09-09   ecshop2.73正式版后 ...

  6. JS高级. 06 缓存、分析解决递归斐波那契数列、jQuery缓存、沙箱、函数的四种调用方式、call和apply修改函数调用方法

    缓存 cache 作用就是将一些常用的数据存储起来 提升性能 cdn //-----------------分析解决递归斐波那契数列<script> //定义一个缓存数组,存储已经计算出来 ...

  7. Mac修改hosts方法

    总有各种各样的原因需要修改hosts文件,那么就来简介下怎么修改.terminal中打开hosts: sudo vim /private/etc/hosts 打开文件后I开启插入模式,在最后一行添加你 ...

  8. 【VS开发】VS2010 MFC中控件、对话框等背景颜色动态修改的方法

    [VS开发]VS2010 MFC中控件.对话框等背景颜色动态修改的方法 标签(空格分隔):[VS开发] 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明: ...

  9. .net 4.0 以下HttpWebRequest Header 修改hosts方法

    .net 4.0 以下HttpWebRequest Header 修改hosts方法 特此记录 public class CusteredHeaderCollection : WebHeaderCol ...

随机推荐

  1. Chapter 2 Open Book——34

    His gaze became appraising. "You put on a good show," he said slowly. 他的凝视变成了评价.“你上演了一场好戏” ...

  2. 【1】ShopNC 模仿笔记(一)

    不断学习,人生将会成功. 1. 序 一直想模仿一个整套的商城, 今天在shopNC 里面看到了想要的一切, 所以把一些组件, 命名规范等记录下来, 提高以后的开发效率. 官方网站 PC : shopN ...

  3. swift 中Value Type VS Class Type

    ios 中Value Type 和 Class Type 有哪些异同点,这个问题是在微信的公共帐号中看到的,觉得挺有意思,这里梳理一下. 1.swift 中为什么要设置值类型? 值类型在参数传递.赋值 ...

  4. Egret 学习之简介,环境搭建及命令行语法 (一)

    1,简介 1)egret是一个开源免费的游戏框架,它使用TypeScript脚本语言进行开发:当游戏完成最终的打包后,可以将程序转换为h5游戏,实现跨平台性:它基于BSD(Berkly Softwar ...

  5. Opencv2.2版本以上CvvImage类的使用

    Opencv 2.2以上的版本不再包含CvvImage类,可有时我们在MFC中显示图片仍然需要CvvImage类,特别进行图像的拷贝.显示等操作的时候. 早期版本的CvvImage.h添加进工程也是可 ...

  6. Python学习日志(一)

    1.os - Normal Method: os.name() : os.getcwd(): 给出当前的目录,python当前的工作目录 os.listdir(): 返回 os.remove():删除 ...

  7. C++实现中缀表达式转前、后缀

    #include<iostream> #include<string> #include<stack> using namespace std; bool isIn ...

  8. mysql 提示too many connections”的解决办法

    最近使用python多线程连接mysq打数据,安装好mysql后,使用500线程连接发现提示:too many connections, 查询方法得知是需要进行配置才行: 产生这种问题的原因是: 连接 ...

  9. winsock编程IOCP模型实现代码

    winsock编程IOCP模型实现代码 话不多说,上代码.借鉴<windows核心编程>部分源码和CSDN小猪部分代码. stdafx.h依赖头文件: #include <iostr ...

  10. servlet之过滤器(转载)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.Filter 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...