银行业务管理软件(6)

这篇博客介绍了如何在Java银行管理软件中应用单子设计模式。实验目标是修改Bank类以包含静态getBanking方法,返回Bank类的唯一实例。Bank构造器设为私有,同时在CustomerReport类中修改代码以使用单子银行对象。通过这些修改,TestBanking应用程序将显示客户报告,列出各个客户的账户余额。

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

实验题目 6:(在5_续1的基础上修改)
在这里插入图片描述

修改 Bank 类来实现单子设计模式:
实验目的:
单子模式。
提示: 1. 修改 Bank 类,创建名为 getBanking 的公有静态方法,它返回一个 Bank 类的实例。
2. 单个的实例应是静态属性,且为私有。同样,Bank 构造器也应该是私有的创建 CustomerReport 类
1.在前面的银行项目练习中,“客户报告”嵌入在 TestBanking 应用程序的main 方法中。在这个练习中,改代码被放在 ,banking.reports 包的
CustomerReport 类中。您的任务是修改这个类,使其使用单一银行对象。
2. 查找标注为注释块/*** ***/的代码行.修改该行以检索单子银行对象。编译并运行 TestBanking 应用程序看到下列输入结果:
CUSTOMER REPORT

Customer:simms,jane
Savings Account:current balance is
$500.00 Checking Account:current
balance is $200.00
Customer:Bryant,owen
尚硅谷 Java 基础实战—Bank 项目
Checking Account:current balance is $200.00
Customer: Soley,Tim
Savings Account:current balance is $1,500.00
Checking Account:current balance is $200.00
Customer:Soley ,Maria
Checking Account:current balance is $200.00
Savings Account:current balance is $150.00

Account.java

package banking;

public class Account {
   
    protected 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){
   
            balance += amt ;
            return true ;
        }else {
   
            System.out.println("请输入正确的存款数");
            return false ;
        }

    }

    public boolean withdraw(double amt){
   
        if (balance >= amt){
   
            balance -= amt ;
            return true ;
        } else {
   
            System.out.println("余额不足!");
            return false ;
        }
    }
}

Bank.java

package banking;

public class Bank {
   
    private Customer[] customers ;
    private int numberOfCustomer ;

    private Bank(){
   
        customers = new Customer[5] ;
        numberOfCustomer = 0 ;
    }
    private static Bank bank = new Bank() ;

    public static 
### 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、付费专栏及课程。

余额充值