Java简单实现控制台银行实例

说起到课堂作业,用java控制输出整个银行系统的相关功能,具体要求如下:(拿代码一定要稍作修改!!!!!!!)

拿代码!!!点赞!!!收藏!!!关注!!!

大学牲不容易,兄弟们!

基本要求:
类设计:

Account类:包含账户ID、余额、账户类型等属性,以及存款、取款、转账方法。

Customer类:包含客户信息,如姓名、ID和其Account列表。

Bank类:管理所有Customer,包括添加客户、开设账户、关闭账户、处理交易等。

功能实现:

实现多种账户类型(如储蓄账户、支票账户),它们继承自Account类,但具有不同的服务费(存款和取款都会收取服务费)。

实现基础的交易处理功能,如转账、存款、取款。

实现交易历史记录功能,记录客户的所有交易。

实现简单的利息计算功能,定期更新账户余额。

添加数据持久化功能,如将客户和账户信息保存到文件。

数据存储:

使用集合类(如ArrayList)存储系统中的客户和账户信息。

异常处理:

添加适当的异常处理,例如在处理交易时检查余额不足或输入错误。

测试类:

创建一个测试类,演示银行系统的各种功能。

//Account类
package Test;

public abstract class Account {
    private String accountId;
    private double balance;
    private String accountType;

    public Account(String accountId, double balance, String accountType) {
        this.accountId = accountId;
        this.balance = balance;
        this.accountType = accountType;
    }

    public String getAccountId() {
        return accountId;
    }

    public double getBalance() {
        return balance;
    }

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

    public String getAccountType() {
        return accountType;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    public abstract void withdraw(double amount);

    public abstract void transfer(Account destinationAccount, double amount);
}
//Bank类
package Test;

import java.util.ArrayList;

public class Bank {
    private ArrayList<Customer> customers;

    public Bank() {
        this.customers = new ArrayList<>();
    }

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

    public void removeCustomer(Customer customer) {
        customers.remove(customer);
    }

    public Customer findCustomerById(String customerId) {//查customer
        for (Customer customer : customers) {
            if (customer.getCustomerId().equals(customerId)) {
                return customer;
            }
        }
        return null;
    }

    public void deposit(String customerId, String accountId, double amount) {
        Customer customer = findCustomerById(customerId);
        if (customer != null) {
            for (Account account : customer.getAccounts()) {
                if (account.getAccountId().equals(accountId)) {
                    account.deposit(amount);
                    System.out.println("Deposit successful. New balance: " + account.getBalance());
                    return;
                }
            }
        }
        System.out.println("Invalid customer ID or account ID.");
    }

    public void withdraw(String customerId, String accountId, double amount) {
        Customer customer = findCustomerById(customerId);
        if (customer != null) {
            for (Account account : customer.getAccounts()) {
                if (account.getAccountId().equals(accountId)) {
                    account.withdraw(amount);
                    System.out.println("Withdrawal successful. New balance: " + account.getBalance());
                    return;
                }
            }
        }
        System.out.println("Invalid customer ID or account ID.");
    }

    public void transfer(String sourceCustomerId, String sourceAccountId, String destinationCustomerId,
                         String destinationAccountId, double amount) {
        Customer sourceCustomer = findCustomerById(sourceCustomerId);
        Customer destinationCustomer = findCustomerById(destinationCustomerId);
        if (sourceCustomer != null && destinationCustomer != null) {
            Account sourceAccount = findAccountById(sourceCustomer, sourceAccountId);
            Account destinationAccount = findAccountById(destinationCustomer, destinationAccountId);
            if (sourceAccount != null && destinationAccount != null) {
                sourceAccount.transfer(destinationAccount, amount);
                System.out.println("Transfer successful. New balance of source account: " + sourceAccount.getBalance() +
                        ", new balance of destination account: " + destinationAccount.getBalance());
                return;
            }
        }
        System.out.println("Invalid customer ID or account ID.");
    }

    private Account findAccountById(Customer customer, String accountId) {
        for (Account account : customer.getAccounts()) {
            if (account.getAccountId().equals(accountId)) {
                return account;
            }
        }
        return null;
    }

    public ArrayList<Customer> getCustomers() {
        return customers;
    }

}
//BankFileManage类
package Test;

import java.io.*;
import java.util.ArrayList;

public class BankFileManager {
    private static final String CUSTOMER_FILE = "customers.txt";
    private static final String ACCOUNT_FILE = "accounts.txt";

    public void saveCustomers(ArrayList<Customer> customers) {
        try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(CUSTOMER_FILE))) {
            outputStream.writeObject(customers);
            System.out.println("Customers saved successfully.");
        } catch (IOException e) {
            System.out.println("Failed to save customers: " + e.getMessage());
        }
    }

    public ArrayList<Customer> loadCustomers() {
        ArrayList<Customer> customers = new ArrayList<>();
        File file = new File(CUSTOMER_FILE);
        if (file.exists()) {
            try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file))) {
                customers = (ArrayList<Customer>) inputStream.readObject();
                System.out.println("Customers loaded successfully.");
            } catch (IOException | ClassNotFoundException e) {
                System.out.println("Failed to load customers: " + e.getMessage());
            }
        }
        return customers;
    }

    public void saveAccounts(ArrayList<Account> accounts) {
        try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(ACCOUNT_FILE))) {
            outputStream.writeObject(accounts);
            System.out.println("Accounts saved successfully.");
        } catch (IOException e) {
            System.out.println("Failed to save accounts: " + e.getMessage());
        }
    }

    public ArrayList<Account> loadAccounts() {
        ArrayList<Account> accounts = new ArrayList<>();
        File file = new File(ACCOUNT_FILE);
        if (file.exists()) {
            try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file))) {
                accounts = (ArrayList<Account>) inputStream.readObject();
                System.out.println("Accounts loaded successfully.");
            } catch (IOException | ClassNotFoundException e) {
                System.out.println("Failed to load accounts: " + e.getMessage());
            }
        }
        return accounts;
    }
}
//CheckingAccount类
package Test;

public class CheckingAccount extends Account{
    private double transactionFee;

    public CheckingAccount(String accountId, double balance, double transactionFee) {
        super(accountId, balance, "Checking");
        this.transactionFee = transactionFee;
    }

    public double getTransactionFee() {
        return transactionFee;
    }

    @Override
    public void withdraw(double amount) {
        if (getBalance() >= (amount + transactionFee)) {
            setBalance(getBalance() - amount - transactionFee);
        } else {
            System.out.println("Insufficient balance.");
        }
    }

    @Override
    public void transfer(Account destinationAccount, double amount) {
        if (getBalance() >= (amount + transactionFee)) {
            setBalance(getBalance() - amount - transactionFee);
            destinationAccount.deposit(amount);
        } else {
            System.out.println("Insufficient balance.");
        }
    }
}
//SavingAccount类
package Test;

public class SavingsAccount extends Account{
    private double interestRate;

    public SavingsAccount(String accountId, double balance, double interestRate) {
        super(accountId, balance, "Savings");
        this.interestRate = interestRate;
    }

    public double getInterestRate() {
        return interestRate;
    }

    @Override
    public void withdraw(double amount) {//取款
        try{
            if (getBalance() >= amount) {
                setBalance(getBalance() - amount);
            } else {
                System.out.println("Insufficient balance.");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void transfer(Account destinationAccount, double amount) {//转账
        try{
            if (getBalance() >= amount) {
                setBalance(getBalance() - amount);
                destinationAccount.deposit(amount);
            } else {
                System.out.println("Insufficient balance.");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
//customer类
package Test;

import java.util.ArrayList;

public class Customer {
    private String customerId;
    private String name;
    private ArrayList<Account> accounts;

    public Customer(String customerId, String name) {
        this.customerId = customerId;
        this.name = name;
        this.accounts = new ArrayList<>();
    }

    public String getCustomerId() {
        return customerId;
    }

    public String getName() {
        return name;
    }

    public ArrayList<Account> getAccounts() {
        return accounts;
    }

    public void addAccount(Account account) {
        accounts.add(account);
    }

    public void removeAccount(Account account) {
        accounts.remove(account);
    }
}
//测试类BankSystemTest
package Test;

public class BankSystemTest {
    public static void main(String[] args) {
        // Create bank
        Bank bank = new Bank();

        // Create customers
        Customer customer1 = new Customer("C001", "John");
        Customer customer2 = new Customer("C002", "Alice");

        // Add customers to bank
        bank.addCustomer(customer1);
        bank.addCustomer(customer2);

        // Create accounts
        Account account1 = new SavingsAccount("A001", 1000, 0.02);
        Account account2 = new CheckingAccount("A002", 2000, 10);
        Account account3 = new SavingsAccount("A003", 5000, 0.03);

        // Add accounts to customers
        customer1.addAccount(account1);
        customer1.addAccount(account2);
        customer2.addAccount(account3);

        // Perform transactions
        bank.deposit("C001", "A001", 500);
        bank.withdraw("C001", "A002", 300);
        bank.transfer("C001", "A002", "C002", "A003", 200);

        // Print account balances
        for (Customer customer : bank.getCustomers()) {
            System.out.println("Customer: " + customer.getName());
            for (Account account : customer.getAccounts()) {
                System.out.println("Account: " + account.getAccountId() + ", Balance: " + account.getBalance());
            }
            System.out.println();
        }
    }
}

运行效果如下:

更多的检查你们自己来看喽!!!

感谢爪子!关注爪子!点赞!收藏!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值