银行转账项目

package Day14;

class Account{
    String id;  // 用户名称
    double balance;  // 用户余额

    public void save(double money){  // 存钱方法
        if (money > 0){
            balance += money;
        }else {
            System.out.println("有误");
        }
    }

    public void transfer(int money, Customer c){  // 转账

        if (money < balance && money > 0){
            balance -= money;
            c.account.balance += money;
        }else {
            System.out.println("余额不足");
        }
    }
    public void withdraw(double money){    // 取钱方法
        if (money < 0){
            System.out.println("数据出错");
        }else if (money > balance){
            System.out.println("余额不足");
        }else {
            balance -= money;
        }
    }
}

class  Customer{  //  客户类
    String name;  //  姓名
    String tel;  //  手机号
    String cid;  // 身份证号码 x 10
    Account account;  // 用户账号
}

class BankClerk{
    public void open(Customer c, Account a){  // 开户
        c.account = a;  // 账号和用户进行绑定
    }
}


public class task{
    public static void main(String[] args) {
        //  创建用户对象(1)
        Customer c1 = new Customer();
        c1.name = "汐";  //  用户名
        c1.tel = "131";  //  电话号码
        c1.cid = "431";    //  身份证号码

        // 创建一个用户(2)
        Customer c2 = new Customer();
        c2.name = "渚";  //  用户名
        c2.tel = "150";  //  电话号码
        c2.cid = "43122";  //  身份证号码

        // 创建账户(1)
        Account a1 = new Account();
        a1.id = "001";  // id
        a1.balance = 1000;  //  余额

        // 创建用户(2)
        Account a2 = new Account();
        a2.id = "002";  // id
        a2.balance = 1000;  //  余额

        // 创建银行对象  让他们进行绑定
        BankClerk b1 = new BankClerk();
        b1.open(c1,a1);// 用户和账号进行绑定  c1.account = a1
        b1.open(c2,a2);// 给渚绑定账户 c2.account = a2
        System.out.println("用户名1:" + c1.name + "账户id是:" + c1.account.id);
        System.out.println("用户名2:" + c2.name + "账户id是:" + c2.account.id);
        //  转账
        c1.account.withdraw(500);  //  转出
        c2.account.save(500);  //  接收
        System.out.println("用户1余额" + c1.account.balance);  // 打印余额
        System.out.println( "用户2余额:" + c2.account.balance);  // 打印余额

        c1.account.transfer(200,  c2);
        //打印转账后余额
        System.out.println("用户1余额" + c1.account.balance);  // 打印余额
        System.out.println( "用户2余额:" + c2.account.balance);  // 打印余额
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值