练习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 ...
随机推荐
- C#隐藏tabcontrol
//tabControl1.SizeMode = TabSizeMode.Fixed; //tabControl1.ItemSize = new Size(0, 1);
- 关于Android平台的搭建的心得---汪永骏
我本来是.net开发的,但看到目前互联网形式都朝着移动端开发迈进.大势所向,我便也开始学习Android的开发 今天就是要聊一下,我对Android开发的一些心得.今天讲的是,我在搭建Android平 ...
- Spring Security(17)——基于方法的权限控制
目录 1.1 intercept-methods定义方法权限控制 1.2 使用pointcut定义方法权限控制 1.3 使用注解定义方法权限控制 1.3.1 JSR-25 ...
- Excel的 OleDb 连接串的格式(Provider=Microsoft.ACE.OLEDB)
string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filePath + ";Exten ...
- android设备中USB转串口demo 下载
http://files.cnblogs.com/guobaPlayer/testUSB2Serial.apk USB转串口demo程序, 无需驱动,只要手机USB是OTG类型,插上我们的模块即可使用 ...
- 物料事务处理接口表 MTL_TRANSACTIONS_INTERFACE 账户别名使用 及 提示无效的分配账户字段
实例: PROCEDURE MAIN(P_ORGANIZATION_ID NUMBER, P_PERIOD_NAME VARCHAR2, XV_STATUS OUT VARCHAR, XV_MESG ...
- 使用webview加载html图片、表单超屏幕问题
webView加载html代码时,使用webView自带的 scalesPageToFit 可以解决图片所带来的超过屏幕问题:但是,所带来的问题就是文字变小了,怎样让图片边小,并且文字还是原来html ...
- Dubbo Zookeeper
发布 Service:每个<dubbo:service/>在spring内部都会生成一个ServiceBean实例,ServiceBean的实例化过程中调用export方法来暴露服务 co ...
- 快速挂载和分离VHD文件的小脚本
=======代码开始======== echo select vdisk file= %VHD文件路径% echo attach vdisk exit 分离VHD的脚本,只需要将上述代码中 at ...
- javascript screen对象
1.screen对象用于获取用户的屏幕信息 2. window.screen.属性 3.对象属性