面向对象练习2(初稿,学习用)

本文描述了一个基于Java的银行管理系统,系统支持借记卡和信用卡账户的开户、登录、存款、取款等操作。借记卡不允许透支,而信用卡有信用额度允许透支。银行类管理所有账户,并能查询账户余额总和及信用额度总和。

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

面向对象模拟银行管理系统: 一般银行账户分为:借记卡账户 和 信用卡账户。概念如下:

​    借记卡账户 DebitAccount :是指先存款后消费(或取现),没有透支功能的银行卡。即存储卡账户,余额必须大于 0。

​    信用卡账户 CreditAccount :信用卡是一种非现金交易付款的方式, 是简单的信贷服务。在借记卡功能的基础上可以透支,但是有透支额度,即透支只能在一定的金额范围内透支。

​    信用额度是指银行在批准你的信用卡的时候给于你信用卡的一个最高透支的限额,你只能在这个额度内刷卡消费,超过了这个额度就无法正常刷卡消费。

​    用 JAVA 代码描述银行的账户,以及账户的操作。

​    (1) 所有银行卡(Account)都具有以下的属性:卡号(id)、卡持有者姓名(name)、密码(password)、身份证号(personId)、邮箱(email)、余额(balance)、

​    (2) 所有银行卡(Account)都具有的行为:

​    存款: deposit(double money),所有卡都一样,不允许重写

​    取款: withdraw(double money),各种卡不一样,在子类实现

​    (3) 信用卡(CreditAccount)账户专有的属性:信用额度(ceiling)

​    行为: 对不同的卡来说 取款方法不一样

​    (4) 借记卡账户 DebitAccount:没有专有属性,与所有卡一样

​    行为: 对不同的卡来说 取款方法不一样

​    (5) 银行 Bank.java。银行中管理所有银行卡,包括借记卡和信用卡。

​    要求:保存在 数组或者 List 集合中。即:银行(Bank)类中只有一个属性,即Account 类型的 ArrayList 或 Account 类型的数组

​    银行对外提供各种相关方法:

​    (1) 开户 即注册。包含除卡号之外的所有属性

​    (2) 登录 public Account login(long id, String password),凭卡号和密码登录

​    (3) 存款 public Account deposit(long id, double money) //传入卡号和金额,返回账户信息

​    (4) 取款 public Account withdraw(long id, double money)//传入卡号和金额,返回账户信息

​    (5) 设置某个账户的信用额度

​    (6) 查询某个账户的余额

​    (7) 查询该行所有账户的信用额度的和(只有信用卡账户有信用额度)

​    (8) 查询该行所有账户的余额的和(所有账户都有余额)

​    (9) 按照用户的卡号查找账户

运行效果参考如下:

```
欢迎使用银行管理系统 请输入指令 1:开户 2:登录 3:退出

1

请选择您要开户的类型 1:借记卡 2:信用卡

1

您选择了借记卡,请输入用户名:

zhangsan

请输入密码:

abc123

请再次输入密码:

abc

对不起,两次输入不同,请重新输入!!

请输入密码:

abc123

请再次输入密码:

abc123

请输入身份证号:

123456789123456789

请输入邮箱 email:

zhangsan

对不起,邮箱格式错误,请重新输入!!

请输入邮箱 email:

zhangsan@126.com

请输入预存款金额:

1000.02

开户成功:卡号为: 1001,类型是借记卡,姓名: zhangsan,身份证号: 123456789123456789,

邮箱:zhangsan@126.com 预存款金额为:1000.02 元

欢迎使用银行管理系统 请输入指令 1:开户 2:登录 3:退出

2

请输入卡号:

1001

请输入密码:

abc

登录失败!卡号或密码错误,请重新登录!

请输入卡号:

1001

请输入密码:

abc123

登录成功!您的卡片类型为借记卡.

请输入指令 1:取款 2:存款 3:退出

1

请输入取款金额;

2000

对不起,您的余额不足,请重新输入:

100

取款成功,您的余额为:900.02

请输入指令 1:取款 2:存款 3:退出

2

请输入存款金额;

2000

取款成功,您的余额为:2900.02

请输入指令 1:取款 2:存款 3:退出

3

zhangsan 退出成功。

欢迎使用银行管理系统 请输入指令 1:开户 2:登录 3:退出

3

谢谢使用,欢迎下次使用!!
```

package test2;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 卡类
 */
class Account {
    private final   long id;//卡号
    private final String name;//卡持有者姓名
    private final String password;//密码
    private final String personId;//身份证号
    private final String email;//邮箱
    protected double balance;//余额,

    public Account(long id, String name, String password, String personId, String email, double balance) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.personId = personId;
        this.email = email;
        this.balance = balance;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public String getPersonId() {
        return personId;
    }

    public String getEmail() {
        return email;
    }

    public double getBalance() {
        return balance;
    }

    public void deposit(double money) {   //存款
        balance += money;
        System.out.println("存款成功,您的余额为:" + balance);
    }

    @Override
    public String toString() {
        return "卡号:" + id + ", 姓名:" + name + ", 身份证号:" + personId + ",邮箱:" + email + ",余额:" + balance;
    }


    public void withdraw(double money) {       //取款
        if (balance < money) {
            System.out.println("对不起,您的余额不足!");
        } else {
            balance -= money;
            System.out.println("取款成功,您的余额为:" + balance);
        }
    }
}

/**
 * 借记卡账户类
 * 是指先存款后消费(或取现),没有透支功能的银行卡。即存储卡账户,余额必须大于0。
 */
class DebitAccount extends Account {
    public DebitAccount(long id, String name, String password, String personId, String email, double balance) {
        super(id, name, password, personId, email, balance);
    }

    //借记卡取款
    @Override
    public void withdraw (double money) {
        if (balance < money) {
            System.out.println("对不起,您的余额不足!");
        }
        if (balance >= money){
            balance -= money;
            System.out.println("取款成功,您的余额为:" + balance);
        }
    }
}

/**
 * 信用卡类
 */
class CreditAccount extends Account {
    private double ceiling;//信用额度

    public CreditAccount(long id, String name, String password, String personId, String email, double balance, double ceiling) {
        super(id, name, password, personId, email, balance);
        this.ceiling = ceiling;
    }

    public double getCeiling() {
        return ceiling;
    }

    public void setCeiling(double ceiling) {
        this.ceiling = ceiling;
    }

    //信用卡取款
    public void withdraw(double money) {
        if (balance + ceiling < money) {
            System.out.println("对不起,您的信用额度不足!");
        } else {
            balance -= money;
            System.out.println("取款成功,您的余额为:" + balance);
        }
    }
}

//银行类
class Bank {
    private final List<Account> accounts;

    public Bank() {
        accounts = new ArrayList<>();
    }

    /**
     * 开户
     */
    public void openAccount(long id, String name, String password, String personId, String email, double balance, int accountType, double ceiling) {
        Account account = null;
        if (accountType == 1) {
            account = new DebitAccount(id, name, password, personId, email, balance);
        } else if (accountType == 2) {
            account = new CreditAccount(id, name, password, personId, email, balance, ceiling);
        }

        if (account != null) {
            accounts.add(account);
            System.out.println("开户成功:" + account.toString());
        }
    }

    /**
     * 登录
     */
    public Account login(long id, String password) {
        for (Account account : accounts) {
            if (account.getId() == id && account.getPassword().equals(password)) {
                return account;
            }
        }
        return null;
    }

    /**
     * 存款
     */
    public void deposit(long id, double money) {
        Account account = getAccountById(id);
        if (account != null) {
            account.deposit(money);
        } else {
            System.out.println("未找到指定账户!");
        }
    }

    /**
     * 取款
     */
    public void withdraw(long id, double money) {
        Account account = getAccountById(id);
        if (account != null) {
            account.withdraw(money);//找到账户
        } else {
            System.out.println("未找到指定账户!");
        }
    }

    /**
     * 设置某个账户的信用额度
     */
    public void setCeiling(long id, double ceiling) {
        Account account = getAccountById(id);
        if (account instanceof CreditAccount) {
            CreditAccount creditAccount = (CreditAccount) account;
            creditAccount.setCeiling(ceiling);
            System.out.println("设置信用额度成功!");
        } else {
            System.out.println("该账户不是信用卡账户!");
        }
    }

    /**
     * 查询该行所有账户的余额的和(所有账户都有余额)
     */
    public double getTotalBalance() {
        double totalBalance = 0.0;
        for (Account account : accounts) {
            totalBalance += account.getBalance();
        }
        return totalBalance;
    }

    /**
     * 查询该行所有账户的信用额度的和(只有信用卡账户有信用额度)
     */
    public double getTotalCeiling() {
        double totalCeiling = 0.0;
        for (Account account : accounts) {
            if (account instanceof CreditAccount) {
                CreditAccount creditAccount = (CreditAccount) account;
                totalCeiling += creditAccount.getCeiling();
            }
        }
        return totalCeiling;
    }


    /**
     * 按照用户的卡号查找账户
     */
    private Account getAccountById(long id) {
        for (Account account : accounts) {
            if (account.getId() == id) {
                return account;
            }
        }
        return null;
    }
}

public class Test{
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值