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

任务
1. 修改Account类
- 修改deposit 方法返回true(意味所有存款是成功的)。
- 修改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
package banking; public class Account
{
//成员属性
private double balance ;//余额 // 无参构造
public Account()
{ }
// 有参构造
public Account(double balance)
{
this.balance = balance;
} // set get
public double getBalance()
{
return balance;
}
public void setBalance(double balance)
{
this.balance = balance;
} public boolean withdraw1( double amt )
{
if(amt>&&amt<balance==true)
{
balance -= amt ;
System.out.print("Withdraw " + amt+" : ");
return true ;
}
else
{
System.out.print("Withdraw " + amt+" : ");
return false ;
}
} public boolean deposit1( double amt )
{
if(amt<)
{
System.out.print("Deposit " + amt+" : " );
return false ;
}
else
{
balance += amt ;
System.out.print("Deposit " + amt +" : ");
return true ;
}
} }
Customer cr1 = new Customer( ) ;
cr1.setFirstName("Jane");
cr1.setLastName("Smith");
cr1.setBalance();
System.out.println("Creating the customer "+cr1.getFirstName() +" "+cr1.getLastName());
System.out.println("Creating her account with a " +cr1.getBalance()+" balance");
System.out.println(cr1.withdraw1() );
System.out.println(cr1.deposit1(22.5));
System.out.println(cr1.withdraw1(47.62));
System.out.println(cr1.withdraw1());
System.out.println("Customer "+cr1.getLastName()+" "+cr1.getFirstName()+
" has a balance of "+cr1.getBalance());

修改withdraw 方法的更多相关文章
- 练习3:修改withdraw 方法
练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功. 任务 1.修改Account类 a.修改deposit 方法返回true(意味所有存款是成功的 ...
- 练习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 ...
随机推荐
- Laravel-Action 对代码的改造
前言 以往写过俩篇文章[积德篇] 如何少写PHP "烂"代码 https://segmentfault.com/a/11...举枪消灭"烂代码"的实战案例 ht ...
- Linux - redis哨兵集群实例
目录 Linux - redis哨兵集群实例 命令整理 配置流程 Linux - redis哨兵集群实例 命令整理 官网地址:http://redisdoc.com/ redis-cli info # ...
- vue 底部bottomnav
<template> <div id="foot"> <div class="tabBar"> <div class= ...
- Sencha Touch中 xclass和xtype区别
1.xclass 就是 Ext.create(xclass) 和 xtype一样的性质,不一定非要是自己创建的. 2.xtype是xclass的简称. 3.使用xtype前,你要new的对象,先要re ...
- [NOIP2016day1T1] 玩具迷題(toy)
题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...
- nyoj_91_阶乘之和_201312131321
阶乘之和 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为正数)的阶乘之和,如9 ...
- hdu_1017_A Mathematical Curiosity_201310280948
A Mathematical Curiosity http://acm.hdu.edu.cn/showproblem.php?pid=1017 Time Limit: 2000/1000 MS (Ja ...
- Git 项目上传至github入门实战并解决常见错误
1.Git GUI 首先,在push到github的项目必须先建立版本(即creat repository的名字一样),一般是先pull下来,再push(为了防止有其他人提交了代码,而你却不知道,造 ...
- 一次源码编译PHP折腾记
前言LINUX环境下编译安装是很折腾人的一件事情,如果没有C/C++功底,碰到编译器报错,肯定要抓狂了 :):),有些软件需要依赖其它库,必须先把依赖库安装好才能进行软件安装.当你学会了编译安装神技之 ...
- Tween公式
Tween公式 4个参数 t:current time(当前时间) b:beginning value(初始值) c: change in value(变化量) d:duration(持续时间) re ...