用银行卡实现存钱取钱转账的方法

这段代码展示了如何使用Java创建一个简单的银行系统,包括客户、账户和银行职员类。客户类包含个人信息和账户,账户类有存款、取款、转账和查看余额的方法。银行职员类用于关联客户和账户。程序进行了存款、取款和转账的示例操作。
摘要由CSDN通过智能技术生成
import java.util.Scanner;

class Account {
    String id;
    double balance;
    double balance2;
    public void sava(double money) { //存钱
        if (money > 0) {
            balance += money;
            balance2+=money;
        } else {
            System.out.println("参数有误");
        }
    }
    public void Eft(double money) { //转账
//        if (money > balance){
//            if (money>balance2){
//                System.out.println("余额不足");
//            }
//        }
//        else {
            balance-=money;
            balance2-=money;
        }
    public void Ass(double money){
        balance +=money;
        balance2 +=money;
    }
    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;
    Account account;
}
class BankClerk{ //银行对象
    public void open(Customer c,Account a){
        c.account=a;
    }
}
public class Method_Exer6 {
    public static void main(String[] args) {
        //创建客户对象
        Customer c = new Customer();
        c.name = "袁总";
        c.tel = "10000";
        c.cid = "10086";
        Customer c2 = new Customer();
        c2.name = "兰总";
        c2.tel = "123456";
        c2.cid = "123456789";
        //创建银行卡账号对象
        Account a = new Account();
        a.id = "7758258";
        a.balance = 0;
        Account a2 = new Account();
        a2.id = "007008009";
        a2.balance2 = 0;
        //银行对象
        BankClerk b = new BankClerk();
        BankClerk p = new BankClerk();
        b.open(c, a);
        p.open(c2, a2);
        System.out.println("姓名:" + c.name + "手机号码:" + c.tel + "身份证号码:" + c.cid + "账户:" + c.account.id + "余额:" + c.account.balance);
        System.out.println("姓名:" + c2.name + "手机号码:" + c2.tel + "身份证号码:" + c2.cid + "账户:" + c2.account.id + "余额" + c2.account.balance2);
        //存款
        c.account.sava(50000);
        System.out.println("姓名:" + c.name + "手机号码:" + c.tel + "身份证号码:" + c.cid + "账户:" + c.account.id + "余额:" + c.account.balance);
        c2.account.sava(30000);
        System.out.println("姓名:" + c2.name + "手机号码:" + c2.tel + "身份证号码:" + c2.cid + "账户:" + c2.account.id + "余额:" + c2.account.balance2);
        //取款
        c.account.withdraw(10000);
        System.out.println("姓名:" + c.name + "手机号码:" + c.tel + "身份证号码:" + c.cid + "账户:" + c.account.id + "余额:" + c.account.balance);
        c2.account.withdraw(40000);
        System.out.println("姓名:" + c2.name + "手机号码:" + c2.tel + "身份证号码:" + c2.cid + "账户:" + c2.account.id + "余额:" + c2.account.balance2);
        //转账
//        c.account.Eft(6000);
//        System.out.println("兰总的账户:" + c.account.id + "袁总的账户:" + c2.account.id + "张三的余额:" + c.account.balance + "李四的余额:" + c2.account.balance2);
        Scanner input = new Scanner(System.in);
        System.out.println("请输入金额:");
        int a1 = input.nextInt();
        if (a1 <= a.balance) {
            c.account.Eft(a1);
            c2.account.Ass(a1);
        }else {
            System.out.println("余额不足");
        }
        System.out.println("兰总的账户:" + c.account.id + "袁总的账户:" + c2.account.id + "张三的余额:" + c.account.balance + "李四的余额:" + c2.account.balance2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值