java定义一个类实现银行账户的概念。
包括的属性有“帐号”和“存款余额”,包括的方法有“存款”、“取款”、“查询余额”和“显示帐号”。编写一个测试类,创建两个不同的账户类对象,并分别完成存款、取款、查询余额、...
包括的属性有“帐号”和“存款余额”,包括的方法有“存款”、“取款”、“查询余额”和“显示帐号”。编写一个测试类,创建两个不同的账户类对象,并分别完成存款、取款、查询余额、显示帐号等操作。
展开
2个回答
展开全部
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
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
推荐于2017-09-10 · 知道合伙人生活技巧行家
关注
展开全部
abstract class BankCard {
protected String name;
protected String cardNo;
protected double money;
public BankCard(String name, String cardNo, double money) {
super();
this.name = name;
this.cardNo = cardNo;
this.money = money;
}
public String getName() {
return name;
}
public String getCardNo() {
return cardNo;
}
public double getMoney() {
return money;
}
public abstract void unputMoney(int someMoney);
public void putMoney(int someMoney) {
this.money += someMoney;
}
}
protected String name;
protected String cardNo;
protected double money;
public BankCard(String name, String cardNo, double money) {
super();
this.name = name;
this.cardNo = cardNo;
this.money = money;
}
public String getName() {
return name;
}
public String getCardNo() {
return cardNo;
}
public double getMoney() {
return money;
}
public abstract void unputMoney(int someMoney);
public void putMoney(int someMoney) {
this.money += someMoney;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询