练习3:修改withdraw 方法
练习目标-使用有返回值的方法:在本练习里,将修改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 方法的更多相关文章
- 修改withdraw 方法
修改withdraw 方法 练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功. 任务 1. 修改Account类 修改deposit 方法返回tr ...
- 练习3:修改withdraw 方法 练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功。
boolean withdraw(double get){ if(get<=balance) { System.out.println("取钱"+get+"元,当余 ...
- Spring Aop 修改目标方法参数和返回值
一.新建注解 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Document ...
- Django的内置登录、退出、修改密码方法
Django中内置的登录.退出.修改密码方法. 1.url.py中使用django.contrib.auth中的views函数,django.views.generic中的TemplateView函数 ...
- ecshop2.73修改密码方法|ecshop2.73修改密码方法
ecshop2.73修改密码方法|ecshop2.73修改密码方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2012-09-09 ecshop2.73正式版后 ...
- JS高级. 06 缓存、分析解决递归斐波那契数列、jQuery缓存、沙箱、函数的四种调用方式、call和apply修改函数调用方法
缓存 cache 作用就是将一些常用的数据存储起来 提升性能 cdn //-----------------分析解决递归斐波那契数列<script> //定义一个缓存数组,存储已经计算出来 ...
- Mac修改hosts方法
总有各种各样的原因需要修改hosts文件,那么就来简介下怎么修改.terminal中打开hosts: sudo vim /private/etc/hosts 打开文件后I开启插入模式,在最后一行添加你 ...
- 【VS开发】VS2010 MFC中控件、对话框等背景颜色动态修改的方法
[VS开发]VS2010 MFC中控件.对话框等背景颜色动态修改的方法 标签(空格分隔):[VS开发] 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明: ...
- .net 4.0 以下HttpWebRequest Header 修改hosts方法
.net 4.0 以下HttpWebRequest Header 修改hosts方法 特此记录 public class CusteredHeaderCollection : WebHeaderCol ...
随机推荐
- Centos 下安装Zabbix Linux 客户端
今天在linux上安装了客户端,过程如下: (1)下载zabbix客户端软件 wget www.zabbix.com/downloads/2.0.3/zabbix_agents_2.0.3.linux ...
- Maven pom项目部署
maven控制台运行程序 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec- ...
- Webdriver常用的元素定位
Webdriver常用定位元素的方法有以下八种: id.name.class name.tag name.link text.partial link text.xpath.class selecto ...
- SQLServer 重建索引前后对比
在做维护项目的时,我们经常会遇到索引维护的问题,通过语句,我们就可以判断某个表的索引是否需要重建. 执行一下语句:先分析表的索引 分析表的索引建立情况:DBCC showcontig('Table') ...
- checkbox:获取所有已选中的值
/**获取选中的checkbox值*/ function getChecked(){ var ids = ""; $("input:checkbox[name='id'] ...
- vue 相对其他热门 框架 优点 --- 待续
react vs vue 1. 处理动画 vue 更有优势 , 这是由于 React 有大量的检查机制 2.性能更高, 在React中,你需要在每个地方去实现 shouldComponentUpda ...
- 小团队git开发模式
实验室要使用Git进行代码管理,但是git非常复杂,各种开发模式也是层出不穷.作为新手的偶们很是发囧啊!!网上搜了一下,发现很多并不适合我们小团队运作(它本身就是为Linux内核管理而开发的分布式代码 ...
- 整合改造百度编译器到thinkphp上传图片到OSS
oss上传类库,放到public下,放到extend下,实例化是报错找不到上传类(我不知道为什么).
- imagick-3.1.0RC2 安装错误
PHP 5.4.8 安装 imagick-3.1.0RC2 安装冒出一大堆错误, 貌似跟 ImageMagick-6.8.0-2 这个版本有关系, 我之前换成低版本的 ImageMagick 就可以顺 ...
- CodeForces 700B Connecting Universities
统计每一条边的贡献,假设$u$是$v$的父节点,$(u,v)$的贡献为:$v$下面大学个数$f[v]$与$2*k-f[v]$的较小值. #pragma comment(linker, "/S ...