用java定义一个类实现银行帐户的概念

2. 定义一个类实现银行帐户的概念,包括的变量有"帐号"和"存款余额",包括的方法有"存款"、"取款"、"查询余额"和”显示帐号”,。定义主类,创建帐户类的对象,并完成相应操作。
提示:关键代码如下:
public int getleftmoney(){
return leftmoney;
}
public void savemoney(double money){
leftmoney+=money;
}
public void getmoney(double money){
if(money<=leftmoney)
leftmoney-=money;
else
System.out.println("只能取:"+leftmoney);
}

bankaccount ba=new bankaccount(888123,1000);
ba.savemoney(21000);
System.out.println("存入21000元后余额为:"+ba.getleftmoney());
ba.getmoney(11500);
System.out.println("11500元后余额为:"+ba.getleftmoney());

/*
*
2. 定义一个类实现银行帐户的概念,包括的变量有"帐号"和"存款余额",包括的方法有"存款"、"取款"、"查询余额"和”显示帐号”,。定义主类,创建帐户类的对象,并完成相应操作。

*/

public class BankAccount {
public String accountId;

public double leftmoney;

public String getAccountId() {
return accountId;
}

public void setAccountId(String accountId) {
this.accountId = accountId;
}

public double getLeftmoney() {
return leftmoney;
}

public void setLeftmoney(double balance) {
this.leftmoney = balance;
}

public BankAccount() {

}

public BankAccount(String accountId, double balance) {
super();
this.accountId = accountId;
this.leftmoney = balance;
}

public void savemoney(double money){
this.leftmoney += money;
}

public void getmoney(double money){
if(money <= leftmoney){
leftmoney -= money;
}else{
System.out.println("只能取:" + leftmoney);
}
}

public static void main(String[] args){
BankAccount ba = new BankAccount("888123",1000);
ba.savemoney(21000);
System.out.println("存入21000元后余额为:" + ba.getLeftmoney());
ba.getmoney(11500);
System.out.println("取出11500元后余额为:" + ba.getLeftmoney());
}

}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答