简易的银行账户管理系统

系统功能:

  1. 设计一个银行账户管理系统,具体功能为:

(1)开户

开户时需要储户输入个人信息,包括姓名,开户金额,完成后储户信息被保存到一个储户基本信息文件中,并反馈给储户开户后的账号,初始密码,开户金额,开户日期。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,密码。

开会类型分为:普通客户,企业客户,VIP客户

(2)存款

存款时储户提供事先获得的账号及存储的金额,将金额加到账户原有金额中,然后返回本次操作的信息包括存储的金额,账号中现有金额,操作时间。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,密码。

(3)取款

取款时储户提供事先获得的账号及要提取的金额,将账号中原有的金额减去要提取的金额,将现金交给储户,然后返回本次操作的信息包括提取的金额,账号中现有的金额,操作时间。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,密码

能够实现外币与人民币的兑换取款。

(4)转账

转账时储户提供事先获得转出账号,转出账号密码,转入账号,转账金额,将转出账号中的金额减去转账金额,转入账号中的金额加上转账金额,然后返回本次操作的信息包括转账的金额。

转出账号现有的金额,本次操作的时间。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,密码。

能区分是本行转账还是跨行转账,跨行的需定义多个银行。

(5)查询

查询时储户提供账号,密码。返回查询信息包括账户的的信息和该账户的历史操作。

(6)修改密码

修改密码时储户提供账号及密码,根据提示输入新密码两次,完成后密码修改成功。同时生成一个操作记录包括账号,操作类型,操作金额,本次操作的具体时间,旧密码,新密码。

(7)销户

销户时储户提供账号及密码,将账号中的金额全部取出,然后将本账号的信息从文件中删除,返回销户成功。

 

类的定义

  1. Customer 类

    • 属性:
      • name: 客户姓名。
      • accountNumber: 账户号码。
      • password: 账户密码。
      • balance: 账户余额。
      • createDate: 账户创建日期。
      • type: 客户类型(普通客户、企业客户、VIP客户)。
    • 构造函数: 用于初始化客户对象。
  2. TransactionRecord 类

    • 属性:
      • accountNumber: 账户号码。
      • operationType: 操作类型(如开户、存款、取款、转账)。
      • amount: 操作金额。
      • operationTime: 操作时间。
      • password: 账户密码(用于存款和取款)。
      • newPassword: 新密码(用于修改密码时)。
    • 构造函数: 有两个构造函数,一个用于一般操作记录,另一个用于修改密码的记录。
    • toString 方法: 根据是否有新密码返回不同的字符串表示。
  3. Bank 类

    • 属性:
      • scanner: 用于接收用户输入的扫描器。
      • customers: 存储客户信息的映射表(账户号码 -> 客户对象)。
      • transactionRecords: 存储交易记录的列表。
    • 构造函数: 初始化客户映射表和交易记录列表。

功能函数

  1. 开设账户 (openAccount):

    • 根据初始余额判断客户类型(普通客户、企业客户、VIP客户)。
    • 创建客户对象并将其存储在 customers 中。
    • 记录开户操作。
  2. 存款 (deposit):

    • 获取客户信息,增加余额,并记录存款操作。
  3. 取款 (withdraw):

    • 根据货币类型处理取款逻辑(人民币、美元、英镑)。
    • 计算新余额并记录取款操作。
  4. 转账 (transfer):

    • 校验转账账户的有效性和密码。
    • 根据是否是本行转账或跨行转账,调用不同的转账方法。
    • 记录转账操作。
  5. 内部转账 (internalTransfer):

    • 处理本行客户之间的转账逻辑。
  6. 外部转账 (externalTransfer):

    • 处理跨行转账逻辑,并扣除手续费。
  7. 查询 (inquire):

    • 验证密码后,打印客户的账户信息及其所有交易记录。

     8. 修改密码 (changePassword )

              1.修改对应账的密码

     9.销户 (closeAccount )

             1.  注销客户的账户,并可选择将余额提取或转账。

 

类的定义

Customer类

import java.util.*;
 class Customer
{
    String name;
    String accountNumber;//账户
    String password;//密码
    double balance;//余额
    Date createDate;//操作日期
    String type;//操作类型
    // 构造函数
    Customer(String name, String accountNumber, String password, double balance, Date createDate, String type) {
        this.name = name;
        this.accountNumber = accountNumber;
        this.password = password;
        this.balance = balance;
        this.createDate = createDate;
        this.type = type;
    }
}

 TransactionRecord类

class TransactionRecord
 {
    String accountNumber;//账户号码
    String operationType;//操作类型(如开户、存款、取款、转账)
    double amount;//操作金额
    Date operationTime;//操作时间
    String password;//初始密码
    String newPassword;//修改后的密码
    String  currencyType;//货币类型
    // 构造函数
    TransactionRecord(String accountNumber, String operationType, double amount, Date operationTime, String password)
    {
        this.accountNumber = accountNumber;
        this.operationType = operationType;
        this.amount = amount;
        this.operationTime = operationTime;
        this.password = password;
    }
    // 修改密码时的构造函数
    TransactionRecord(String accountNumber, String operationType, double amount, Date operationTime, String oldPassword, String newPassword) 
{
        this.accountNumber = accountNumber;
        this.operationType = operationType;
        this.amount = amount;
        this.operationTime = operationTime;
        this.password = oldPassword;
        this.newPassword = newPassword;
    }
    @Override
    public String toString()
    {
        if (newPassword != null)
        {
            return "账号:" + accountNumber + " 操作类型:" + operationType + " 操作金额:" + amount + " 操作时间:" + operationTime + " 旧密码:" + password + " 新密码:" + newPassword;
        } 
else 
{
            return "账号:" + accountNumber + " 操作类型:" + operationType + " 操作金额:" + amount + " 操作时间:" + operationTime + " 密码:" + password;
        }
    }
}

 Bank类

class Bank
 {
    Scanner scanner = new Scanner(System.in);
    Map<String, Customer> customers; // 储户信息
    List<TransactionRecord> transactionRecords; // 操作记录
    // 构造函数
    Bank()
    {
        customers = new HashMap<>();
        transactionRecords = new ArrayList<>();
    }

 函数功能:

开户

// 开户功能
    void openAccount(String name, String accountNumber, String password, double initialBalance, Date createDate)
    {
        // 检查账户号码是否已经存在
        if (customers.containsKey(accountNumber)) {
            System.out.println("错误:账户号码 " + accountNumber + " 已经被注册,请选择其他账户号码。");
            return; // 结束开户流程
        }
        String type = "普通客户";
        if (initialBalance > 10000 && initialBalance <= 50000)
        {
            type = "企业客户";
        }
        else if (initialBalance > 50000 )
        {
            type = "VIP客户";
        }
        Customer customer = new Customer(name, accountNumber, password, initialBalance, createDate, type);
        customers.put(accountNumber, customer);
        // 生成开户操作记录
        transactionRecords.add(new TransactionRecord(accountNumber, "开户", initialBalance, new Date(), password));
        System.out.println("开户成功!账号:" + accountNumber + " 初始密码:" + password + " 开户金额:" + initialBalance + " 开户日期:" + createDate + " 客户类型:" + type);
    }

存款

  void deposit(String accountNumber, double depositAmount)
    {
        Customer customer = customers.get(accountNumber);
        double newBalance = customer.balance + depositAmount;
        customer.balance = newBalance;
        // 生成存款操作记录
        transactionRecords.add(new TransactionRecord(accountNumber, "存款", depositAmount, new Date(), customer.password));
        System.out.println("存款成功!存款金额:" + depositAmount + " 账号余额:" + newBalance + " 操作时间:" + new Date());
    }

取款

 void withdraw(String accountNumber, double withdrawAmount, String currencyType) {
        Customer customer = customers.get(accountNumber);
        double newBalance;
        // 根据选择的货币类型进行外币兑换
        switch(currencyType) {
            case "人民币":
                // 处理人民币取款逻辑
                newBalance = customer.balance - withdrawAmount;
                customer.balance = newBalance;
                // 生成交易记录
                TransactionRecord transactionRecord = new TransactionRecord(accountNumber, "取款", withdrawAmount, new Date(), customer.password, "");
                // 添加交易记录到银行的操作记录列表
                this.transactionRecords.add(transactionRecord);
                break;
            case "美元":
                // 处理美元取款逻辑(根据汇率进行兑换)
                double exchangeRateUSD = 6.5; // 假设人民币兑换美元的汇率为6.5
                double withdrawAmountUSD = withdrawAmount / exchangeRateUSD;
                newBalance = customer.balance - withdrawAmountUSD;
                customer.balance = newBalance;
                // 生成交易记录
                TransactionRecord transactionRecordUSD = new TransactionRecord(accountNumber, "取款", withdrawAmountUSD, new Date(), customer.password, "");
                // 添加交易记录到银行的操作记录列表
                this.transactionRecords.add(transactionRecordUSD);
                break;
            case "英镑":
                // 处理英镑取款逻辑(根据汇率进行兑换)
                double exchangeRateGBP = 8.5; // 假设人民币兑换英镑的汇率为8.5
                double withdrawAmountGBP = withdrawAmount / exchangeRateGBP;
                newBalance = customer.balance - withdrawAmountGBP;
                customer.balance = newBalance;
                // 生成交易记录
                TransactionRecord transactionRecordGBP = new TransactionRecord(accountNumber, "取款", withdrawAmountGBP, new Date(), customer.password, "");
                // 添加交易记录到银行的操作记录列表
                this.transactionRecords.add(transactionRecordGBP);
                break;
        }
    }

转账

//转账
    void transfer(String fromAccount, String toAccount, double transferAmount, String transferPassword)
    {
        Customer fromCustomer = customers.get(fromAccount);
        Customer toCustomer = customers.get(toAccount);
        if (fromCustomer == null || toCustomer == null)
        {
            System.out.println("无效的账号,转账失败!");
            return;
        }
        if (!fromCustomer.password.equals(transferPassword))
        {
            System.out.println("密码错误,无法完成转账操作。");
            return;
        }
        if (fromCustomer.balance < transferAmount)
        {
            System.out.println("转出账号余额不足,无法完成转账操作。");
            return;
        }
        // 判断是本行转账还是跨行转账
        if (fromAccount.charAt(0) == '0' && toAccount.charAt(0) == '0')
        {
            // 本行转账逻辑
            System.out.println("本行转账开始...");
            internalTransfer(fromAccount, toAccount, transferAmount);
            System.out.println("本行转账成功!转出账号:" + fromAccount + " 转入账号:" + toAccount + " 转账金额:" + transferAmount);
        }
        else
        {
            // 跨行转账逻辑
            externalTransfer(fromAccount, toAccount, transferAmount);
            System.out.println("跨行转账成功!转出账号:" + fromAccount + " 转入账号:" + toAccount + " 转账金额:" + transferAmount+"手续费:"+transferAmount*0.0001);
        }
    }
    void internalTransfer(String fromAccount, String toAccount, double transferAmount) {
        Customer fromCustomer = customers.get(fromAccount);
        Customer toCustomer = customers.get(toAccount);
        fromCustomer.balance -= transferAmount;
        toCustomer.balance += transferAmount;
        // 生成转账操作记录
        transactionRecords.add(new TransactionRecord(fromAccount, "本行转账", transferAmount, new Date(), toAccount));
    }

    void externalTransfer(String fromAccount, String toAccount, double transferAmount) {
        Customer fromCustomer = customers.get(fromAccount);
        Customer toCustomer = customers.get(toAccount);
        // 跨行转账可能涉及手续费等逻辑
        fromCustomer.balance -= transferAmount;
        fromCustomer.balance-=transferAmount*0.0001;//扣除手续费
        toCustomer.balance += transferAmount;
        // 生成转账操作记录
        transactionRecords.add(new TransactionRecord(fromAccount, "跨行转账", transferAmount, new Date(), toAccount));
    }

查询

void inquire(String accountNumber, String password) //查询功能
    {
        Customer customer = customers.get(accountNumber);
        if (customer != null && customer.password.equals(password)) {
            String customerType = customer.type;
            // 返回账户信息
            System.out.println("账户信息:姓名" + customer.name + " " + "账户账号" + customer.accountNumber + " " + "账户余额" + customer.balance + " 客户类型:" + customerType);
            // 打印特定账户的操作记录
            for (TransactionRecord record : transactionRecords) {
                if (record.accountNumber.equals(accountNumber)) {
                    System.out.println(record);
                }
            }
        } else {
            System.out.println("密码错误,无法查看信息!");
        }
    }

修改密码

void changePassword(String accountNumber, String oldPassword, String newPassword)    // 修改密码
    {
        Customer customer = customers.get(accountNumber);
        if (customer == null || !customer.password.equals(oldPassword)) {
            System.out.println("密码错误,无法修改密码!");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请再次输入新密码:");
        String confirmNewPassword = scanner.next();
        if (!newPassword.equals(confirmNewPassword))
        {
            System.out.println("两次输入的新密码不一致,请重新修改密码!");
            return;
        }
        customer.password = newPassword;
        // 生成修改密码操作记录
        transactionRecords.add(new TransactionRecord(accountNumber, "修改密码", 0, new Date(), oldPassword, newPassword));
        System.out.println("密码修改成功!账号:" + accountNumber + " 新密码:" + newPassword + " 操作时间:" + new Date());
    }

 注销账户

void closeAccount(String accountNumber, String password)//注销账户
    {
        Customer customer = customers.get(accountNumber);
        double balance = customer.balance;
        // 返回账户余额
        System.out.println("账户余额:" + balance);
        System.out.println("余额取出还是转账:");
        System.out.println("1取出,2转账");
        int choice_1=scanner.nextInt();
        switch(choice_1)
        {
            case 1:
                System.out.println("余额已全部取出,销户成功");
                System.out.println("期待您的下次使用!!!");
                break;
            case 2:
                System.out.println("请输入转账账号");
                String toAccount=scanner.next();
                Customer fromCustomer = customers.get(accountNumber);
                Customer toCustomer = customers.get(toAccount);
                if (accountNumber.charAt(0) == '0' && toAccount.charAt(0) == '0')
                {
                    // 本行转账
                    System.out.println("本行转账开始...");
                    internalTransfer(accountNumber, toAccount, balance);
                    System.out.println("本行转账成功!转出账号:" + accountNumber + " 转入账号:" + toAccount + " 转账金额:" + balance);
                }
                else
                {
                    // 跨行转账
                    fromCustomer.balance -= balance;
                    toCustomer.balance += balance;
                    System.out.println("跨行转账成功!转出账号:" + accountNumber + " 转入账号:" + toAccount + " 转账金额:" + balance);
                }
                break;
        }
        customers.remove(accountNumber);
        // 删除操作记录
        transactionRecords.removeIf(record -> record.accountNumber.equals(accountNumber));
    }
}

 主函数

public class Main
{
    public static void main(String[] args)
    {
        Bank bank = new Bank();
        Scanner scanner = new Scanner(System.in);
        while (true)
        {
            System.out.println("欢迎使用银行账户管理系统,请选择操作:");
            System.out.println("1. 开户");
            System.out.println("2. 存款");
            System.out.println("3. 取款");
            System.out.println("4. 转账");
            System.out.println("5. 查询");
            System.out.println("6. 修改密码");
            System.out.println("7. 销户");
            System.out.println("8. 退出");
            int choice = scanner.nextInt();
            switch (choice)
            {
                case 1:
                    System.out.println("请输入姓名:");
                    String name = scanner.next();
                    System.out.println("请输入账号:");
                    String accountNumber = scanner.next();
                    System.out.println("请输入密码:");
                    String password = scanner.next();
                    System.out.println("请输入初始存款金额:");
                    double initialBalance = scanner.nextDouble();
                    Date createDate = new Date();
                    bank.openAccount(name, accountNumber, password, initialBalance, createDate);
                    break;
                case 2:
                    System.out.println("请输入存款账号:");
                    String depositAccount = scanner.next();
                    System.out.println("请输入存款金额:");
                    double depositAmount = scanner.nextDouble();
                    bank.deposit(depositAccount, depositAmount);
                    break;
                case 3:
                    System.out.println("请输入账号:");
                    String accountNumbers = scanner.next();
                    System.out.println("请输入取款金额:");
                    double withdrawAmount = scanner.nextDouble();
                    System.out.println("请选择货币类型(人民币/美元/英镑):");
                    String currencyType = scanner.next();
                    bank.withdraw(accountNumbers, withdrawAmount, currencyType);
                    // 打印取款后的账户余额
                    Customer customer3 = bank.customers.get(accountNumbers);
                    System.out.println("账户余额:" + customer3.balance);
                    break;
                case 4:
                    System.out.println("请输入转出账号:");
                    String fromAccount = scanner.next();
                    System.out.println("请输入转入账号:");
                    String toAccount = scanner.next();
                    System.out.println("请输入转账金额:");
                    double transferAmount = scanner.nextDouble();
                    System.out.println("请输入转出账号密码:");
                    String transferPassword = scanner.next();
                    bank.transfer(fromAccount, toAccount, transferAmount, transferPassword);
                    break;
                case 5:
                    System.out.println("请输入查询账号:");
                    String inquireAccount = scanner.next();
                    System.out.println("请输入密码:");
                    String inquirePassword = scanner.next();
                    bank.inquire(inquireAccount, inquirePassword);
                    break;
                case 6:
                    System.out.println("请输入账号:");
                    String changePwdAccount = scanner.next();
                    System.out.println("请输入旧密码:");
                    String oldPassword = scanner.next();
                    System.out.println("请输入新密码:");
                    String newPassword = scanner.next();
                    bank.changePassword(changePwdAccount, oldPassword, newPassword);
                    break;

                case 7:
                    System.out.println("=======欢迎您进入银行销户界面========");
                    System.out.println("您要注销当前的账户吗? 请输入:Yes或者No!");
                    String select = scanner.next();
                    if (select.equalsIgnoreCase("Yes")) {
                        System.out.println("请输入销户账号:");
                        String closeAccount = scanner.next();
                        System.out.println("请输入密码:");
                        String closePassword = scanner.next();

                        // 验证账号和密码
                        if (bank.customers.containsKey(closeAccount)) {
                            Customer customer = bank.customers.get(closeAccount);
                            if (customer.password.equals(closePassword)) {
                                // 处理账户余额
                                double balance = customer.balance;
                                System.out.println("账号:" + closeAccount + " 的余额为:" + balance);
                                System.out.println("确认销户请输入:Confirm");
                                String confirm = scanner.next();
                                if (confirm.equalsIgnoreCase("Confirm"))
                                {
                                    bank.closeAccount(closeAccount, closePassword);
                                    System.out.println("账户已成功销户!");
                                } else
                                {
                                    System.out.println("取消销户操作!");
                                }
                            }
                            else
                            {
                                System.out.println("密码错误!销户操作取消!");
                            }
                        } else
                        {
                            System.out.println("账号不存在!销户操作取消!");
                        }
                    } else
                    {
                        System.out.println("取消销户操作!");
                    }
                    break;

                case 8:
                    System.out.println("感谢使用银行账户管理系统,再见!");
                    System.exit(0);
                default:
                    System.out.println("无效选择,请重新输入!");
                    break;
            }
        }
    }
}

PS:一个计科萌新,还望大佬们多多指教🤞🤞

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值