JAVA项目:简单的银行账户系统1

这是我在初学Java的第一个实战项目,在很多视频也有相关的介绍,
这是非常适合新手的一个简单的代码。
这里就不赘述,直接上代码!

Account类的编译模块
这里主要是对客户的基本账户操作:

public class Account {
    private double balance;//账户余额

    public Account(double init_balance) {
        // TODO Auto-generated constructor stub
        balance = init_balance;
    }

    public double getBalance(){
        return balance;
    }

//  存钱
    public void desposit(double amt){//存钱数
        balance+=amt;
    }

//  取钱
    public void withdraw(double amt){//取钱数(当余额不足时提醒)
        if(balance>=amt){
        balance-=amt;
        }else{
            System.out.println("余额不足!");
        }
    }
}

在基本的操作完成后我们要进行测试,下面是测试代码:

import Banking1.Account;

public class testbanking1 {

        public static void main(String[] args){
            Account account;

//          Create an account that can has a 500.00 balance
            account = new Account(500.00);
            System.out.println("Create an account that can has a 500.00 balance");

//          code
            account.withdraw(150);
            System.out.println("Withdraw 150.00");
//          code
            account.desposit(22.50);
            System.out.println("Deposit 22.50");

//          code
            account.desposit(47.62);
            System.out.println("Withdraw 47.62");
//          code
//          Print out the final account balance
            System.out.println("The account has a balance of "+account.getBalance());

        }
}

这是最简单的一个小项目,但值得练手,适合初入的java新手测试。

你可能感兴趣的:(java)