银行业务管理软件 (3)

该实验旨在修改银行账户类的deposit和withdraw方法,使deposit返回成功标志,并确保withdraw不会导致负余额。当withdraw金额大于余额时返回false,否则扣除金额并返回true。在TestBanking程序中,展示了不同交易操作及其结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验题目 3:
修改 withdraw 方法以返回一个布尔值,指示交易是否成功。
实验目的:
使用有返回值的方法。
在这里插入图片描述

提 示:

  1. 修改 Account 类
    a. 修改 deposit 方法返回 true(意味所有存款是成功的)。
    b. 修改 withdraw 方法来检查提款数目是否大于余额。如果amt小于balance, 则从余额中扣除提款数目并返回 true,否则余额不变返回false。
    2.在 exercise3 主目录编译并运行 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.java

package banking;

public class Account {
   
    private double balance ;

    public Account(){
   

    }

    public Account(double init_balance){
   
        balance = init_balance ;
    }

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

    public double getBalance() {
   
        return balance;
    }

    public boolean deposit(double amt){
   
        if (amt > 0){
   
            
### C++ 开发银行业务管理软件 #### 使用C++/CLI实现银行系统的部分功能 为了满足银行储蓄业务管理系统的需求[^3],可以利用C++/CLI来创建一个能够处理托管和非托管代码的混合应用程序。这使得系统不仅能在性能上保持高效,在与其他.NET组件交互时也更加灵活。 下面是一个简单的例子,展示如何定义并初始化`BankAccount`类: ```cpp // BankAccount.h #pragma once using namespace System; public ref class BankAccount { private: int accountNumber; double balance; public: // 默认构造函数 BankAccount() : accountNumber(0), balance(0.0) {} // 带参数的构造函数 BankAccount(int num, double initBalance); void Deposit(double amount); bool Withdraw(double amount); String^ GetInfo(); }; ``` ```cpp // BankAccount.cpp #include "BankAccount.h" BankAccount::BankAccount(int num, double initBalance) { this->accountNumber = num; if (initBalance >= 0) this->balance = initBalance; } void BankAccount::Deposit(double amount) { if (amount > 0) this->balance += amount; } bool BankAccount::Withdraw(double amount) { if ((this->balance -= amount) >= 0) return true; else { this->balance += amount; // 撤销非法取款尝试 return false; } } String^ BankAccount::GetInfo() { return String::Format("账号:{0},余额:${1}", this->accountNumber.ToString(), this->balance.ToString()); } ``` 此示例展示了基本账户操作的功能,如存款、取款以及获取账户详情。对于更复杂的场景,则可能涉及到更多特性,比如异常处理机制、线程安全措施等。 考虑到实际应用中的安全性考量,建议采用现代C++标准库提供的智能指针和其他资源管理工具以防止内存泄漏等问题的发生。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值