public class Account {
private int id;
private double money;
private static int now = 10000;
public Account() {
this.id = now;
now++;
}
public Account(double money) {
this.id = now;
this.money = 0;
System.out.println("开户账号为"+id);
now++;
}
public void save(double money) {
this.money = this.money + money;
System.out.println("账号"+id+"存入"+money+"余额"+this.money);
}
public void withdraw(double money) {
if(money <= this.money) {
this.money = this.money - money;
System.out.println("账号"+id+"取钱"+money+"成功"+"剩余"+this.money);
}else {
System.out.println("账号"+id+"取钱"+money+"余额不足");
}
}
public double getBalance() {
return money;
}
public int getId() {
return id;
}
}
测试类`
public class TestAccount1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Account a1 = new Account();
System.out.println("开户账号为:"+a1.getId()+"余额:"+a1.getBalance());
a1.save(2000);
a1.withdraw(10000);
System.out.println("账号:"+a1.getId()+"余额:"+a1.getBalance());
Account a2 = new Account(10000);
System.out.println("账号:"+a2.getId()+"余额:"+a2.getBalance());
a2.save(10000);
a2.withdraw(5000);
System.out.println("账号:"+a2.getId()+"余额:"+a2.getBalance());
}
}