zoukankan      html  css  js  c++  java
  • 修改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

  • 相关阅读:
    【凡尘】---react-redux---【react】
    React生命周期详解
    写文章很难,ai自动生成文章为你来排忧
    怎么用ai智能写作【智媒ai伪原创】快速写文章?
    给大家介绍个Seo伪原创工具吧,可以免费用的哈
    自媒体文章难写,在线伪原创文章生成就简单了
    内容创作难吗 不妨试试智媒ai伪原创
    Ai伪原创工具,轻松几秒出爆文
    什么AI写作软件靠谱,好用?
    分享个免费伪原创工具 关键词自动生成文章
  • 原文地址:https://www.cnblogs.com/smile-dream/p/5915467.html
Copyright © 2011-2022 走看看