当前位置:首页 » 编程语言 » java银行账户类

java银行账户类

发布时间: 2024-09-27 06:20:12

java定义一个类实现银行账户的概念。

public class BankAccount {

String account ;
int account_num;

//构造函数
public BankAccount(String account,int account_num){
this.account=account;
this.account_num=account_num;
}
//存钱
public String addNum(int num){
account_num= account_num+num;
return "存钱成功";
}

//取钱
public String getNum(int num){
String result;
if(account_num>num){
account_num =account_num-num;
result="取钱成功";
}else{
result="账户余额不足,还剩"+account_num;
}
return result;
}

//显示余额
public String displayNum(){
return "账户余额:"+account_num;
}

//显示账号
public String displayAccount(){
return "账户:"+account;
}

public static void main(String[] args){
BankAccount lucy = new BankAccount("lucy",100);
BankAccount jack = new BankAccount("jack",50);
//显示他们各自的余额
System.out.println("===显示他们各自的余额===");
System.out.println("lucy:"+lucy.displayNum());
System.out.println("jack:"+jack.displayNum());
//存钱取钱
System.out.println("===存钱取钱===");
System.out.println("lucy取50:"+lucy.getNum(50));
System.out.println("jack存200:"+jack.addNum(200));
//显示账户 显示余额
System.out.println("===存钱取钱===");
System.out.println(lucy.displayAccount()+""+lucy.displayNum());
System.out.println(jack.displayAccount()+""+jack.displayNum());

}

}

运行结果:
===显示他们各自的余额===
lucy:账户余额:100
jack:账户余额:50
===存钱取钱===
lucy取50:取钱成功
jack存200:存钱成功
===存钱取钱===
账户:lucy账户余额:50
账户:jack账户余额:250

热点内容
linux动态库静态编译 发布:2024-11-24 19:38:24 浏览:291
为什么编译一直出错 发布:2024-11-24 19:30:24 浏览:234
如何查看qq仙境电脑配置 发布:2024-11-24 19:30:14 浏览:625
怎么用苹果玩安卓账号 发布:2024-11-24 19:29:34 浏览:157
2022款雅阁哪个配置全景天窗 发布:2024-11-24 19:25:48 浏览:841
64解压缩软件官方下载 发布:2024-11-24 19:23:35 浏览:523
php图形库 发布:2024-11-24 19:21:02 浏览:496
c语言递归算法n 发布:2024-11-24 19:18:46 浏览:32
在c语言中表示什么 发布:2024-11-24 19:04:46 浏览:408
discuz友情链接缓存 发布:2024-11-24 19:00:11 浏览:693