´¢Ðî¹ÞÀàµÄ´úÂëÈçÏ£º

³ÌÐò´úÂ룺
public class SavingsAccount {
private int balance;
public SavingsAccount(int balance) {
this.balance = balance;
}
public void deposit(int amount) {
if (amount > 0 && (amount % 10 == 0 || amount % 5 == 0)) {
balance += amount;
System.out.println("³É¹¦´æÈë" + amount + "Ôª£¬µ±Ç°Óà¶îΪ" + balance + "Ôª");
} else {
System.out.println("´æÈë½ð¶î±ØÐëΪÕýÕûÊýÇÒΪ10»ò5µÄ±¶Êý");
}
}
public int withdraw(int amount) {
if (amount % 2 != 0) {
System.out.println("È¡¿î½ð¶î±ØÐëΪ2µÄ±¶Êý");
return 0;
} else if (amount > balance) {
System.out.println("Óà¶î²»×ã");
return 0;
} else {
balance -= amount;
System.out.println("³É¹¦È¡³ö" + amount + "Ôª£¬µ±Ç°Óà¶îΪ" + balance + "Ôª");
return amount;
}
}
public int getBalance() {
return balance;
}
}
²âÊÔÀàµÄ´úÂëÈçÏ£º

³ÌÐò´úÂ룺
public class SavingsAccountTest {
public static void main(String[] args) {
SavingsAccount account = new SavingsAccount(1000);
account.deposit(50);
account.deposit(15);
account.deposit(-10);
account.deposit(7);
account.deposit(12);
account.withdraw(5);
account.withdraw(7);
account.withdraw(100);
account.withdraw(20);
account.withdraw(30);
System.out.println("µ±Ç°Óà¶îΪ£º" + account.getBalance());
}
}
Êä³ö½á¹ûÈçÏ£º
³É¹¦´æÈë50Ôª£¬µ±Ç°Óà¶îΪ1050Ôª
´æÈë½ð¶î±ØÐëΪÕýÕûÊýÇÒΪ10»ò5µÄ±¶Êý
´æÈë½ð¶î±ØÐëΪÕýÕûÊýÇÒΪ10»ò5µÄ±¶Êý
³É¹¦´æÈë10Ôª£¬µ±Ç°Óà¶îΪ1060Ôª
³É¹¦´æÈë12Ôª£¬µ±Ç°Óà¶îΪ1072Ôª
³É¹¦È¡³ö5Ôª£¬µ±Ç°Óà¶îΪ1067Ôª
È¡¿î½ð¶î±ØÐëΪ2µÄ±¶Êý
Óà¶î²»×ã
³É¹¦È¡³ö20Ôª£¬µ±Ç°Óà¶îΪ1047Ôª
³É¹¦È¡³ö30Ôª£¬µ±Ç°Óà¶îΪ1017Ôª
µ±Ç°Óà¶îΪ£º1017