Define a bank account class BankAccount to realize the concept of bank account, define two variables in the BankAccount class: "account number" (account_number) and "deposit balance" (leftmoney), and then define four methods: "deposit" (savemoney), " "withdrawal" (getmoney), "query balance" (getleftmoney), construction method (BankAccount).
Finally, create an object ba of the BankAccount class in the main() method, assuming that the account number of ba is: 123456, and the initial deposit balance is 500 yuan. First deposit 1,000 yuan into the account, and then withdraw 2,000 yuan.
public class BankAccount {
String account_number;
int leftmoney;
public BankAccount() {
}
public BankAccount(String account_number, int leftmoney) {
super();
this.account_number = account_number;
this.leftmoney = leftmoney;
}
public void savemoney(int money) {
leftmoney += money;
}
public void getmoney(int money) {
if (leftmoney > money) {
leftmoney -= money;
} else {
System.out.println("超出余额");
}
}
public int getleftmoney() {
return leftmoney;
}
public static void main(String[] args) {
BankAccount ba = new BankAccount("123456", 500);
ba.savemoney(1000);
ba.getmoney(2000);
}
}