Java基础编程题目——编写一个简单的银行账户类

创建简单的银行账户类,包括开户人、账号和存款余额三个属性,以及查询余额、存款、取款等方法。创建两个账户验证类的设计。

public class Banks {
    public static void main(String[] args) {
        bank wang = new bank("ling", 123, 10000);
        bank ze = new bank("hui", 124, 20000);
        bank lon = new bank("he", 125, 3000);

        wang.inquire();        //查询余额
        System.out.println();

        ze.deposit(5000);      //成功取款5000
        System.out.println();

        ze.deposit(20000);     //20000取款失败
        System.out.println();

        lon.withdraw(3000);    //存款3000
        System.out.println();
    }
}

class bank {
    String name;
    int num;
    int balance;

    public bank(String name, int num, int balance) {
        this.name = name;
        this.num = num;
        this.balance = balance;
    }

    public void inquire() {
        System.out.println("余额:" + this.balance);
    }

    public void deposit(int a) {
        if(this.balance - a >= 0) {
            System.out.println("取款成功!");
            this.balance -= a;
        }
        else
            System.out.println("余额不足!");
        this.inquire();
    }

    public void withdraw(int a) {
        this.balance += a;
        System.out.println("存款成功!");
        this.inquire();
    }
}

你可能感兴趣的:(Java编程题目)