练习目标-使用有返回值的方法:在本练习里,将修改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. Symfony命令大全

    执行命令: $ php bin/console 查看一下命令 Symfony version 3.1.5 - app/dev/debug Usage: command [options] [argum ...

  2. 转:iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解

    1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS开发者基 ...

  3. [河南省ACM省赛-第四届] 表达式求值(nyoj 305)

    栈的模拟应用: #include<iostream> #include<cstdio> #include<cstring> #include<string&g ...

  4. Python调用(运行)外部程序

    在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...

  5. CharSequence 接口

    java中有些方法需要用到CharSequence 类型的参数,笔者百度了一下,总结出一下几点: 1.CharSequence 是一个接口,可以直接用“=”赋值一段字符串,但是不能用new新建一个对象 ...

  6. HTML5本地存储详解

    HTML5storage提供了一种方式让网站能够把信息存储到你本地的计算机上,并再以后需要的时候进行获取.这个概念和cookie相似,区别是它是为了更大容量存储设计的.Cookie的大小是受限的,并且 ...

  7. WCF发布错误及解决方案

    一:在本机直接运行时出错 使用WCF写了一个小程序测试一下它的功能在运行时报错.“添加服务失败.服务元数据可能无法访问.请确保服务正在运行并且正在公开元数据.” 如下图所示: 查了下资料把它解决了,记 ...

  8. Java Day03 面向对象程序设计

    1.面向对象 面向对象是指一种程序设计泛型,同时也是一种程序开发的方法. 2.类 类是一种抽象的概念,类中包含了数据与对数据的操纵. 具有相同特性(数据元素)和行为(功能)的对象的抽象就是类.类是对象 ...

  9. 小谈数据库Char、VarChar、NVarChar差异

    1. char      固定长度,最长n个字符.   2. varchar      最大长度为n的可变字符串. (n为某一整数,不同数据库,最大长度n不同)   char和varchar区别:   ...

  10. 用VB把xls转换为xlsx

    Sub xls批量转换成xlsx()Application.ScreenUpdating = FalseMsgBox "现在开始转换,请稍候!"mypath = ThisWorkb ...