设计并实现一个小型银行系统,使用Java面向对象编程技术

在某个社交平台上刷到了,大学生的一个小练习,关于编写一个小的银行案例,下面具体分享一下代码,希望大佬多来指正,我们互相学习。
设计并实现一个小型银行系统,使用Java面向对象编程技术_第1张图片
基本要求:类设计:Account类: 包含账户ID、余额、账户类型等属性,以及存款、取款、转账方法。Customer类:包含客户信息,如姓名、ID和其Account列表。Bank类: 管理所有Customer,包括添加客户、开设账户、关闭账户、处理交易等。
功能实现:
实现多种账户类型(如储蓄账户、支票账户),它们继承自Account类,但具有不同的服务费(存款和取款都会收取服务费)实现基础的交易处理功能,如转账、存款、取款。
实现交易历史记录功能,记录客户的所有交易。
实现简单的利息计算功能,定期更新账户余额。
添加数据持久化功能,如将客户和账户信息保存到文件。
数据存储:
使用集合类(如ArrayList)存储系统中的客户和账户信息
异常处理:
添加适当的异常处理,例如在处理交易时检查余额不足或输入错误。测试类:
创建一个测试类,演示银行系统的各种功能。

首先看一下目录结构

设计并实现一个小型银行系统,使用Java面向对象编程技术_第2张图片
Account.java

package com.account;

/**
 * @program: BnakDemo
 * @description:
 * @author: Casey Hu
 * @create: 2023-11-26 13:58
 **/

public class Account {
    //账户ID
    String accountId = null;

    //账户余额(单位 元 )
    Double accountBalance = 0.00;

    //账户类型属性
    String accountType = null;

    //账户状态 true是打开,false是关闭
    boolean accountStatu = false;

    public Account(String accountId,String accountType) {
        this.accountId=accountId;
        this.accountType=accountType;
        this.accountStatu=true;
    }
    //存款
    public Double deposit(Double money){
        if(money>0){
            accountBalance=accountBalance+money;
        }
        return accountBalance;
    }

    //取款
    public Double withdraw(Double money){
        if(money>0){
            if(money<accountBalance){
                accountBalance=accountBalance-money;
            }else {
                new Exception("取款金额不得超过当前账户余额!");
            }
        }
        return accountBalance;
    }

    //转账
    public void transfer(Account account, Double money){
        Double balance = account.getAccountBalance();
        if(money>0){
            if(money<accountBalance){
                balance=balance +money;
                accountBalance=accountBalance-money;
            }else {
                new Exception("转账金额不得超过当前账户余额!");
            }
        }
        account.setAccountBalance(balance);
        System.out.println("向账户"+account.getAccountId()+"转账"+money+" 成功!");
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public Double getAccountBalance() {
        return accountBalance;
    }

    public void setAccountBalance(Double accountBalance) {
        this.accountBalance = accountBalance;
    }

    public String getAccountType() {
        return accountType;
    }

    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }

    public void settlementInterest() {
    }
}

saveAccount.java

package com.account;

/**
 * @program: BnakDemo
 * @description: 储蓄账户
 * @author: Casey Hu
 * @create: 2023-11-26 14:17
 **/

public class saveAccount extends Account {

    Double commission=0.001;

    public saveAccount(String saveAccountId) {
        super(saveAccountId, "save");
    }

    //重写存款方法
    @Override
    public Double deposit(Double money){
        if(money>0){
            money=money-money*commission; //扣除手续费
        }else {
            money=0.00; //金额异常 不进行操作
        }
        return super.deposit(money);
    }
    //重写取款方法
    @Override
    public Double withdraw(Double money) {
        if(money>0&&accountBalance>0){
            if (accountBalance-(money*commission)>0) {
                money=money+money*commission; //扣除手续费
            }else {
                money=0.00; //金额异常 不进行操作
            }
        }else {
            money=0.00; //金额异常 不进行操作
        }
        return super.withdraw(money);
    }
    //重写转账方法
    @Override
    public void transfer(Account account, Double money) {
        super.transfer(account, money);
    }

    //结算利息 按年利率算的
    public void settlementInterest(){
        accountBalance=accountBalance+accountBalance*0.011;
    }
}

checkAccount.java

package com.account;

/**
 * @program: BnakDemo
 * @description: 支票账户
 * @author: Casey Hu
 * @create: 2023-11-26 14:37
 **/

public class checkAccount extends Account{
    Double commission=0.002;

    public checkAccount(String accountId) {
        super(accountId, "check");
    }
    //重写存款方法
    @Override
    public Double deposit(Double money){
        if(money>0){
            money=money-money*commission; //扣除手续费
        }else {
            money=0.00; //金额异常 不进行操作
        }
        return super.deposit(money);
    }
    //重写取款方法
    @Override
    public Double withdraw(Double money) {
        if(money>0&&accountBalance>0){
            if (accountBalance-(money*commission)>0) {
                money=money+money*commission; //扣除手续费
            }else {
                money=0.00; //金额异常 不进行操作
            }
        }else {
            money=0.00; //金额异常 不进行操作
        }
        return super.withdraw(money);
    }
    //重写转账方法
    @Override
    public void transfer(Account account, Double money) {
        super.transfer(account, money);
    }
    //结算利息 按年利率算的
    public void settlementInterest(){
        accountBalance=accountBalance+accountBalance*0.021;
    }
}

Customer.java

package com.customer;

import com.account.Account;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: BnakDemo
 * @description: 客户
 * @author: Casey Hu
 * @create: 2023-11-26 14:39
 **/

public class Customer {

    //客户ID
    private String customerId;

    //客户姓名
    private String customerName;

    //账户列表
    private List<Account> accountList=new ArrayList<>();

    //交易历史
    private List<String> tranHistory=new ArrayList<>();

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public List<Account> getAccountList() {
        return accountList;
    }

    public void setAccountList(List<Account> accountList) {
        this.accountList = accountList;
    }

    public List<String> getTranHistory() {
        return tranHistory;
    }

    public void setTranHistory(List<String> tranHistory) {
        this.tranHistory = tranHistory;
    }
}

Bank.java

package com.bank;


import com.account.checkAccount;
import com.account.saveAccount;
import com.customer.Customer;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @program: BnakDemo
 * @description: 银行
 * @author: Casey Hu
 * @create: 2023-11-26 15:01
 **/

public class Bank {
    private List<Customer> customer;

    public void addCustomer(Customer customer){
        this.customer.add(customer);
    }

    public void addAccount(Customer customer, String accountType){
        String accountId=getAccountId();
        if("check".equals(accountType)){
            customer.getAccountList().add(new checkAccount(accountId)); //如果开账户为 支票
        }else{
            customer.getAccountList().add(new saveAccount(accountId));
        }
    }

    public String getAccountId(){
        Random random = new Random();
        String number = "";
        for (int i = 0; i < 10; i++) {
            number += random.nextInt(10);
        }
        return number;
    }

    //数据持久化功能
    public void makeNoRealFile(Customer customer) {
        try {
            Date date = new Date(); // 获取当前日期
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); // 定义日期格式
            String formattedDate = formatter.format(date); // 将日期格式化为指定格式
            String filename = "bank_" +formattedDate+ ".txt";
            String filePath = "E:\\BnakDemo\\BnakDemo\\src\\main\\resources\\" + filename; // 资源文件路径
            StringBuffer result= new StringBuffer();

            result.append(customer.getCustomerName()+"^");
            for (int i=0;i<customer.getAccountList().size();i++){
                result.append(customer.getAccountList().get(i).getAccountId()+"|");
                result.append(customer.getAccountList().get(i).getAccountType()+"|");
                result.append(customer.getAccountList().get(i).getAccountBalance()+"|");
            }
            result.append(customer.getCustomerId()+"^");
            for (int j=0;j<customer.getTranHistory().size();j++){
                result.append(customer.getTranHistory().get(j)+"|");
            }
            File file = new File(filePath);
            file.createNewFile();
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            BufferedWriter bw = new BufferedWriter(write);
            bw.write(result.toString());
            bw.close();
            System.out.println("文件内容:"+result);
            System.out.println("文件保存成功!");
        }catch (Exception e){
         e.printStackTrace();
        }
    }
}

BankTest.java

import com.account.Account;
import com.bank.Bank;
import com.customer.Customer;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: BnakDemo
 * @description:
 * @author: Casey Hu
 * @create: 2023-11-26 16:27
 **/
public class BankTest {
    public static void main(String[] args) {
        List<String> tranHistory=new ArrayList<>();
        //第一步先 创建一个客户对象
        Customer customer = new Customer();
        customer.setCustomerId("110101198001010053"); //给客户id
        customer.setCustomerName("Casey Hu");
        //第二步 创建一个银行对象
        Bank bank=new Bank();
        bank.addAccount(customer,"save");//这个用户创建了一个储蓄账户
        Account account1 = customer.getAccountList().get(0);
        //第二步 开始存钱
        account1.deposit(10000.00); //存入1万元
        System.out.println(account1.getAccountBalance());
        tranHistory.add("存入10000.00");
        //第三步 开始取钱
        account1.withdraw(5000.00);//取出5千元
        System.out.println(account1.getAccountBalance());//还剩多少钱
        tranHistory.add("取出5000.00");
        //第四步 转账
        bank.addAccount(customer,"check");//这个用户创建了一个储蓄账户
        Account account2 = customer.getAccountList().get(1);
        account1.transfer(account2,3000.00);
        System.out.println("账户"+account1.getAccountId()+"余额:"+account1.getAccountBalance());
        System.out.println("账户"+account2.getAccountId()+"余额:"+account2.getAccountBalance());
        tranHistory.add("账户"+account1.getAccountId()+"向账户"+account2.getAccountId()+"转账3000.00");
        //第五步 结算利息
        account1.settlementInterest();
        account2.settlementInterest();
        System.out.println("账户"+account1.getAccountId()+"余额:"+account1.getAccountBalance());
        System.out.println("账户"+account2.getAccountId()+"余额:"+account2.getAccountBalance());
        tranHistory.add("账户"+account1.getAccountId()+"结算利息");
        tranHistory.add("账户"+account2.getAccountId()+"结算利息");
        customer.setTranHistory(tranHistory);
        //数据持久化
        bank.makeNoRealFile(customer);

    }
}

要源码的私信我,或者加我微信,打包发你。其实以上复制下来就是源码了

祝您万事顺心,没事点个赞呗,关注一下也行啊,有啥要求您评论哈

你可能感兴趣的:(java,开发语言)