java 银行功能的实现

 
 

定义一个银行帐户类BankAccount实现银行帐户的概念,在BankAccount类中定义两个变量:"帐号" (account_number) 和"存款余额"(leftmoney),再定义四个方法:"存款" (savemoney)、"取款"(getmoney) 、 "查询余额" (getleftmoney)、构造方法(BankAccount)。

最后,在main()方法中创建一个BankAccount类的对象ba,假设ba的账号为:123456,初始的存款余额为500元。首先向该账户存入1000元,再取出2000元。

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());
}

}

猜你喜欢

转载自blog.csdn.net/qq_42070071/article/details/80561108