模拟客户在银行存取款

/*
写一个名为:Account的类模拟账户
    属性有:账户id,余额balance,年利率annualInterestRate
    方法有:各个属性的get和set方法。取款方法:withdraw(),存款方法deposit().
写一个测试程序
    创建一个Customer,名字叫Jane Smith,他有一个账号为1000,余额为2000,年利率为1.23%的账户
    对Jane Smith进行操作:
        存入100;再取出960,再取出2000
        打印信息为:
           成功存入:100
           成功取出:960
           余额不足,取钱失败!
 */
class Account{
    private String id;
    private double balance;
    private double annualInterestRate;

//    无参构造方法

    public Account() {
    }

//    有参构造方法

    public Account(String id, double balance, double annualInterestRate) {
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

//    setter and getter

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }
//    取款方法
    public void withdraw(double money){
        if (money <= 0){
            System.out.println("取款金额不正确!");
            return;
        }
        if (getBalance() < money){
            System.out.println("余额不足,取钱失败!");
            return;
        }
        this.setBalance(this.getBalance() - money);
        System.out.println("成功取出:" + money);
    }
//    存款方法
    public void deposit(double money){
        if (money <= 0){
            System.out.println("取款金额不正确!");
            return;
        }
        balance += money;
        System.out.println("成功存入:" + money);
    }
}
//客户类
class Customer{
//    客户姓名
    private String name;
//    客户手中银行账户
    private Account account;

//    无参构造方法
    public Customer() {
    }

//    有参构造方法
    public Customer(String name, Account account) {
        this.name = name;
        this.account = account;
    }

//    setter and getter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}
class CustomerText{
    public static void main(String[] args) {
//        创建账户对象
        Account account = new Account("1000",2000,0.0123);
//        创建客户对象,将account传给客户对象,表明这个账户是客户的
        Customer customer = new Customer("Jane Smith",account);
//        以下是对Jane Smith 账户的操作
//        存入100
        customer.getAccount().deposit(100);
//        取出960
        customer.getAccount().withdraw(960);
//        再取出2000
        customer.getAccount().withdraw(2000);
        System.out.println("顾客:" + customer.getName() + "有一个账户,账号是:" + customer.getAccount().getId() + "账号年利率为:" + customer.getAccount().getAnnualInterestRate() + "账户余额是:" + customer.getAccount().getBalance());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值